@@ -49,30 +49,31 @@ The example below shows assert used with some common types.
49
49
50
50
Comparisons
51
51
52
- Package https://godoc.org /gotest.tools/assert/cmp provides
52
+ Package http:/ /gotest.tools/assert/cmp provides
53
53
many common comparisons. Additional comparisons can be written to compare
54
54
values in other ways. See the example Assert (CustomComparison).
55
55
56
56
Automated migration from testify
57
57
58
- gty-migrate-from-testify is a binary which can update source code which uses
59
- testify assertions to use the assertions provided by this package.
58
+ gty-migrate-from-testify is a command which translates Go source code from
59
+ testify assertions to the assertions provided by this package.
60
60
61
- See http://bit.do/ cmd- gty-migrate-from-testify.
61
+ See http://gotest.tools/assert/ cmd/ gty-migrate-from-testify.
62
62
63
63
64
64
*/
65
- package assert // import "gotest.tools/assert"
65
+ package assert // import "gotest.tools/v3/ assert"
66
66
67
67
import (
68
68
"fmt"
69
69
"go/ast"
70
70
"go/token"
71
+ "reflect"
71
72
72
73
gocmp "github.com/google/go-cmp/cmp"
73
- "gotest.tools/assert/cmp"
74
- "gotest.tools/internal/format"
75
- "gotest.tools/internal/source"
74
+ "gotest.tools/v3/ assert/cmp"
75
+ "gotest.tools/v3/ internal/format"
76
+ "gotest.tools/v3/ internal/source"
76
77
)
77
78
78
79
// BoolOrComparison can be a bool, or cmp.Comparison. See Assert() for usage.
@@ -118,8 +119,8 @@ func assert(
118
119
return true
119
120
120
121
case error :
121
- msg := "error is not nil: "
122
- t .Log (format .WithCustomMessage (failureMessage + msg + check . Error () , msgAndArgs ... ))
122
+ msg := failureMsgFromError ( check )
123
+ t .Log (format .WithCustomMessage (failureMessage + msg , msgAndArgs ... ))
123
124
124
125
case cmp.Comparison :
125
126
success = runComparison (t , argSelector , check , msgAndArgs ... )
@@ -174,6 +175,15 @@ func logFailureFromBool(t TestingT, msgAndArgs ...interface{}) {
174
175
t .Log (format .WithCustomMessage (failureMessage + msg , msgAndArgs ... ))
175
176
}
176
177
178
+ func failureMsgFromError (err error ) string {
179
+ // Handle errors with non-nil types
180
+ v := reflect .ValueOf (err )
181
+ if v .Kind () == reflect .Ptr && v .IsNil () {
182
+ return fmt .Sprintf ("error is not nil: error has type %T" , err )
183
+ }
184
+ return "error is not nil: " + err .Error ()
185
+ }
186
+
177
187
func boolFailureMessage (expr ast.Expr ) (string , error ) {
178
188
if binaryExpr , ok := expr .(* ast.BinaryExpr ); ok && binaryExpr .Op == token .NEQ {
179
189
x , err := source .FormatNode (binaryExpr .X )
@@ -202,17 +212,20 @@ func boolFailureMessage(expr ast.Expr) (string, error) {
202
212
return "expression is false: " + formatted , nil
203
213
}
204
214
205
- // Assert performs a comparison. If the comparison fails the test is marked as
215
+ // Assert performs a comparison. If the comparison fails, the test is marked as
206
216
// failed, a failure message is logged, and execution is stopped immediately.
207
217
//
208
- // The comparison argument may be one of three types: bool, cmp.Comparison or
209
- // error.
210
- // When called with a bool the failure message will contain the literal source
211
- // code of the expression.
212
- // When called with a cmp.Comparison the comparison is responsible for producing
213
- // a helpful failure message.
214
- // When called with an error a nil value is considered success. A non-nil error
215
- // is a failure, and Error() is used as the failure message.
218
+ // The comparison argument may be one of three types:
219
+ // bool
220
+ // True is success. False is a failure.
221
+ // The failure message will contain the literal source code of the expression.
222
+ // cmp.Comparison
223
+ // Uses cmp.Result.Success() to check for success of failure.
224
+ // The comparison is responsible for producing a helpful failure message.
225
+ // http://gotest.tools/assert/cmp provides many common comparisons.
226
+ // error
227
+ // A nil value is considered success.
228
+ // A non-nil error is a failure, err.Error() is used as the failure message.
216
229
func Assert (t TestingT , comparison BoolOrComparison , msgAndArgs ... interface {}) {
217
230
if ht , ok := t .(helperT ); ok {
218
231
ht .Helper ()
@@ -260,10 +273,10 @@ func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{}) {
260
273
assert (t , t .FailNow , argsAfterT , cmp .Equal (x , y ), msgAndArgs ... )
261
274
}
262
275
263
- // DeepEqual uses google/go-cmp (http ://bit.do/ go-cmp) to assert two values are
264
- // equal and fails the test if they are not equal.
276
+ // DeepEqual uses google/go-cmp (https ://godoc.org/github.com/google/ go-cmp/cmp)
277
+ // to assert two values are equal and fails the test if they are not equal.
265
278
//
266
- // Package https://godoc.org /gotest.tools/assert/opt provides some additional
279
+ // Package http:/ /gotest.tools/assert/opt provides some additional
267
280
// commonly used Options.
268
281
//
269
282
// This is equivalent to Assert(t, cmp.DeepEqual(x, y)).
@@ -295,14 +308,19 @@ func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...interf
295
308
}
296
309
297
310
// ErrorType fails the test if err is nil, or err is not the expected type.
311
+ // Equivalent to Assert(t, cmp.ErrorType(err, expected)).
298
312
//
299
313
// Expected can be one of:
300
- // a func(error) bool which returns true if the error is the expected type,
301
- // an instance of (or a pointer to) a struct of the expected type,
302
- // a pointer to an interface the error is expected to implement,
303
- // a reflect.Type of the expected struct or interface.
304
- //
305
- // Equivalent to Assert(t, cmp.ErrorType(err, expected)).
314
+ // func(error) bool
315
+ // Function should return true if the error is the expected type.
316
+ // type struct{}, type &struct{}
317
+ // A struct or a pointer to a struct.
318
+ // Fails if the error is not of the same type as expected.
319
+ // type &interface{}
320
+ // A pointer to an interface type.
321
+ // Fails if err does not implement the interface.
322
+ // reflect.Type
323
+ // Fails if err does not implement the reflect.Type
306
324
func ErrorType (t TestingT , err error , expected interface {}, msgAndArgs ... interface {}) {
307
325
if ht , ok := t .(helperT ); ok {
308
326
ht .Helper ()
0 commit comments