assert

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: MIT Imports: 14 Imported by: 0

README

assert-go

Go Report Card GoDoc go.dev

Package assert simplifies writing test assertions.

Output will contain a helpful diff rendered using as well as the source code of the expression being tested. For example, if you call assert.Equal(t, car.Name, "Porsche"), the error message will include "car.Name".

Additional options and custom comparators can be registered using RegisterOptions, or passed in as the last parameter to the function call. For example, to indicate that unexported fields should be ignored on MyType, you can use:

 assert.RegisterOptions(
     cmpopts.IgnoreUnexported(MyType{}),
 )

See the go-cmp docs for more options.

Usage

func Test(t *testing.T) {
    message := "foo"
    assert.Equal(t, message, "bar")
    // message (-got +want): {string}:
    //          -: "foo"
    //          +: "bar"

    p := Person{Name: "Alice"}
    assert.Equal(t, p, Person{Name: "Bob"})
    // p (-got +want): {domain_test.Person}.Name:
    //          -: "Alice"
    //          +: "Bob"
}

Documentation

Overview

Package assert simplifies writing test assertions.

Output will contain a helpful diff rendered using as well as the source code of the expression being tested. For example, if you call assert.Equal(t, car.Name, "Porsche"), the error message will include "car.Name".

Additional options and custom comparators can be registered using RegisterOptions, or passed in as the last parameter to the function call. For example, to indicate that unexported fields should be ignored on MyType, you can use:

assert.RegisterOptions(
    cmpopts.IgnoreUnexported(MyType{}),
)

See the go-cmp docs for more options: https://godoc.org/github.com/google/go-cmp/cmp.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(t testingT, got, want string) bool

Contains asserts that got contains want.

func Empty

func Empty(t testingT, got interface{}) bool

Empty asserts that got is empty.

func Equal

func Equal(t testingT, got, want interface{}, opts ...cmp.Option) bool

Equal asserts that got and want are assertEqual.

func ErrorContains

func ErrorContains(t testingT, got error, want string) bool

ErrorContains asserts that the error message contains the wanted string.

func False

func False(t testingT, got bool) bool

False asserts that got is false.

func Ignore

func Ignore(paths ...string) cmp.Option

Ignore configures assert to ignore the specified field paths when testing equality. Nested paths may be expressed with periods (e.g. "User.ID").

func JSONEqual

func JSONEqual(t testingT, got, want interface{}, opts ...cmp.Option) bool

JSONEqual asserts that got and want are equal when represented as JSON. If either are already strings, they will be considered raw JSON. Otherwise, they will be marshaled to JSON before comparison.

func JSONLookup

func JSONLookup(t testingT, subject interface{}, path string) interface{}

JSONLookup fetches a value from a JSON object using the path expression.

func JSONPath

func JSONPath(t testingT, subject interface{}, path string, want interface{}, opts ...cmp.Option) bool

JSONPath asserts that evaluating the path expression against the subject results in want. The subject and want parameters are both converted to their JSON representation before being evaluated.

func Match

func Match(t testingT, got, want string) bool

Match asserts that got matches the regex want.

func Must

func Must(t testingT, err error)

Must asserts that err is nil, calling t.Fatal otherwise.

func Nil

func Nil(t testingT, got interface{}) bool

Nil asserts that got is nil.

func NotEmpty

func NotEmpty(t testingT, got interface{}) bool

NotEmpty asserts that got is not empty.

func NotEqual

func NotEqual(t testingT, got, want interface{}, opts ...cmp.Option) bool

NotEqual asserts that got and want are not equal.

func NotNil

func NotNil(t testingT, got interface{}) bool

NotNil asserts that got is not nil.

func RegisterOptions

func RegisterOptions(opts ...cmp.Option)

RegisterOptions registers a default option for all tests in the current package. It's intended to be used in an init function, like:

func init() {
    assert.RegisterOptions(
        cmp.Comparer(func(x, y *Thing) bool {
            return x.ID == y.ID
        }),
    )
}

Note that due to how "go test" operates, these options will not leak between packages.

func True

func True(t testingT, got bool) bool

True asserts that got is true.

Types

This section is empty.

Jump to

Keyboard shortcuts

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