File: test_list.c

package info (click to toggle)
libcleri 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 560 kB
  • sloc: ansic: 4,572; makefile: 51; sh: 40
file content (164 lines) | stat: -rw-r--r-- 4,313 bytes parent folder | download
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
155
156
157
158
159
160
161
162
163
164
#include "../test.h"
#include "../helpers.h"


static int test_list(void)
{
    test_start("list");

    cleri_grammar_t * grammar;
    cleri_t * k_hi, * delimiter, * list;

    k_hi = cleri_keyword(0, "hi", false);
    delimiter = cleri_token(0, ",");
    list = cleri_list(0, k_hi, delimiter, 0, 0, false);
    grammar = cleri_grammar(list, NULL);

    // assert statements
    _assert (list->via.list->min == 0);
    _assert (list->via.list->max == 0);
    _assert (list->via.list->opt_closing == false);
    _assert_is_valid (grammar, "hi, hi, hi");
    _assert_is_valid (grammar, "hi");
    _assert_is_valid (grammar, "");
    _assert_is_not_valid (grammar, "hi,");
    _assert_parse_str (
        grammar,
        "hi.",
        "error at line 1, position 2, "
        "unexpected character `.`, expecting: , or end_of_statement",
        NULL);
    _assert_parse_str2 (
        grammar,
        "hi.",
        "error at line 1, position 2, unexpected character `.`",
        NULL);

    /* check if list children is really NULL */
    {
        cleri_parse_t * pr = cleri_parse(grammar, "");
        _assert (pr);
        _assert (pr->is_valid);
        _assert (pr->tree->children->cl_obj->tp == CLERI_TP_LIST);
        _assert (pr->tree->children->children == NULL);
        cleri_parse_free(pr);
    }

    cleri_grammar_free(grammar);

    return test_end();
}

static int test_list_all_options(void)
{
    test_start("list (all_options)");

    cleri_grammar_t * grammar;
    cleri_t * k_hi, * delimiter, * list;

    k_hi = cleri_keyword(0, "hi", false);
    delimiter = cleri_token(0, "-");
    list = cleri_list(0, k_hi, delimiter, 1, 3, true);
    grammar = cleri_grammar(list, NULL);

    // assert statements
    _assert (list->via.list->min == 1);
    _assert (list->via.list->max == 3);
    _assert (list->via.list->opt_closing == true);
    _assert_is_valid (grammar, "hi - hi - hi");
    _assert_is_valid (grammar, "hi-hi-hi-");
    _assert_is_valid (grammar, "hi-");
    _assert_is_valid (grammar, "hi");
    _assert_is_not_valid (grammar, "");
    _assert_is_not_valid (grammar, "-");
    _assert_is_not_valid (grammar, "hi-hi-hi-hi");
    _assert_parse_str (
        grammar,
        "hi-hi-hi-hi-hi",
        "error at line 1, position 9, "
        "unexpected `hi`, expecting: end_of_statement",
        NULL);
    _assert_parse_str (
        grammar,
        "hi.",
        "error at line 1, position 2"
        ", unexpected character `.`, expecting: - or end_of_statement",
        NULL);
    _assert_parse_str (
        grammar,
        "",
        "error at line 1, position 0, expecting: hi",
        NULL);
    _assert_parse_str2 (
        grammar,
        "hi-hi-hi-hi-hi",
        "error at line 1, position 9, unexpected `hi`",
        NULL);
    _assert_parse_str2 (
        grammar,
        "hi.",
        "error at line 1, position 2, unexpected character `.`",
        NULL);
    _assert_parse_str2 (
        grammar,
        "",
        "error at line 1, position 0",
        NULL);
    cleri_grammar_free(grammar);

    return test_end();
}

static int test_list_vs_single(void)
{
    test_start("list (vs_single)");

    cleri_grammar_t * grammar;
    cleri_t * k_hi, * delimiter, * list, * choice;

    k_hi = cleri_keyword(0, "hi", false);
    delimiter = cleri_token(0, ",");
    list = cleri_list(0, k_hi, delimiter, 1, 0, false);
    choice = cleri_choice(0, true, 2, k_hi, list);

    grammar = cleri_grammar(choice, NULL);

    // assert statements
    _assert_is_valid (grammar, "hi, hi, hi");
    _assert_parse_str (
        grammar,
        "hi, hello",
        "error at line 1, position 4, unexpected `hello`, expecting: hi",
        NULL);
    _assert_parse_str (
        grammar,
        "hello",
        "error at line 1, position 0, unexpected `hello`, expecting: hi",
        NULL);
    _assert_parse_str2 (
        grammar,
        "hi, hello",
        "error at line 1, position 4, unexpected `hello`",
        NULL);
    _assert_parse_str2 (
        grammar,
        "hello",
        "error at line 1, position 0, unexpected `hello`",
        NULL);
    cleri_grammar_free(grammar);

    return test_end();
}




int main()
{
    return (
        test_list() ||
        test_list_all_options() ||
        test_list_vs_single() ||
        0
    );
}