want: 4d63.com/want Index | Examples | Files

package want

import "4d63.com/want"

Package want is a package that provides equality functions for testing Go code. It contains only this one function and is intended to be succinct and focused on making the most common action in test code simple, testing equality expectations.

A simple test function looks like this:

func TestAbs(t *testing.T) {
    want.Eq(t, Abs(-1), 1)
}

If the check passes, the verbose output looks like this:

--- PASS: TestAbs (0.00s)
    test.go:2: want.Eq(t, Abs(-1), 1): got 1

If the check fails, the output looks like this:

--- FAIL: TestAbs (0.00s)
    test.go:2: want.Eq(t, Abs(-1), 1): got 0, want 1

Got and want

The terms got and want are used to describe what you got as a result of running the code, and what you want to have gotten. These terms are commonly found in the Go stdlib and it's own testing docs. In some other testing libraries they are sometimes referred to actual and expected.

Nesting

Checks can be nested using the bool return value of a prior check.

func TestAbs(t *testing.T) {
    if want.Eq(t, Abs(-1), 1) {
        ...
    }
}

Breaking early

Checks can cause a test to stop at a failure using the bool return value.

func TestAbs(t *testing.T) {
    if !want.Eq(t, Abs(-1), 1) {
        return
    }
    ...
}

Comparison

Comparison of got and want is done using Google's cmp Go module: https://github.com/google/go-cmp/cmp

Diffs

Diffs can be enabled in error output of Eq comparisons. To enable diffs instantiate Want with DiffEnabled true and use its functions instead of the package functions.

If the check fails, the output looks like this:

--- FAIL: TestAbs (0.00s)
    test.go:2: want.Eq(t, Abs(-1), 1): {int}:
           -: 0
           +: 1

Index

Examples

Package Files

want.go

func Eq Uses

func Eq(tb testing.TB, got, want interface{}) bool

Eq compares got to want and reports an error to tb if they are not equal. Returns true if equal.

Code:

want.Eq(t, Abs(-1), 1)

func False Uses

func False(tb testing.TB, got bool) bool

False checks if got is false and reports an error to tb if it is not false. Returns true if false.

func Nil Uses

func Nil(tb testing.TB, got interface{}) bool

Nil checks if got is nil and reports an error to tb if it is not nil. Returns true if nil.

Code:

want.Nil(t, thing)

func NotEq Uses

func NotEq(tb testing.TB, got, notWant interface{}) bool

NotEq compares got to want and reports an error to tb if they are equal. Returns true if not equal.

Code:

want.NotEq(t, Abs(-1), -1)

func NotNil Uses

func NotNil(tb testing.TB, got interface{}) bool

NotNil checks if got is not nil and reports an error to tb if it is nil. Returns true if not nil.

Code:

want.NotNil(t, thing)

func True Uses

func True(tb testing.TB, got bool) bool

True checks if got is true and reports an error to tb if it is not true. Returns true if true.

type Want Uses

type Want struct {
    // DiffEnabled when true enables comparison diffs in error reports.
    DiffEnabled bool
}

A Want is a set of options for configuring the behavior of the library. Its zero value (Want{}) is usable and is equivalent to invoking the package functions Eq and NotEq.

func (*Want) Eq Uses

func (w *Want) Eq(tb testing.TB, got, want interface{}) bool

Eq compares got to want and reports an error to tb if they are not equal. Returns true if equal.

func (*Want) False Uses

func (w *Want) False(tb testing.TB, got bool) bool

False checks if got is false and reports an error to tb if it is not false. Returns true if false.

func (*Want) Nil Uses

func (w *Want) Nil(tb testing.TB, got interface{}) bool

Nil checks if got is nil and reports an error to tb if it is not nil. Returns true if nil.

func (*Want) NotEq Uses

func (w *Want) NotEq(tb testing.TB, got, notWant interface{}) bool

NotEq compares got to want and reports an error to tb if they are equal. Returns true if not equal.

func (*Want) NotNil Uses

func (w *Want) NotNil(tb testing.TB, got interface{}) bool

NotNil checks if got is not nil and reports an error to tb if it is nil. Returns true if not nil.

func (*Want) True Uses

func (w *Want) True(tb testing.TB, got bool) bool

True checks if got is true and reports an error to tb if it is not true. Returns true if true.

Package want imports 5 packages (graph). Updated 2018-12-28. Refresh now. Tools for package owners.