aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/gopkg.in/check.v1/checkers_test.go
blob: 5c697474696b3a8330634580a5440f72605d44c9 (plain) (blame)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package check_test

import (
    "errors"
    "gopkg.in/check.v1"
    "reflect"
    "runtime"
)

type CheckersS struct{}

var _ = check.Suite(&CheckersS{})

func testInfo(c *check.C, checker check.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 *check.C, checker check.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...)
    result_, error_ := checker.Check(params, names)
    if result_ != result || error_ != error {
        c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)",
            info.Name, params, result_, error_, result, error)
    }
    return params, names
}

func (s *CheckersS) TestComment(c *check.C) {
    bug := check.Commentf("a %d bc", 42)
    comment := bug.CheckCommentString()
    if comment != "a 42 bc" {
        c.Fatalf("Commentf returned %#v", comment)
    }
}

func (s *CheckersS) TestIsNil(c *check.C) {
    testInfo(c, check.IsNil, "IsNil", []string{"value"})

    testCheck(c, check.IsNil, true, "", nil)
    testCheck(c, check.IsNil, false, "", "a")

    testCheck(c, check.IsNil, true, "", (chan int)(nil))
    testCheck(c, check.IsNil, false, "", make(chan int))
    testCheck(c, check.IsNil, true, "", (error)(nil))
    testCheck(c, check.IsNil, false, "", errors.New(""))
    testCheck(c, check.IsNil, true, "", ([]int)(nil))
    testCheck(c, check.IsNil, false, "", make([]int, 1))
    testCheck(c, check.IsNil, false, "", int(0))
}

func (s *CheckersS) TestNotNil(c *check.C) {
    testInfo(c, check.NotNil, "NotNil", []string{"value"})

    testCheck(c, check.NotNil, false, "", nil)
    testCheck(c, check.NotNil, true, "", "a")

    testCheck(c, check.NotNil, false, "", (chan int)(nil))
    testCheck(c, check.NotNil, true, "", make(chan int))
    testCheck(c, check.NotNil, false, "", (error)(nil))
    testCheck(c, check.NotNil, true, "", errors.New(""))
    testCheck(c, check.NotNil, false, "", ([]int)(nil))
    testCheck(c, check.NotNil, true, "", make([]int, 1))
}

func (s *CheckersS) TestNot(c *check.C) {
    testInfo(c, check.Not(check.IsNil), "Not(IsNil)", []string{"value"})

    testCheck(c, check.Not(check.IsNil), false, "", nil)
    testCheck(c, check.Not(check.IsNil), true, "", "a")
}

type simpleStruct struct {
    i int
}

func (s *CheckersS) TestEquals(c *check.C) {
    testInfo(c, check.Equals, "Equals", []string{"obtained", "expected"})

    // The simplest.
    testCheck(c, check.Equals, true, "", 42, 42)
    testCheck(c, check.Equals, false, "", 42, 43)

    // Different native types.
    testCheck(c, check.Equals, false, "", int32(42), int64(42))

    // With nil.
    testCheck(c, check.Equals, false, "", 42, nil)

    // Slices
    testCheck(c, check.Equals, false, "runtime error: comparing uncomparable type []uint8", []byte{1, 2}, []byte{1, 2})

    // Struct values
    testCheck(c, check.Equals, true, "", simpleStruct{1}, simpleStruct{1})
    testCheck(c, check.Equals, false, "", simpleStruct{1}, simpleStruct{2})

    // Struct pointers
    testCheck(c, check.Equals, false, "", &simpleStruct{1}, &simpleStruct{1})
    testCheck(c, check.Equals, false, "", &simpleStruct{1}, &simpleStruct{2})
}

