aboutsummaryrefslogtreecommitdiffstats
path: root/tests/blogc-make/check_atom.c
blob: 6faa53ecdae5fa26ad4f10b780ef9da2ae1c3f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2017 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <stdlib.h>
#include <string.h>

#include "../../src/blogc-make/atom.h"
#include "../../src/blogc-make/settings.h"
#include "../../src/common/file.h"
#include "../../src/common/error.h"
#include "../../src/common/utils.h"


static void
test_atom_file(void **state)
{
    bm_settings_t *settings = bc_malloc(sizeof(bm_settings_t));
    settings->settings = bc_trie_new(free);
    bc_trie_insert(settings->settings, "atom_prefix", bc_strdup("atom"));
    bc_trie_insert(settings->settings, "atom_ext", bc_strdup(".xml"));
    bc_trie_insert(settings->settings, "post_prefix", bc_strdup("post"));

    bc_error_t *err = NULL;
    char *rv = bm_atom_deploy(settings, &err);

    assert_non_null(rv);
    assert_null(err);

    size_t cmp_len;
    char *cmp = bc_file_get_contents(rv, true, &cmp_len, &err);

    assert_non_null(cmp);
    assert_null(err);

    assert_string_equal(cmp,
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
        "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"
        "  <title type=\"text\">{{ SITE_TITLE }}{% ifdef FILTER_TAG %} - "
            "{{ FILTER_TAG }}{% endif %}</title>\n"
        "  <id>{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}"
        "{% endif %}.xml</id>\n"
        "  <updated>{{ DATE_FIRST_FORMATTED }}</updated>\n"
        "  <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/\" />\n"
        "  <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}"
            "/{{ FILTER_TAG }}{% endif %}.xml\" rel=\"self\" />\n"
        "  <author>\n"
        "    <name>{{ AUTHOR_NAME }}</name>\n"
        "    <email>{{ AUTHOR_EMAIL }}</email>\n"
        "  </author>\n"
        "  <subtitle type=\"text\">{{ SITE_TAGLINE }}</subtitle>\n"
        "  {% block listing %}\n"
        "  <entry>\n"
        "    <title type=\"text\">{{ TITLE }}</title>\n"
        "    <id>{{ BASE_URL }}/post/{{ FILENAME }}/</id>\n"
        "    <updated>{{ DATE_FORMATTED }}</updated>\n"
        "    <published>{{ DATE_FORMATTED }}</published>\n"
        "    <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/post/{{ FILENAME }}/\" />\n"
        "    <author>\n"
        "      <name>{{ AUTHOR_NAME }}</name>\n"
        "      <email>{{ AUTHOR_EMAIL }}</email>\n"
        "    </author>\n"
        "    <content type=\"html\"><![CDATA[{{ CONTENT }}]]></content>\n"
        "  </entry>\n"
        "  {% endblock %}\n"
        "</feed>\n");

    free(cmp);
    bm_atom_destroy(rv);
    free(rv);
    bc_trie_free(settings->settings);
    free(settings);
}


static void
test_atom_dir(void **state)
{
    bm_settings_t *settings = bc_malloc(sizeof(bm_settings_t));
    settings->settings = bc_trie_new(free);
    bc_trie_insert(settings->settings, "atom_prefix", bc_strdup("atom"));
    bc_trie_insert(settings->settings, "atom_ext", bc_strdup("/index.xml"));
    bc_trie_insert(settings->settings, "post_prefix", bc_strdup("post"));

    bc_error_t *err = NULL;
    char *rv = bm_atom_deploy(settings, &err);

    assert_non_null(rv);
    assert_null(err);

    size_t cmp_len;
    char *cmp = bc_file_get_contents(rv, true, &cmp_len, &err);

    assert_non_null(cmp);
    assert_null(err);

    assert_string_equal(cmp,
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
        "<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"
        "  <title type=\"text\">{{ SITE_TITLE }}{% ifdef FILTER_TAG %} - "
            "{{ FILTER_TAG }}{% endif %}</title>\n"
        "  <id>{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}/{{ FILTER_TAG }}"
        "{% endif %}/index.xml</id>\n"
        "  <updated>{{ DATE_FIRST_FORMATTED }}</updated>\n"
        "  <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/\" />\n"
        "  <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/atom{% ifdef FILTER_TAG %}"
            "/{{ FILTER_TAG }}{% endif %}/index.xml\" rel=\"self\" />\n"
        "  <author>\n"
        "    <name>{{ AUTHOR_NAME }}</name>\n"
        "    <email>{{ AUTHOR_EMAIL }}</email>\n"
        "  </author>\n"
        "  <subtitle type=\"text\">{{ SITE_TAGLINE }}</subtitle>\n"
        "  {% block listing %}\n"
        "  <entry>\n"
        "    <title type=\"text\">{{ TITLE }}</title>\n"
        "    <id>{{ BASE_URL }}/post/{{ FILENAME }}/</id>\n"
        "    <updated>{{ DATE_FORMATTED }}</updated>\n"
        "    <published>{{ DATE_FORMATTED }}</published>\n"
        "    <link href=\"{{ BASE_DOMAIN }}{{ BASE_URL }}/post/{{ FILENAME }}/\" />\n"
        "    <author>\n"
        "      <name>{{ AUTHOR_NAME }}</name>\n"
        "      <email>{{ AUTHOR_EMAIL }}</email>\n"
        "    </author>\n"
        "    <content type=\"html\"><![CDATA[{{ CONTENT }}]]></content>\n"
        "  </entry>\n"
        "  {% endblock %}\n"
        "</feed>\n");

    free(cmp);
    bm_atom_destroy(rv);
    free(rv);
    bc_trie_free(settings->settings);
    free(settings);
}


int
main(void)
{
    const UnitTest tests[] = {
        unit_test(test_atom_file),
        unit_test(test_atom_dir),
    };
    return run_tests(tests);
}