github.com/gechr/complete@v0.0.0-20191016221035-401475e3ce1e/predict_test.go (about)

     1  package complete
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"sort"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestPredicate(t *testing.T) {
    12  	t.Parallel()
    13  	initTests()
    14  
    15  	tests := []struct {
    16  		name    string
    17  		p       Predictor
    18  		argList []string
    19  		want    []string
    20  	}{
    21  		{
    22  			name: "set",
    23  			p:    PredictSet("a", "b", "c"),
    24  			want: []string{"a", "b", "c"},
    25  		},
    26  		{
    27  			name: "set/empty",
    28  			p:    PredictSet(),
    29  			want: []string{},
    30  		},
    31  		{
    32  			name: "anything",
    33  			p:    PredictAnything,
    34  			want: []string{},
    35  		},
    36  		{
    37  			name: "or: word with nil",
    38  			p:    PredictOr(PredictSet("a"), nil),
    39  			want: []string{"a"},
    40  		},
    41  		{
    42  			name: "or: nil with word",
    43  			p:    PredictOr(nil, PredictSet("a")),
    44  			want: []string{"a"},
    45  		},
    46  		{
    47  			name: "or: nil with nil",
    48  			p:    PredictOr(PredictNothing, PredictNothing),
    49  			want: []string{},
    50  		},
    51  		{
    52  			name: "or: word with word with word",
    53  			p:    PredictOr(PredictSet("a"), PredictSet("b"), PredictSet("c")),
    54  			want: []string{"a", "b", "c"},
    55  		},
    56  		{
    57  			name: "files/txt",
    58  			p:    PredictFiles("*.txt"),
    59  			want: []string{"./", "dir/", "outer/", "a.txt", "b.txt", "c.txt", ".dot.txt"},
    60  		},
    61  		{
    62  			name:    "files/txt",
    63  			p:       PredictFiles("*.txt"),
    64  			argList: []string{"./dir/"},
    65  			want:    []string{"./dir/"},
    66  		},
    67  		{
    68  			name:    "complete files inside dir if it is the only match",
    69  			p:       PredictFiles("foo"),
    70  			argList: []string{"./dir/", "./d"},
    71  			want:    []string{"./dir/", "./dir/foo"},
    72  		},
    73  		{
    74  			name:    "complete files inside dir when argList includes file name",
    75  			p:       PredictFiles("*"),
    76  			argList: []string{"./dir/f", "./dir/foo"},
    77  			want:    []string{"./dir/foo"},
    78  		},
    79  		{
    80  			name:    "files/md",
    81  			p:       PredictFiles("*.md"),
    82  			argList: []string{""},
    83  			want:    []string{"./", "dir/", "outer/", "readme.md"},
    84  		},
    85  		{
    86  			name:    "files/md with ./ prefix",
    87  			p:       PredictFiles("*.md"),
    88  			argList: []string{".", "./"},
    89  			want:    []string{"./", "./dir/", "./outer/", "./readme.md"},
    90  		},
    91  		{
    92  			name:    "dirs",
    93  			p:       PredictDirs("*"),
    94  			argList: []string{"di", "dir", "dir/"},
    95  			want:    []string{"dir/"},
    96  		},
    97  		{
    98  			name:    "dirs with ./ prefix",
    99  			p:       PredictDirs("*"),
   100  			argList: []string{"./di", "./dir", "./dir/"},
   101  			want:    []string{"./dir/"},
   102  		},
   103  		{
   104  			name:    "predict anything in dir",
   105  			p:       PredictFiles("*"),
   106  			argList: []string{"dir", "dir/", "di"},
   107  			want:    []string{"dir/", "dir/foo", "dir/bar"},
   108  		},
   109  		{
   110  			name:    "predict anything in dir with ./ prefix",
   111  			p:       PredictFiles("*"),
   112  			argList: []string{"./dir", "./dir/", "./di"},
   113  			want:    []string{"./dir/", "./dir/foo", "./dir/bar"},
   114  		},
   115  		{
   116  			name:    "root directories",
   117  			p:       PredictDirs("*"),
   118  			argList: []string{""},
   119  			want:    []string{"./", "dir/", "outer/"},
   120  		},
   121  		{
   122  			name:    "root directories with ./ prefix",
   123  			p:       PredictDirs("*"),
   124  			argList: []string{".", "./"},
   125  			want:    []string{"./", "./dir/", "./outer/"},
   126  		},
   127  		{
   128  			name:    "nested directories",
   129  			p:       PredictDirs("*.md"),
   130  			argList: []string{"ou", "outer", "outer/"},
   131  			want:    []string{"outer/", "outer/inner/"},
   132  		},
   133  		{
   134  			name:    "nested directories with ./ prefix",
   135  			p:       PredictDirs("*.md"),
   136  			argList: []string{"./ou", "./outer", "./outer/"},
   137  			want:    []string{"./outer/", "./outer/inner/"},
   138  		},
   139  		{
   140  			name:    "nested inner directory",
   141  			p:       PredictFiles("*.md"),
   142  			argList: []string{"outer/i"},
   143  			want:    []string{"outer/inner/", "outer/inner/readme.md"},
   144  		},
   145  	}
   146  
   147  	for _, tt := range tests {
   148  
   149  		// no args in argList, means an empty argument
   150  		if len(tt.argList) == 0 {
   151  			tt.argList = append(tt.argList, "")
   152  		}
   153  
   154  		for _, arg := range tt.argList {
   155  			t.Run(tt.name+"/arg="+arg, func(t *testing.T) {
   156  
   157  				matches := tt.p.Predict(newArgs(arg))
   158  
   159  				sort.Strings(matches)
   160  				sort.Strings(tt.want)
   161  
   162  				got := strings.Join(matches, ",")
   163  				want := strings.Join(tt.want, ",")
   164  
   165  				if got != want {
   166  					t.Errorf("failed %s\ngot = %s\nwant: %s", t.Name(), got, want)
   167  				}
   168  			})
   169  		}
   170  	}
   171  }
   172  
   173  func TestMatchFile(t *testing.T) {
   174  	t.Parallel()
   175  
   176  	// Change to tests directory for testing completion of
   177  	// files and directories
   178  	err := os.Chdir("../tests")
   179  	if err != nil {
   180  		panic(err)
   181  	}
   182  
   183  	type matcherTest struct {
   184  		prefix string
   185  		want   bool
   186  	}
   187  
   188  	tests := []struct {
   189  		long  string
   190  		tests []matcherTest
   191  	}{
   192  		{
   193  			long: "file.txt",
   194  			tests: []matcherTest{
   195  				{prefix: "", want: true},
   196  				{prefix: "f", want: true},
   197  				{prefix: "./f", want: true},
   198  				{prefix: "./.", want: false},
   199  				{prefix: "file.", want: true},
   200  				{prefix: "./file.", want: true},
   201  				{prefix: "file.txt", want: true},
   202  				{prefix: "./file.txt", want: true},
   203  				{prefix: "other.txt", want: false},
   204  				{prefix: "/other.txt", want: false},
   205  				{prefix: "/file.txt", want: false},
   206  				{prefix: "/fil", want: false},
   207  				{prefix: "/file.txt2", want: false},
   208  				{prefix: "/.", want: false},
   209  			},
   210  		},
   211  		{
   212  			long: "./file.txt",
   213  			tests: []matcherTest{
   214  				{prefix: "", want: true},
   215  				{prefix: "f", want: true},
   216  				{prefix: "./f", want: true},
   217  				{prefix: "./.", want: false},
   218  				{prefix: "file.", want: true},
   219  				{prefix: "./file.", want: true},
   220  				{prefix: "file.txt", want: true},
   221  				{prefix: "./file.txt", want: true},
   222  				{prefix: "other.txt", want: false},
   223  				{prefix: "/other.txt", want: false},
   224  				{prefix: "/file.txt", want: false},
   225  				{prefix: "/fil", want: false},
   226  				{prefix: "/file.txt2", want: false},
   227  				{prefix: "/.", want: false},
   228  			},
   229  		},
   230  		{
   231  			long: "/file.txt",
   232  			tests: []matcherTest{
   233  				{prefix: "", want: true},
   234  				{prefix: "f", want: false},
   235  				{prefix: "./f", want: false},
   236  				{prefix: "./.", want: false},
   237  				{prefix: "file.", want: false},
   238  				{prefix: "./file.", want: false},
   239  				{prefix: "file.txt", want: false},
   240  				{prefix: "./file.txt", want: false},
   241  				{prefix: "other.txt", want: false},
   242  				{prefix: "/other.txt", want: false},
   243  				{prefix: "/file.txt", want: true},
   244  				{prefix: "/fil", want: true},
   245  				{prefix: "/file.txt2", want: false},
   246  				{prefix: "/.", want: false},
   247  			},
   248  		},
   249  		{
   250  			long: "./",
   251  			tests: []matcherTest{
   252  				{prefix: "", want: true},
   253  				{prefix: ".", want: true},
   254  				{prefix: "./", want: true},
   255  				{prefix: "./.", want: false},
   256  			},
   257  		},
   258  	}
   259  
   260  	for _, tt := range tests {
   261  		for _, ttt := range tt.tests {
   262  			name := fmt.Sprintf("long=%q&prefix=%q", tt.long, ttt.prefix)
   263  			t.Run(name, func(t *testing.T) {
   264  				got := matchFile(tt.long, ttt.prefix)
   265  				if got != ttt.want {
   266  					t.Errorf("Failed %s: got = %t, want: %t", name, got, ttt.want)
   267  				}
   268  			})
   269  		}
   270  	}
   271  }