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
|
package glob
import (
"regexp"
"testing"
)
func assertEquals(t *testing.T, expected string, result *Glob) {
r, _ := regexp.Compile(expected)
if r.String() != result.Regexp.String() {
t.Fatal("Expected ", expected, ", got ", result.Regexp, "\n")
}
}
func TestGeneralMatching(t *testing.T) {
glob, _ := Compile("main{.go,.c}")
if glob.Match([]byte("main.go")) != true {
t.Fatal("main{.go, .c}", "did not match", "main.go")
}
if glob.Match([]byte("main.c")) != true {
t.Fatal("main{.go, .c}", "did not match", "main.c")
}
if glob.Match([]byte("main")) != false {
t.Fatal("main{.go, .c}", "matched", "main")
}
}
func TestStarBecomesDotStar(t *testing.T) {
result, _ := Compile("gl*b")
assertEquals(t, "^gl.*b$", result)
}
func TestEscapedStarIsUnchanged(t *testing.T) {
result, _ := Compile("gl\\*b")
assertEquals(t, "^gl\\*b$", result)
}
func TestQuestionMarkBecomesDot(t *testing.T) {
result, _ := Compile("gl?b")
assertEquals(t, "^gl.b$", result)
}
func TestEscapedQuestionMRkIsUnchanged(t *testing.T) {
result, _ := Compile("gl\\?b")
assertEquals(t, "^gl\\?b$", result)
}
func TestCharacterClassesDontNeedConversion(t *testing.T) {
result, _ := Compile("gl[-o]b")
assertEquals(t, "^gl[-o]b$", result)
}
func TestEscapedClassesAreUnchanged(t *testing.T) {
result, _ := Compile("gl\\[-o\\]b")
assertEquals(t, "^gl\\[-o\\]b$", result)
}
func TestNegationInCharacterClasses(t *testing.T) {
result, _ := Compile("gl[!a-n!p-z]b")
assertEquals(t, "^gl[^a-n!p-z]b$", result)
}
func TestNestedNegationInCharacterClasses(t *testing.T) {
result, _ := Compile("gl[[!a-n]!p-z]b")
assertEquals(t, "^gl[[^a-n]!p-z]b$", result)
}
func TestEscapeCaratIfItIsTheFirstCharInACharacterClass(t *testing.T) {
result, _ := Compile("gl[^o]b")
assertEquals(t, "^gl[\\^o]b$", result)
}
func TestMetacharsAreEscaped(t *testing.T) {
result, _ := Compile("gl?*.()+|^$@%b")
assertEquals(t, "^gl..*\\.\\(\\)\\+\\|\\^\\$\\@\\%b$", result)
}
func TestMetacharsInCharacterClassesDontNeedEscaping(t *testing.T) {
result, _ := Compile("gl[?*.()+|^$@%]b")
assertEquals(t, "^gl[?*.()+|^$@%]b$", result)
}
func TestEscapedBackslashIsUnchanged(t *testing.T) {
result, _ := Compile("gl\\\\b")
assertEquals(t, "^gl\\\\b$", result)
}
func TestSlashQAndSlashEAreEscaped(t *testing.T) {
result, _ := Compile("\\Qglob\\E")
assertEquals(t, "^\\\\Qglob\\\\E$", result)
}
func TestBracesAreTurnedIntoGroups(t *testing.T) {
result, _ := Compile("{glob,regex}")
assertEquals(t, "^(glob|regex)$", result)
}
func TestEscapedBracesAreUnchanged(t *testing.T) {
result, _ := Compile("\\{glob\\}")
assertEquals(t, "^\\{glob\\}$", result)
}
func TestCommasDontNeedEscaping(t *testing.T) {
result, _ := Compile("{glob\\,regex},")
assertEquals(t, "^(glob,regex),$", result)
}
|