| 1 |
package glob |
| 2 |
|
| 3 |
import ( |
| 4 |
|
| 5 |
"regexp" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
func TestMatching(t *testing.T) { |
| 11 |
var re *regexp.Regexp |
| 12 |
|
| 13 |
re = Globexp("a") |
| 14 |
if !re.MatchString("a") { |
| 15 |
t.Error("should match exactly") |
| 16 |
} |
| 17 |
|
| 18 |
re = Globexp("src/**/*.html") |
| 19 |
if !re.MatchString("src/test.html") { |
| 20 |
t.Error("/**/ should match zero directories") |
| 21 |
} |
| 22 |
|
| 23 |
re = Globexp("src/**/*.html") |
| 24 |
if !re.MatchString("src/foo/bar/test.html") { |
| 25 |
t.Error("/**/ should match intermediate directories") |
| 26 |
} |
| 27 |
|
| 28 |
re = Globexp("**/*.html") |
| 29 |
if !re.MatchString("test.html") { |
| 30 |
t.Error("**/ should match zero leading directories") |
| 31 |
} |
| 32 |
|
| 33 |
re = Globexp("**/*.html") |
| 34 |
if !re.MatchString("src/foo/bar/test.html") { |
| 35 |
t.Error("**/ should match leading directories") |
| 36 |
} |
| 37 |
|
| 38 |
re = Globexp("src/**/test.html") |
| 39 |
if !re.MatchString("src/foo/bar/test.html") { |
| 40 |
t.Error("** should match exact directories") |
| 41 |
} |
| 42 |
|
| 43 |
re = Globexp("*.js") |
| 44 |
if !re.MatchString(".config.js") { |
| 45 |
t.Error("* should match dot") |
| 46 |
} |
| 47 |
|
| 48 |
re = Globexp("*.js") |
| 49 |
if re.MatchString("foo/.config.js") { |
| 50 |
t.Error("* should not match directories") |
| 51 |
} |
| 52 |
|
| 53 |
re = Globexp("**/*.js") |
| 54 |
if !re.MatchString(".config.js") { |
| 55 |
t.Error("**/ slash should be optional") |
| 56 |
} |
| 57 |
|
| 58 |
re = Globexp("**/test.{html,js}") |
| 59 |
if !re.MatchString("src/test.html") || !re.MatchString("src/test.js") { |
| 60 |
t.Error("{} should match options") |
| 61 |
} |
| 62 |
|
| 63 |
re = Globexp("**/{{{{VERSION}}/*.foo") |
| 64 |
if !re.MatchString("src/{{VERSION}}/1.foo") { |
| 65 |
t.Error("{} should be escapable") |
| 66 |
} |
| 67 |
|
| 68 |
re = Globexp("public/**/*.uml") |
| 69 |
if !re.MatchString("public/{{VERSION}}/123/.4-5/a b/main-diagram.uml") { |
| 70 |
t.Error("should handle special chars") |
| 71 |
} |
| 72 |
|
| 73 |
re = Globexp("example/views/**/*.go.html") |
| 74 |
if !re.MatchString("example/views/admin/layout.go.html") { |
| 75 |
t.Error("should handle multiple subdirs admin") |
| 76 |
} |
| 77 |
if !re.MatchString("example/views/front/indexl.go.html") { |
| 78 |
t.Error("should handle multiple subdirs admin") |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestGlob(t *testing.T) { |
| 83 |
files, regexps, _ := Glob([]string{"./test/foo.txt"}) |
| 84 |
if len(files) != 1 { |
| 85 |
t.Log("files", files) |
| 86 |
t.Error("should return file with no patterns") |
| 87 |
} |
| 88 |
if len(files) != len(regexps) { |
| 89 |
t.Error("Unequal amount of files and regexps") |
| 90 |
} |
| 91 |
|
| 92 |
files, _, _ = Glob([]string{"test/**/*.txt"}) |
| 93 |
if len(files) != 5 { |
| 94 |
t.Log("files", files) |
| 95 |
t.Error("should return all txt files") |
| 96 |
} |
| 97 |
|
| 98 |
files, _, _ = Glob([]string{"test/**/*.txt", "!**/*sub1.txt"}) |
| 99 |
if len(files) != 3 { |
| 100 |
t.Error("should return all go files but those suffixed with sub1.txt") |
| 101 |
} |
| 102 |
for _, file := range files { |
| 103 |
if strings.HasSuffix(file.Path, "sub1.txt") { |
| 104 |
t.Error("should have excluded a negated pattern") |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestPatternRoot(t *testing.T) { |
| 110 |
s := PatternRoot("example/views/**/*.go.html") |
| 111 |
if s != "example/views" { |
| 112 |
t.Error("did not calculate root dir from pattern") |
| 113 |
} |
| 114 |
} |