Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | package glob |
| 6 | |
| 7 | import ( |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func same(a, b []string) bool { |
| 12 | if len(a) != len(b) { |
| 13 | return false |
| 14 | } |
| 15 | for i := range a { |
| 16 | if a[i] != b[i] { |
| 17 | return false |
| 18 | } |
| 19 | } |
| 20 | return true |
| 21 | } |
| 22 | |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 23 | func TestStripFixedElements(t *testing.T) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 24 | tests := []struct { |
| 25 | pattern string |
| 26 | fixed []string |
| 27 | rest string |
| 28 | }{ |
| 29 | {"*", nil, "*"}, |
| 30 | {"a/b/c/*", []string{"a", "b", "c"}, "*"}, |
| 31 | {"a/b/*/...", []string{"a", "b"}, "*/..."}, |
| 32 | {"a/b/c/...", []string{"a", "b", "c"}, "..."}, |
| 33 | {"a/the\\?rain.in\\*spain", []string{"a", "the?rain.in*spain"}, ""}, |
| 34 | } |
| 35 | for _, test := range tests { |
| 36 | g, err := Parse(test.pattern) |
| 37 | if err != nil { |
| 38 | t.Fatalf("parsing %q: %q", test.pattern, err.Error()) |
| 39 | } |
Robin Thellend | 2f1878c | 2015-07-14 14:18:47 -0700 | [diff] [blame] | 40 | if f, ng := g.SplitFixedElements(); !same(f, test.fixed) || test.rest != ng.String() { |
| 41 | t.Fatalf("SplitFixedElements(%q) got %q,%q, expected %q,%q", test.pattern, f, ng.String(), test.fixed, test.rest) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestMatch(t *testing.T) { |
| 47 | tests := []struct { |
| 48 | pattern string |
| 49 | name string |
| 50 | matched bool |
| 51 | }{ |
| 52 | {"...", "", true}, |
| 53 | {"***", "", true}, |
| 54 | {"...", "a", true}, |
| 55 | {"***", "a", true}, |
| 56 | {"a", "", false}, |
| 57 | {"a", "a", true}, |
| 58 | {"a", "b", false}, |
| 59 | {"a*", "a", true}, |
| 60 | {"a*", "b", false}, |
| 61 | {"a*b", "ab", true}, |
| 62 | {"a*b", "afoob", true}, |
| 63 | {"a\\*", "a", false}, |
| 64 | {"a\\*", "a*", true}, |
| 65 | {"\\\\", "\\", true}, |
| 66 | {"?", "?", true}, |
| 67 | {"?", "a", true}, |
| 68 | {"?", "", false}, |
| 69 | {"*?", "", false}, |
| 70 | {"*?", "a", true}, |
| 71 | {"*?", "ab", true}, |
| 72 | {"*?", "abv", true}, |
| 73 | {"[abc]", "a", true}, |
| 74 | {"[abc]", "b", true}, |
| 75 | {"[abc]", "c", true}, |
| 76 | {"[abc]", "d", false}, |
| 77 | {"[a-c]", "a", true}, |
| 78 | {"[a-c]", "b", true}, |
| 79 | {"[a-c]", "c", true}, |
| 80 | {"[a-c]", "d", false}, |
| 81 | {"\\[abc]", "a", false}, |
| 82 | {"\\[abc]", "[abc]", true}, |
| 83 | {"a/*", "a", true}, |
| 84 | {"a/*", "b", false}, |
| 85 | {"a/...", "a", true}, |
| 86 | {"a/...", "b", false}, |
| 87 | } |
| 88 | for i, test := range tests { |
| 89 | g, err := Parse(test.pattern) |
| 90 | if err != nil { |
| 91 | t.Errorf("unexpected parsing error for %q (#%d): %v", test.pattern, i, err) |
| 92 | continue |
| 93 | } |
| 94 | if matched := g.Head().Match(test.name); matched != test.matched { |
| 95 | t.Errorf("unexpected result for %q.Match(%q) (#%d). Got %v, expected %v", test.pattern, test.name, i, matched, test.matched) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | func TestFixedPrefix(t *testing.T) { |
| 101 | tests := []struct { |
| 102 | pattern string |
| 103 | prefix string |
| 104 | full bool |
| 105 | }{ |
| 106 | {"", "", true}, |
| 107 | {"...", "", false}, |
| 108 | {"***", "", false}, |
| 109 | {"...", "", false}, |
| 110 | {"***", "", false}, |
| 111 | {"a", "a", true}, |
| 112 | {"*a", "", false}, |
| 113 | {"a*", "a", false}, |
| 114 | {"a*b", "a", false}, |
| 115 | {"a\\*", "a*", true}, |
| 116 | {"\\\\", "\\", true}, |
| 117 | {"?", "", false}, |
| 118 | {"\\?", "?", true}, |
| 119 | {"*?", "", false}, |
| 120 | {"[abc]", "", false}, |
| 121 | {"\\[abc]", "[abc]", true}, |
| 122 | {"\\[abc]*", "[abc]", false}, |
| 123 | } |
| 124 | for i, test := range tests { |
| 125 | g, err := Parse(test.pattern) |
| 126 | if err != nil { |
| 127 | t.Errorf("unexpected parsing error for %q (#%d): %v", test.pattern, i, err) |
| 128 | continue |
| 129 | } |
| 130 | if prefix, full := g.Head().FixedPrefix(); prefix != test.prefix || full != test.full { |
| 131 | t.Errorf("unexpected result for %q.FixedPrefix() (#%d). Got (%q,%v), expected (%q,%v)", test.pattern, i, prefix, full, test.prefix, test.full) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestBadPattern(t *testing.T) { |
| 137 | tests := []string{"[", "[foo", "[^foo", "\\", "a\\", "abc[foo", "a//b"} |
| 138 | for _, test := range tests { |
| 139 | if _, err := Parse(test); err == nil { |
| 140 | t.Errorf("Unexpected success for %q", test) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 144 | |
| 145 | func TestExactMatch(t *testing.T) { |
| 146 | tests := []struct { |
| 147 | pattern string |
| 148 | elems []string |
| 149 | matched bool |
| 150 | exact bool |
| 151 | }{ |
Tilak Sharma | 9cb954f | 2014-10-09 09:49:36 -0700 | [diff] [blame] | 152 | // Test recursive. |
| 153 | {"...", []string{}, true, true}, |
| 154 | {"...", []string{"a"}, true, false}, |
| 155 | {"a/...", []string{"a"}, true, true}, |
| 156 | {"a/...", []string{"a", "b"}, true, false}, |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 157 | // Test one element, fixed. |
| 158 | {"a", []string{"a"}, true, true}, |
| 159 | {"a", []string{"b"}, false, false}, |
| 160 | {"\\\\", []string{"\\"}, true, true}, |
| 161 | // Test one element, containing *. |
| 162 | {"*", []string{"*"}, true, false}, |
| 163 | {"*", []string{"abc"}, true, false}, |
| 164 | {"\\*", []string{"*"}, true, true}, |
| 165 | {"\\*", []string{"abc"}, false, false}, |
| 166 | {"\\\\*", []string{"\\"}, true, false}, |
| 167 | {"\\\\*", []string{"\\*"}, true, false}, |
| 168 | {"\\\\*", []string{"\\abc"}, true, false}, |
| 169 | // Test one element, containing ?. |
| 170 | {"?", []string{"?"}, true, false}, |
| 171 | {"?", []string{"a"}, true, false}, |
| 172 | {"\\?", []string{"?"}, true, true}, |
| 173 | {"\\?", []string{"a"}, false, false}, |
| 174 | {"\\\\?", []string{"\\"}, false, false}, |
| 175 | {"\\\\?", []string{"\\?"}, true, false}, |
| 176 | {"\\\\?", []string{"\\a"}, true, false}, |
| 177 | // Test one element, containing []. |
| 178 | {"[abc]", []string{"c"}, true, false}, |
| 179 | {"\\[abc\\]", []string{"[abc]"}, true, true}, |
| 180 | {"\\[abc\\]", []string{"a"}, false, false}, |
| 181 | {"\\\\[abc]", []string{"\\a"}, true, false}, |
| 182 | {"\\\\[abc]", []string{"a"}, false, false}, |
| 183 | {"[\\\\]", []string{"\\"}, true, false}, |
| 184 | {"[\\\\]", []string{"a"}, false, false}, |
| 185 | // Test multiple elements. |
| 186 | {"a/b", []string{"a", "b"}, true, true}, |
| 187 | {"a/\\*", []string{"a", "*"}, true, true}, |
| 188 | {"a/\\?", []string{"a", "?"}, true, true}, |
| 189 | {"a/\\[b\\]", []string{"a", "[b]"}, true, true}, |
| 190 | {"a/*", []string{"a", "bc"}, true, false}, |
| 191 | {"a/?", []string{"a", "b"}, true, false}, |
| 192 | {"a/[bc]", []string{"a", "b"}, true, false}, |
| 193 | {"a/*/c", []string{"a", "b", "c"}, true, false}, |
| 194 | {"a/?/c", []string{"a", "b", "c"}, true, false}, |
| 195 | {"a/[bc]/d", []string{"a", "b", "d"}, true, false}, |
| 196 | } |
| 197 | for _, test := range tests { |
| 198 | g, err := Parse(test.pattern) |
| 199 | if err != nil { |
| 200 | t.Fatalf("parsing %q: %q", test.pattern, err.Error()) |
| 201 | } |
| 202 | matched, exact, _ := g.PartialMatch(0, test.elems) |
| 203 | if matched != test.matched || exact != test.exact { |
Tilak Sharma | 9cb954f | 2014-10-09 09:49:36 -0700 | [diff] [blame] | 204 | t.Fatalf("'%v'.PartialMatch(0, '%v') got (%v, %v), expected (%v, %v)", |
Tilak Sharma | 577ce8d | 2014-09-22 10:25:00 -0700 | [diff] [blame] | 205 | test.pattern, test.elems, matched, exact, test.matched, test.exact) |
| 206 | } |
| 207 | } |
| 208 | } |