-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathdata_test.go
151 lines (122 loc) · 3.1 KB
/
data_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
package data
import (
"fmt"
"strings"
"testing"
)
func TestList(t *testing.T) {
// List data
list := List()
// Should get a filled list of map[string][]string
if len(list) != len(Data) {
t.Errorf("Expected %d items, got %d", len(Data), len(list))
}
}
func ExampleGet() {
// Get data
data := Get("person")
// Print the first name
fmt.Println(data["prefix"])
// Output:
// [Mr. Mrs. Ms. Miss Dr.]
}
func ExampleGet_sub() {
// Get data
data := GetSubData("person", "prefix")
// Print the first name
fmt.Println(data[0])
// Output:
// Mr.
}
func TestGetData(t *testing.T) {
// Get data
data := Get("things")
// Should get empty map
if len(data) != 0 {
t.Errorf("Expected 0 things, got %d", len(data))
}
}
func TestGetSub(t *testing.T) {
// Get data
data := GetSubData("things", "cake")
// Should get empty map
if len(data) != 0 {
t.Errorf("Expected 0 things, got %d", len(data))
}
}
func ExampleSet() {
// Add data
Set("desserts", map[string][]string{
"cake": {"chocolate", "vanilla"},
"pie": {"apple", "pecan"},
"ice cream": {"strawberry", "vanilla"},
})
}
func ExampleSetSub() {
// Add data
SetSub("desserts", "cake", []string{"chocolate", "vanilla"})
}
func TestAddReplaceRemove(t *testing.T) {
// Add data
Set("desserts", map[string][]string{
"cake": {"chocolate", "vanilla"},
"pie": {"apple", "pecan"},
"ice cream": {"strawberry", "vanilla"},
})
if len(Data["desserts"]) != 3 {
t.Errorf("Expected 3 desserts, got %d", len(Data["desserts"]))
}
if len(Data["desserts"]["cake"]) != 2 {
t.Errorf("Expected 2 cakes, got %d", len(Data["desserts"]["cake"]))
}
// Replace data
Set("desserts", map[string][]string{
"cake": {"carrot", "banana"},
"pie": {"cherry", "peach"},
"ice cream": {"caramel", "rainbow"},
})
if len(Data["desserts"]) != 3 {
t.Errorf("Expected 3 desserts, got %d", len(Data["desserts"]))
}
if len(Data["desserts"]["cake"]) != 2 {
t.Errorf("Expected 2 cakes, got %d", len(Data["desserts"]["cake"]))
}
// Remove data
Remove("desserts")
if len(Data["desserts"]) != 0 {
t.Errorf("Expected 0 desserts, got %d", len(Data["desserts"]))
}
}
func TestAddReplaceRemoveSub(t *testing.T) {
// Add data
SetSub("desserts", "cake", []string{"chocolate", "vanilla"})
if len(Data["desserts"]["cake"]) != 2 {
t.Errorf("Expected 2 cakes, got %d", len(Data["desserts"]["cake"]))
}
// Replace data
SetSub("desserts", "cake", []string{"carrot", "banana"})
if len(Data["desserts"]["cake"]) != 2 {
t.Errorf("Expected 2 cakes, got %d", len(Data["desserts"]["cake"]))
}
// Remove data
RemoveSub("desserts", "cake")
if len(Data["desserts"]["cake"]) != 0 {
t.Errorf("Expected 0 cakes, got %d", len(Data["desserts"]["cake"]))
}
}
func TestTrailingSpaces(t *testing.T) {
assertSpaces := func(selector, str string) {
t.Helper()
if str != strings.Trim(str, " ") {
t.Errorf("In %s dataset: %q has leading or trailing space", selector, str)
}
}
for dataKey, data := range Data {
for key, values := range data {
assertSpaces(dataKey, key)
for _, v := range values {
assertSpaces(dataKey, v)
}
}
}
}