2017-12-22 16:33:58 -05:00
|
|
|
/*Package assert provides assertions for comparing expected values to actual
|
|
|
|
values. When an assertion fails a helpful error message is printed.
|
2018-01-16 17:20:43 -05:00
|
|
|
|
|
|
|
Assert and Check
|
|
|
|
|
|
|
|
Assert() and Check() both accept a Comparison, and fail the test when the
|
|
|
|
comparison fails. The one difference is that Assert() will end the test execution
|
|
|
|
immediately (using t.FailNow()) whereas Check() will fail the test (using t.Fail()),
|
|
|
|
return the value of the comparison, then proceed with the rest of the test case.
|
|
|
|
|
2018-06-08 12:09:51 -04:00
|
|
|
Example usage
|
2018-01-16 17:20:43 -05:00
|
|
|
|
|
|
|
The example below shows assert used with some common types.
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-08 12:09:51 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2018-01-16 17:20:43 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEverything(t *testing.T) {
|
|
|
|
// booleans
|
2017-12-22 16:33:58 -05:00
|
|
|
assert.Assert(t, ok)
|
2018-01-16 17:20:43 -05:00
|
|
|
assert.Assert(t, !missing)
|
|
|
|
|
|
|
|
// primitives
|
|
|
|
assert.Equal(t, count, 1)
|
|
|
|
assert.Equal(t, msg, "the message")
|
|
|
|
assert.Assert(t, total != 10) // NotEqual
|
|
|
|
|
|
|
|
// errors
|
|
|
|
assert.NilError(t, closer.Close())
|
2017-12-22 16:33:58 -05:00
|
|
|
assert.Error(t, err, "the exact error message")
|
|
|
|
assert.ErrorContains(t, err, "includes this")
|
|
|
|
assert.ErrorType(t, err, os.IsNotExist)
|
2018-01-16 17:20:43 -05:00
|
|
|
|
|
|
|
// complex types
|
2017-12-22 16:33:58 -05:00
|
|
|
assert.DeepEqual(t, result, myStruct{Name: "title"})
|
2018-01-16 17:20:43 -05:00
|
|
|
assert.Assert(t, is.Len(items, 3))
|
|
|
|
assert.Assert(t, len(sequence) != 0) // NotEmpty
|
|
|
|
assert.Assert(t, is.Contains(mapping, "key"))
|
|
|
|
|
|
|
|
// pointers and interface
|
|
|
|
assert.Assert(t, is.Nil(ref))
|
|
|
|
assert.Assert(t, ref != nil) // NotNil
|
|
|
|
}
|
|
|
|
|
|
|
|
Comparisons
|
|
|
|
|
2018-06-08 12:09:51 -04:00
|
|
|
Package https://godoc.org/gotest.tools/assert/cmp provides
|
2018-01-16 17:20:43 -05:00
|
|
|
many common comparisons. Additional comparisons can be written to compare
|
2017-12-22 16:33:58 -05:00
|
|
|
values in other ways. See the example Assert (CustomComparison).
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2018-06-08 12:09:51 -04:00
|
|
|
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.
|
|
|
|
|
|
|
|
See http://bit.do/cmd-gty-migrate-from-testify.
|
|
|
|
|
|
|
|
|
2018-01-16 17:20:43 -05:00
|
|
|
*/
|
2018-06-08 12:09:51 -04:00
|
|
|
package assert // import "gotest.tools/assert"
|
2018-01-16 17:20:43 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-12-22 16:33:58 -05:00
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
gocmp "github.com/google/go-cmp/cmp"
|
2018-06-08 12:09:51 -04:00
|
|
|
"gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/internal/format"
|
|
|
|
"gotest.tools/internal/source"
|
2018-01-16 17:20:43 -05:00
|
|
|
)
|
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
// BoolOrComparison can be a bool, or cmp.Comparison. See Assert() for usage.
|
2018-01-16 17:20:43 -05:00
|
|
|
type BoolOrComparison interface{}
|
|
|
|
|
|
|
|
// TestingT is the subset of testing.T used by the assert package.
|
|
|
|
type TestingT interface {
|
|
|
|
FailNow()
|
|
|
|
Fail()
|
|
|
|
Log(args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type helperT interface {
|
|
|
|
Helper()
|
|
|
|
}
|
|
|
|
|
|
|
|
const failureMessage = "assertion failed: "
|
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
// nolint: gocyclo
|
2018-01-16 17:20:43 -05:00
|
|
|
func assert(
|
|
|
|
t TestingT,
|
|
|
|
failer func(),
|
2017-12-22 16:33:58 -05:00
|
|
|
argSelector argSelector,
|
2018-01-16 17:20:43 -05:00
|
|
|
comparison BoolOrComparison,
|
|
|
|
msgAndArgs ...interface{},
|
|
|
|
) bool {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
var success bool
|
2018-01-16 17:20:43 -05:00
|
|
|
switch check := comparison.(type) {
|
|
|
|
case bool:
|
|
|
|
if check {
|
|
|
|
return true
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
logFailureFromBool(t, msgAndArgs...)
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
// Undocumented legacy comparison without Result type
|
|
|
|
case func() (success bool, message string):
|
|
|
|
success = runCompareFunc(t, check, msgAndArgs...)
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
case nil:
|
|
|
|
return true
|
2018-01-16 17:20:43 -05:00
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
case error:
|
|
|
|
msg := "error is not nil: "
|
|
|
|
t.Log(format.WithCustomMessage(failureMessage+msg+check.Error(), msgAndArgs...))
|
|
|
|
|
|
|
|
case cmp.Comparison:
|
|
|
|
success = runComparison(t, argSelector, check, msgAndArgs...)
|
|
|
|
|
|
|
|
case func() cmp.Result:
|
|
|
|
success = runComparison(t, argSelector, check, msgAndArgs...)
|
2018-01-16 17:20:43 -05:00
|
|
|
|
|
|
|
default:
|
2017-12-22 16:33:58 -05:00
|
|
|
t.Log(fmt.Sprintf("invalid Comparison: %v (%T)", check, check))
|
|
|
|
}
|
|
|
|
|
|
|
|
if success {
|
|
|
|
return true
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
failer()
|
|
|
|
return false
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
func runCompareFunc(
|
|
|
|
t TestingT,
|
|
|
|
f func() (success bool, message string),
|
|
|
|
msgAndArgs ...interface{},
|
|
|
|
) bool {
|
2018-01-16 17:20:43 -05:00
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
if success, message := f(); !success {
|
|
|
|
t.Log(format.WithCustomMessage(failureMessage+message, msgAndArgs...))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
func logFailureFromBool(t TestingT, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
const stackIndex = 3 // Assert()/Check(), assert(), formatFailureFromBool()
|
|
|
|
const comparisonArgPos = 1
|
|
|
|
args, err := source.CallExprArgs(stackIndex)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := boolFailureMessage(args[comparisonArgPos])
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
|
|
|
msg = "expression is false"
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(format.WithCustomMessage(failureMessage+msg, msgAndArgs...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func boolFailureMessage(expr ast.Expr) (string, error) {
|
|
|
|
if binaryExpr, ok := expr.(*ast.BinaryExpr); ok && binaryExpr.Op == token.NEQ {
|
|
|
|
x, err := source.FormatNode(binaryExpr.X)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
y, err := source.FormatNode(binaryExpr.Y)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return x + " is " + y, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if unaryExpr, ok := expr.(*ast.UnaryExpr); ok && unaryExpr.Op == token.NOT {
|
|
|
|
x, err := source.FormatNode(unaryExpr.X)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return x + " is true", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
formatted, err := source.FormatNode(expr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return "expression is false: " + formatted, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-01-16 17:20:43 -05:00
|
|
|
func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
assert(t, t.FailNow, argsFromComparisonCall, comparison, msgAndArgs...)
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
// Check performs a comparison. If the comparison fails the test is marked as
|
|
|
|
// failed, a failure message is logged, and Check returns false. Otherwise returns
|
|
|
|
// true.
|
|
|
|
//
|
|
|
|
// See Assert for details about the comparison arg and failure messages.
|
2018-01-16 17:20:43 -05:00
|
|
|
func Check(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) bool {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
return assert(t, t.Fail, argsFromComparisonCall, comparison, msgAndArgs...)
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
|
|
|
|
2017-12-22 16:33:58 -05:00
|
|
|
// NilError fails the test immediately if err is not nil.
|
|
|
|
// This is equivalent to Assert(t, err)
|
2018-01-16 17:20:43 -05:00
|
|
|
func NilError(t TestingT, err error, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
assert(t, t.FailNow, argsAfterT, err, msgAndArgs...)
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal uses the == operator to assert two values are equal and fails the test
|
2018-06-08 12:09:51 -04:00
|
|
|
// if they are not equal.
|
|
|
|
//
|
|
|
|
// If the comparison fails Equal will use the variable names for x and y as part
|
|
|
|
// of the failure message to identify the actual and expected values.
|
|
|
|
//
|
|
|
|
// If either x or y are a multi-line string the failure message will include a
|
|
|
|
// unified diff of the two values. If the values only differ by whitespace
|
|
|
|
// the unified diff will be augmented by replacing whitespace characters with
|
|
|
|
// visible characters to identify the whitespace difference.
|
|
|
|
//
|
|
|
|
// This is equivalent to Assert(t, cmp.Equal(x, y)).
|
2018-01-16 17:20:43 -05:00
|
|
|
func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2017-12-22 16:33:58 -05:00
|
|
|
assert(t, t.FailNow, argsAfterT, cmp.Equal(x, y), msgAndArgs...)
|
|
|
|
}
|
|
|
|
|
2018-06-08 12:09:51 -04:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Package https://godoc.org/gotest.tools/assert/opt provides some additional
|
|
|
|
// commonly used Options.
|
|
|
|
//
|
2017-12-22 16:33:58 -05:00
|
|
|
// This is equivalent to Assert(t, cmp.DeepEqual(x, y)).
|
|
|
|
func DeepEqual(t TestingT, x, y interface{}, opts ...gocmp.Option) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
assert(t, t.FailNow, argsAfterT, cmp.DeepEqual(x, y, opts...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error fails the test if err is nil, or the error message is not the expected
|
|
|
|
// message.
|
|
|
|
// Equivalent to Assert(t, cmp.Error(err, message)).
|
|
|
|
func Error(t TestingT, err error, message string, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
assert(t, t.FailNow, argsAfterT, cmp.Error(err, message), msgAndArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorContains fails the test if err is nil, or the error message does not
|
|
|
|
// contain the expected substring.
|
|
|
|
// Equivalent to Assert(t, cmp.ErrorContains(err, substring)).
|
|
|
|
func ErrorContains(t TestingT, err error, substring string, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
assert(t, t.FailNow, argsAfterT, cmp.ErrorContains(err, substring), msgAndArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorType fails the test if err is nil, or err is not the expected type.
|
|
|
|
//
|
|
|
|
// Expected can be one of:
|
|
|
|
// a func(error) bool which returns true if the error is the expected type,
|
2018-06-08 12:09:51 -04:00
|
|
|
// an instance of (or a pointer to) a struct of the expected type,
|
2017-12-22 16:33:58 -05:00
|
|
|
// 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 ErrorType(t TestingT, err error, expected interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
|
|
|
assert(t, t.FailNow, argsAfterT, cmp.ErrorType(err, expected), msgAndArgs...)
|
2018-01-16 17:20:43 -05:00
|
|
|
}
|