assert

package
v0.0.0-...-3de4a33 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package assert provides convenience assert methods to complement the built in go testing library. It's intended to add onto standard Go tests. Example usage:

func TestSomething(t *testing.T) {
	i, err := doSomething()
	assert.NoErr(t, err)
	assert.Equal(t, i, 123, "returned integer")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t Tester, expected, actual interface{}, noun string)

Equal ensures that the actual value returned from a test was equal to an expected. it uses reflect.DeepEqual to do so. name is used to describe the values being compared. it's used in the error string if actual != expected.

func EqualValues

func EqualValues(t Tester, expected, actual interface{}, noun string) bool

EqualValues asserts that two objects are equal or convertable to the same types and equal.

assert.EqualValues(t, uint32(123), int32(123))

func Error

func Error(t Tester, expected error, actual error)

Err calls t.Fatalf if expected is not equal to actual. it uses reflect.DeepEqual to determine if the errors are equal

func ExistsError

func ExistsError(t Tester, err error, noun string)

ExistsErr calls t.Fatalf if err == nil. The message will explain that the error described by noun was nil when it shouldn't have been

func False

func False(t Tester, b bool, fmtStr string, vals ...interface{})

False is the equivalent of True(t, !b, fmtStr, vals...).

func Len

func Len(t Tester, object interface{}, length int, noun string) bool

Len asserts that the specified object has specific length. Len also fails if the object has a type that len() not accept.

assert.Len(t, mySlice, 3)

func Nil

func Nil(t Tester, i interface{}, noun string)

Nil uses reflect.DeepEqual(i, nil) to determine if i is nil. if it's not, Nil calls t.Fatalf explaining that the noun i is not nil when it should have been

func NoError

func NoError(t Tester, e error)

NoErr calls t.Fatalf if e is not nil.

func NotEmpty

func NotEmpty(t Tester, i interface{}, noun string) bool

NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.

if assert.NotEmpty(t, obj) {
  assert.Equal(t, "two", obj[1])
}

func NotEqual

func NotEqual(t Tester, expected, actual interface{}, noun string) bool

NotEqual asserts that the specified values are NOT equal.

assert.NotEqual(t, obj1, obj2)

Pointer variable equality is determined based on the equality of the referenced values (as opposed to the memory addresses).

func NotEqualValues

func NotEqualValues(t Tester, expected, actual interface{}, noun string) bool

NotEqualValues asserts that two objects are not equal even when converted to the same type

assert.NotEqualValues(t, obj1, obj2)

func NotNil

func NotNil(t Tester, i interface{}, noun string)

NotNil uses reflect.DeepEqual(i, nil) to determine if i is nil. if it is, NotNil calls t.Fatalf explaining that the noun i is nil when it shouldn't have been.

func NotRegexp

func NotRegexp(t Tester, rx interface{}, str interface{}, noun string) bool

NotRegexp asserts that a specified regexp does not match a string.

assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
assert.NotRegexp(t, "^start", "it's not starting")

func Regexp

func Regexp(t Tester, rx interface{}, str interface{}, noun string) bool

Regexp asserts that a specified regexp matches a string.

assert.Regexp(t, regexp.MustCompile("start"), "it's starting")
assert.Regexp(t, "start...$", "it's not starting")

func True

func True(t Tester, b bool, fmtStr string, vals ...interface{})

True fails the test if b is false. on failure, it calls t.Fatalf(fmtStr, vals...)

Types

type Equaler

type Equaler interface {
	Equal(Equaler) bool
}

Equaler determines if a type is equal to any other type that conforms to Equaler. All types passed to assert.Equal are checked to see if they conform to this interface, and if they do, their Equal function is called to check for their equality. This call is made instead of the call to reflect.DeepEqual

type Tester

type Tester interface {
	Fatalf(string, ...interface{})
}

Tester is a stub interface that *testing.T conforms to. It is used in all exported function calls in this assert library so that the library can be tested, or a caller can use a custom testing library. As said before, however, the most widely used implementation of this interface will be *testing.T. Example usage:

func TestSomething(t *testing.T) {
	assert.Equal(t, "something", "something", "something")
}

func WithFrameWrapper

func WithFrameWrapper(t Tester) Tester

WithFrameWrapper returns the original Tester, wrapped by a frameWrapper that adds context about how many frames to backtrack on the call stack when identifying the source of a failed assertion. If the Tester passed in is already a frameWrapper, the Tester wrapped by that frameWrapper is unwrapped and re-wrapped with updated context.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL