github.com/divan/complete@v0.0.0-20170515130636-337e95201af7/predict_test.go (about)

     1  package complete
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestPredicate(t *testing.T) {
    10  	t.Parallel()
    11  	initTests()
    12  
    13  	tests := []struct {
    14  		name    string
    15  		p       Predictor
    16  		argList []string
    17  		want    []string
    18  	}{
    19  		{
    20  			name: "set",
    21  			p:    PredictSet("a", "b", "c"),
    22  			want: []string{"a", "b", "c"},
    23  		},
    24  		{
    25  			name:    "set with does",
    26  			p:       PredictSet("./..", "./x"),
    27  			argList: []string{"./.", "./.."},
    28  			want:    []string{"./.."},
    29  		},
    30  		{
    31  			name: "set/empty",
    32  			p:    PredictSet(),
    33  			want: []string{},
    34  		},
    35  		{
    36  			name: "anything",
    37  			p:    PredictAnything,
    38  			want: []string{},
    39  		},
    40  		{
    41  			name: "or: word with nil",
    42  			p:    PredictOr(PredictSet("a"), nil),
    43  			want: []string{"a"},
    44  		},
    45  		{
    46  			name: "or: nil with word",
    47  			p:    PredictOr(nil, PredictSet("a")),
    48  			want: []string{"a"},
    49  		},
    50  		{
    51  			name: "or: nil with nil",
    52  			p:    PredictOr(PredictNothing, PredictNothing),
    53  			want: []string{},
    54  		},
    55  		{
    56  			name: "or: word with word with word",
    57  			p:    PredictOr(PredictSet("a"), PredictSet("b"), PredictSet("c")),
    58  			want: []string{"a", "b", "c"},
    59  		},
    60  		{
    61  			name: "files/txt",
    62  			p:    PredictFiles("*.txt"),
    63  			want: []string{"./", "./dir/", "./outer/", "./a.txt", "./b.txt", "./c.txt", "./.dot.txt"},
    64  		},
    65  		{
    66  			name:    "files/txt",
    67  			p:       PredictFiles("*.txt"),
    68  			argList: []string{"./dir/"},
    69  			want:    []string{"./dir/"},
    70  		},
    71  		{
    72  			name:    "complete files inside dir if it is the only match",
    73  			p:       PredictFiles("foo"),
    74  			argList: []string{"./dir/", "./d"},
    75  			want:    []string{"./dir/", "./dir/foo"},
    76  		},
    77  		{
    78  			name:    "complete files inside dir when argList includes file name",
    79  			p:       PredictFiles("*"),
    80  			argList: []string{"./dir/f", "./dir/foo"},
    81  			want:    []string{"./dir/foo"},
    82  		},
    83  		{
    84  			name:    "files/md",
    85  			p:       PredictFiles("*.md"),
    86  			argList: []string{"", ".", "./"},
    87  			want:    []string{"./", "./dir/", "./outer/", "./readme.md"},
    88  		},
    89  		{
    90  			name:    "dirs",
    91  			p:       PredictDirs("*"),
    92  			argList: []string{"./dir/", "./di", "di", "dir", "dir/"},
    93  			want:    []string{"./dir/"},
    94  		},
    95  		{
    96  			name:    "predict anything in dir",
    97  			p:       PredictFiles("*"),
    98  			argList: []string{"./dir", "dir", "./dir/", "./di"},
    99  			want:    []string{"./dir/", "./dir/foo", "./dir/bar"},
   100  		},
   101  		{
   102  			name:    "root directories",
   103  			p:       PredictDirs("*"),
   104  			argList: []string{"", ".", "./"},
   105  			want:    []string{"./", "./dir/", "./outer/"},
   106  		},
   107  		{
   108  			name:    "nested directories",
   109  			p:       PredictDirs("*.md"),
   110  			argList: []string{"ou", "./ou", "./outer", "./outer/"},
   111  			want:    []string{"./outer/", "./outer/inner/"},
   112  		},
   113  		{
   114  			name:    "nested inner directory",
   115  			p:       PredictFiles("*.md"),
   116  			argList: []string{"outer/i"},
   117  			want:    []string{"./outer/inner/", "./outer/inner/readme.md"},
   118  		},
   119  	}
   120  
   121  	for _, tt := range tests {
   122  
   123  		// no args in argList, means an empty argument
   124  		if len(tt.argList) == 0 {
   125  			tt.argList = append(tt.argList, "")
   126  		}
   127  
   128  		for _, arg := range tt.argList {
   129  			t.Run(tt.name+"/arg="+arg, func(t *testing.T) {
   130  
   131  				matches := tt.p.Predict(newArgs(strings.Split(arg, " ")))
   132  
   133  				sort.Strings(matches)
   134  				sort.Strings(tt.want)
   135  
   136  				got := strings.Join(matches, ",")
   137  				want := strings.Join(tt.want, ",")
   138  
   139  				if got != want {
   140  					t.Errorf("failed %s\ngot = %s\nwant: %s", t.Name(), got, want)
   141  				}
   142  			})
   143  		}
   144  	}
   145  }