Skip to content

Commit ef35568

Browse files
committed
add calls to assert pkg to cover almost all
1 parent 5cc1710 commit ef35568

File tree

1 file changed

+111
-30
lines changed

1 file changed

+111
-30
lines changed

assert/assert_test.go

Lines changed: 111 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,64 +25,71 @@ func ExampleNotNil() {
2525
sample := func(b *byte) (err error) {
2626
defer err2.Handle(&err, "sample")
2727

28-
assert.NotNil(b)
28+
assert.Nil(b) // OK
29+
assert.NotNil(b) // Not OK
2930
return err
3031
}
3132
var b *byte
3233
err := sample(b)
3334
fmt.Printf("%v", err)
34-
// Output: sample: assert_test.go:28: ExampleNotNil.func1(): assertion failure: pointer should not be nil
35+
// Output: sample: assert_test.go:29: ExampleNotNil.func1(): assertion failure: pointer should not be nil
3536
}
3637

3738
func ExampleMNotNil() {
3839
sample := func(b map[string]byte) (err error) {
3940
defer err2.Handle(&err, "sample")
4041

41-
assert.MNotNil(b)
42+
assert.MEmpty(b) // OK
43+
assert.MNil(b) // OK
44+
assert.MNotNil(b) // Not OK
4245
return err
4346
}
4447
var b map[string]byte
4548
err := sample(b)
4649
fmt.Printf("%v", err)
47-
// Output: sample: assert_test.go:41: ExampleMNotNil.func1(): assertion failure: map should not be nil
50+
// Output: sample: assert_test.go:44: ExampleMNotNil.func1(): assertion failure: map should not be nil
4851
}
4952

5053
func ExampleCNotNil() {
5154
sample := func(c chan byte) (err error) {
5255
defer err2.Handle(&err, "sample")
5356

54-
assert.CNotNil(c)
57+
assert.CNil(c) // OK
58+
assert.CNotNil(c) // Not OK
5559
return err
5660
}
5761
var c chan byte
5862
err := sample(c)
5963
fmt.Printf("%v", err)
60-
// Output: sample: assert_test.go:54: ExampleCNotNil.func1(): assertion failure: channel should not be nil
64+
// Output: sample: assert_test.go:58: ExampleCNotNil.func1(): assertion failure: channel should not be nil
6165
}
6266

6367
func ExampleSNotNil() {
6468
sample := func(b []byte) (err error) {
6569
defer err2.Handle(&err, "sample")
6670

67-
assert.SNotNil(b)
71+
assert.SEmpty(b) // OK
72+
assert.SNil(b) // OK
73+
assert.SNotNil(b) // Not OK
6874
return err
6975
}
7076
var b []byte
7177
err := sample(b)
7278
fmt.Printf("%v", err)
73-
// Output: sample: assert_test.go:67: ExampleSNotNil.func1(): assertion failure: slice should not be nil
79+
// Output: sample: assert_test.go:73: ExampleSNotNil.func1(): assertion failure: slice should not be nil
7480
}
7581

7682
func ExampleEqual() {
7783
sample := func(b []byte) (err error) {
7884
defer err2.Handle(&err, "sample")
7985

80-
assert.Equal(len(b), 3)
86+
assert.NotEqual(b[0], 3) // OK, b[0] != 3; (b[0] == 1)
87+
assert.Equal(b[1], 1) // Not OK, b[1] == 2
8188
return err
8289
}
8390
err := sample([]byte{1, 2})
8491
fmt.Printf("%v", err)
85-
// Output: sample: assert_test.go:80: ExampleEqual.func1(): assertion failure: equal: got '2', want '3'
92+
// Output: sample: assert_test.go:87: ExampleEqual.func1(): assertion failure: equal: got '2', want '1'
8693
}
8794

8895
func ExampleSLen() {
@@ -94,7 +101,7 @@ func ExampleSLen() {
94101
}
95102
err := sample([]byte{1, 2})
96103
fmt.Printf("%v", err)
97-
// Output: sample: assert_test.go:92: ExampleSLen.func1(): assertion failure: length: got '2', want '3'
104+
// Output: sample: assert_test.go:99: ExampleSLen.func1(): assertion failure: length: got '2', want '3'
98105
}
99106

100107
func ExampleSNotEmpty() {
@@ -106,7 +113,7 @@ func ExampleSNotEmpty() {
106113
}
107114
err := sample([]byte{})
108115
fmt.Printf("%v", err)
109-
// Output: sample: assert_test.go:104: ExampleSNotEmpty.func1(): assertion failure: slice should not be empty
116+
// Output: sample: assert_test.go:111: ExampleSNotEmpty.func1(): assertion failure: slice should not be empty
110117
}
111118

