]> git.lizzy.rs Git - micro.git/blob - cmd/micro/util_test.go
Merge pull request #1200 from Calinou/add-systemd-timer-section
[micro.git] / cmd / micro / util_test.go
1 package main
2
3 import (
4         "testing"
5 )
6
7 func TestNumOccurences(t *testing.T) {
8         var tests = []struct {
9                 inputStr  string
10                 inputChar byte
11                 want      int
12         }{
13                 {"aaaa", 'a', 4},
14                 {"\trfd\ta", '\t', 2},
15                 {"∆ƒ\tø ® \t\t", '\t', 3},
16         }
17         for _, test := range tests {
18                 if got := NumOccurrences(test.inputStr, test.inputChar); got != test.want {
19                         t.Errorf("NumOccurences(%s, %c) = %d", test.inputStr, test.inputChar, got)
20                 }
21         }
22 }
23
24 func TestSpaces(t *testing.T) {
25         var tests = []struct {
26                 input int
27                 want  string
28         }{
29                 {4, "    "},
30                 {0, ""},
31         }
32         for _, test := range tests {
33                 if got := Spaces(test.input); got != test.want {
34                         t.Errorf("Spaces(%d) = \"%s\"", test.input, got)
35                 }
36         }
37 }
38
39 func TestIsWordChar(t *testing.T) {
40         if IsWordChar("t") == false {
41                 t.Errorf("IsWordChar(t) = false")
42         }
43         if IsWordChar("T") == false {
44                 t.Errorf("IsWordChar(T) = false")
45         }
46         if IsWordChar("5") == false {
47                 t.Errorf("IsWordChar(5) = false")
48         }
49         if IsWordChar("_") == false {
50                 t.Errorf("IsWordChar(_) = false")
51         }
52         if IsWordChar("ß") == false {
53                 t.Errorf("IsWordChar(ß) = false")
54         }
55         if IsWordChar("~") == true {
56                 t.Errorf("IsWordChar(~) = true")
57         }
58         if IsWordChar(" ") == true {
59                 t.Errorf("IsWordChar( ) = true")
60         }
61         if IsWordChar(")") == true {
62                 t.Errorf("IsWordChar()) = true")
63         }
64         if IsWordChar("\n") == true {
65                 t.Errorf("IsWordChar(\n)) = true")
66         }
67 }
68
69 func TestStringWidth(t *testing.T) {
70         tabsize := 4
71         if w := StringWidth("1\t2", tabsize); w != 5 {
72                 t.Error("StringWidth 1 Failed. Got", w)
73         }
74         if w := StringWidth("\t", tabsize); w != 4 {
75                 t.Error("StringWidth 2 Failed. Got", w)
76         }
77         if w := StringWidth("1\t", tabsize); w != 4 {
78                 t.Error("StringWidth 3 Failed. Got", w)
79         }
80         if w := StringWidth("\t\t", tabsize); w != 8 {
81                 t.Error("StringWidth 4 Failed. Got", w)
82         }
83         if w := StringWidth("12\t2\t", tabsize); w != 8 {
84                 t.Error("StringWidth 5 Failed. Got", w)
85         }
86 }
87
88 func TestWidthOfLargeRunes(t *testing.T) {
89         tabsize := 4
90         if w := WidthOfLargeRunes("1\t2", tabsize); w != 2 {
91                 t.Error("WidthOfLargeRunes 1 Failed. Got", w)
92         }
93         if w := WidthOfLargeRunes("\t", tabsize); w != 3 {
94                 t.Error("WidthOfLargeRunes 2 Failed. Got", w)
95         }
96         if w := WidthOfLargeRunes("1\t", tabsize); w != 2 {
97                 t.Error("WidthOfLargeRunes 3 Failed. Got", w)
98         }
99         if w := WidthOfLargeRunes("\t\t", tabsize); w != 6 {
100                 t.Error("WidthOfLargeRunes 4 Failed. Got", w)
101         }
102         if w := WidthOfLargeRunes("12\t2\t", tabsize); w != 3 {
103                 t.Error("WidthOfLargeRunes 5 Failed. Got", w)
104         }
105 }
106
107 func assertEqual(t *testing.T, expected interface{}, result interface{}) {
108         if expected != result {
109                 t.Fatalf("Expected: %d != Got: %d", expected, result)
110         }
111 }
112
113 func assertTrue(t *testing.T, condition bool) {
114         if !condition {
115                 t.Fatalf("Condition was not true. Got false")
116         }
117 }
118
119 func TestGetPathRelativeWithDot(t *testing.T) {
120         path, cursorPosition := GetPathAndCursorPosition("./myfile:10:5")
121
122         assertEqual(t, path, "./myfile")
123         assertEqual(t, "10", cursorPosition[0])
124         assertEqual(t, "5", cursorPosition[1])
125 }
126 func TestGetPathRelativeWithDotWindows(t *testing.T) {
127         path, cursorPosition := GetPathAndCursorPosition(".\\myfile:10:5")
128
129         assertEqual(t, path, ".\\myfile")
130         assertEqual(t, "10", cursorPosition[0])
131         assertEqual(t, cursorPosition[1], "5")
132 }
133 func TestGetPathRelativeNoDot(t *testing.T) {
134         path, cursorPosition := GetPathAndCursorPosition("myfile:10:5")
135
136         assertEqual(t, path, "myfile")
137         assertEqual(t, "10", cursorPosition[0])
138
139         assertEqual(t, cursorPosition[1], "5")
140 }
141 func TestGetPathAbsoluteWindows(t *testing.T) {
142         path, cursorPosition := GetPathAndCursorPosition("C:\\myfile:10:5")
143
144         assertEqual(t, path, "C:\\myfile")
145         assertEqual(t, "10", cursorPosition[0])
146
147         assertEqual(t, cursorPosition[1], "5")
148
149         path, cursorPosition = GetPathAndCursorPosition("C:/myfile:10:5")
150
151         assertEqual(t, path, "C:/myfile")
152         assertEqual(t, "10", cursorPosition[0])
153         assertEqual(t, "5", cursorPosition[1])
154 }
155
156 func TestGetPathAbsoluteUnix(t *testing.T) {
157         path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile:10:5")
158
159         assertEqual(t, path, "/home/user/myfile")
160         assertEqual(t, "10", cursorPosition[0])
161         assertEqual(t, "5", cursorPosition[1])
162 }
163
164 func TestGetPathRelativeWithDotWithoutLineAndColumn(t *testing.T) {
165         path, cursorPosition := GetPathAndCursorPosition("./myfile")
166
167         assertEqual(t, path, "./myfile")
168         // no cursor position in filename, nil should be returned
169         assertTrue(t, cursorPosition == nil)
170 }
171 func TestGetPathRelativeWithDotWindowsWithoutLineAndColumn(t *testing.T) {
172         path, cursorPosition := GetPathAndCursorPosition(".\\myfile")
173
174         assertEqual(t, path, ".\\myfile")
175         assertTrue(t, cursorPosition == nil)
176
177 }
178 func TestGetPathRelativeNoDotWithoutLineAndColumn(t *testing.T) {
179         path, cursorPosition := GetPathAndCursorPosition("myfile")
180
181         assertEqual(t, path, "myfile")
182         assertTrue(t, cursorPosition == nil)
183
184 }
185 func TestGetPathAbsoluteWindowsWithoutLineAndColumn(t *testing.T) {
186         path, cursorPosition := GetPathAndCursorPosition("C:\\myfile")
187
188         assertEqual(t, path, "C:\\myfile")
189         assertTrue(t, cursorPosition == nil)
190
191         path, cursorPosition = GetPathAndCursorPosition("C:/myfile")
192
193         assertEqual(t, path, "C:/myfile")
194         assertTrue(t, cursorPosition == nil)
195
196 }
197 func TestGetPathAbsoluteUnixWithoutLineAndColumn(t *testing.T) {
198         path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile")
199
200         assertEqual(t, path, "/home/user/myfile")
201         assertTrue(t, cursorPosition == nil)
202
203 }
204 func TestGetPathSingleLetterFileRelativePath(t *testing.T) {
205         path, cursorPosition := GetPathAndCursorPosition("a:5:6")
206
207         assertEqual(t, path, "a")
208         assertEqual(t, "5", cursorPosition[0])
209         assertEqual(t, "6", cursorPosition[1])
210 }
211 func TestGetPathSingleLetterFileAbsolutePathWindows(t *testing.T) {
212         path, cursorPosition := GetPathAndCursorPosition("C:\\a:5:6")
213
214         assertEqual(t, path, "C:\\a")
215         assertEqual(t, "5", cursorPosition[0])
216         assertEqual(t, "6", cursorPosition[1])
217
218         path, cursorPosition = GetPathAndCursorPosition("C:/a:5:6")
219
220         assertEqual(t, path, "C:/a")
221         assertEqual(t, "5", cursorPosition[0])
222         assertEqual(t, "6", cursorPosition[1])
223 }
224 func TestGetPathSingleLetterFileAbsolutePathUnix(t *testing.T) {
225         path, cursorPosition := GetPathAndCursorPosition("/home/user/a:5:6")
226
227         assertEqual(t, path, "/home/user/a")
228         assertEqual(t, "5", cursorPosition[0])
229         assertEqual(t, "6", cursorPosition[1])
230 }
231 func TestGetPathSingleLetterFileAbsolutePathWindowsWithoutLineAndColumn(t *testing.T) {
232         path, cursorPosition := GetPathAndCursorPosition("C:\\a")
233
234         assertEqual(t, path, "C:\\a")
235         assertTrue(t, cursorPosition == nil)
236
237         path, cursorPosition = GetPathAndCursorPosition("C:/a")
238
239         assertEqual(t, path, "C:/a")
240         assertTrue(t, cursorPosition == nil)
241
242 }
243 func TestGetPathSingleLetterFileAbsolutePathUnixWithoutLineAndColumn(t *testing.T) {
244         path, cursorPosition := GetPathAndCursorPosition("/home/user/a")
245
246         assertEqual(t, path, "/home/user/a")
247         assertTrue(t, cursorPosition == nil)
248
249 }
250
251 func TestGetPathRelativeWithDotOnlyLine(t *testing.T) {
252         path, cursorPosition := GetPathAndCursorPosition("./myfile:10")
253
254         assertEqual(t, path, "./myfile")
255         assertEqual(t, "10", cursorPosition[0])
256         assertEqual(t, "0", cursorPosition[1])
257 }
258 func TestGetPathRelativeWithDotWindowsOnlyLine(t *testing.T) {
259         path, cursorPosition := GetPathAndCursorPosition(".\\myfile:10")
260
261         assertEqual(t, path, ".\\myfile")
262         assertEqual(t, "10", cursorPosition[0])
263         assertEqual(t, "0", cursorPosition[1])
264 }
265 func TestGetPathRelativeNoDotOnlyLine(t *testing.T) {
266         path, cursorPosition := GetPathAndCursorPosition("myfile:10")
267
268         assertEqual(t, path, "myfile")
269         assertEqual(t, "10", cursorPosition[0])
270         assertEqual(t, "0", cursorPosition[1])
271 }
272 func TestGetPathAbsoluteWindowsOnlyLine(t *testing.T) {
273         path, cursorPosition := GetPathAndCursorPosition("C:\\myfile:10")
274
275         assertEqual(t, path, "C:\\myfile")
276         assertEqual(t, "10", cursorPosition[0])
277         assertEqual(t, "0", cursorPosition[1])
278
279         path, cursorPosition = GetPathAndCursorPosition("C:/myfile:10")
280
281         assertEqual(t, path, "C:/myfile")
282         assertEqual(t, "10", cursorPosition[0])
283         assertEqual(t, "0", cursorPosition[1])
284 }
285 func TestGetPathAbsoluteUnixOnlyLine(t *testing.T) {
286         path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile:10")
287
288         assertEqual(t, path, "/home/user/myfile")
289         assertEqual(t, "10", cursorPosition[0])
290         assertEqual(t, "0", cursorPosition[1])
291 }
292 func TestParseCursorLocationOneArg(t *testing.T) {
293         location, err := ParseCursorLocation([]string{"3"})
294
295         assertEqual(t, 3, location.Y)
296         assertEqual(t, 0, location.X)
297         assertEqual(t, nil, err)
298 }
299 func TestParseCursorLocationTwoArgs(t *testing.T) {
300         location, err := ParseCursorLocation([]string{"3", "15"})
301
302         assertEqual(t, 3, location.Y)
303         assertEqual(t, 15, location.X)
304         assertEqual(t, nil, err)
305 }
306 func TestParseCursorLocationNoArgs(t *testing.T) {
307         location, err := ParseCursorLocation(nil)
308         // the expected result is the start position - 0, 0
309         assertEqual(t, 0, location.Y)
310         assertEqual(t, 0, location.X)
311         // an error will be present here as the positions we're parsing are a nil
312         assertTrue(t, err != nil)
313 }
314 func TestParseCursorLocationFirstArgNotValidNumber(t *testing.T) {
315         // the messenger is necessary as ParseCursorLocation
316         // puts a message in it on error
317         messenger = new(Messenger)
318         _, err := ParseCursorLocation([]string{"apples", "1"})
319         // the expected result is the start position - 0, 0
320         assertTrue(t, messenger.hasMessage)
321         assertTrue(t, err != nil)
322 }
323 func TestParseCursorLocationSecondArgNotValidNumber(t *testing.T) {
324         // the messenger is necessary as ParseCursorLocation
325         // puts a message in it on error
326         messenger = new(Messenger)
327         _, err := ParseCursorLocation([]string{"1", "apples"})
328         // the expected result is the start position - 0, 0
329         assertTrue(t, messenger.hasMessage)
330         assertTrue(t, err != nil)
331 }