2018-02-28 10:11:02 -05:00
|
|
|
/*Package assert provides assertions for comparing expected values to actual
|
|
|
|
values. When an assertion fails a helpful error message is printed.
|
|
|
|
|
|
|
|
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:23:38 -04:00
|
|
|
Example usage
|
2018-02-28 10:11:02 -05:00
|
|
|
|
|
|
|
The example below shows assert used with some common types.
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-06-08 12:23:38 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
2018-02-28 10:11:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEverything(t *testing.T) {
|
|
|
|
// booleans
|
|
|
|
assert.Assert(t, ok)
|
|
|
|
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-21 15:25:54 -05:00
|
|
|
assert.Error(t, err, "the exact error message")
|
|
|
|
assert.ErrorContains(t, err, "includes this")
|
|
|
|
assert.ErrorType(t, err, os.IsNotExist)
|
2018-02-28 10:11:02 -05:00
|
|
|
|
|
|
|
// complex types
|
|
|
|
assert.DeepEqual(t, result, myStruct{Name: "title"})
|
|
|
|
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
|
|
|
|
|
2022-03-01 09:50:32 -05:00
|
|
|
Package http://pkg.go.dev/gotest.tools/v3/assert/cmp provides
|
2018-02-28 10:11:02 -05:00
|
|
|
many common comparisons. Additional comparisons can be written to compare
|
|
|
|
values in other ways. See the example Assert (CustomComparison).
|
|
|
|
|
2018-06-08 12:23:38 -04:00
|
|
|
Automated migration from testify
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
gty-migrate-from-testify is a command which translates Go source code from
|
|
|
|
testify assertions to the assertions provided by this package.
|
2018-06-08 12:23:38 -04:00
|
|
|
|
2022-03-01 09:50:32 -05:00
|
|
|
See http://pkg.go.dev/gotest.tools/v3/assert/cmd/gty-migrate-from-testify.
|
2018-06-08 12:23:38 -04:00
|
|
|
|
|
|
|
|
2018-02-28 10:11:02 -05:00
|
|
|
*/
|
2020-02-22 12:12:14 -05:00
|
|
|
package assert // import "gotest.tools/v3/assert"
|
2018-02-28 10:11:02 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
gocmp "github.com/google/go-cmp/cmp"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert/cmp"
|
2022-03-01 09:50:32 -05:00
|
|
|
"gotest.tools/v3/internal/assert"
|
2018-02-28 10:11:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// BoolOrComparison can be a bool, or cmp.Comparison. See Assert() for usage.
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
// Assert performs a comparison. If the comparison fails, the test is marked as
|
2018-02-28 10:11:02 -05:00
|
|
|
// failed, a failure message is logged, and execution is stopped immediately.
|
|
|
|
//
|
2020-02-22 12:12:14 -05:00
|
|
|
// 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.
|
2022-03-01 09:50:32 -05:00
|
|
|
// http://pkg.go.dev/gotest.tools/v3/assert/cmp provides many common comparisons.
|
2020-02-22 12:12:14 -05:00
|
|
|
// error
|
|
|
|
// A nil value is considered success.
|
|
|
|
// A non-nil error is a failure, err.Error() is used as the failure message.
|
2018-02-28 10:11:02 -05:00
|
|
|
func Assert(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsFromComparisonCall, comparison, msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-02-28 10:11:02 -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.
|
|
|
|
func Check(t TestingT, comparison BoolOrComparison, msgAndArgs ...interface{}) bool {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsFromComparisonCall, comparison, msgAndArgs...) {
|
|
|
|
t.Fail()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NilError fails the test immediately if err is not nil.
|
|
|
|
// This is equivalent to Assert(t, err)
|
|
|
|
func NilError(t TestingT, err error, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, err, msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Equal uses the == operator to assert two values are equal and fails the test
|
2018-06-08 12:23:38 -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-02-28 10:11:02 -05:00
|
|
|
func Equal(t TestingT, x, y interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.Equal(x, y), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
// 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.
|
2018-06-08 12:23:38 -04:00
|
|
|
//
|
2022-03-01 09:50:32 -05:00
|
|
|
// Package http://pkg.go.dev/gotest.tools/v3/assert/opt provides some additional
|
2018-06-08 12:23:38 -04:00
|
|
|
// commonly used Options.
|
|
|
|
//
|
2018-02-28 10:11:02 -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()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.DeepEqual(x, y, opts...)) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-21 15:25:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.Error(err, message), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-21 15:25:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.ErrorContains(err, substring), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2017-12-21 15:25:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorType fails the test if err is nil, or err is not the expected type.
|
2020-02-22 12:12:14 -05:00
|
|
|
// Equivalent to Assert(t, cmp.ErrorType(err, expected)).
|
2017-12-21 15:25:54 -05:00
|
|
|
//
|
|
|
|
// Expected can be one of:
|
2020-02-22 12:12:14 -05:00
|
|
|
// 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
|
2017-12-21 15:25:54 -05:00
|
|
|
func ErrorType(t TestingT, err error, expected interface{}, msgAndArgs ...interface{}) {
|
|
|
|
if ht, ok := t.(helperT); ok {
|
|
|
|
ht.Helper()
|
|
|
|
}
|
2022-03-01 09:50:32 -05:00
|
|
|
if !assert.Eval(t, assert.ArgsAfterT, cmp.ErrorType(err, expected), msgAndArgs...) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2018-02-28 10:11:02 -05:00
|
|
|
}
|