@@ -25,64 +25,71 @@ func ExampleNotNil() {
25
25
sample := func (b * byte ) (err error ) {
26
26
defer err2 .Handle (& err , "sample" )
27
27
28
- assert .NotNil (b )
28
+ assert .Nil (b ) // OK
29
+ assert .NotNil (b ) // Not OK
29
30
return err
30
31
}
31
32
var b * byte
32
33
err := sample (b )
33
34
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
35
36
}
36
37
37
38
func ExampleMNotNil () {
38
39
sample := func (b map [string ]byte ) (err error ) {
39
40
defer err2 .Handle (& err , "sample" )
40
41
41
- assert .MNotNil (b )
42
+ assert .MEmpty (b ) // OK
43
+ assert .MNil (b ) // OK
44
+ assert .MNotNil (b ) // Not OK
42
45
return err
43
46
}
44
47
var b map [string ]byte
45
48
err := sample (b )
46
49
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
48
51
}
49
52
50
53
func ExampleCNotNil () {
51
54
sample := func (c chan byte ) (err error ) {
52
55
defer err2 .Handle (& err , "sample" )
53
56
54
- assert .CNotNil (c )
57
+ assert .CNil (c ) // OK
58
+ assert .CNotNil (c ) // Not OK
55
59
return err
56
60
}
57
61
var c chan byte
58
62
err := sample (c )
59
63
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
61
65
}
62
66
63
67
func ExampleSNotNil () {
64
68
sample := func (b []byte ) (err error ) {
65
69
defer err2 .Handle (& err , "sample" )
66
70
67
- assert .SNotNil (b )
71
+ assert .SEmpty (b ) // OK
72
+ assert .SNil (b ) // OK
73
+ assert .SNotNil (b ) // Not OK
68
74
return err
69
75
}
70
76
var b []byte
71
77
err := sample (b )
72
78
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
74
80
}
75
81
76
82
func ExampleEqual () {
77
83
sample := func (b []byte ) (err error ) {
78
84
defer err2 .Handle (& err , "sample" )
79
85
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
81
88
return err
82
89
}
83
90
err := sample ([]byte {1 , 2 })
84
91
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 '
86
93
}
87
94
88
95
func ExampleSLen () {
@@ -94,7 +101,7 @@ func ExampleSLen() {
94
101
}
95
102
err := sample ([]byte {1 , 2 })
96
103
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'
98
105
}
99
106
100
107
func ExampleSNotEmpty () {
@@ -106,7 +113,7 @@ func ExampleSNotEmpty() {
106
113
}
107
114
err := sample ([]byte {})
108
115
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
110
117
}
111
118
112
119
func ExampleNotEmpty () {
@@ -119,7 +126,7 @@ func ExampleNotEmpty() {
119
126
}
120
127
err := sample ("" )
121
128
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
123
130
}
124
131
125
132
func ExampleMKeyExists () {
@@ -136,7 +143,7 @@ func ExampleMKeyExists() {
136
143
}
137
144
err := sample ("2" )
138
145
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
140
147
}
141
148
142
149
func ExampleZero () {
@@ -149,33 +156,34 @@ func ExampleZero() {
149
156
var b int8 = 1 // we want sample to assert the violation.
150
157
err := sample (b )
151
158
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')
153
160
}
154
161
155
162
func ExampleSLonger () {
156
163
sample := func (b []byte ) (err error ) {
157
164
defer err2 .Handle (& err , "sample" )
158
165
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
161
168
return err
162
169
}
163
170
err := sample ([]byte {01 }) // len = 1
164
171
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'
166
173
}
167
174
168
175
func ExampleMShorter () {
169
176
sample := func (b map [byte ]byte ) (err error ) {
170
177
defer err2 .Handle (& err , "sample" )
171
178
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
174
182
return err
175
183
}
176
184
err := sample (map [byte ]byte {01 : 01 }) // len = 1
177
185
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'
179
187
}
180
188
181
189
func ExampleSShorter () {
@@ -188,7 +196,7 @@ func ExampleSShorter() {
188
196
}
189
197
err := sample ([]byte {01 }) // len = 1
190
198
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)
192
200
}
193
201
194
202
func ExampleLess () {
@@ -203,7 +211,7 @@ func ExampleLess() {
203
211
var b int8 = 1
204
212
err := sample (b )
205
213
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'
207
215
}
208
216
209
217
func ExampleGreater () {
@@ -218,7 +226,7 @@ func ExampleGreater() {
218
226
var b int8 = 2
219
227
err := sample (b )
220
228
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'
222
230
}
223
231
224
232
func ExampleNotZero () {
@@ -231,34 +239,38 @@ func ExampleNotZero() {
231
239
var b int8
232
240
err := sample (b )
233
241
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)
235
243
}
236
244
237
245
func ExampleMLen () {
238
246
sample := func (b map [int ]byte ) (err error ) {
239
247
defer err2 .Handle (& err , "sample" )
240
248
241
- assert .MLen (b , 3 )
249
+ assert .MLonger (b , 1 ) // OK
250
+ assert .MShorter (b , 3 ) // OK
251
+ assert .MLen (b , 3 ) // Not OK
242
252
return err
243
253
}
244
254
err := sample (map [int ]byte {1 : 1 , 2 : 2 })
245
255
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'
247
257
}
248
258
249
259
func ExampleCLen () {
250
260
sample := func (b chan int ) (err error ) {
251
261
defer err2 .Handle (& err , "sample" )
252
262
253
- assert .CLen (b , 3 )
263
+ assert .CLonger (b , 1 ) // OK
264
+ assert .CShorter (b , 3 ) // OK
265
+ assert .CLen (b , 3 ) // Not OK
254
266
return err
255
267
}
256
268
d := make (chan int , 2 )
257
269
d <- int (1 )
258
270
d <- int (1 )
259
271
err := sample (d )
260
272
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'
262
274
}
263
275
264
276
func ExampleThatNot () {
@@ -269,14 +281,83 @@ func ExampleThatNot() {
269
281
return err
270
282
}
271
283
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
273
285
defer assert .SetAsserter (assert .Plain )()
274
286
275
287
err := sample ()
276
288
fmt .Printf ("%v" , err )
277
289
// Output: testing: run example: overrides if Plain asserter
278
290
}
279
291
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
+
280
361
func BenchmarkMKeyExists (b * testing.B ) {
281
362
bs := map [int ]int {0 : 0 , 1 : 1 }
282
363
for n := 0 ; n < b .N ; n ++ {
0 commit comments