dte test coverage


Directory: ./
File: test/syntax.c
Date: 2025-02-14 16:55:22
Exec Total Coverage
Lines: 155 158 98.1%
Functions: 2 2 100.0%
Branches: 10 12 83.3%

Line Branch Exec Source
1 #include "test.h"
2 #include "block-iter.h"
3 #include "config.h"
4 #include "editor.h"
5 #include "encoding.h"
6 #include "syntax/bitset.h"
7 #include "syntax/highlight.h"
8 #include "util/log.h"
9 #include "util/utf8.h"
10 #include "window.h"
11
12 1 static void test_bitset(TestContext *ctx)
13 {
14 1 BitSetWord set[BITSET_NR_WORDS(256)];
15 1 ASSERT_TRUE(sizeof(set) >= 32);
16
17 1 memset(set, 0, sizeof(set));
18 1 EXPECT_FALSE(bitset_contains(set, '0'));
19 1 EXPECT_FALSE(bitset_contains(set, 'a'));
20 1 EXPECT_FALSE(bitset_contains(set, 'z'));
21 1 EXPECT_FALSE(bitset_contains(set, '!'));
22 1 EXPECT_FALSE(bitset_contains(set, '\0'));
23
24 1 bitset_add_char_range(set, "0-9a-fxy");
25 1 EXPECT_TRUE(bitset_contains(set, '0'));
26 1 EXPECT_TRUE(bitset_contains(set, '8'));
27 1 EXPECT_TRUE(bitset_contains(set, '9'));
28 1 EXPECT_TRUE(bitset_contains(set, 'a'));
29 1 EXPECT_TRUE(bitset_contains(set, 'b'));
30 1 EXPECT_TRUE(bitset_contains(set, 'f'));
31 1 EXPECT_TRUE(bitset_contains(set, 'x'));
32 1 EXPECT_TRUE(bitset_contains(set, 'y'));
33 1 EXPECT_FALSE(bitset_contains(set, 'g'));
34 1 EXPECT_FALSE(bitset_contains(set, 'z'));
35 1 EXPECT_FALSE(bitset_contains(set, 'A'));
36 1 EXPECT_FALSE(bitset_contains(set, 'F'));
37 1 EXPECT_FALSE(bitset_contains(set, 'X'));
38 1 EXPECT_FALSE(bitset_contains(set, 'Z'));
39 1 EXPECT_FALSE(bitset_contains(set, '{'));
40 1 EXPECT_FALSE(bitset_contains(set, '`'));
41 1 EXPECT_FALSE(bitset_contains(set, '/'));
42 1 EXPECT_FALSE(bitset_contains(set, ':'));
43 1 EXPECT_FALSE(bitset_contains(set, '\0'));
44
45 1 BITSET_INVERT(set);
46 1 EXPECT_FALSE(bitset_contains(set, '0'));
47 1 EXPECT_FALSE(bitset_contains(set, '8'));
48 1 EXPECT_FALSE(bitset_contains(set, '9'));
49 1 EXPECT_FALSE(bitset_contains(set, 'a'));
50 1 EXPECT_FALSE(bitset_contains(set, 'b'));
51 1 EXPECT_FALSE(bitset_contains(set, 'f'));
52 1 EXPECT_FALSE(bitset_contains(set, 'x'));
53 1 EXPECT_FALSE(bitset_contains(set, 'y'));
54 1 EXPECT_TRUE(bitset_contains(set, 'g'));
55 1 EXPECT_TRUE(bitset_contains(set, 'z'));
56 1 EXPECT_TRUE(bitset_contains(set, 'A'));
57 1 EXPECT_TRUE(bitset_contains(set, 'F'));
58 1 EXPECT_TRUE(bitset_contains(set, 'X'));
59 1 EXPECT_TRUE(bitset_contains(set, 'Z'));
60 1 EXPECT_TRUE(bitset_contains(set, '{'));
61 1 EXPECT_TRUE(bitset_contains(set, '`'));
62 1 EXPECT_TRUE(bitset_contains(set, '/'));
63 1 EXPECT_TRUE(bitset_contains(set, ':'));
64 1 EXPECT_TRUE(bitset_contains(set, '\0'));
65
66 1 memset(set, 0, sizeof(set));
67 1 bitset_add_char_range(set, "\1-?r-r^-b");
68 1 EXPECT_TRUE(bitset_contains(set, '\1'));
69 1 EXPECT_TRUE(bitset_contains(set, '\2'));
70 1 EXPECT_TRUE(bitset_contains(set, ' '));
71 1 EXPECT_TRUE(bitset_contains(set, '!'));
72 1 EXPECT_TRUE(bitset_contains(set, '>'));
73 1 EXPECT_TRUE(bitset_contains(set, '?'));
74 1 EXPECT_TRUE(bitset_contains(set, 'r'));
75 1 EXPECT_TRUE(bitset_contains(set, '^'));
76 1 EXPECT_TRUE(bitset_contains(set, '_'));
77 1 EXPECT_TRUE(bitset_contains(set, '`'));
78 1 EXPECT_TRUE(bitset_contains(set, 'a'));
79 1 EXPECT_TRUE(bitset_contains(set, 'b'));
80 1 EXPECT_FALSE(bitset_contains(set, '\0'));
81 1 EXPECT_FALSE(bitset_contains(set, '@'));
82 1 EXPECT_FALSE(bitset_contains(set, 'c'));
83 1 EXPECT_FALSE(bitset_contains(set, 'q'));
84 1 EXPECT_FALSE(bitset_contains(set, 's'));
85 1 EXPECT_FALSE(bitset_contains(set, 'A'));
86
87 1 memset(set, 0, sizeof(set));
88 1 bitset_add_char_range(set, "\x03-\xFC");
89 1 EXPECT_TRUE(bitset_contains(set, '\x03'));
90 1 EXPECT_TRUE(bitset_contains(set, '\x40'));
91 1 EXPECT_TRUE(bitset_contains(set, '\x7F'));
92 1 EXPECT_TRUE(bitset_contains(set, '\x80'));
93 1 EXPECT_TRUE(bitset_contains(set, '\xFC'));
94 1 EXPECT_FALSE(bitset_contains(set, '\x00'));
95 1 EXPECT_FALSE(bitset_contains(set, '\x01'));
96 1 EXPECT_FALSE(bitset_contains(set, '\x02'));
97 1 EXPECT_FALSE(bitset_contains(set, '\xFE'));
98 1 EXPECT_FALSE(bitset_contains(set, '\xFF'));
99
2/2
✓ Branch 0 (81→79) taken 250 times.
✓ Branch 1 (81→82) taken 1 times.
251 for (unsigned int i = 3; i <= 0xFC; i++) {
100 250 IEXPECT_TRUE(bitset_contains(set, i));
101 }
102
103 1 memset(set, 0, sizeof(set));
104 1 bitset_add_char_range(set, "?-@");
105 1 EXPECT_TRUE(bitset_contains(set, '?'));
106 1 EXPECT_TRUE(bitset_contains(set, '@'));
107 1 EXPECT_FALSE(bitset_contains(set, '>'));
108 1 EXPECT_FALSE(bitset_contains(set, 'A'));
109
110 1 memset(set, 0, sizeof(set));
111 1 bitset_add_char_range(set, "z-a");
112
2/2
✓ Branch 0 (91→89) taken 4 times.
✓ Branch 1 (91→92) taken 1 times.
5 FOR_EACH_I(i, set) {
113 4 EXPECT_UINT_EQ(set[i], 0);
114 }
115
116 1 BITSET_INVERT(set);
117
2/2
✓ Branch 0 (96→94) taken 4 times.
✓ Branch 1 (96→97) taken 1 times.
5 FOR_EACH_I(i, set) {
118 4 EXPECT_UINT_EQ(set[i], bitset_word_max());
119 }
120 1 }
121
122 1 static void test_hl_line(TestContext *ctx)
123 {
124
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 1 times.
1 if (!get_builtin_config("syntax/c")) {
125 LOG_INFO("syntax/c not available; skipping %s()", __func__);
126 return;
127 }
128
129 1 EditorState *e = ctx->userdata;
130 1 Window *window = e->window;
131 1 ASSERT_NONNULL(window);
132 1 View *view = window_open_file(window, "test/data/test.c", NULL);
133 1 ASSERT_NONNULL(view);
134 1 Buffer *buffer = view->buffer;
135 1 ASSERT_NONNULL(buffer);
136 1 const size_t line_nr = 5;
137 1 ASSERT_TRUE(buffer->nl >= line_nr);
138
139 1 Syntax *syn = buffer->syntax;
140 1 ASSERT_NONNULL(syn);
141 1 ASSERT_NONNULL(syn->start_state);
142 1 EXPECT_STREQ(syn->name, "c");
143 1 EXPECT_FALSE(syn->heredoc);
144
145 1 const StyleMap *styles = &e->styles;
146 1 PointerArray *lss = &buffer->line_start_states;
147 1 BlockIter tmp = block_iter(buffer);
148 1 hl_fill_start_states(syn, lss, styles, &tmp, buffer->nl);
149 1 block_iter_goto_line(&view->cursor, line_nr - 1);
150 1 view_update(view);
151 1 ASSERT_EQ(view->cx, 0);
152 1 ASSERT_EQ(view->cy, line_nr - 1);
153
154 1 StringView line;
155 1 fetch_this_line(&view->cursor, &line);
156 1 ASSERT_EQ(line.length, 65);
157
158 1 bool next_changed;
159 1 const TermStyle **hl = hl_line(syn, lss, styles, &line, line_nr, &next_changed);
160 1 ASSERT_NONNULL(hl);
161 1 EXPECT_TRUE(next_changed);
162
163 1 const TermStyle *t = find_style(styles, "text");
164 1 const TermStyle *c = find_style(styles, "constant");
165 1 const TermStyle *s = find_style(styles, "string");
166 1 const TermStyle *x = find_style(styles, "special");
167 1 const TermStyle *n = find_style(styles, "numeric");
168 1 const TermStyle *y = find_style(styles, "type");
169 1 ASSERT_NONNULL(t);
170 1 ASSERT_NONNULL(c);
171 1 ASSERT_NONNULL(s);
172 1 ASSERT_NONNULL(x);
173 1 ASSERT_NONNULL(n);
174 1 ASSERT_NONNULL(y);
175 1 EXPECT_EQ(t->fg, COLOR_DEFAULT);
176 1 EXPECT_EQ(c->fg, COLOR_CYAN);
177 1 EXPECT_EQ(s->fg, COLOR_YELLOW);
178 1 EXPECT_EQ(x->fg, COLOR_MAGENTA);
179 1 EXPECT_EQ(n->fg, COLOR_BLUE);
180 1 EXPECT_EQ(y->fg, COLOR_GREEN);
181
182 1 const TermStyle *const expected_styles[] = {
183 t, t, t, t, t, t, t, t, t, t, t, t, c, c, c, c,
184 c, c, t, t, s, s, s, s, s, s, s, s, s, s, s, s,
185 s, s, s, s, x, x, s, t, t, s, s, s, s, s, t, t,
186 x, x, x, t, t, t, y, y, y, y, y, y, t, n, n, t,
187 t
188 };
189
190 1 size_t i = 0;
191
2/2
✓ Branch 0 (49→43) taken 65 times.
✓ Branch 1 (49→50) taken 1 times.
66 for (size_t pos = 0; pos < line.length; i++) {
192 65 CodePoint u = u_get_char(line.data, line.length, &pos);
193 65 IEXPECT_EQ(u, line.data[i]);
194
1/2
✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→47) taken 65 times.
65 if (i >= ARRAYLEN(expected_styles)) {
195 continue;
196 }
197 65 IEXPECT_TRUE(same_style(hl[i], expected_styles[i]));
198 }
199
200 1 EXPECT_EQ(i, ARRAYLEN(expected_styles));
201 1 window_close(window);
202 }
203
204 static const TestEntry tests[] = {
205 TEST(test_bitset),
206 TEST(test_hl_line),
207 };
208
209 const TestGroup syntax_tests = TEST_GROUP(tests);
210