Documentation
¶
Overview ¶
Package want is a package that provides equality functions for testing Go code. It is 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, and the type is a bool, int, or float, the output looks like this:
--- PASS: TestAbs (0.00s) test.go:2: want.Eq(t, Abs(-1), 1): got 0, want 1
If the check fails, and the type is a string, array, slice, or complex type, the output looks like this:
--- FAIL: TestAbs (0.00s) test.go:2: want.Eq(t, Abs(-1), 1): int( -: 0 +: 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
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Eq ¶
Eq compares got to want and reports an error to tb if they are not equal. Returns true if equal.
Example (Fail) ¶
want.Eq(t, Abs(-1), 0)
Output: want.Eq(t, Abs(-1), 0): got 1, want 0
Example (Pass) ¶
want.Eq(t, Abs(-1), 1)
Output: want.Eq(t, Abs(-1), 1): got 1
func False ¶
False checks if got is false and reports an error to tb if it is not false. Returns true if false.
func Nil ¶
Nil checks if got is nil and reports an error to tb if it is not nil. Returns true if nil.
Example (Fail) ¶
want.Nil(t, thing)
Output: want.Nil(t, thing): got 0, want <nil>
Example (Pass) ¶
want.Nil(t, nil)
Output: want.Nil(t, nil): got <nil>
func NotEq ¶
NotEq compares got to want and reports an error to tb if they are equal. Returns true if not equal.
Example (Fail) ¶
want.NotEq(t, Abs(-1), 1)
Output: want.NotEq(t, Abs(-1), 1): got 1, want not 1
Example (Pass) ¶
want.NotEq(t, Abs(-1), -1)
Output: want.NotEq(t, Abs(-1), -1): got 1, not -1
Types ¶
This section is empty.