7 func TestNumOccurences(t *testing.T) {
14 {"\trfd\ta", '\t', 2},
15 {"∆ƒ\tø ® \t\t", '\t', 3},
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)
24 func TestSpaces(t *testing.T) {
25 var tests = []struct {
32 for _, test := range tests {
33 if got := Spaces(test.input); got != test.want {
34 t.Errorf("Spaces(%d) = \"%s\"", test.input, got)
39 func TestIsWordChar(t *testing.T) {
40 if IsWordChar("t") == false {
41 t.Errorf("IsWordChar(t) = false")
43 if IsWordChar("T") == false {
44 t.Errorf("IsWordChar(T) = false")
46 if IsWordChar("5") == false {
47 t.Errorf("IsWordChar(5) = false")
49 if IsWordChar("_") == false {
50 t.Errorf("IsWordChar(_) = false")
52 if IsWordChar("ß") == false {
53 t.Errorf("IsWordChar(ß) = false")
55 if IsWordChar("~") == true {
56 t.Errorf("IsWordChar(~) = true")
58 if IsWordChar(" ") == true {
59 t.Errorf("IsWordChar( ) = true")
61 if IsWordChar(")") == true {
62 t.Errorf("IsWordChar()) = true")
64 if IsWordChar("\n") == true {
65 t.Errorf("IsWordChar(\n)) = true")
69 func TestStringWidth(t *testing.T) {
71 if w := StringWidth("1\t2", tabsize); w != 5 {
72 t.Error("StringWidth 1 Failed. Got", w)
74 if w := StringWidth("\t", tabsize); w != 4 {
75 t.Error("StringWidth 2 Failed. Got", w)
77 if w := StringWidth("1\t", tabsize); w != 4 {
78 t.Error("StringWidth 3 Failed. Got", w)
80 if w := StringWidth("\t\t", tabsize); w != 8 {
81 t.Error("StringWidth 4 Failed. Got", w)
83 if w := StringWidth("12\t2\t", tabsize); w != 8 {
84 t.Error("StringWidth 5 Failed. Got", w)
88 func TestWidthOfLargeRunes(t *testing.T) {
90 if w := WidthOfLargeRunes("1\t2", tabsize); w != 2 {
91 t.Error("WidthOfLargeRunes 1 Failed. Got", w)
93 if w := WidthOfLargeRunes("\t", tabsize); w != 3 {
94 t.Error("WidthOfLargeRunes 2 Failed. Got", w)
96 if w := WidthOfLargeRunes("1\t", tabsize); w != 2 {
97 t.Error("WidthOfLargeRunes 3 Failed. Got", w)
99 if w := WidthOfLargeRunes("\t\t", tabsize); w != 6 {
100 t.Error("WidthOfLargeRunes 4 Failed. Got", w)
102 if w := WidthOfLargeRunes("12\t2\t", tabsize); w != 3 {
103 t.Error("WidthOfLargeRunes 5 Failed. Got", w)
107 func assertEqual(t *testing.T, expected interface{}, result interface{}) {
108 if expected != result {
109 t.Fatalf("Expected: %d != Got: %d", expected, result)
113 func assertTrue(t *testing.T, condition bool) {
115 t.Fatalf("Condition was not true. Got false")
119 func TestGetPathRelativeWithDot(t *testing.T) {
120 path, cursorPosition := GetPathAndCursorPosition("./myfile:10:5")
122 assertEqual(t, path, "./myfile")
123 assertEqual(t, "10", cursorPosition[0])
124 assertEqual(t, "5", cursorPosition[1])
126 func TestGetPathRelativeWithDotWindows(t *testing.T) {
127 path, cursorPosition := GetPathAndCursorPosition(".\\myfile:10:5")
129 assertEqual(t, path, ".\\myfile")
130 assertEqual(t, "10", cursorPosition[0])
131 assertEqual(t, cursorPosition[1], "5")
133 func TestGetPathRelativeNoDot(t *testing.T) {
134 path, cursorPosition := GetPathAndCursorPosition("myfile:10:5")
136 assertEqual(t, path, "myfile")
137 assertEqual(t, "10", cursorPosition[0])
139 assertEqual(t, cursorPosition[1], "5")
141 func TestGetPathAbsoluteWindows(t *testing.T) {
142 path, cursorPosition := GetPathAndCursorPosition("C:\\myfile:10:5")
144 assertEqual(t, path, "C:\\myfile")
145 assertEqual(t, "10", cursorPosition[0])
147 assertEqual(t, cursorPosition[1], "5")
149 path, cursorPosition = GetPathAndCursorPosition("C:/myfile:10:5")
151 assertEqual(t, path, "C:/myfile")
152 assertEqual(t, "10", cursorPosition[0])
153 assertEqual(t, "5", cursorPosition[1])
156 func TestGetPathAbsoluteUnix(t *testing.T) {
157 path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile:10:5")
159 assertEqual(t, path, "/home/user/myfile")
160 assertEqual(t, "10", cursorPosition[0])
161 assertEqual(t, "5", cursorPosition[1])
164 func TestGetPathRelativeWithDotWithoutLineAndColumn(t *testing.T) {
165 path, cursorPosition := GetPathAndCursorPosition("./myfile")
167 assertEqual(t, path, "./myfile")
168 // no cursor position in filename, nil should be returned
169 assertTrue(t, cursorPosition == nil)
171 func TestGetPathRelativeWithDotWindowsWithoutLineAndColumn(t *testing.T) {
172 path, cursorPosition := GetPathAndCursorPosition(".\\myfile")
174 assertEqual(t, path, ".\\myfile")
175 assertTrue(t, cursorPosition == nil)
178 func TestGetPathRelativeNoDotWithoutLineAndColumn(t *testing.T) {
179 path, cursorPosition := GetPathAndCursorPosition("myfile")
181 assertEqual(t, path, "myfile")
182 assertTrue(t, cursorPosition == nil)
185 func TestGetPathAbsoluteWindowsWithoutLineAndColumn(t *testing.T) {
186 path, cursorPosition := GetPathAndCursorPosition("C:\\myfile")
188 assertEqual(t, path, "C:\\myfile")
189 assertTrue(t, cursorPosition == nil)
191 path, cursorPosition = GetPathAndCursorPosition("C:/myfile")
193 assertEqual(t, path, "C:/myfile")
194 assertTrue(t, cursorPosition == nil)
197 func TestGetPathAbsoluteUnixWithoutLineAndColumn(t *testing.T) {
198 path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile")
200 assertEqual(t, path, "/home/user/myfile")
201 assertTrue(t, cursorPosition == nil)
204 func TestGetPathSingleLetterFileRelativePath(t *testing.T) {
205 path, cursorPosition := GetPathAndCursorPosition("a:5:6")
207 assertEqual(t, path, "a")
208 assertEqual(t, "5", cursorPosition[0])
209 assertEqual(t, "6", cursorPosition[1])
211 func TestGetPathSingleLetterFileAbsolutePathWindows(t *testing.T) {
212 path, cursorPosition := GetPathAndCursorPosition("C:\\a:5:6")
214 assertEqual(t, path, "C:\\a")
215 assertEqual(t, "5", cursorPosition[0])
216 assertEqual(t, "6", cursorPosition[1])
218 path, cursorPosition = GetPathAndCursorPosition("C:/a:5:6")
220 assertEqual(t, path, "C:/a")
221 assertEqual(t, "5", cursorPosition[0])
222 assertEqual(t, "6", cursorPosition[1])
224 func TestGetPathSingleLetterFileAbsolutePathUnix(t *testing.T) {
225 path, cursorPosition := GetPathAndCursorPosition("/home/user/a:5:6")
227 assertEqual(t, path, "/home/user/a")
228 assertEqual(t, "5", cursorPosition[0])
229 assertEqual(t, "6", cursorPosition[1])
231 func TestGetPathSingleLetterFileAbsolutePathWindowsWithoutLineAndColumn(t *testing.T) {
232 path, cursorPosition := GetPathAndCursorPosition("C:\\a")
234 assertEqual(t, path, "C:\\a")
235 assertTrue(t, cursorPosition == nil)
237 path, cursorPosition = GetPathAndCursorPosition("C:/a")
239 assertEqual(t, path, "C:/a")
240 assertTrue(t, cursorPosition == nil)
243 func TestGetPathSingleLetterFileAbsolutePathUnixWithoutLineAndColumn(t *testing.T) {
244 path, cursorPosition := GetPathAndCursorPosition("/home/user/a")
246 assertEqual(t, path, "/home/user/a")
247 assertTrue(t, cursorPosition == nil)
251 func TestGetPathRelativeWithDotOnlyLine(t *testing.T) {
252 path, cursorPosition := GetPathAndCursorPosition("./myfile:10")
254 assertEqual(t, path, "./myfile")
255 assertEqual(t, "10", cursorPosition[0])
256 assertEqual(t, "0", cursorPosition[1])
258 func TestGetPathRelativeWithDotWindowsOnlyLine(t *testing.T) {
259 path, cursorPosition := GetPathAndCursorPosition(".\\myfile:10")
261 assertEqual(t, path, ".\\myfile")
262 assertEqual(t, "10", cursorPosition[0])
263 assertEqual(t, "0", cursorPosition[1])
265 func TestGetPathRelativeNoDotOnlyLine(t *testing.T) {
266 path, cursorPosition := GetPathAndCursorPosition("myfile:10")
268 assertEqual(t, path, "myfile")
269 assertEqual(t, "10", cursorPosition[0])
270 assertEqual(t, "0", cursorPosition[1])
272 func TestGetPathAbsoluteWindowsOnlyLine(t *testing.T) {
273 path, cursorPosition := GetPathAndCursorPosition("C:\\myfile:10")
275 assertEqual(t, path, "C:\\myfile")
276 assertEqual(t, "10", cursorPosition[0])
277 assertEqual(t, "0", cursorPosition[1])
279 path, cursorPosition = GetPathAndCursorPosition("C:/myfile:10")
281 assertEqual(t, path, "C:/myfile")
282 assertEqual(t, "10", cursorPosition[0])
283 assertEqual(t, "0", cursorPosition[1])
285 func TestGetPathAbsoluteUnixOnlyLine(t *testing.T) {
286 path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile:10")
288 assertEqual(t, path, "/home/user/myfile")
289 assertEqual(t, "10", cursorPosition[0])
290 assertEqual(t, "0", cursorPosition[1])
292 func TestParseCursorLocationOneArg(t *testing.T) {
293 location, err := ParseCursorLocation([]string{"3"})
295 assertEqual(t, 3, location.Y)
296 assertEqual(t, 0, location.X)
297 assertEqual(t, nil, err)
299 func TestParseCursorLocationTwoArgs(t *testing.T) {
300 location, err := ParseCursorLocation([]string{"3", "15"})
302 assertEqual(t, 3, location.Y)
303 assertEqual(t, 15, location.X)
304 assertEqual(t, nil, err)
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)
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)
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)