complete/match/match_test.go

130 lines
2.9 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 (
2017-05-11 12:48:40 -05:00
"fmt"
2017-05-07 11:53:55 -05:00
"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 {
2017-05-11 12:48:40 -05:00
m Match
long string
tests []matcherTest
2017-05-06 14:59:42 -05:00
}{
{
2017-05-11 12:48:40 -05:00
m: Prefix,
long: "abcd",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "ab", want: true},
{prefix: "ac", want: false},
},
},
{
2017-05-11 12:48:40 -05:00
m: Prefix,
long: "",
tests: []matcherTest{
{prefix: "ac", want: false},
{prefix: "", want: true},
},
},
{
2017-05-11 12:48:40 -05:00
m: File,
long: "file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: true},
{prefix: "./f", want: true},
{prefix: "./.", want: false},
{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},
{prefix: "/.", want: false},
},
},
{
2017-05-11 12:48:40 -05:00
m: File,
long: "./file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: true},
{prefix: "./f", want: true},
{prefix: "./.", want: false},
{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},
{prefix: "/.", want: false},
},
},
{
2017-05-11 12:48:40 -05:00
m: File,
long: "/file.txt",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: "f", want: false},
{prefix: "./f", want: false},
{prefix: "./.", 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},
{prefix: "/.", want: false},
},
},
{
2017-05-11 12:48:40 -05:00
m: File,
long: "./",
tests: []matcherTest{
{prefix: "", want: true},
{prefix: ".", want: true},
{prefix: "./", want: true},
{prefix: "./.", want: false},
},
2017-05-06 14:59:42 -05:00
},
}
for _, tt := range tests {
for _, ttt := range tt.tests {
2017-05-11 12:48:40 -05:00
name := fmt.Sprintf("matcher=%T&long='%s'&prefix='%s'", tt.m, tt.long, ttt.prefix)
t.Run(name, func(t *testing.T) {
2017-05-11 12:48:40 -05:00
got := tt.m(tt.long, 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
}
}