aboutsummaryrefslogtreecommitdiffstats
path: root/tests/blogc/check_filelist_parser.c
blob: 5883eb731a1bb5cc79d5de8c4936f0793a503e8b (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2020 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 <string.h>
#include <stdlib.h>
#include "../../src/common/utils.h"
#include "../../src/blogc/filelist-parser.h"


static void
test_filelist_parse_empty(void **state)
{
    bc_slist_t *l = blogc_filelist_parse("", 0);
    assert_null(l);
}


static void
test_filelist_parse(void **state)
{
    const char *a =
        "content/post/post1.txt";
    bc_slist_t *l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_null(l->next);
    bc_slist_free_full(l, free);
    a =
        "content/post/post1.txt\n";
    l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_null(l->next);
    bc_slist_free_full(l, free);
    a =
        "content/post/post1.txt\n"
        "content/post/post2.txt\n";
    l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_string_equal(l->next->data, "content/post/post2.txt");
    assert_null(l->next->next);
    bc_slist_free_full(l, free);
    a =
        "content/post/post1.txt\n"
        "content/post/post2.txt\n"
        "content/post/post3.txt";
    l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_string_equal(l->next->data, "content/post/post2.txt");
    assert_string_equal(l->next->next->data, "content/post/post3.txt");
    assert_null(l->next->next->next);
    bc_slist_free_full(l, free);
    a =
        "   content/post/post1.txt\n"
        "content/post/post2.txt\t\n"
        "\tcontent/post/post3.txt";
    l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_string_equal(l->next->data, "content/post/post2.txt");
    assert_string_equal(l->next->next->data, "content/post/post3.txt");
    assert_null(l->next->next->next);
    bc_slist_free_full(l, free);
}


static void
test_filelist_parse_crlf(void **state)
{
    const char *a =
        "content/post/post1.txt\r\n";
    bc_slist_t *l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_null(l->next);
    bc_slist_free_full(l, free);
    a =
        "content/post/post1.txt\r\n"
        "content/post/post2.txt\r\n";
    l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_string_equal(l->next->data, "content/post/post2.txt");
    assert_null(l->next->next);
    bc_slist_free_full(l, free);
    a =
        "content/post/post1.txt\r\n"
        "content/post/post2.txt\r\n"
        "content/post/post3.txt";
    l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_string_equal(l->next->data, "content/post/post2.txt");
    assert_string_equal(l->next->next->data, "content/post/post3.txt");
    assert_null(l->next->next->next);
    bc_slist_free_full(l, free);
    a =
        "   content/post/post1.txt\r\n"
        "content/post/post2.txt\t\r\n"
        "\tcontent/post/post3.txt";
    l = blogc_filelist_parse(a, strlen(a));
    assert_non_null(l);
    assert_string_equal(l->data, "content/post/post1.txt");
    assert_string_equal(l->next->data, "content/post/post2.txt");
    assert_string_equal(l->next->next->data, "content/post/post3.txt");
    assert_null(l->next->next->next);
    bc_slist_free_full(l, free);
}


int
main(void)
{
    const UnitTest tests[] = {
        unit_test(test_filelist_parse_empty),
        unit_test(test_filelist_parse),
        unit_test(test_filelist_parse_crlf),
    };
    return run_tests(tests);
}