]> git.lizzy.rs Git - micro.git/blob - cmd/micro/util_test.go
Merge pull request #425 from adrianvoica/master
[micro.git] / cmd / micro / util_test.go
1 package main
2
3 import (
4         "reflect"
5         "testing"
6 )
7
8 func TestNumOccurences(t *testing.T) {
9         var tests = []struct {
10                 inputStr  string
11                 inputChar byte
12                 want      int
13         }{
14                 {"aaaa", 'a', 4},
15                 {"\trfd\ta", '\t', 2},
16                 {"∆ƒ\tø ® \t\t", '\t', 3},
17         }
18         for _, test := range tests {
19                 if got := NumOccurrences(test.inputStr, test.inputChar); got != test.want {
20                         t.Errorf("NumOccurences(%s, %c) = %d", test.inputStr, test.inputChar, got)
21                 }
22         }
23 }
24
25 func TestSpaces(t *testing.T) {
26         var tests = []struct {
27                 input int
28                 want  string
29         }{
30                 {4, "    "},
31                 {0, ""},
32         }
33         for _, test := range tests {
34                 if got := Spaces(test.input); got != test.want {
35                         t.Errorf("Spaces(%d) = \"%s\"", test.input, got)
36                 }
37         }
38 }
39
40 func TestIsWordChar(t *testing.T) {
41         if IsWordChar("t") == false {
42                 t.Errorf("IsWordChar(t) = false")
43         }
44         if IsWordChar("T") == false {
45                 t.Errorf("IsWordChar(T) = false")
46         }
47         if IsWordChar("5") == false {
48                 t.Errorf("IsWordChar(5) = false")
49         }
50         if IsWordChar("_") == false {
51                 t.Errorf("IsWordChar(_) = false")
52         }
53         if IsWordChar("ß") == false {
54                 t.Errorf("IsWordChar(ß) = false")
55         }
56         if IsWordChar("~") == true {
57                 t.Errorf("IsWordChar(~) = true")
58         }
59         if IsWordChar(" ") == true {
60                 t.Errorf("IsWordChar( ) = true")
61         }
62         if IsWordChar(")") == true {
63                 t.Errorf("IsWordChar()) = true")
64         }
65         if IsWordChar("\n") == true {
66                 t.Errorf("IsWordChar(\n)) = true")
67         }
68 }
69
70 func TestJoinAndSplitCommandArgs(t *testing.T) {
71         tests := []struct {
72                 Query  []string
73                 Wanted string
74         }{
75                 {[]string{`test case`}, `"test case"`},
76                 {[]string{`quote "test"`}, `"quote \"test\""`},
77                 {[]string{`slash\\\ test`}, `"slash\\\\\\ test"`},
78                 {[]string{`path 1`, `path\" 2`}, `"path 1" "path\\\" 2"`},
79                 {[]string{`foo`}, `foo`},
80                 {[]string{`foo\"bar`}, `"foo\\\"bar"`},
81                 {[]string{``}, ``},
82                 {[]string{`"`}, `"\""`},
83                 {[]string{`a`, ``}, `a `},
84                 {[]string{``, ``, ``, ``}, `   `},
85                 {[]string{"\n"}, `"\n"`},
86                 {[]string{"foo\tbar"}, `"foo\tbar"`},
87         }
88
89         for i, test := range tests {
90                 if result := JoinCommandArgs(test.Query...); test.Wanted != result {
91                         t.Errorf("JoinCommandArgs failed at Test %d\nGot: %q", i, result)
92                 }
93
94                 if result := SplitCommandArgs(test.Wanted); !reflect.DeepEqual(test.Query, result) {
95                         t.Errorf("SplitCommandArgs failed at Test %d\nGot: `%q`", i, result)
96                 }
97         }
98
99         splitTests := []struct {
100                 Query  string
101                 Wanted []string
102         }{
103                 {`"hallo""Welt"`, []string{`halloWelt`}},
104                 {`"hallo" "Welt"`, []string{`hallo`, `Welt`}},
105                 {`\"`, []string{`\"`}},
106                 {`"foo`, []string{`"foo`}},
107                 {`"foo"`, []string{`foo`}},
108                 {`"\"`, []string{`"\"`}},
109                 {`"C:\\"foo.txt`, []string{`C:\foo.txt`}},
110                 {`"\n"new"\n"line`, []string{"\nnew\nline"}},
111         }
112
113         for i, test := range splitTests {
114                 if result := SplitCommandArgs(test.Query); !reflect.DeepEqual(test.Wanted, result) {
115                         t.Errorf("SplitCommandArgs failed at Split-Test %d\nGot: `%q`", i, result)
116                 }
117         }
118 }
119
120 func TestStringWidth(t *testing.T) {
121         tabsize := 4
122         if w := StringWidth("1\t2", tabsize); w != 5 {
123                 t.Error("StringWidth 1 Failed. Got", w)
124         }
125         if w := StringWidth("\t", tabsize); w != 4 {
126                 t.Error("StringWidth 2 Failed. Got", w)
127         }
128         if w := StringWidth("1\t", tabsize); w != 4 {
129                 t.Error("StringWidth 3 Failed. Got", w)
130         }
131         if w := StringWidth("\t\t", tabsize); w != 8 {
132                 t.Error("StringWidth 4 Failed. Got", w)
133         }
134         if w := StringWidth("12\t2\t", tabsize); w != 8 {
135                 t.Error("StringWidth 5 Failed. Got", w)
136         }
137 }
138
139 func TestWidthOfLargeRunes(t *testing.T) {
140         tabsize := 4
141         if w := WidthOfLargeRunes("1\t2", tabsize); w != 2 {
142                 t.Error("WidthOfLargeRunes 1 Failed. Got", w)
143         }
144         if w := WidthOfLargeRunes("\t", tabsize); w != 3 {
145                 t.Error("WidthOfLargeRunes 2 Failed. Got", w)
146         }
147         if w := WidthOfLargeRunes("1\t", tabsize); w != 2 {
148                 t.Error("WidthOfLargeRunes 3 Failed. Got", w)
149         }
150         if w := WidthOfLargeRunes("\t\t", tabsize); w != 6 {
151                 t.Error("WidthOfLargeRunes 4 Failed. Got", w)
152         }
153         if w := WidthOfLargeRunes("12\t2\t", tabsize); w != 3 {
154                 t.Error("WidthOfLargeRunes 5 Failed. Got", w)
155         }
156 }