1package complete
2
3import (
4	"fmt"
5	"strings"
6	"testing"
7
8	"github.com/stretchr/testify/assert"
9)
10
11func TestArgs(t *testing.T) {
12	t.Parallel()
13	tests := []struct {
14		line          string
15		completed     string
16		last          string
17		lastCompleted string
18	}{
19		{
20			line:          "a b c",
21			completed:     "b",
22			last:          "c",
23			lastCompleted: "b",
24		},
25		{
26			line:          "a b ",
27			completed:     "b",
28			last:          "",
29			lastCompleted: "b",
30		},
31		{
32			line:          "",
33			completed:     "",
34			last:          "",
35			lastCompleted: "",
36		},
37		{
38			line:          "a",
39			completed:     "",
40			last:          "a",
41			lastCompleted: "",
42		},
43		{
44			line:          "a ",
45			completed:     "",
46			last:          "",
47			lastCompleted: "",
48		},
49	}
50
51	for _, tt := range tests {
52		t.Run(tt.line, func(t *testing.T) {
53
54			a := newArgs(tt.line)
55
56			if got, want := strings.Join(a.Completed, " "), tt.completed; got != want {
57				t.Errorf("%s failed: Completed = %q, want %q", t.Name(), got, want)
58			}
59			if got, want := a.Last, tt.last; got != want {
60				t.Errorf("Last = %q, want %q", got, want)
61			}
62			if got, want := a.LastCompleted, tt.lastCompleted; got != want {
63				t.Errorf("%s failed: LastCompleted = %q, want %q", t.Name(), got, want)
64			}
65		})
66	}
67}
68
69func TestArgs_From(t *testing.T) {
70	t.Parallel()
71	tests := []struct {
72		line         string
73		from         int
74		newLine      string
75		newCompleted string
76	}{
77		{
78			line:         "a b c",
79			from:         0,
80			newLine:      "b c",
81			newCompleted: "b",
82		},
83		{
84			line:         "a b c",
85			from:         1,
86			newLine:      "c",
87			newCompleted: "",
88		},
89		{
90			line:         "a b c",
91			from:         2,
92			newLine:      "",
93			newCompleted: "",
94		},
95		{
96			line:         "a b c",
97			from:         3,
98			newLine:      "",
99			newCompleted: "",
100		},
101		{
102			line:         "a b c ",
103			from:         0,
104			newLine:      "b c ",
105			newCompleted: "b c",
106		},
107		{
108			line:         "a b c ",
109			from:         1,
110			newLine:      "c ",
111			newCompleted: "c",
112		},
113		{
114			line:         "a b c ",
115			from:         2,
116			newLine:      "",
117			newCompleted: "",
118		},
119		{
120			line:         "",
121			from:         0,
122			newLine:      "",
123			newCompleted: "",
124		},
125		{
126			line:         "",
127			from:         1,
128			newLine:      "",
129			newCompleted: "",
130		},
131	}
132
133	for _, tt := range tests {
134		t.Run(fmt.Sprintf("%s/%d", tt.line, tt.from), func(t *testing.T) {
135
136			a := newArgs("cmd " + tt.line)
137			n := a.from(tt.from)
138
139			assert.Equal(t, tt.newLine, strings.Join(n.All, " "))
140			assert.Equal(t, tt.newCompleted, strings.Join(n.Completed, " "))
141		})
142	}
143}
144
145func TestArgs_Directory(t *testing.T) {
146	t.Parallel()
147	initTests()
148
149	tests := []struct {
150		line      string
151		directory string
152	}{
153		{
154			line:      "a b c",
155			directory: "./",
156		},
157		{
158			line:      "a b c /tm",
159			directory: "/",
160		},
161		{
162			line:      "a b c /tmp",
163			directory: "/tmp/",
164		},
165		{
166			line:      "a b c /tmp ",
167			directory: "./",
168		},
169		{
170			line:      "a b c ./",
171			directory: "./",
172		},
173		{
174			line:      "a b c ./dir",
175			directory: "./dir/",
176		},
177		{
178			line:      "a b c dir",
179			directory: "dir/",
180		},
181		{
182			line:      "a b c ./di",
183			directory: "./",
184		},
185		{
186			line:      "a b c ./dir ",
187			directory: "./",
188		},
189		{
190			line:      "a b c ./di",
191			directory: "./",
192		},
193		{
194			line:      "a b c ./a.txt",
195			directory: "./",
196		},
197		{
198			line:      "a b c ./a.txt/x",
199			directory: "./",
200		},
201	}
202
203	for _, tt := range tests {
204		t.Run(tt.line, func(t *testing.T) {
205
206			a := newArgs(tt.line)
207
208			if got, want := a.Directory(), tt.directory; got != want {
209				t.Errorf("%s failed: directory = %q, want %q", t.Name(), got, want)
210			}
211		})
212	}
213}
214