112119
func ExampleNotEmpty() {
@@ -119,7 +126,7 @@ func ExampleNotEmpty() {
119126
}
120127
err := sample("")
121128
fmt.Printf("%v", err)
122-
// Output: sample: assert_test.go:117: ExampleNotEmpty.func1(): assertion failure: string should not be empty
129+
// Output: sample: assert_test.go:124: ExampleNotEmpty.func1(): assertion failure: string should not be empty
123130
}
124131

125132
func ExampleMKeyExists() {
@@ -136,7 +143,7 @@ func ExampleMKeyExists() {
136143
}
137144
err := sample("2")
138145
fmt.Printf("%v", err)
139-
// Output: sample: assert_test.go:134: ExampleMKeyExists.func1(): assertion failure: key '2' doesn't exist
146+
// Output: sample: assert_test.go:141: ExampleMKeyExists.func1(): assertion failure: key '2' doesn't exist
140147
}
141148

142149
func ExampleZero() {
@@ -149,33 +156,34 @@ func ExampleZero() {
149156
var b int8 = 1 // we want sample to assert the violation.
150157
err := sample(b)
151158
fmt.Printf("%v", err)
152-
// Output: sample: assert_test.go:146: ExampleZero.func1(): assertion failure: got '1', want (== '0')
159+
// Output: sample: assert_test.go:153: ExampleZero.func1(): assertion failure: got '1', want (== '0')
153160
}
154161

155162
func ExampleSLonger() {
156163
sample := func(b []byte) (err error) {
157164
defer err2.Handle(&err, "sample")
158165

159-
assert.SLonger(b, 0) // ok
160-
assert.SLonger(b, 1) // not ok
166+
assert.SLonger(b, 0) // OK
167+
assert.SLonger(b, 1) // Not OK
161168
return err
162169
}
163170
err := sample([]byte{01}) // len = 1
164171
fmt.Printf("%v", err)
165-
// Output: sample: assert_test.go:160: ExampleSLonger.func1(): assertion failure: got '1', should be longer than '1'
172+
// Output: sample: assert_test.go:167: ExampleSLonger.func1(): assertion failure: got '1', should be longer than '1'
166173
}
167174

168175
func ExampleMShorter() {
169176
sample := func(b map[byte]byte) (err error) {
170177
defer err2.Handle(&err, "sample")
171178

172-
assert.MShorter(b, 1) // ok
173-
assert.MShorter(b, 0) // not ok
179+
assert.MNotEmpty(b) // OK
180+
assert.MShorter(b, 1) // OK
181+
assert.MShorter(b, 0) // Not OK
174182
return err
175183
}
176184
err := sample(map[byte]byte{01: 01}) // len = 1
177185
fmt.Printf("%v", err)
178-
// Output: sample: assert_test.go:172: ExampleMShorter.func1(): assertion failure: got '1', should be shorter than '1'
186+
// Output: sample: assert_test.go:180: ExampleMShorter.func1(): assertion failure: got '1', should be shorter than '1'
179187
}
180188

181189
func ExampleSShorter() {
@@ -188,7 +196,7 @@ func ExampleSShorter() {
188196
}
189197
err := sample([]byte{01}) // len = 1
190198
fmt.Printf("%v", err)
191-
// Output: sample: assert_test.go:186: ExampleSShorter.func1(): assertion failure: got '1', should be shorter than '0': optional message (test_str)
199+
// Output: sample: assert_test.go:194: ExampleSShorter.func1(): assertion failure: got '1', should be shorter than '0': optional message (test_str)
192200
}
193201

194202
func ExampleLess() {
@@ -203,7 +211,7 @@ func ExampleLess() {
203211
var b int8 = 1
204212
err := sample(b)
205213
fmt.Printf("%v", err)
206-
// Output: sample: assert_test.go:200: ExampleLess.func1(): assertion failure: got '1', want >= '1'
214+
// Output: sample: assert_test.go:208: ExampleLess.func1(): assertion failure: got '1', want >= '1'
207215
}
208216

209217
func ExampleGreater() {
@@ -218,7 +226,7 @@ func ExampleGreater() {
218226
var b int8 = 2
219227
err := sample(b)
220228
fmt.Printf("%v", err)
221-
// Output: sample: assert_test.go:215: ExampleGreater.func1(): assertion failure: got '2', want <= '2'
229+
// Output: sample: assert_test.go:223: ExampleGreater.func1(): assertion failure: got '2', want <= '2'
222230
}
223231

224232
func ExampleNotZero() {
@@ -231,34 +239,38 @@ func ExampleNotZero() {
231239
var b int8
232240
err := sample(b)
233241
fmt.Printf("%v", err)
234-
// Output: sample: assert_test.go:228: ExampleNotZero.func1(): assertion failure: got '0', want (!= 0)
242+
// Output: sample: assert_test.go:236: ExampleNotZero.func1(): assertion failure: got '0', want (!= 0)
235243
}
236244

237245
func ExampleMLen() {
238246
sample := func(b map[int]byte) (err error) {
239247
defer err2.Handle(&err, "sample")
240248

241-
assert.MLen(b, 3)
249+
assert.MLonger(b, 1) // OK
250+
assert.MShorter(b, 3) // OK
251+
assert.MLen(b, 3) // Not OK
242252
return err
243253
}
244254
err := sample(map[int]byte{1: 1, 2: 2})
245255
fmt.Printf("%v", err)
246-
// Output: sample: assert_test.go:241: ExampleMLen.func1(): assertion failure: length: got '2', want '3'
256+
// Output: sample: assert_test.go:251: ExampleMLen.func1(): assertion failure: length: got '2', want '3'
247257
}
248258

249259
func ExampleCLen() {
250260
sample := func(b chan int) (err error) {
251261
defer err2.Handle(&err, "sample")
252262

253-
assert.CLen(b, 3)
263+
assert.CLonger(b, 1) // OK
264+
assert.CShorter(b, 3) // OK
265+
assert.CLen(b, 3) // Not OK
254266
return err
255267
}
256268
d := make(chan int, 2)
257269
d <- int(1)
258270
d <- int(1)
259271
err := sample(d)
260272
fmt.Printf("%v", err)
261-
// Output: sample: assert_test.go:253: ExampleCLen.func1(): assertion failure: length: got '2', want '3'
273+
// Output: sample: assert_test.go:265: ExampleCLen.func1(): assertion failure: length: got '2', want '3'
262274
}
263275

264276
func ExampleThatNot() {
@@ -269,14 +281,83 @@ func ExampleThatNot() {
269281
return err
270282
}
271283

272-
// set asserter for this thread/goroutine only, we want plain errors
284+
// Set a specific asserter for this goroutine only, we want plain errors
273285
defer assert.SetAsserter(assert.Plain)()
274286

275287
err := sample()
276288
fmt.Printf("%v", err)
277289
// Output: testing: run example: overrides if Plain asserter
278290
}
279291

292+
func ExampleINotNil() {
293+
sample := func(b error) (err error) {
294+
defer err2.Handle(&err, "sample")
295+
296+
assert.INotNil(b) // OK
297+
assert.INil(b) // Not OK
298+
return err
299+
}
300+
var b = fmt.Errorf("test")
301+
err := sample(b)
302+
fmt.Printf("%v", err)
303+
// Output: sample: assert_test.go:297: ExampleINotNil.func1(): assertion failure: interface should be nil
304+
}
305+
306+
func ExampleLen() {
307+
sample := func(b string) (err error) {
308+
defer err2.Handle(&err, "sample")
309+
310+
assert.Shorter(b, 3) // OK
311+
assert.Longer(b, 1) // OK
312+
assert.Len(b, 3) // Not OK
313+
return err
314+
}
315+
err := sample("12")
316+
fmt.Printf("%v", err)
317+
// Output: sample: assert_test.go:312: ExampleLen.func1(): assertion failure: length: got '2', want '3'
318+
}
319+
320+
func ExampleDeepEqual() {
321+
sample := func(b []byte) (err error) {
322+
defer err2.Handle(&err, "sample")
323+
324+
assert.NoError(err)
325+
assert.NotDeepEqual(len(b), 3) // OK, correct size is 2
326+
assert.DeepEqual(len(b), 3) // Not OK, size is still 2
327+
return err
328+
}
329+
err := sample([]byte{1, 2})
330+
fmt.Printf("%v", err)
331+
// Output: sample: assert_test.go:326: ExampleDeepEqual.func1(): assertion failure: got '2', want '3'
332+
}
333+
334+
func ExampleError() {
335+
sample := func(b error) (err error) {
336+
defer err2.Handle(&err, "sample")
337+
338+
assert.Error(b) // OK
339+
assert.NoError(b) // Not OK
340+
return err
341+
}
342+
var b = fmt.Errorf("test")
343+
err := sample(b)
344+
fmt.Printf("%v", err)
345+
// Output: sample: assert_test.go:339: ExampleError.func1(): assertion failure: test
346+
}
347+
348+
func ExampleNotImplemented() {
349+
sample := func(_ error) (err error) {
350+
defer err2.Handle(&err, "sample")
351+
352+
assert.NotImplemented() // Not OK
353+
return err
354+
}
355+
var b = fmt.Errorf("test")
356+
err := sample(b)
357+
fmt.Printf("%v", err)
358+
// Output: sample: assert_test.go:352: ExampleNotImplemented.func1(): assertion failure: not implemented
359+
}
360+
280361
func BenchmarkMKeyExists(b *testing.B) {
281362
bs := map[int]int{0: 0, 1: 1}
282363
for n := 0; n < b.N; n++ {

0 commit comments

Comments
 (0)