diff options
| author | Eyal Posener <[email protected]> | 2017-05-13 22:44:36 +0300 |
|---|---|---|
| committer | Eyal Posener <[email protected]> | 2017-05-13 22:44:36 +0300 |
| commit | a32482e70da76715645de14fa0a6201eace3df53 (patch) | |
| tree | b4d012d2f5714c479d54e9df3738508237add7c0 /args_test.go | |
| parent | c7377ba2de5df7097776c07f1ca68ca0fc8ee90e (diff) | |
test: add args tests
Diffstat (limited to 'args_test.go')
| -rw-r--r-- | args_test.go | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/args_test.go b/args_test.go new file mode 100644 index 0000000..78d346f --- /dev/null +++ b/args_test.go @@ -0,0 +1,215 @@ +package complete + +import ( + "fmt" + "strings" + "testing" +) + +func TestArgs(t *testing.T) { + t.Parallel() + tests := []struct { + line string + completed string + last string + lastCompleted string + }{ + { + line: "a b c", + completed: "b", + last: "c", + lastCompleted: "b", + }, + { + line: "a b ", + completed: "b", + last: "", + lastCompleted: "b", + }, + { + line: "", + completed: "", + last: "", + lastCompleted: "", + }, + { + line: "a", + completed: "", + last: "a", + lastCompleted: "", + }, + { + line: "a ", + completed: "", + last: "", + lastCompleted: "", + }, + } + + for _, tt := range tests { + t.Run(tt.line, func(t *testing.T) { + + a := newArgs(strings.Split(tt.line, " ")) + + if got, want := strings.Join(a.Completed, " "), tt.completed; got != want { + t.Errorf("%s failed: Completed = %q, want %q", t.Name(), got, want) + } + if got, want := a.Last, tt.last; got != want { + t.Errorf("Last = %q, want %q", got, want) + } + if got, want := a.LastCompleted, tt.lastCompleted; got != want { + t.Errorf("%s failed: LastCompleted = %q, want %q", t.Name(), got, want) + } + }) + } +} + +func TestArgs_From(t *testing.T) { + t.Parallel() + tests := []struct { + line string + from int + newLine string + newCompleted string + }{ + { + line: "a b c", + from: 0, + newLine: "b c", + newCompleted: "b", + }, + { + line: "a b c", + from: 1, + newLine: "c", + newCompleted: "", + }, + { + line: "a b c", + from: 2, + newLine: "", + newCompleted: "", + }, + { + line: "a b c", + from: 3, + newLine: "", + newCompleted: "", + }, + { + line: "a b c ", + from: 0, + newLine: "b c ", + newCompleted: "b c", + }, + { + line: "a b c ", + from: 1, + newLine: "c ", + newCompleted: "c", + }, + { + line: "a b c ", + from: 2, + newLine: "", + newCompleted: "", + }, + { + line: "", + from: 0, + newLine: "", + newCompleted: "", + }, + { + line: "", + from: 1, + newLine: "", + newCompleted: "", + }, + } + + for _, tt := range tests { + t.Run(fmt.Sprintf("%s/%d", tt.line, tt.from), func(t *testing.T) { + + a := newArgs(strings.Split(tt.line, " ")) + n := a.from(tt.from) + + if got, want := strings.Join(n.All, " "), tt.newLine; got != want { + t.Errorf("%s failed: all = %q, want %q", t.Name(), got, want) + } + if got, want := strings.Join(n.Completed, " "), tt.newCompleted; got != want { + t.Errorf("%s failed: completed = %q, want %q", t.Name(), got, want) + } + }) + } +} + +func TestArgs_Directory(t *testing.T) { + t.Parallel() + initTests() + + tests := []struct { + line string + directory string + }{ + { + line: "a b c", + directory: "./", + }, + { + line: "a b c /tm", + directory: "/", + }, + { + line: "a b c /tmp", + directory: "/tmp", + }, + { + line: "a b c /tmp ", + directory: "./", + }, + { + line: "a b c ./", + directory: "./", + }, + { + line: "a b c ./dir", + directory: "./dir/", + }, + { + line: "a b c dir", + directory: "./dir/", + }, + { + line: "a b c ./di", + directory: "./", + }, + { + line: "a b c ./dir ", + directory: "./", + }, + { + line: "a b c ./di", + directory: "./", + }, + { + line: "a b c ./a.txt", + directory: "./", + }, + { + line: "a b c ./a.txt/x", + directory: "./", + }, + } + + for _, tt := range tests { + t.Run(tt.line, func(t *testing.T) { + + a := newArgs(strings.Split(tt.line, " ")) + + if got, want := a.Directory(), tt.directory; got != want { + t.Errorf("%s failed: directory = %q, want %q", t.Name(), got, want) + } + }) + } +} |
