-
Notifications
You must be signed in to change notification settings - Fork 3
/
interface_test.go
157 lines (129 loc) · 3.06 KB
/
interface_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
package slicer
import (
"encoding/json"
"testing"
)
func TestInterfaceAdd(t *testing.T) {
s := Interface()
var a interface{} = 1
var b interface{} = "hello"
s.Add(a)
s.Add(b)
expected := `[1,"hello"]`
actual, _ := json.Marshal(s.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
// Add more than one value
s.Clear()
s.Add(a, b)
expected = `[1,"hello"]`
actual, _ = json.Marshal(s.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
func TestInterfaceAddUnique(t *testing.T) {
s := Interface()
var a interface{} = 1
var b interface{} = "hello"
s.AddUnique(a)
s.AddUnique(a)
s.AddUnique(b)
expected := `[1,"hello"]`
actual, _ := json.Marshal(s.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
// AddUnique more than one value
s.Clear()
s.AddUnique(a, b, b, a)
expected = `[1,"hello"]`
actual, _ = json.Marshal(s.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
func TestInterfaceAddSlice(t *testing.T) {
s := Interface()
var a interface{} = 1
var b interface{} = "hello"
s.Add(a)
s.Add(b)
extras := []interface{}{true, 6.6}
s.AddSlice(extras)
expected := `[1,"hello",true,6.6]`
actual, _ := json.Marshal(s.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
func TestInterfaceAddSlicer(t *testing.T) {
s := Interface()
var a interface{} = 1
var b interface{} = "hello"
s.Add(a)
s.Add(b)
p := Interface()
var c interface{} = true
var d interface{} = 6.6
p.Add(c)
p.Add(d)
s.AddSlicer(p)
expected := `[1,"hello",true,6.6]`
actual, _ := json.Marshal(s.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
func TestInterfaceFilter(t *testing.T) {
s := Interface()
s.Add(1)
s.Add("hello")
s.Add(true)
s.Add("world")
s.Add(9.9)
result := s.Filter(func(i interface{}) bool {
_, ok := i.(string)
return ok
})
expected := `["hello","world"]`
actual, _ := json.Marshal(result.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
func TestInterfaceEach(t *testing.T) {
s := Interface()
s.Add(1)
s.Add("hello")
s.Add(true)
s.Add("world")
s.Add(9.9)
result := String()
s.Each(func(i interface{}) {
val, ok := i.(string)
if ok {
result.Add(val + "!")
}
})
expected := `["hello!","world!"]`
actual, _ := json.Marshal(result.AsSlice())
if expected != string(actual) {
t.Errorf("Expected '%s', but got '%s'", expected, actual)
}
}
// TestOptionalInterfaceSlice tests when you construct a Interface with
// an existing slice
func TestOptionalInterfaceSlice(t *testing.T) {
data := []interface{}{"one", "two", "three"}
s := Interface(data)
result := ""
s.Each(func(elem interface{}) {
result += elem.(string)
})
expected := "onetwothree"
if expected != result {
t.Errorf("Expected '%s', but got '%s'", expected, result)
}
}