Repository navigation
Expand file tree
/
Copy pathcli2api_test.go
More file actions
134 lines (124 loc) · 3.31 KB
/
Copy pathcli2api_test.go
File metadata and controls
134 lines (124 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Timelinize
Copyright (c) 2013 Matthew Holt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tlzapp
import "testing"
func TestMakeForm(t *testing.T) {
for i, tc := range []struct {
input []string
expected string
}{
{
input: []string{"--foo", "bar"},
expected: "foo=bar",
},
{
input: []string{"--foo"},
expected: "foo=true",
},
{
input: []string{"--foo", "1"},
expected: "foo=1",
},
{
input: []string{"--foo", "bar", "--key", "val"},
expected: "foo=bar&key=val",
},
{
input: []string{"--bool1", "--bool2"},
expected: "bool1=true&bool2=true",
},
} {
actual := makeForm(tc.input)
if actual != tc.expected {
t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expected, actual)
}
}
}
func TestMakeJSON(t *testing.T) {
for i, tc := range []struct {
input []string
expected string
shouldErr bool
}{
{
input: []string{"--foo", "bar"},
expected: `{"foo":"bar"}`,
},
{
input: []string{"--bool"},
expected: `{"bool":true}`,
},
{
input: []string{"--top.sub", "val"},
expected: `{"top":{"sub":"val"}}`,
},
{
input: []string{"--top", "1"},
expected: `{"top":1}`,
},
{
input: []string{"--top.sub1.sub2", "val", "--float", "1.5"},
expected: `{"float":1.5,"top":{"sub1":{"sub2":"val"}}}`,
},
{
input: []string{"--arr[0]", "val"},
expected: `{"arr":["val"]}`,
},
{
input: []string{"--arr[1]", "val"},
expected: `{"arr":[null,"val"]}`,
},
{
input: []string{"--arr[0].foo", "bar", "--arr[1].foo", "baz"},
expected: `{"arr":[{"foo":"bar"},{"foo":"baz"}]}`,
},
{
input: []string{"--arr.[0].foo", "bar", "--arr.[1].foo", "baz"},
expected: `{"arr":[{"foo":"bar"},{"foo":"baz"}]}`,
},
{
input: []string{"--foo-bar"},
expected: `{"foo_bar":true}`,
},
{
input: []string{"--[0]", "foo", "--bar"},
shouldErr: true,
},
} {
actual, err := makeJSON(tc.input)
if err == nil && tc.shouldErr {
t.Errorf("Test %d: Should have errored, but did not", i)
} else if err != nil && !tc.shouldErr {
t.Errorf("Test %d: Should NOT have errored, but did: %v", i, err)
}
if string(actual) != tc.expected {
t.Errorf("Test %d: %v\nExpected: '%s'\n Got: '%s'", i, tc.input, tc.expected, actual)
}
}
}
func TestSanitizeFlag(t *testing.T) {
want := "foo_bar"
if got := sanitizeFlag("--foo-bar"); got != want {
t.Errorf("Wanted '%s' but got '%s'", want, got)
}
want = "foo_bar.flub_dub"
if got := sanitizeFlag("--foo-bar.flub-dub"); got != want {
t.Errorf("Wanted '%s' but got '%s'", want, got)
}
want = "foo_bar.flub_dub"
if got := sanitizeFlag("--foo_bar.flub_dub"); got != want {
t.Errorf("Wanted '%s' but got '%s'", want, got)
}
}