complete/predict_test.go

130 lines
2.4 KiB
Go
Raw Normal View History

2017-05-06 14:48:34 -05:00
package complete
import (
"sort"
"strings"
"testing"
)
func TestPredicate(t *testing.T) {
t.Parallel()
2017-05-06 15:06:58 -05:00
initTests()
2017-05-06 14:48:34 -05:00
tests := []struct {
name string
2017-05-11 12:28:31 -05:00
p Predictor
2017-05-06 14:48:34 -05:00
arg string
want []string
}{
{
name: "set",
p: PredictSet("a", "b", "c"),
want: []string{"a", "b", "c"},
},
{
name: "set with does",
p: PredictSet("./..", "./x"),
arg: "./.",
want: []string{"./.."},
},
2017-05-06 14:48:34 -05:00
{
name: "set/empty",
p: PredictSet(),
want: []string{},
},
{
name: "anything",
p: PredictAnything,
want: []string{},
},
{
name: "or: word with nil",
2017-05-11 12:28:31 -05:00
p: PredictOr(PredictSet("a"), nil),
2017-05-06 14:48:34 -05:00
want: []string{"a"},
},
{
name: "or: nil with word",
2017-05-11 12:28:31 -05:00
p: PredictOr(nil, PredictSet("a")),
2017-05-06 14:48:34 -05:00
want: []string{"a"},
},
{
name: "or: nil with nil",
2017-05-11 12:28:31 -05:00
p: PredictOr(PredictNothing, PredictNothing),
2017-05-06 14:48:34 -05:00
want: []string{},
},
{
name: "or: word with word with word",
2017-05-11 12:28:31 -05:00
p: PredictOr(PredictSet("a"), PredictSet("b"), PredictSet("c")),
2017-05-06 14:48:34 -05:00
want: []string{"a", "b", "c"},
},
{
name: "files/txt",
p: PredictFiles("*.txt"),
want: []string{"./", "./dir/", "./a.txt", "./b.txt", "./c.txt", "./.dot.txt"},
2017-05-06 14:48:34 -05:00
},
{
name: "files/txt",
p: PredictFiles("*.txt"),
arg: "./dir/",
want: []string{"./dir/"},
2017-05-06 14:48:34 -05:00
},
{
name: "files/x",
p: PredictFiles("x"),
arg: "./dir/",
want: []string{"./dir/", "./dir/x"},
2017-05-06 14:48:34 -05:00
},
{
name: "files/*",
p: PredictFiles("x*"),
arg: "./dir/",
want: []string{"./dir/", "./dir/x"},
2017-05-06 14:48:34 -05:00
},
{
name: "files/md",
p: PredictFiles("*.md"),
want: []string{"./", "./dir/", "./readme.md"},
2017-05-06 14:48:34 -05:00
},
{
name: "dirs",
p: PredictDirs("*"),
2017-05-06 14:48:34 -05:00
arg: "./dir/",
want: []string{"./dir/"},
},
{
name: "dirs and files",
p: PredictFiles("*"),
arg: "./dir",
want: []string{"./dir/", "./dir/x"},
2017-05-06 14:48:34 -05:00
},
{
name: "dirs",
p: PredictDirs("*"),
want: []string{"./", "./dir/"},
},
{
name: "subdir",
p: PredictFiles("*"),
arg: "./dir/",
want: []string{"./dir/", "./dir/x"},
2017-05-06 14:48:34 -05:00
},
}
for _, tt := range tests {
t.Run(tt.name+"?arg='"+tt.arg+"'", func(t *testing.T) {
2017-05-11 12:28:31 -05:00
matches := tt.p.Predict(newArgs(strings.Split(tt.arg, " ")))
2017-05-11 12:28:31 -05:00
sort.Strings(matches)
2017-05-06 14:48:34 -05:00
sort.Strings(tt.want)
2017-05-11 12:28:31 -05:00
got := strings.Join(matches, ",")
2017-05-06 14:48:34 -05:00
want := strings.Join(tt.want, ",")
if got != want {
t.Errorf("failed %s\ngot = %s\nwant: %s", t.Name(), got, want)
2017-05-06 14:48:34 -05:00
}
})
}
}