forked from manythumbed/checkers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
184 lines (151 loc) · 4.23 KB
/
container_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
179
180
181
182
183
184
package checkers
import (
"sort"
. "gopkg.in/check.v1"
)
type ContainerSuite struct{}
func (s *ContainerSuite) TestContains(c *C) {
a := []int{2, 3, 4}
c.Check(a, Contains, a[0])
c.Check(a, Contains, a[1])
c.Check(a, Contains, a[2])
c.Check(a, Contains, 2)
c.Check(a, Contains, 3)
c.Check(a, Contains, 4)
c.Check(a, Not(Contains), 5)
c.Check(a, Not(Contains), a)
c.Check(a, Not(Contains), "x")
b := []x{x{"1"}, x{"2"}}
c.Check(b, Contains, b[0])
c.Check(b, Contains, b[1])
c.Check(b, Contains, x{"1"})
c.Check(b, Contains, x{"2"})
c.Check(b, Not(Contains), x{"3"})
c.Check(b, Not(Contains), y{0})
c.Check("1234", Contains, "23")
c.Check("1234", Contains, "4")
c.Check("1234", Contains, "")
c.Check("1234", Not(Contains), "0")
}
func (s *ContainerSuite) TestIsIn(c *C) {
a := []int{2, 3, 4}
c.Check(a[0], IsIn, a)
c.Check(a[1], IsIn, a)
c.Check(a[2], IsIn, a)
c.Check(2, IsIn, a)
c.Check(3, IsIn, a)
c.Check(4, IsIn, a)
c.Check(5, Not(IsIn), a)
c.Check(a, Not(IsIn), a)
c.Check("x", Not(IsIn), a)
b := []x{x{"1"}, x{"2"}}
c.Check(b[0], IsIn, b)
c.Check(b[1], IsIn, b)
c.Check(x{"1"}, IsIn, b)
c.Check(x{"2"}, IsIn, b)
c.Check(x{"3"}, Not(IsIn), b)
c.Check(y{0}, Not(IsIn), b)
c.Check("23", IsIn, "1234")
c.Check("4", IsIn, "1234")
c.Check("", IsIn, "1234")
c.Check("0", Not(IsIn), "1234")
}
func (s *ContainerSuite) TestSliceEquals(c *C) {
type Point struct{ X, Y int }
c.Check([]string{}, SliceEquals, []string{})
c.Check([]Point{}, SliceEquals, []Point{})
c.Check([]Point{{1, 3}, {2, 10}}, SliceEquals, []Point{{1, 3}, {2, 10}})
c.Check([]string{}, Not(SliceEquals), []int{})
c.Check([]int{}, Not(SliceEquals), []int64{})
c.Check([]int{1, 2}, Not(SliceEquals), []int64{2, 1})
c.Check([]int{1, 2}, Not(SliceEquals), []int64{1, 2, 3})
c.Check([]int{1, 2, 3}, Not(SliceEquals), []int64{1})
}
func (s *ContainerSuite) TestMapEquals(c *C) {
type Point struct{ X, Y int }
c.Check(map[int]string{}, MapEquals, map[int]string{})
c.Check(map[int]Point{}, MapEquals, map[int]Point{})
c.Check(map[int]Point{1: {1, 2}}, MapEquals, map[int]Point{1: {1, 2}})
c.Check(map[int]string{}, Not(MapEquals), map[int]int{})
c.Check(map[int]Point{1: {1, 2}}, Not(MapEquals), map[int]Point{2: {1, 2}})
c.Check(map[int]Point{1: {1, 2}}, Not(MapEquals), map[int]Point{1: {2, 2}})
c.Check(map[int]Point{1: {1, 2}}, Not(MapEquals), map[int]Point{1: {1, 2}, 2: {1, 2}})
c.Check(map[int]Point{1: {1, 2}, 2: {1, 2}}, Not(MapEquals), map[int]Point{1: {1, 2}})
}
func (s *ContainerSuite) TestSameContent(c *C) {
//// positive cases ////
// same
c.Check(
[]int{1, 2, 3}, SameContent,
[]int{1, 2, 3})
// empty
c.Check(
[]int{}, SameContent,
[]int{})
// single
c.Check(
[]int{1}, SameContent,
[]int{1})
// different order
c.Check(
[]int{1, 2, 3}, SameContent,
[]int{3, 2, 1})
// multiple copies of same
c.Check(
[]int{1, 1, 2}, SameContent,
[]int{2, 1, 1})
type test struct {
s string
i int
}
// test structs
c.Check(
[]test{{"a", 1}, {"b", 2}}, SameContent,
[]test{{"b", 2}, {"a", 1}})
//// negative cases ////
// different contents
c.Check(
[]int{1, 3, 2, 5}, Not(SameContent),
[]int{5, 2, 3, 4})
// different size slices
c.Check(
[]int{1, 2, 3}, Not(SameContent),
[]int{1, 2})
// different counts of same items
c.Check(
[]int{1, 1, 2}, Not(SameContent),
[]int{1, 2, 2})
/// Error cases ///
// note: for these tests, we can't use Not, since Not passes the error value through
// and checks with a non-empty error always count as failed
// Oddly, there doesn't seem to actually be a way to check for an error from a Checker.
// different type
res, err := SameContent.Check([]interface{}{
[]string{"1", "2"},
[]int{1, 2},
}, []string{})
c.Check(res, IsFalse)
c.Check(err, Not(Equals), "")
// obtained not a slice
res, err = SameContent.Check([]interface{}{
"test",
[]int{1},
}, []string{})
c.Check(res, IsFalse)
c.Check(err, Not(Equals), "")
// expected not a slice
res, err = SameContent.Check([]interface{}{
[]int{1},
"test",
}, []string{})
c.Check(res, IsFalse)
c.Check(err, Not(Equals), "")
}
func (s *ContainerSuite) TestIsSorted(c *C) {
var a sort.IntSlice
c.Check(a, IsSorted)
a = []int{2, 3, 4}
c.Check(a, IsSorted)
a = []int{-2, 0, 4}
c.Check(a, IsSorted)
}