Skip to content

Commit 3dd08d5

Browse files
committed
Add tests for the IgnoreMatcher
1 parent 521f8ce commit 3dd08d5

3 files changed

Lines changed: 168 additions & 1 deletion

File tree

‎src/meson.build‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
ignore_matcher_library = static_library(
2+
'ignore-matcher',
3+
'IgnoreMatcher.vala',
4+
vala_vapi: 'ignore-matcher.vapi',
5+
dependencies: [
6+
vala_linter_dep,
7+
posix_dep
8+
],
9+
install: false
10+
)
11+
12+
ignore_matcher_dep = declare_dependency(
13+
link_with: ignore_matcher_library,
14+
dependencies: [
15+
vala_linter_dep,
16+
posix_dep
17+
],
18+
include_directories: include_directories('.')
19+
)
20+
121
app_files = files (
222
'Application.vala',
323
'FileData.vala',
4-
'IgnoreMatcher.vala'
524
)
625

726
vala_lint = executable(
827
meson.project_name(),
928
app_files,
1029
dependencies: [
1130
vala_linter_dep,
31+
ignore_matcher_dep,
1232
posix_dep,
1333
json_dep
1434
],

‎test/IgnoreMatcherTest.vala‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2026 Colin Kiama
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public
15+
* License along with this program; if not, write to the
16+
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17+
* Boston, MA 02110-1301 USA.
18+
*/
19+
20+
class IgnoreMatcherTest : GLib.Object {
21+
ValaLint.IgnoreMatcher new_matcher (string[] patterns) {
22+
var matcher = new ValaLint.IgnoreMatcher (Posix.FNM_PERIOD | Posix.FNM_PATHNAME);
23+
foreach (var pattern in patterns) {
24+
matcher.add_pattern (pattern);
25+
}
26+
27+
return matcher;
28+
}
29+
30+
void test_literal_match () {
31+
var matcher = new_matcher ({ "build" });
32+
33+
assert (matcher.matches ("build"));
34+
assert (!matcher.matches ("build2"));
35+
36+
// FNM_PATHNAME means a bare directory name must not match a nested path.
37+
assert (!matcher.matches ("build/foo.c"));
38+
}
39+
40+
void test_wildcard_does_not_cross_path_separator () {
41+
var matcher = new_matcher ({ "*.vala" });
42+
43+
assert (matcher.matches ("Foo.vala"));
44+
assert (!matcher.matches ("src/Foo.vala"));
45+
}
46+
47+
void test_question_mark_matches_single_char () {
48+
var matcher = new_matcher ({ "?.vala" });
49+
50+
assert (matcher.matches ("a.vala"));
51+
assert (!matcher.matches ("ab.vala"));
52+
}
53+
54+
void test_period_flag_excludes_leading_dot_files () {
55+
var matcher = new_matcher ({ "*.vala" });
56+
57+
assert (!matcher.matches (".hidden.vala"));
58+
59+
var matcher_without_period = new ValaLint.IgnoreMatcher (Posix.FNM_PATHNAME);
60+
matcher_without_period.add_pattern ("*.vala");
61+
62+
assert (matcher_without_period.matches (".hidden.vala"));
63+
}
64+
65+
void test_extglob_one_or_more_group () {
66+
var matcher = new_matcher ({ "+(foo|bar).vala" });
67+
68+
assert (matcher.matches ("foo.vala"));
69+
assert (matcher.matches ("bar.vala"));
70+
assert (!matcher.matches ("baz.vala"));
71+
}
72+
73+
void test_extglob_optional_group () {
74+
var matcher = new_matcher ({ "ignore?(d).vala" });
75+
76+
assert (matcher.matches ("ignore.vala"));
77+
assert (matcher.matches ("ignored.vala"));
78+
assert (!matcher.matches ("ignores.vala"));
79+
}
80+
81+
void test_extglob_exactly_one_group () {
82+
var matcher = new_matcher ({ "@(foo|bar).vala" });
83+
84+
assert (matcher.matches ("foo.vala"));
85+
assert (matcher.matches ("bar.vala"));
86+
assert (!matcher.matches ("foobar.vala"));
87+
}
88+
89+
void test_bracket_expression () {
90+
var matcher = new_matcher ({ "[abc].vala" });
91+
92+
assert (matcher.matches ("a.vala"));
93+
assert (matcher.matches ("b.vala"));
94+
assert (!matcher.matches ("d.vala"));
95+
}
96+
97+
void test_negated_bracket_expression () {
98+
var matcher = new_matcher ({ "[!abc].vala" });
99+
100+
assert (!matcher.matches ("a.vala"));
101+
assert (!matcher.matches ("b.vala"));
102+
assert (matcher.matches ("d.vala"));
103+
}
104+
105+
void test_blank_and_comment_patterns_are_ignored () {
106+
var matcher = new_matcher ({ "", "#build" });
107+
108+
assert (!matcher.matches (""));
109+
assert (!matcher.matches ("#build"));
110+
assert (!matcher.matches ("build"));
111+
}
112+
113+
void test_multiple_patterns_are_matched_with_or () {
114+
var matcher = new_matcher ({ "build", "po", "~*.vala" });
115+
116+
assert (matcher.matches ("build"));
117+
assert (matcher.matches ("po"));
118+
assert (matcher.matches ("~backup.vala"));
119+
assert (!matcher.matches ("src/Foo.vala"));
120+
}
121+
122+
public static int main (string[] args) {
123+
var test = new IgnoreMatcherTest ();
124+
125+
test.test_literal_match ();
126+
test.test_wildcard_does_not_cross_path_separator ();
127+
test.test_question_mark_matches_single_char ();
128+
test.test_period_flag_excludes_leading_dot_files ();
129+
test.test_extglob_one_or_more_group ();
130+
test.test_extglob_optional_group ();
131+
test.test_extglob_exactly_one_group ();
132+
test.test_bracket_expression ();
133+
test.test_negated_bracket_expression ();
134+
test.test_blank_and_comment_patterns_are_ignored ();
135+
test.test_multiple_patterns_are_matched_with_or ();
136+
137+
return 0;
138+
}
139+
}

‎test/meson.build‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,20 @@ auto_fix_test = executable(
6262
dependencies: test_deps
6363
)
6464

65+
ignore_matcher_test = executable(
66+
'ignore-matcher-test',
67+
'IgnoreMatcherTest.vala',
68+
vala_args: vala_args,
69+
dependencies: test_deps + [ignore_matcher_dep]
70+
)
71+
6572
test_envars = [
6673
'G_DEBUG=fatal-warnings',
6774
]
6875

6976
test('file', file_test, env: test_envars)
7077
test('content', content_test, env: test_envars)
7178
test('auto-fix', auto_fix_test, env: test_envars)
79+
test('ignore-matcher', ignore_matcher_test, env: test_envars)
7280

7381
test('self', vala_lint, workdir: meson.current_source_dir(), env: test_envars)

0 commit comments

Comments
 (0)