File: test_repeat.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 (121 lines) | stat: -rw-r--r-- 3,000 bytes parent folder | download | duplicates (2)
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
#include "../test.h"
#include "../helpers.h"


static const char * translate(cleri_t * o __attribute__((unused)))
{
    /* this should ignore expecting: ... */
    return "";
}

static int test_repeat(void)
{
    test_start("repeat");

    cleri_grammar_t * grammar;
    cleri_t * k_hi, * repeat;

    k_hi = cleri_keyword(0, "hi", false);
    repeat = cleri_repeat(0, k_hi, 0, 0);
    grammar = cleri_grammar(repeat, NULL);

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

    return test_end();
}

static int test_repeat_all_options(void)
{
    test_start("repeat (all_options)");

    cleri_grammar_t * grammar;
    cleri_t * k_hi, * repeat;

    k_hi = cleri_keyword(0, "hi", false);
    repeat = cleri_repeat(0, k_hi, 1, 3);
    grammar = cleri_grammar(repeat, NULL);

    // assert statements
    _assert (repeat->via.repeat->min == 1);
    _assert (repeat->via.repeat->max == 3);
    _assert_is_valid (grammar, "hi  hi hi");
    _assert_is_valid (grammar, "hi");
    _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 8, expecting: end_of_statement",
        NULL);
    _assert_parse_str (
        grammar,
        "hi.",
        "error at line 1, position 2, "
        "unexpected character `.`, expecting: hi 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 8",
        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();
}

int main()
{
    return (
        test_repeat() ||
        test_repeat_all_options() ||
        0
    );
}