dte test coverage


Directory: ./
File: test/editorconfig.c
Date: 2024-12-21 16:03:22
Exec Total Coverage
Lines: 109 109 100.0%
Functions: 3 3 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 #include <stdlib.h>
2 #include "test.h"
3 #include "editorconfig/editorconfig.h"
4 #include "editorconfig/ini.h"
5 #include "editorconfig/match.h"
6 #include "util/path.h"
7
8 1 static void test_ini_parse(TestContext *ctx)
9 {
10 1 static const char input[] =
11 " \t key = val \n"
12 "\n"
13 " \t [section 1] \n"
14 "xyz = 123\n"
15 "\tfoo bar = this;is#not#a;comment\n"
16 "[section 2]\n"
17 " x=0"
18 ;
19
20 1 IniParser ini = {
21 .input = input,
22 .input_len = sizeof(input) - 1,
23 };
24
25 1 ASSERT_TRUE(ini_parse(&ini));
26 1 ASSERT_NONNULL(ini.name.data);
27 1 ASSERT_NONNULL(ini.value.data);
28 1 EXPECT_NULL(ini.section.data);
29 1 EXPECT_EQ(ini.pos, 17);
30 1 EXPECT_EQ(ini.name_count, 1);
31 1 EXPECT_TRUE(strview_equal_cstring(&ini.name, "key"));
32 1 EXPECT_TRUE(strview_equal_cstring(&ini.value, "val"));
33
34 1 ASSERT_TRUE(ini_parse(&ini));
35 1 ASSERT_NONNULL(ini.name.data);
36 1 ASSERT_NONNULL(ini.value.data);
37 1 ASSERT_NONNULL(ini.section.data);
38 1 EXPECT_EQ(ini.pos, 45);
39 1 EXPECT_EQ(ini.name_count, 1);
40 1 EXPECT_TRUE(strview_equal_cstring(&ini.section, "section 1"));
41 1 EXPECT_TRUE(strview_equal_cstring(&ini.name, "xyz"));
42 1 EXPECT_TRUE(strview_equal_cstring(&ini.value, "123"));
43
44 1 ASSERT_TRUE(ini_parse(&ini));
45 1 ASSERT_NONNULL(ini.name.data);
46 1 ASSERT_NONNULL(ini.value.data);
47 1 ASSERT_NONNULL(ini.section.data);
48 1 EXPECT_EQ(ini.pos, 78);
49 1 EXPECT_EQ(ini.name_count, 2);
50 1 EXPECT_TRUE(strview_equal_cstring(&ini.section, "section 1"));
51 1 EXPECT_TRUE(strview_equal_cstring(&ini.name, "foo bar"));
52 1 EXPECT_TRUE(strview_equal_cstring(&ini.value, "this;is#not#a;comment"));
53
54 1 ASSERT_TRUE(ini_parse(&ini));
55 1 ASSERT_NONNULL(ini.name.data);
56 1 ASSERT_NONNULL(ini.value.data);
57 1 ASSERT_NONNULL(ini.section.data);
58 1 EXPECT_EQ(ini.pos, 94);
59 1 EXPECT_EQ(ini.name_count, 1);
60 1 EXPECT_TRUE(strview_equal_cstring(&ini.section, "section 2"));
61 1 EXPECT_TRUE(strview_equal_cstring(&ini.name, "x"));
62 1 EXPECT_TRUE(strview_equal_cstring(&ini.value, "0"));
63
64 1 EXPECT_FALSE(ini_parse(&ini));
65 1 }
66
67 1 static void test_editorconfig_pattern_match(TestContext *ctx)
68 {
69 #define patmatch(s, f) (ec_pattern_match(s, STRLEN(s), f))
70
71 1 EXPECT_TRUE(patmatch("*", "file.c"));
72 1 EXPECT_TRUE(patmatch("*.{c,h}", "file.c"));
73 1 EXPECT_TRUE(patmatch("*.{foo}", "file.foo"));
74
75 1 EXPECT_TRUE(patmatch("*.{foo{bar,baz}}", "file.foobaz"));
76 1 EXPECT_FALSE(patmatch("*.{foo{bar,baz}}", "file.foo"));
77
78 1 EXPECT_TRUE(patmatch("a/**/b/c/*.[ch]", "a/zzz/yyy/foo/b/c/file.h"));
79 1 EXPECT_FALSE(patmatch("a/*/b/c/*.[ch]", "a/zzz/yyy/foo/b/c/file.h"));
80
81 1 EXPECT_TRUE(patmatch("}*.{x,y}", "}foo.y"));
82 1 EXPECT_FALSE(patmatch("}*.{x,y}", "foo.y"));
83 1 EXPECT_TRUE(patmatch("{}*.{x,y}", "foo.y"));
84
85 1 EXPECT_TRUE(patmatch("*.[xyz]", "foo.z"));
86 1 EXPECT_FALSE(patmatch("*.[xyz", "foo.z"));
87 1 EXPECT_TRUE(patmatch("*.[xyz", "foo.[xyz"));
88
89 1 EXPECT_TRUE(patmatch("*.[!xyz]", "foo.a"));
90 1 EXPECT_FALSE(patmatch("*.[!xyz]", "foo.z"));
91
92 1 EXPECT_TRUE(patmatch("*.[", "foo.["));
93 1 EXPECT_TRUE(patmatch("*.[a", "foo.[a"));
94
95 1 EXPECT_TRUE(patmatch("*.[abc]def", "foo.bdef"));
96
97 1 EXPECT_TRUE(patmatch("x{{foo,},}", "x"));
98 1 EXPECT_TRUE(patmatch("x{{foo,},}", "xfoo"));
99
100 1 EXPECT_TRUE(patmatch("file.{,,x,,y,,}", "file.x"));
101 1 EXPECT_TRUE(patmatch("file.{,,x,,y,,}", "file."));
102 1 EXPECT_FALSE(patmatch("file.{,,x,,y,,}", "file.z"));
103
104 1 EXPECT_TRUE(patmatch("*.x,y,z", "file.x,y,z"));
105 1 EXPECT_TRUE(patmatch("*.{x,y,z}", "file.y"));
106 1 EXPECT_FALSE(patmatch("*.{x,y,z}", "file.x,y,z"));
107 1 EXPECT_FALSE(patmatch("*.{x,y,z}", "file.{x,y,z}"));
108
109 1 EXPECT_TRUE(patmatch("file.{{{a,b,{c,,d}}}}", "file.d"));
110 1 EXPECT_TRUE(patmatch("file.{{{a,b,{c,,d}}}}", "file."));
111 1 EXPECT_FALSE(patmatch("file.{{{a,b,{c,d}}}}", "file."));
112
113 1 EXPECT_TRUE(patmatch("file.{c[vl]d,inc}", "file.cvd"));
114 1 EXPECT_TRUE(patmatch("file.{c[vl]d,inc}", "file.cld"));
115 1 EXPECT_TRUE(patmatch("file.{c[vl]d,inc}", "file.inc"));
116 1 EXPECT_FALSE(patmatch("file.{c[vl]d,inc}", "file.cd"));
117
118 1 EXPECT_TRUE(patmatch("a?b.c", "a_b.c"));
119 1 EXPECT_FALSE(patmatch("a?b.c", "a/b.c"));
120
121 1 EXPECT_TRUE(patmatch("a\\[.abc", "a[.abc"));
122 1 EXPECT_TRUE(patmatch("a\\{.abc", "a{.abc"));
123 1 EXPECT_TRUE(patmatch("a\\*.abc", "a*.abc"));
124 1 EXPECT_TRUE(patmatch("a\\?.abc", "a?.abc"));
125 1 EXPECT_FALSE(patmatch("a\\*.abc", "az.abc"));
126 1 EXPECT_FALSE(patmatch("a\\?.abc", "az.abc"));
127
128 1 EXPECT_TRUE(patmatch("{{{a}}}", "a"));
129 1 EXPECT_FALSE(patmatch("{{{a}}", "a"));
130
131 // It's debatable whether this edge case behavior is sensible,
132 // but it's tested here anyway for the sake of UBSan coverage
133 1 EXPECT_TRUE(patmatch("*.xyz\\", "file.xyz\\"));
134 1 EXPECT_FALSE(patmatch("*.xyz\\", "file.xyz"));
135
136 #undef patmatch
137 1 }
138
139 1 static void test_get_editorconfig_options(TestContext *ctx)
140 {
141 1 EditorConfigOptions opts = editorconfig_options_init();
142 1 char *path = path_absolute("test/data/file.0foo.z");
143 1 ASSERT_NONNULL(path);
144 1 EXPECT_EQ(get_editorconfig_options(path, &opts), 0);
145 1 free(path);
146 1 EXPECT_EQ(opts.indent_style, INDENT_STYLE_SPACE);
147 1 EXPECT_EQ(opts.indent_size, 3);
148 1 EXPECT_EQ(opts.tab_width, 3);
149 1 EXPECT_EQ(opts.max_line_length, 68);
150 1 EXPECT_FALSE(opts.indent_size_is_tab);
151
152 1 path = path_absolute("test/data/file.foo");
153 1 ASSERT_NONNULL(path);
154 1 EXPECT_EQ(get_editorconfig_options(path, &opts), 0);
155 1 free(path);
156 1 EXPECT_EQ(opts.indent_style, INDENT_STYLE_UNSPECIFIED);
157 1 EXPECT_EQ(opts.indent_size, 0);
158 1 EXPECT_EQ(opts.tab_width, 0);
159 1 EXPECT_EQ(opts.max_line_length, 0);
160 1 EXPECT_FALSE(opts.indent_size_is_tab);
161 1 }
162
163 static const TestEntry tests[] = {
164 TEST(test_ini_parse),
165 TEST(test_editorconfig_pattern_match),
166 TEST(test_get_editorconfig_options),
167 };
168
169 const TestGroup editorconfig_tests = TEST_GROUP(tests);
170