func (s *CheckersS) TestDeepEquals(c *check.C) {
    testInfo(c, check.DeepEquals, "DeepEquals", []string{"obtained", "expected"})

    // The simplest.
    testCheck(c, check.DeepEquals, true, "", 42, 42)
    testCheck(c, check.DeepEquals, false, "", 42, 43)

    // Different native types.
    testCheck(c, check.DeepEquals, false, "", int32(42), int64(42))

    // With nil.
    testCheck(c, check.DeepEquals, false, "", 42, nil)

    // Slices
    testCheck(c, check.DeepEquals, true, "", []byte{1, 2}, []byte{1, 2})
    testCheck(c, check.DeepEquals, false, "", []byte{1, 2}, []byte{1, 3})

    // Struct values
    testCheck(c, check.DeepEquals, true, "", simpleStruct{1}, simpleStruct{1})
    testCheck(c, check.DeepEquals, false, "", simpleStruct{1}, simpleStruct{2})

    // Struct pointers
    testCheck(c, check.DeepEquals, true, "", &simpleStruct{1}, &simpleStruct{1})
    testCheck(c, check.DeepEquals, false, "", &simpleStruct{1}, &simpleStruct{2})
}

func (s *CheckersS) TestHasLen(c *check.C) {
    testInfo(c, check.HasLen, "HasLen", []string{"obtained", "n"})

    testCheck(c, check.HasLen, true, "", "abcd", 4)
    testCheck(c, check.HasLen, true, "", []int{1, 2}, 2)
    testCheck(c, check.HasLen, false, "", []int{1, 2}, 3)

    testCheck(c, check.HasLen, false, "n must be an int", []int{1, 2}, "2")
    testCheck(c, check.HasLen, false, "obtained value type has no length", nil, 2)
}

func (s *CheckersS) TestErrorMatches(c *check.C) {
    testInfo(c, check.ErrorMatches, "ErrorMatches", []string{"value", "regex"})

    testCheck(c, check.ErrorMatches, false, "Error value is nil", nil, "some error")
    testCheck(c, check.ErrorMatches, false, "Value is not an error", 1, "some error")
    testCheck(c, check.ErrorMatches, true, "", errors.New("some error"), "some error")
    testCheck(c, check.ErrorMatches, true, "", errors.New("some error"), "so.*or")

    // Verify params mutation
    params, names := testCheck(c, check.ErrorMatches, false, "", errors.New("some error"), "other error")
    c.Assert(params[0], check.Equals, "some error")
    c.Assert(names[0], check.Equals, "error")
}

func (s *CheckersS) TestMatches(c *check.C) {
    testInfo(c, check.Matches, "Matches", []string{"value", "regex"})

    // Simple matching
    testCheck(c, check.Matches, true, "", "abc", "abc")
    testCheck(c, check.Matches, true, "", "abc", "a.c")

    // Must match fully
    testCheck(c, check.Matches, false, "", "abc", "ab")
    testCheck(c, check.Matches, false, "", "abc", "bc")

    // String()-enabled values accepted
    testCheck(c, check.Matches, true, "", reflect.ValueOf("abc"), "a.c")
    testCheck(c, check.Matches, false, "", reflect.ValueOf("abc"), "a.d")

    // Some error conditions.
    testCheck(c, check.Matches, false, "Obtained value is not a string and has no .String()", 1, "a.c")
    testCheck(c, check.Matches, false, "Can't compile regex: error parsing regexp: missing closing ]: `[c$`", "abc", "a[c")
}

