2017-05-07 11:53:55 -05:00
|
|
|
package match
|
2017-05-06 14:59:42 -05:00
|
|
|
|
2017-05-07 11:53:55 -05:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
2017-05-06 14:59:42 -05:00
|
|
|
|
|
|
|
func TestMatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-05-07 11:53:55 -05:00
|
|
|
|
|
|
|
// Change to tests directory for testing completion of
|
|
|
|
// files and directories
|
|
|
|
err := os.Chdir("../tests")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2017-05-06 14:59:42 -05:00
|
|
|
|
2017-05-09 23:28:43 -05:00
|
|
|
type matcherTest struct {
|
2017-05-06 14:59:42 -05:00
|
|
|
prefix string
|
|
|
|
want bool
|
2017-05-09 23:28:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
m Matcher
|
|
|
|
tests []matcherTest
|
2017-05-06 14:59:42 -05:00
|
|
|
}{
|
|
|
|
{
|
2017-05-09 23:28:43 -05:00
|
|
|
m: Prefix("abcd"),
|
|
|
|
tests: []matcherTest{
|
|
|
|
{prefix: "", want: true},
|
|
|
|
{prefix: "ab", want: true},
|
|
|
|
{prefix: "ac", want: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
m: Prefix(""),
|
|
|
|
tests: []matcherTest{
|
|
|
|
{prefix: "ac", want: false},
|
|
|
|
{prefix: "", want: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
m: File("file.txt"),
|
|
|
|
tests: []matcherTest{
|
|
|
|
{prefix: "", want: true},
|
|
|
|
{prefix: "f", want: true},
|
|
|
|
{prefix: "./f", want: true},
|
2017-05-10 16:38:24 -05:00
|
|
|
{prefix: "./.", want: false},
|
2017-05-09 23:28:43 -05:00
|
|
|
{prefix: "file.", want: true},
|
|
|
|
{prefix: "./file.", want: true},
|
|
|
|
{prefix: "file.txt", want: true},
|
|
|
|
{prefix: "./file.txt", want: true},
|
|
|
|
{prefix: "other.txt", want: false},
|
|
|
|
{prefix: "/other.txt", want: false},
|
|
|
|
{prefix: "/file.txt", want: false},
|
|
|
|
{prefix: "/fil", want: false},
|
|
|
|
{prefix: "/file.txt2", want: false},
|
2017-05-10 16:38:24 -05:00
|
|
|
{prefix: "/.", want: false},
|
2017-05-09 23:28:43 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
m: File("./file.txt"),
|
|
|
|
tests: []matcherTest{
|
|
|
|
{prefix: "", want: true},
|
|
|
|
{prefix: "f", want: true},
|
|
|
|
{prefix: "./f", want: true},
|
2017-05-10 16:38:24 -05:00
|
|
|
{prefix: "./.", want: false},
|
2017-05-09 23:28:43 -05:00
|
|
|
{prefix: "file.", want: true},
|
|
|
|
{prefix: "./file.", want: true},
|
|
|
|
{prefix: "file.txt", want: true},
|
|
|
|
{prefix: "./file.txt", want: true},
|
|
|
|
{prefix: "other.txt", want: false},
|
|
|
|
{prefix: "/other.txt", want: false},
|
|
|
|
{prefix: "/file.txt", want: false},
|
|
|
|
{prefix: "/fil", want: false},
|
|
|
|
{prefix: "/file.txt2", want: false},
|
2017-05-10 16:38:24 -05:00
|
|
|
{prefix: "/.", want: false},
|
2017-05-09 23:28:43 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
m: File("/file.txt"),
|
|
|
|
tests: []matcherTest{
|
2017-05-10 16:38:24 -05:00
|
|
|
{prefix: "", want: true},
|
2017-05-09 23:28:43 -05:00
|
|
|
{prefix: "f", want: false},
|
|
|
|
{prefix: "./f", want: false},
|
2017-05-10 16:38:24 -05:00
|
|
|
{prefix: "./.", want: false},
|
2017-05-09 23:28:43 -05:00
|
|
|
{prefix: "file.", want: false},
|
|
|
|
{prefix: "./file.", want: false},
|
|
|
|
{prefix: "file.txt", want: false},
|
|
|
|
{prefix: "./file.txt", want: false},
|
|
|
|
{prefix: "other.txt", want: false},
|
|
|
|
{prefix: "/other.txt", want: false},
|
|
|
|
{prefix: "/file.txt", want: true},
|
|
|
|
{prefix: "/fil", want: true},
|
|
|
|
{prefix: "/file.txt2", want: false},
|
2017-05-10 16:38:24 -05:00
|
|
|
{prefix: "/.", want: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
m: File("./"),
|
|
|
|
tests: []matcherTest{
|
|
|
|
{prefix: "", want: true},
|
|
|
|
{prefix: ".", want: true},
|
|
|
|
{prefix: "./", want: true},
|
|
|
|
{prefix: "./.", want: false},
|
2017-05-09 23:28:43 -05:00
|
|
|
},
|
2017-05-06 14:59:42 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2017-05-09 23:28:43 -05:00
|
|
|
for _, ttt := range tt.tests {
|
2017-05-10 16:38:24 -05:00
|
|
|
name := "matcher='" + tt.m.String() + "'&prefix='" + ttt.prefix + "'"
|
2017-05-09 23:28:43 -05:00
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
got := tt.m.Match(ttt.prefix)
|
|
|
|
if got != ttt.want {
|
|
|
|
t.Errorf("Failed %s: got = %t, want: %t", name, got, ttt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-05-06 14:59:42 -05:00
|
|
|
}
|
|
|
|
}
|