|
@@ -49,30 +49,31 @@ The example below shows assert used with some common types.
|
|
|
|
|
|
Comparisons
|
|
|
|
|
|
-Package https://godoc.org/gotest.tools/assert/cmp provides
|
|
|
+Package http://gotest.tools/assert/cmp provides
|
|
|
many common comparisons. Additional comparisons can be written to compare
|
|
|
values in other ways. See the example Assert (CustomComparison).
|
|
|
|
|
|
Automated migration from testify
|
|
|
|
|
|
-gty-migrate-from-testify is a binary which can update source code which uses
|
|
|
-testify assertions to use the assertions provided by this package.
|
|
|
+gty-migrate-from-testify is a command which translates Go source code from
|
|
|
+testify assertions to the assertions provided by this package.
|
|
|
|
|
|
-See http://bit.do/cmd-gty-migrate-from-testify.
|
|
|
+See http://gotest.tools/assert/cmd/gty-migrate-from-testify.
|
|
|
|
|
|
|
|
|
*/
|
|
|
-package assert // import "gotest.tools/assert"
|
|
|
+package assert // import "gotest.tools/v3/assert"
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"go/ast"
|
|
|
"go/token"
|
|
|
+ "reflect"
|
|
|
|
|
|
gocmp "github.com/google/go-cmp/cmp"
|
|
|
- "gotest.tools/assert/cmp"
|
|
|
- "gotest.tools/internal/format"
|
|
|
- "gotest.tools/internal/source"
|
|
|
+ "gotest.tools/v3/assert/cmp"
|
|
|
+ "gotest.tools/v3/internal/format"
|
|
|
+ "gotest.tools/v3/internal/source"
|
|
|
)
|
|
|
|
|
|
// BoolOrComparison can be a bool, or cmp.Comparison. See Assert() for usage.
|
|
@@ -118,8 +119,8 @@ func assert(
|
|
|
return true
|
|
|
|
|
|
case error:
|
|
|
- msg := "error is not nil: "
|
|
|
- t.Log(format.WithCustomMessage(failureMessage+msg+check.Error(), msgAndArgs...))
|
|
|
+ msg := failureMsgFromError(check)
|
|
|
+ t.Log(format.WithCustomMessage(failureMessage+msg, msgAndArgs...))
|
|
|
|
|
|
case cmp.Comparison:
|
|
|
success = runComparison(t, argSelector, check, msgAndArgs...)
|
|
@@ -174,6 +175,15 @@ func logFailureFromBool(t TestingT, msgAndArgs ...interface{}) {
|
|
|
t.Log(format.WithCustomMessage(failureMessage+msg, msgAndArgs...))
|
|
|
}
|
|
|
|
|
|
+func failureMsgFromError(err error) string {
|
|
|
+ // Handle errors with non-nil types
|
|
|
+ v := reflect.ValueOf(err)
|
|
|
+ if v.Kind() == reflect.Ptr && v.IsNil() {
|
|
|
+ return fmt.Sprintf("error is not nil: error has type %T", err)
|
|
|
+ }
|
|
|
+ return "error is not nil: " + err.Error()
|
|
|
+}
|
|
|
+
|
|
|
func boolFailureMessage(expr ast.Expr) (string, error) {
|
|
|
if binaryExpr, ok := expr.(*ast.BinaryExpr); ok && binaryExpr.Op == token.NEQ {
|
|
|
x, err := source.FormatNode(binaryExpr.X)
|
|
@@ -202,17 +212,20 @@ func boolFailureMessage(expr ast.Expr) (string, error) {
|
|
|
return "expression is false: " + formatted, nil
|
|
|
}
|
|
|
|
|
|
-// Assert performs a comparison. If the comparison fails the test is marked as
|
|
|
+// Assert performs a comparison. If the comparison fails, the test is marked as
|
|
|
// failed, a failure message is logged, and execution is stopped immediately.
|
|
|
//
|
|
|
-// The comparison argument may be one of three types: bool, cmp.Comparison or
|
|
|
-// error.
|
|
|
-// When called with a bool the failure message will contain the literal source
|
|
|
-// code of the expression.
|
|
|
-// When called with a cmp.Comparison the comparison is responsible for producing
|
|
|
-// a helpful failure message.
|
|
|
-// When called with an error a nil value is considered success. A non-nil error
|
|
|
-// is a failure, and Error() is used as the failure message.
|
|
|
+// The comparison argument may be one of three types:
|
|
|
+// bool
|
|
|
+// True is success. False is a failure.
|
|
|
+// The failure message will contain the literal source code of the expression.
|
|
|
+// cmp.Comparison
|
|
|
+// Uses cmp.Result.Success() to check for success of failure.
|
|
|
+// The comparison is responsible for producing a helpful failure message.
|
|
|
+// http://gotest.tools/assert/cmp provides many common comparisons.
|
|
|
+// error
|
|
|
+// A nil value is considered success.
|
|
|
+// A non-nil error is a failure, err.Error() is used as the failure message.
|
|
|
func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) {
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
ht.Helper()
|
|
@@ -260,10 +273,10 @@ func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{}) {
|
|
|
assert(t, t.FailNow, argsAfterT, cmp.Equal(x, y), msgAndArgs...)
|
|
|
}
|
|
|
|
|
|
-// DeepEqual uses google/go-cmp (http://bit.do/go-cmp) to assert two values are
|
|
|
-// equal and fails the test if they are not equal.
|
|
|
+// DeepEqual uses google/go-cmp (https://godoc.org/github.com/google/go-cmp/cmp)
|
|
|
+// to assert two values are equal and fails the test if they are not equal.
|
|
|
//
|
|
|
-// Package https://godoc.org/gotest.tools/assert/opt provides some additional
|
|
|
+// Package http://gotest.tools/assert/opt provides some additional
|
|
|
// commonly used Options.
|
|
|
//
|
|
|
// This is equivalent to Assert(t, cmp.DeepEqual(x, y)).
|
|
@@ -295,14 +308,19 @@ func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...interf
|
|
|
}
|
|
|
|
|
|
// ErrorType fails the test if err is nil, or err is not the expected type.
|
|
|
+// Equivalent to Assert(t, cmp.ErrorType(err, expected)).
|
|
|
//
|
|
|
// Expected can be one of:
|
|
|
-// a func(error) bool which returns true if the error is the expected type,
|
|
|
-// an instance of (or a pointer to) a struct of the expected type,
|
|
|
-// a pointer to an interface the error is expected to implement,
|
|
|
-// a reflect.Type of the expected struct or interface.
|
|
|
-//
|
|
|
-// Equivalent to Assert(t, cmp.ErrorType(err, expected)).
|
|
|
+// func(error) bool
|
|
|
+// Function should return true if the error is the expected type.
|
|
|
+// type struct{}, type &struct{}
|
|
|
+// A struct or a pointer to a struct.
|
|
|
+// Fails if the error is not of the same type as expected.
|
|
|
+// type &interface{}
|
|
|
+// A pointer to an interface type.
|
|
|
+// Fails if err does not implement the interface.
|
|
|
+// reflect.Type
|
|
|
+// Fails if err does not implement the reflect.Type
|
|
|
func ErrorType(t TestingT, err error, expected interface{}, msgAndArgs ...interface{}) {
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
ht.Helper()
|