-
Notifications
You must be signed in to change notification settings - Fork 0
/
begin_test.go
157 lines (125 loc) · 3.58 KB
/
begin_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 rangechain
import (
"github.com/halprin/rangechain/keyvalue"
"github.com/stretchr/testify/assert"
"slices"
"testing"
)
func TestFromSlice(t *testing.T) {
assert := assert.New(t)
inputSlice := []string{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
expectedOutput := []interface{}{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
chain := FromSlice(inputSlice)
slice, err := chain.Slice()
assert.Equal(expectedOutput, slice)
assert.Nil(err)
}
func TestFromArray(t *testing.T) {
assert := assert.New(t)
input := [...]string{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
expectedOutput := []interface{}{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
chain := FromArray(input)
slice, err := chain.Slice()
assert.Equal(expectedOutput, slice)
assert.Nil(err)
}
func TestFromChannel(t *testing.T) {
assert := assert.New(t)
innerInput := []string{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
expectedOutput := []interface{}{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
input := createTestStringChannel(innerInput)
chain := FromChannel(input)
slice, err := chain.Slice()
assert.Equal(expectedOutput, slice)
assert.Nil(err)
}
func TestFromMap(t *testing.T) {
assert := assert.New(t)
key1 := "DogCow"
value1 := 3
key2 := "goes"
value2 := 92
key3 := "Moof!"
value3 := 26
input := map[string]int{
key1: value1,
key2: value2,
key3: value3,
}
chain := FromMap(input)
expectedOutput := []interface{}{
&testKeyValue{
TheKey: key1,
TheValue: value1,
},
&testKeyValue{
TheKey: key2,
TheValue: value2,
},
&testKeyValue{
TheKey: key3,
TheValue: value3,
},
}
slice, err := chain.Slice()
//not testing the order because we are not guaranteed the order in which a map is iterated over
assertEqualsBasedOnKeyValuerInterface(t, expectedOutput, slice)
assert.Nil(err)
}
func TestFromIterator(t *testing.T) {
assert := assert.New(t)
inputSlice := []string{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
inputIterator := slices.Values(inputSlice)
expectedOutput := []interface{}{"DogCows", "goes", "Moof!", "Do", "you", "like", "Clarus", "the", "DogCow?"}
chain := FromIterator(inputIterator)
slice, err := chain.Slice()
assert.Equal(expectedOutput, slice)
assert.Nil(err)
}
type testKeyValue struct {
TheKey interface{}
TheValue interface{}
}
func (t *testKeyValue) Key() interface{} {
return t.TheKey
}
func (t *testKeyValue) Value() interface{} {
return t.TheValue
}
func createTestStringChannel(stringSlice []string) chan string {
stringChannel := make(chan string)
go func() {
for _, currentString := range stringSlice {
stringChannel <- currentString
}
close(stringChannel)
}()
return stringChannel
}
func assertEqualsBasedOnKeyValuerInterface(t *testing.T, expected []interface{}, actual []interface{}) {
assert := assert.New(t)
assert.Len(actual, len(expected))
for _, expectedValue := range expected {
expectedKeyValuer, isType := expectedValue.(keyvalue.KeyValuer)
if !isType {
continue
}
keyToFind := expectedKeyValuer.Key()
foundMatch := false
for _, actualValue := range actual {
actualKeyValuer, isType := actualValue.(keyvalue.KeyValuer)
if !isType {
continue
}
actualKey := actualKeyValuer.Key()
if actualKey != keyToFind {
continue
}
if expectedKeyValuer.Value() != actualKeyValuer.Value() {
continue
}
foundMatch = true
}
assert.True(foundMatch)
}
}