-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
identifier_test.go
178 lines (170 loc) · 4.05 KB
/
identifier_test.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"reflect"
"testing"
)
var identifierTests = []struct {
values map[string]map[string]string
id *Identifier
found bool
value string
}{
{
values: nil,
id: &Identifier{key: "foo"},
found: false,
},
{
values: map[string]map[string]string{"": {"foo": "example"}},
id: &Identifier{key: "foo"},
found: true,
value: "example",
},
{
values: map[string]map[string]string{"": {"foo": "example"}},
id: &Identifier{key: "bar"},
found: false,
},
{
values: map[string]map[string]string{"example": {"foo": "example"}},
id: &Identifier{scope: "example", key: "foo"},
found: true,
value: "example",
},
{
values: map[string]map[string]string{"": {"foo": "example"}},
id: &Identifier{scope: "example", key: "foo"},
found: false,
},
{
values: map[string]map[string]string{"example": {"foo": "example"}},
id: &Identifier{scope: "example", key: "bar"},
found: false,
},
}
func Test_prompt(t *testing.T) {
id1 := &Identifier{key: "foo"}
got := id1.prompt()
if got != "foo: " {
t.Errorf("prompt is not correct for %+v (found: %+v, got: %+v)", id1, "foo: ", got)
}
id2 := &Identifier{scope: "foo", key: "bar"}
got = id2.prompt()
if got != "[foo] bar: " {
t.Errorf("prompt is not correct for %+v (found: %+v, got: %+v)", id2, "[foo] bar: ", got)
}
idg := &IdentifierGroup{scope: "foo", keys: []string{"bar", "baz", "qux"}}
got = idg.prompt()
if got != "[foo] bar, baz, qux: " {
t.Errorf("prompt is not correct for %+v (found: %+v, got: %+v)", idg, "[foo] bar, baz, qux: ", got)
}
}
func Test_found(t *testing.T) {
for _, test := range identifierTests {
got := found(test.values, test.id)
if got != test.found {
t.Errorf("found not correct for %+v (found: %+v, got: %+v)", test.id, test.found, got)
}
}
}
func Test_collect(t *testing.T) {
ids := []*Identifier{
{scope: "foo", key: "foo"},
{scope: "foo", key: "bar"},
{scope: "zoo", key: "foo"},
{scope: "foo", key: "foo"},
{scope: "foo", key: "baz"},
{scope: "qux", key: "bar"},
}
expectedFoo := &IdentifierGroup{
scope: "foo",
keys: []string{"foo", "bar", "baz"},
}
expectedBar := &IdentifierGroup{
scope: "bar",
keys: nil,
}
idgFoo := collect(ids, "foo")
if !reflect.DeepEqual(idgFoo, expectedFoo) {
t.Errorf("collect not correct (expected: %+v, got: %+v)", expectedFoo, idgFoo)
}
idgBar := collect(ids, "bar")
if !reflect.DeepEqual(idgBar, expectedBar) {
t.Errorf("collect not correct (expected: %+v, got: %+v)", expectedBar, idgBar)
}
}
func Test_insert(t *testing.T) {
values := make(map[string]map[string]string)
id := &Identifier{key: "foo"}
value := "bar"
insert(values, id, value)
v, ok := values[""]["foo"]
if !ok {
t.Errorf("insert failed for %+v", id)
}
if v != value {
t.Errorf("insert not correctly for %+v (found: %+v, got: %+v)", id, v, value)
}
id = &Identifier{scope: "foo", key: "bar"}
value = "example"
insert(values, id, value)
v, ok = values["foo"]["bar"]
if !ok {
t.Errorf("insert failed for %+v", id)
}
if v != value {
t.Errorf("insert not correctly for %+v (found: %+v, got: %+v)", id, v, value)
}
}
func Test_empty(t *testing.T) {
tests := []struct {
values map[string]map[string]string
expected bool
}{
{
values: nil,
expected: true,
},
{
values: map[string]map[string]string{
"foo": {},
},
expected: true,
},
{
values: map[string]map[string]string{
"foo": {
"bar": "",
"baz": "",
},
},
expected: true,
},
{
values: map[string]map[string]string{
"foo": {
"bar": "",
"baz": "",
},
"bar": {
"qux": "quux",
},
},
expected: false,
},
}
for _, test := range tests {
got := empty(test.values)
if got != test.expected {
t.Errorf("empty not correctly for %+v (expected: %+v, got: %+v)", test.values, test.expected, got)
}
}
}
func Test_lookup(t *testing.T) {
for _, test := range identifierTests {
got := lookup(test.values, test.id)
if got != test.value {
t.Errorf("lookup not correct for %+v (expected: %+v, got: %+v)", test.id, test.value, got)
}
}
}