complete/match/match_test.go

107 lines
2.4 KiB
Go
Raw Normal View History

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
type matcherTest struct {
2017-05-06 14:59:42 -05:00
prefix string
want bool
}
tests := []struct {
m Matcher
tests []matcherTest
2017-05-06 14:59:42 -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},
{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},
},
},
{
m: File("./file.txt"),
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: true},
{prefix: "./f", want: true},
{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},
},
},
{
m: File("/file.txt"),
tests: []matcherTest{
{prefix: "", want: false},
{prefix: "f", want: false},
{prefix: "./f", want: false},
{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-06 14:59:42 -05:00
},
}
for _, tt := range tests {
for _, ttt := range tt.tests {
name := "matcher:" + tt.m.String() + "/prefix:" + ttt.prefix
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
}
}