-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathcheckers_test.go
258 lines (223 loc) · 9.1 KB
/
checkers_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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// -*- Mode: Go; indent-tabs-mode: t -*-
//
// 20160229: The tests with gccgo on powerpc fail for this file
// and it will loop endlessly. This is not reproducible
// with gccgo on amd64. Given that it's a relatively little
// used arch we disable the tests in here to workaround this
// gccgo bug.
// +build !ppc
/*
* Copyright (C) 2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package testutil
import (
"reflect"
"runtime"
"testing"
. "gopkg.in/check.v1"
)
func Test(t *testing.T) {
TestingT(t)
}
type CheckersS struct{}
var _ = Suite(&CheckersS{})
func testInfo(c *C, checker Checker, name string, paramNames []string) {
info := checker.Info()
if info.Name != name {
c.Fatalf("Got name %s, expected %s", info.Name, name)
}
if !reflect.DeepEqual(info.Params, paramNames) {
c.Fatalf("Got param names %#v, expected %#v", info.Params, paramNames)
}
}
func testCheck(c *C, checker Checker, result bool, error string, params ...interface{}) ([]interface{}, []string) {
info := checker.Info()
if len(params) != len(info.Params) {
c.Fatalf("unexpected param count in test; expected %d got %d", len(info.Params), len(params))
}
names := append([]string{}, info.Params...)
resultActual, errorActual := checker.Check(params, names)
if resultActual != result || errorActual != error {
c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)",
info.Name, params, resultActual, errorActual, result, error)
}
return params, names
}
func (s *CheckersS) TestUnsupportedTypes(c *C) {
testInfo(c, Contains, "Contains", []string{"container", "elem"})
testCheck(c, Contains, false, "int is not a supported container", 5, nil)
testCheck(c, Contains, false, "bool is not a supported container", false, nil)
testCheck(c, Contains, false, "element is a int but expected a string", "container", 1)
}
func (s *CheckersS) TestContainsVerifiesTypes(c *C) {
testInfo(c, Contains, "Contains", []string{"container", "elem"})
testCheck(c, Contains,
false, "container has items of type int but expected element is a string",
[...]int{1, 2, 3}, "foo")
testCheck(c, Contains,
false, "container has items of type int but expected element is a string",
[]int{1, 2, 3}, "foo")
// This looks tricky, Contains looks at _values_, not at keys
testCheck(c, Contains,
false, "container has items of type int but expected element is a string",
map[string]int{"foo": 1, "bar": 2}, "foo")
testCheck(c, Contains,
false, "container has items of type int but expected element is a string",
map[string]int{"foo": 1, "bar": 2}, "foo")
}
type animal interface {
Sound() string
}
type dog struct{}
func (d *dog) Sound() string {
return "bark"
}
type cat struct{}
func (c *cat) Sound() string {
return "meow"
}
type tree struct{}
func (s *CheckersS) TestContainsVerifiesInterfaceTypes(c *C) {
testCheck(c, Contains,
false, "container has items of interface type testutil.animal but expected element does not implement it",
[...]animal{&dog{}, &cat{}}, &tree{})
testCheck(c, Contains,
false, "container has items of interface type testutil.animal but expected element does not implement it",
[]animal{&dog{}, &cat{}}, &tree{})
testCheck(c, Contains,
false, "container has items of interface type testutil.animal but expected element does not implement it",
map[string]animal{"dog": &dog{}, "cat": &cat{}}, &tree{})
}
func (s *CheckersS) TestContainsString(c *C) {
c.Assert("foo", Contains, "f")
c.Assert("foo", Contains, "fo")
c.Assert("foo", Not(Contains), "foobar")
}
type myString string
func (s *CheckersS) TestContainsCustomString(c *C) {
c.Assert(myString("foo"), Contains, myString("f"))
c.Assert(myString("foo"), Contains, myString("fo"))
c.Assert(myString("foo"), Not(Contains), myString("foobar"))
c.Assert("foo", Contains, myString("f"))
c.Assert("foo", Contains, myString("fo"))
c.Assert("foo", Not(Contains), myString("foobar"))
c.Assert(myString("foo"), Contains, "f")
c.Assert(myString("foo"), Contains, "fo")
c.Assert(myString("foo"), Not(Contains), "foobar")
}
func (s *CheckersS) TestContainsArray(c *C) {
c.Assert([...]int{1, 2, 3}, Contains, 1)
c.Assert([...]int{1, 2, 3}, Contains, 2)
c.Assert([...]int{1, 2, 3}, Contains, 3)
c.Assert([...]int{1, 2, 3}, Not(Contains), 4)
c.Assert([...]animal{&dog{}, &cat{}}, Contains, &dog{})
c.Assert([...]animal{&cat{}}, Not(Contains), &dog{})
}
func (s *CheckersS) TestContainsSlice(c *C) {
c.Assert([]int{1, 2, 3}, Contains, 1)
c.Assert([]int{1, 2, 3}, Contains, 2)
c.Assert([]int{1, 2, 3}, Contains, 3)
c.Assert([]int{1, 2, 3}, Not(Contains), 4)
c.Assert([]animal{&dog{}, &cat{}}, Contains, &dog{})
c.Assert([]animal{&cat{}}, Not(Contains), &dog{})
}
func (s *CheckersS) TestContainsMap(c *C) {
c.Assert(map[string]int{"foo": 1, "bar": 2}, Contains, 1)
c.Assert(map[string]int{"foo": 1, "bar": 2}, Contains, 2)
c.Assert(map[string]int{"foo": 1, "bar": 2}, Not(Contains), 3)
c.Assert(map[string]animal{"dog": &dog{}, "cat": &cat{}}, Contains, &dog{})
c.Assert(map[string]animal{"cat": &cat{}}, Not(Contains), &dog{})
}
// Arbitrary type that is not comparable
type myStruct struct {
attrs map[string]string
}
func (s *CheckersS) TestContainsUncomparableType(c *C) {
if runtime.Compiler != "go" {
c.Skip("this test only works on go (not gccgo)")
}
elem := myStruct{map[string]string{"k": "v"}}
containerArray := [...]myStruct{elem}
containerSlice := []myStruct{elem}
containerMap := map[string]myStruct{"foo": elem}
errMsg := "runtime error: comparing uncomparable type testutil.myStruct"
testInfo(c, Contains, "Contains", []string{"container", "elem"})
testCheck(c, Contains, false, errMsg, containerArray, elem)
testCheck(c, Contains, false, errMsg, containerSlice, elem)
testCheck(c, Contains, false, errMsg, containerMap, elem)
}
func (s *CheckersS) TestDeepContainsUnsupportedTypes(c *C) {
testInfo(c, DeepContains, "DeepContains", []string{"container", "elem"})
testCheck(c, DeepContains, false, "int is not a supported container", 5, nil)
testCheck(c, DeepContains, false, "bool is not a supported container", false, nil)
testCheck(c, DeepContains, false, "element is a int but expected a string", "container", 1)
}
func (s *CheckersS) TestDeepContainsVerifiesTypes(c *C) {
testInfo(c, DeepContains, "DeepContains", []string{"container", "elem"})
testCheck(c, DeepContains,
false, "container has items of type int but expected element is a string",
[...]int{1, 2, 3}, "foo")
testCheck(c, DeepContains,
false, "container has items of type int but expected element is a string",
[]int{1, 2, 3}, "foo")
// This looks tricky, DeepContains looks at _values_, not at keys
testCheck(c, DeepContains,
false, "container has items of type int but expected element is a string",
map[string]int{"foo": 1, "bar": 2}, "foo")
}
func (s *CheckersS) TestDeepContainsString(c *C) {
c.Assert("foo", DeepContains, "f")
c.Assert("foo", DeepContains, "fo")
c.Assert("foo", Not(DeepContains), "foobar")
}
func (s *CheckersS) TestDeepContainsCustomString(c *C) {
c.Assert(myString("foo"), DeepContains, myString("f"))
c.Assert(myString("foo"), DeepContains, myString("fo"))
c.Assert(myString("foo"), Not(DeepContains), myString("foobar"))
c.Assert("foo", DeepContains, myString("f"))
c.Assert("foo", DeepContains, myString("fo"))
c.Assert("foo", Not(DeepContains), myString("foobar"))
c.Assert(myString("foo"), DeepContains, "f")
c.Assert(myString("foo"), DeepContains, "fo")
c.Assert(myString("foo"), Not(DeepContains), "foobar")
}
func (s *CheckersS) TestDeepContainsArray(c *C) {
c.Assert([...]int{1, 2, 3}, DeepContains, 1)
c.Assert([...]int{1, 2, 3}, DeepContains, 2)
c.Assert([...]int{1, 2, 3}, DeepContains, 3)
c.Assert([...]int{1, 2, 3}, Not(DeepContains), 4)
}
func (s *CheckersS) TestDeepContainsSlice(c *C) {
c.Assert([]int{1, 2, 3}, DeepContains, 1)
c.Assert([]int{1, 2, 3}, DeepContains, 2)
c.Assert([]int{1, 2, 3}, DeepContains, 3)
c.Assert([]int{1, 2, 3}, Not(DeepContains), 4)
}
func (s *CheckersS) TestDeepContainsMap(c *C) {
c.Assert(map[string]int{"foo": 1, "bar": 2}, DeepContains, 1)
c.Assert(map[string]int{"foo": 1, "bar": 2}, DeepContains, 2)
c.Assert(map[string]int{"foo": 1, "bar": 2}, Not(DeepContains), 3)
}
func (s *CheckersS) TestDeepContainsUncomparableType(c *C) {
elem := myStruct{map[string]string{"k": "v"}}
containerArray := [...]myStruct{elem}
containerSlice := []myStruct{elem}
containerMap := map[string]myStruct{"foo": elem}
testInfo(c, DeepContains, "DeepContains", []string{"container", "elem"})
testCheck(c, DeepContains, true, "", containerArray, elem)
testCheck(c, DeepContains, true, "", containerSlice, elem)
testCheck(c, DeepContains, true, "", containerMap, elem)
}