Skip to content

Commit

Permalink
Add test for ~ autocompletion
Browse files Browse the repository at this point in the history
Signed-off-by: anmolbabu <[email protected]>
  • Loading branch information
anmol babu authored and anmolbabu committed Nov 29, 2018
1 parent 5eb2df8 commit a6eeb79
Showing 1 changed file with 66 additions and 4 deletions.
70 changes: 66 additions & 4 deletions predict_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package complete

import (
"os"
"sort"
"strings"
"testing"
Expand All @@ -11,10 +12,13 @@ func TestPredicate(t *testing.T) {
initTests()

tests := []struct {
name string
p Predictor
argList []string
want []string
name string
p Predictor
argList []string
want []string
prepEnv func() (string, map[string]string, error)
cleanEnv func(dirTreeBase string)
checkEqual func(dirTreeMappings map[string]string, got []string) bool
}{
{
name: "set",
Expand Down Expand Up @@ -110,6 +114,47 @@ func TestPredicate(t *testing.T) {
argList: []string{"./dir", "./dir/", "./di"},
want: []string{"./dir/", "./dir/foo", "./dir/bar"},
},
{
name: "predict anything in home directory with `~` prefix",
p: PredictFiles("*"),
argList: []string{"~/foo"},
want: []string{"~/foo", "~/foo/foo.md", "~/foo/foo-dir"},
prepEnv: func() (string, map[string]string, error) {
basePath, dirTreeMappings, err := CreateDirTree(
`~`,
"foo",
[]FileProperties{
FileProperties{
FilePath: "foo.md",
FileParent: "",
FileType: RegularFile,
ModificationType: CREATE,
},
FileProperties{
FilePath: "foo-dir",
FileParent: "",
FileType: Directory,
ModificationType: CREATE,
},
},
)
return basePath, dirTreeMappings, err
},
cleanEnv: func(dirTreeBase string) {
os.RemoveAll(dirTreeBase)
},
checkEqual: func(dirTreeMappings map[string]string, got []string) bool {
want := []string{dirTreeMappings["foo"], dirTreeMappings["foo/foo.md"], dirTreeMappings["foo/-dir"]}
sort.Strings(got)
sort.Strings(want)
gotStr := strings.Join(got, ",")
wantStr := strings.Join(want, ",")
if gotStr != wantStr {
return false
}
return true
},
},
{
name: "root directories",
p: PredictDirs("*"),
Expand Down Expand Up @@ -142,8 +187,21 @@ func TestPredicate(t *testing.T) {
},
}

var basePath string
var err error
var dirTreeMappings map[string]string

for _, tt := range tests {

if tt.prepEnv != nil {
if basePath, dirTreeMappings, err = tt.prepEnv(); err != nil {
t.Errorf("error setting up env. Error %v", err)
}
}
if tt.cleanEnv != nil {
defer tt.cleanEnv(basePath)
}

// no args in argList, means an empty argument
if len(tt.argList) == 0 {
tt.argList = append(tt.argList, "")
Expand All @@ -157,6 +215,10 @@ func TestPredicate(t *testing.T) {
sort.Strings(matches)
sort.Strings(tt.want)

if tt.checkEqual != nil {
tt.checkEqual(dirTreeMappings, matches)
return
}
got := strings.Join(matches, ",")
want := strings.Join(tt.want, ",")

Expand Down

0 comments on commit a6eeb79

Please sign in to comment.