|
| 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 | +} |
0 commit comments