Documentation
¶
Overview ¶
Package assert provides helper functions for unittests, in a style of hamcrest, gtest and gmock. It is a thin wrapper around the "h" package ( https://godoc.org/github.com/grailbio/testutil/h).
Features:
- Succinctly check if a value is ==, <, <=, another value.
- Hamcrest-style assertions.
Example:
package foo_test import ( "testing" "github.com/grailbio/testutil/assert" "github.com/grailbio/testutil/h" "github.com/grailbio/foo" ) func TestFoo(t* testing.T) { assert.EQ(t, foo.DoFoo(), 10) assert.LT(t, foo.DoBar(), 15) assert.EQ(t, foo.DoBar(), []int{1, 2}) }
The only difference between packages expect and assert is that expect.XXX will call testing.T.Error on error, whereas assert.XXX will call testing.T.Fatal on error. So you should use expect if the test code should continue after a failure, but assert if the testcase should abort immediately on failure. They can be mixed within one test.
Matchers:
Most of the actual matching features are implemented in the github.com/grailbio/testutil/h package. Package assert is just a thin wrapper around the matchers defined in h.
assert.That supports fine-grain expectation matching, in the style of gmock(https://github.com/google/googletest/blob/master/googlemock/docs/CheatSheet.md) and hamcrest(https://en.wikipedia.org/wiki/Hamcrest). For example:
// Check that every element in the slice is < 10. assert.That(t, []int{15, 16}, h.Each(h.LT(10)))
Package testutil/h defines many common matchers.
- h.LT, h.LE, h.GT, h.GE for arithmetic comparisons.
- h.EQ and h.NEQ to compare equality of two values. They can be used not just for scalar values, but for structs, slices, and maps too.
- h.Contains checks if a slice or an array contains a given value or matcher.
assert.That(t, []int{15, 16}, h.Contains(15)) assert.That(t, []int{15, 16}, h.Contains(h.LE(15)))
- h.Each checks if every value of a slice or an array matches a given value or matcher.
- h.Not(m) negates the match result of m.
// Check that every element in the slice doesn't start with "b" assert.That(t, []int{"abc", "abd"}, h.Each(h.Not(h.HasPrefix("b"))))
- h.Regexp, h.HasPrefix, h.HasSubstr, h.HasSuffix checks properties of a string.
Index ¶
- func EQ(t TB, got, want interface{}, msgs ...interface{})
- func False(t TB, got bool, msgs ...interface{})
- func GE(t TB, x, y interface{}, msgs ...interface{})
- func GT(t TB, x, y interface{}, msgs ...interface{})
- func HasPrefix(t TB, got interface{}, prefix string, msgs ...interface{})
- func HasSubstr(t TB, got interface{}, sub string, msgs ...interface{})
- func LE(t TB, x, y interface{}, msgs ...interface{})
- func LT(t TB, x, y interface{}, msgs ...interface{})
- func NEQ(t TB, got, want interface{}, msgs ...interface{})
- func Nil(t TB, x interface{}, msgs ...interface{})
- func NoError(t TB, got error, msgs ...interface{})
- func NotNil(t TB, x interface{}, msgs ...interface{})
- func Regexp(t TB, got interface{}, re interface{}, msgs ...interface{})
- func That(t TB, val interface{}, m *h.Matcher, msgs ...interface{})
- func True(t TB, got bool, msgs ...interface{})
- type TB
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EQ ¶
func EQ(t TB, got, want interface{}, msgs ...interface{})
EQ checks if the two values are equal. It is a shorthand for That(got, h.EQ(want), ...).
If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.EQ(t, 42, 42) assert.EQ(t, uint(42), uint(42)) assert.EQ(t, uint8(42), uint8(42)) assert.EQ(t, uint16(42), uint16(42)) assert.EQ(t, uint32(42), uint32(42)) assert.EQ(t, uint64(42), uint64(42)) assert.EQ(t, int8(42), int8(42)) assert.EQ(t, int16(42), int16(42)) assert.EQ(t, int32(42), int32(42)) assert.EQ(t, int64(42), int64(42)) assert.EQ(t, "42", "42") assert.EQ(t, 42.0, 42.0) assert.EQ(t, []int{42, 43}, []int{42, 43}) assert.EQ(t, [...]int{42, 43}, [...]int{42, 43}) assert.EQ(t, map[int]int{1: 2, 3: 4}, map[int]int{3: 4, 1: 2}) }
Example (InterfaceAndPointers) ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} type tt struct { x int y string } var xi, yi interface{} xi = tt{10, "x"} yi = tt{10, "x"} assert.EQ(t, xi, yi) xp := &tt{10, "x"} yp := &tt{10, "x"} assert.EQ(t, xp, yp) }
Example (Structs) ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} type tt struct { x int y string } assert.EQ(t, tt{10, "x"}, tt{10, "x"}) assert.EQ(t, tt{x: 10, y: "x"}, tt{y: "x", x: 10}) }
func False ¶
False checks if got==false. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.False(t, false) assert.False(t, 1 == 2) }
func GE ¶
func GE(t TB, x, y interface{}, msgs ...interface{})
GE checks if x >= y. x and y must be of the same type and operator '>=' must be defined for the type in Go. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.GE(t, 42, 42) assert.GE(t, 43, 42) assert.GE(t, "43", "42") assert.GE(t, "43", "123") }
func GT ¶
func GT(t TB, x, y interface{}, msgs ...interface{})
GT checks if x > y. x and y must be of the same type and operator '>' must be defined for the type in Go. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.GT(t, 43, 42) assert.GT(t, "43", "42") assert.GT(t, "43", "123") }
func HasPrefix ¶
HasPrefix checks if the value "got" starts with "prefix". If "got" is not a string, it is converted to string using fmt.Sprintf("%v"). If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.HasPrefix(t, "12345", "123") assert.HasPrefix(t, 12345, "123") }
func HasSubstr ¶
HasSubstr checks if the value "got" contains "sub". If "got" is not a string, it is converted to string using fmt.Sprintf("%v"). If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.HasSubstr(t, "12345", "234") assert.HasSubstr(t, 12345, "234") }
func LE ¶
func LE(t TB, x, y interface{}, msgs ...interface{})
LE checks if x <= y. x and y must be of the same type and operator '<=' must be defined for the type in Go. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.LE(t, 42, 42) assert.LE(t, 42, 43) assert.LE(t, "42", "43") assert.LE(t, "123", "43") }
func LT ¶
func LT(t TB, x, y interface{}, msgs ...interface{})
LT checks if x < y. x and y must be of the same type and operator '<' must be defined for the type in Go. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.LT(t, 42, 43) assert.LT(t, "42", "43") assert.LT(t, "123", "43") }
func NEQ ¶
func NEQ(t TB, got, want interface{}, msgs ...interface{})
NEQ checks if want != got. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "math" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.NEQ(t, 42, 43) assert.NEQ(t, "42", "43") assert.NEQ(t, 42.0, 43.0) assert.NEQ(t, []int{42, 43}, []int{43, 42}) assert.NEQ(t, [...]int{42, 43}, [...]int{43, 42}) nan := math.NaN() assert.NEQ(t, nan, nan) assert.NEQ(t, nan, 0.0) }
func Nil ¶
func Nil(t TB, x interface{}, msgs ...interface{})
Nil checks if the value is nil. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.Nil(t, nil) var m map[int]int assert.Nil(t, m) }
func NoError ¶
NoError is an alias of Nil. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.NoError(t, nil) var e error assert.NoError(t, e) }
func NotNil ¶
func NotNil(t TB, x interface{}, msgs ...interface{})
NotNil checks if the value is not nil. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.NotNil(t, false) assert.NotNil(t, 1) assert.NotNil(t, map[int]int{}) }
func Regexp ¶
func Regexp(t TB, got interface{}, re interface{}, msgs ...interface{})
Regexp checks if the value "got" matches a regexp. "re" can be either a string or an object of type *regexp.Regexp. If "got" is not a string, it is converted to string using fmt.Sprintf("%v"). The value is matched using regexp.Find. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
func That ¶
That checks if the gives value matches the matcher. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
func True ¶
True checks if got==true. If msgs... is not empty, msgs[0] must be a format string, and they are printed using fmt.Printf on error.
Example ¶
package main import ( "fmt" "github.com/grailbio/testutil/assert" ) type T struct{} func (t *T) Error(args ...interface{}) { fmt.Println(args...) } func (t *T) Fatal(args ...interface{}) { fmt.Println(args...) panic("fatal") } func main() { t := &T{} assert.True(t, true) assert.True(t, 1 == 1) }