func (s *CheckersS) TestPanics(c *check.C) {
    testInfo(c, check.Panics, "Panics", []string{"function", "expected"})

    // Some errors.
    testCheck(c, check.Panics, false, "Function has not panicked", func() bool { return false }, "BOOM")
    testCheck(c, check.Panics, false, "Function must take zero arguments", 1, "BOOM")

    // Plain strings.
    testCheck(c, check.Panics, true, "", func() { panic("BOOM") }, "BOOM")
    testCheck(c, check.Panics, false, "", func() { panic("KABOOM") }, "BOOM")
    testCheck(c, check.Panics, true, "", func() bool { panic("BOOM") }, "BOOM")

    // Error values.
    testCheck(c, check.Panics, true, "", func() { panic(errors.New("BOOM")) }, errors.New("BOOM"))
    testCheck(c, check.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))

    type deep struct{ i int }
    // Deep value
    testCheck(c, check.Panics, true, "", func() { panic(&deep{99}) }, &deep{99})

    // Verify params/names mutation
    params, names := testCheck(c, check.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))
    c.Assert(params[0], check.ErrorMatches, "KABOOM")
    c.Assert(names[0], check.Equals, "panic")

    // Verify a nil panic
    testCheck(c, check.Panics, true, "", func() { panic(nil) }, nil)
    testCheck(c, check.Panics, false, "", func() { panic(nil) }, "NOPE")
}

func (s *CheckersS) TestPanicMatches(c *check.C) {
    testInfo(c, check.PanicMatches, "PanicMatches", []string{"function", "expected"})

    // Error matching.
    testCheck(c, check.PanicMatches, true, "", func() { panic(errors.New("BOOM")) }, "BO.M")
    testCheck(c, check.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BO.M")

    // Some errors.
    testCheck(c, check.PanicMatches, false, "Function has not panicked", func() bool { return false }, "BOOM")
    testCheck(c, check.PanicMatches, false, "Function must take zero arguments", 1, "BOOM")

    // Plain strings.
    testCheck(c, check.PanicMatches, true, "", func() { panic("BOOM") }, "BO.M")
    testCheck(c, check.PanicMatches, false, "", func() { panic("KABOOM") }, "BOOM")
    testCheck(c, check.PanicMatches, true, "", func() bool { panic("BOOM") }, "BO.M")

    // Verify params/names mutation
    params, names := testCheck(c, check.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BOOM")
    c.Assert(params[0], check.Equals, "KABOOM")
    c.Assert(names[0], check.Equals, "panic")

    // Verify a nil panic
    testCheck(c, check.PanicMatches, false, "Panic value is not a string or an error", func() { panic(nil) }, "")
}

func (s *CheckersS) TestFitsTypeOf(c *check.C) {
    testInfo(c, check.FitsTypeOf, "FitsTypeOf", []string{"obtained", "sample"})

    // Basic types
    testCheck(c, check.FitsTypeOf, true, "", 1, 0)
    testCheck(c, check.FitsTypeOf, false, "", 1, int64(0))

    // Aliases
    testCheck(c, check.FitsTypeOf, false, "", 1, errors.New(""))
    testCheck(c, check.FitsTypeOf, false, "", "error", errors.New(""))
    testCheck(c, check.FitsTypeOf, true, "", errors.New("error"), errors.New(""))

    // Structures
    testCheck(c, check.FitsTypeOf, false, "", 1, simpleStruct{})
    testCheck(c, check.FitsTypeOf, false, "", simpleStruct{42}, &simpleStruct{})
    testCheck(c, check.FitsTypeOf, true, "", simpleStruct{42}, simpleStruct{})
    testCheck(c, check.FitsTypeOf, true, "", &simpleStruct{42}, &simpleStruct{})

    // Some bad values
    testCheck(c, check.FitsTypeOf, false, "Invalid sample value", 1, interface{}(nil))
    testCheck(c, check.FitsTypeOf, false, "", interface{}(nil), 0)
}

func (s *CheckersS) TestImplements(c *check.C) {
    testInfo(c, check.Implements, "Implements", []string{"obtained", "ifaceptr"})

    var e error
    var re runtime.Error
    testCheck(c, check.Implements, true, "", errors.New(""), &e)
    testCheck(c, check.Implements, false, "", errors.New(""), &re)

    // Some bad values
    testCheck(c, check.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, errors.New(""))
    testCheck(c, check.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, interface{}(nil))
    testCheck(c, check.Implements, false, "", interface{}(nil), &e)
}