]> git.lizzy.rs Git - micro.git/blob - cmd/micro/util_test.go
Merge
[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("~") == true {
54                 t.Errorf("IsWordChar(~) = true")
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{`"hallo""Welt"`}},
104                 {`\"`, []string{`\"`}},
105                 {`"\"`, []string{`"\"`}},
106         }
107
108         for i, test := range splitTests {
109                 if result := SplitCommandArgs(test.Query); !reflect.DeepEqual(test.Wanted, result) {
110                         t.Errorf("SplitCommandArgs failed at Split-Test %d\nGot: `%q`", i, result)
111                 }
112         }
113 }