1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
package fnmatch_test
import (
"testing"
"github.com/danwakefield/fnmatch"
)
// This is a set of tests ported from a set of tests for C fnmatch
// found at http://www.mail-archive.com/bug-gnulib@gnu.org/msg14048.html
func TestMatch(t *testing.T) {
assert := func(p, s string) {
if !fnmatch.Match(p, s, 0) {
t.Errorf("Assertion failed: Match(%#v, %#v, 0)", p, s)
}
}
assert("", "")
assert("*", "")
assert("*", "foo")
assert("*", "bar")
assert("*", "*")
assert("**", "f")
assert("**", "foo.txt")
assert("*.*", "foo.txt")
assert("foo*.txt", "foobar.txt")
assert("foo.txt", "foo.txt")
assert("foo\\.txt", "foo.txt")
if fnmatch.Match("foo\\.txt", "foo.txt", fnmatch.FNM_NOESCAPE) {
t.Errorf("Assertion failed: Match(%#v, %#v, FNM_NOESCAPE) == false", "foo\\.txt", "foo.txt")
}
}
func TestWildcard(t *testing.T) {
// A wildcard pattern "*" should match anything
cases := []struct {
pattern string
input string
flags int
want bool
}{
{"*", "", 0, true},
{"*", "foo", 0, true},
{"*", "*", 0, true},
{"*", " ", 0, true},
{"*", ".foo", 0, true},
{"*", "わたし", 0, true},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestWildcardSlash(t *testing.T) {
cases := []struct {
pattern string
input string
flags int
want bool
}{
// Should match / when flags are 0
{"*", "foo/bar", 0, true},
{"*", "/", 0, true},
{"*", "/foo", 0, true},
{"*", "foo/", 0, true},
// Shouldnt match / when flags include FNM_PATHNAME
{"*", "foo/bar", fnmatch.FNM_PATHNAME, false},
{"*", "/", fnmatch.FNM_PATHNAME, false},
{"*", "/foo", fnmatch.FNM_PATHNAME, false},
{"*", "foo/", fnmatch.FNM_PATHNAME, false},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
for _, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"fnmatch.Match('%s', '%s', %d) should be %v not %v",
c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestWildcardFNMPeriod(t *testing.T) {
// FNM_PERIOD means that . is not matched in some circumstances.
cases := []struct {
pattern string
input string
flags int
want bool
}{
{"*", ".foo", fnmatch.FNM_PERIOD, false},
{"/*", "/.foo", fnmatch.FNM_PERIOD, true},
{"/*", "/.foo", fnmatch.FNM_PERIOD | fnmatch.FNM_PATHNAME, false},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestQuestionMark(t *testing.T) {
//A question mark pattern "?" should match a single character
cases := []struct {
pattern string
input string
flags int
want bool
}{
{"?", "", 0, false},
{"?", "f", 0, true},
{"?", ".", 0, true},
{"?", "?", 0, true},
{"?", "foo", 0, false},
{"?", "わ", 0, true},
{"?", "わた", 0, false},
// Even '/' when flags are 0
{"?", "/", 0, true},
// Except '/' when flags include FNM_PATHNAME
{"?", "/", fnmatch.FNM_PATHNAME, false},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestQuestionMarkExceptions(t *testing.T) {
//When flags include FNM_PERIOD a '?' might not match a '.' character.
cases := []struct {
pattern string
input string
flags int
want bool
}{
{"?", ".", fnmatch.FNM_PERIOD, false},
{"foo?", "foo.", fnmatch.FNM_PERIOD, true},
{"/?", "/.", fnmatch.FNM_PERIOD, true},
{"/?", "/.", fnmatch.FNM_PERIOD | fnmatch.FNM_PATHNAME, false},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestRange(t *testing.T) {
azPat := "[a-z]"
cases := []struct {
pattern string
input string
flags int
want bool
}{
// Should match a single character inside its range
{azPat, "a", 0, true},
{azPat, "q", 0, true},
{azPat, "z", 0, true},
{"[わ]", "わ", 0, true},
// Should not match characters outside its range
{azPat, "-", 0, false},
{azPat, " ", 0, false},
{azPat, "D", 0, false},
{azPat, "é", 0, false},
//Should only match one character
{azPat, "ab", 0, false},
{azPat, "", 0, false},
// Should not consume more of the pattern than necessary
{azPat + "foo", "afoo", 0, true},
// Should match '-' if it is the first/last character or is
// backslash escaped
{"[-az]", "-", 0, true},
{"[-az]", "a", 0, true},
{"[-az]", "b", 0, false},
{"[az-]", "-", 0, true},
{"[a\\-z]", "-", 0, true},
{"[a\\-z]", "b", 0, false},
// ignore '\\' when FNM_NOESCAPE is given
{"[a\\-z]", "\\", fnmatch.FNM_NOESCAPE, true},
{"[a\\-z]", "-", fnmatch.FNM_NOESCAPE, false},
// Should be negated if starting with ^ or !"
{"[^a-z]", "a", 0, false},
{"[!a-z]", "b", 0, false},
{"[!a-z]", "é", 0, true},
{"[!a-z]", "わ", 0, true},
// Still match '-' if following the negation character
{"[^-az]", "-", 0, false},
{"[^-az]", "b", 0, true},
// Should support multiple characters/ranges
{"[abc]", "a", 0, true},
{"[abc]", "c", 0, true},
{"[abc]", "d", 0, false},
{"[a-cg-z]", "c", 0, true},
{"[a-cg-z]", "h", 0, true},
{"[a-cg-z]", "d", 0, false},
//Should not match '/' when flags is FNM_PATHNAME
{"[abc/def]", "/", 0, true},
{"[abc/def]", "/", fnmatch.FNM_PATHNAME, false},
{"[.-0]", "/", 0, true}, // The range [.-0] includes /
{"[.-0]", "/", fnmatch.FNM_PATHNAME, false},
// Should normally be case-sensitive
{"[a-z]", "A", 0, false},
{"[A-Z]", "a", 0, false},
//Except when FNM_CASEFOLD is given
{"[a-z]", "A", fnmatch.FNM_CASEFOLD, true},
{"[A-Z]", "a", fnmatch.FNM_CASEFOLD, true},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestBackSlash(t *testing.T) {
cases := []struct {
pattern string
input string
flags int
want bool
}{
//A backslash should escape the following characters
{"\\\\", "\\", 0, true},
{"\\*", "*", 0, true},
{"\\*", "foo", 0, false},
{"\\?", "?", 0, true},
{"\\?", "f", 0, false},
{"\\[a-z]", "[a-z]", 0, true},
{"\\[a-z]", "a", 0, false},
{"\\foo", "foo", 0, true},
{"\\わ", "わ", 0, true},
// Unless FNM_NOESCAPE is given
{"\\\\", "\\", fnmatch.FNM_NOESCAPE, false},
{"\\\\", "\\\\", fnmatch.FNM_NOESCAPE, true},
{"\\*", "foo", fnmatch.FNM_NOESCAPE, false},
{"\\*", "\\*", fnmatch.FNM_NOESCAPE, true},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestLiteral(t *testing.T) {
cases := []struct {
pattern string
input string
flags int
want bool
}{
//Literal characters should match themselves
{"foo", "foo", 0, true},
{"foo", "foobar", 0, false},
{"foobar", "foo", 0, false},
{"foo", "Foo", 0, false},
{"わたし", "わたし", 0, true},
// And perform case-folding when FNM_CASEFOLD is given
{"foo", "FOO", fnmatch.FNM_CASEFOLD, true},
{"FoO", "fOo", fnmatch.FNM_CASEFOLD, true},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
func TestFNMLeadingDir(t *testing.T) {
cases := []struct {
pattern string
input string
flags int
want bool
}{
// FNM_LEADING_DIR should ignore trailing '/*'
{"foo", "foo/bar", 0, false},
{"foo", "foo/bar", fnmatch.FNM_LEADING_DIR, true},
{"*", "foo/bar", fnmatch.FNM_PATHNAME, false},
{"*", "foo/bar", fnmatch.FNM_PATHNAME | fnmatch.FNM_LEADING_DIR, true},
}
for tc, c := range cases {
got := fnmatch.Match(c.pattern, c.input, c.flags)
if got != c.want {
t.Errorf(
"Testcase #%d failed: fnmatch.Match('%s', '%s', %d) should be %v not %v",
tc, c.pattern, c.input, c.flags, c.want, got,
)
}
}
}
|