Documentation ¶
Overview ¶
Package assert, for writing checks in unit tests
This package provides functions to reduce the amount of code needed to write simple assertions. It implements the best practice pattern where the output of a failure explains what the check "got" and what it wanted. The assert functions are defined such that writing requires less code but still are easy to understand. It works by decorating the standard testing.T type in your test and report (Fatal) the offending asserting call if a check fails.
Example
import ( "testing.T" "github.com/emicklei/assert" ) func TestShoeFits(t *testing.T) { shoeSize := 42 assert.That(t,"shoeSize",shoeSize).Equals(46) }
which will report
got [42] (int) for "shoeSize" but want [46] (int)
Examples: (using the dot import)
Assert(t,"err",err).IsNil() Assert(t,"isOffline",isOffline).IsTrue() Assert(t,"country",country).Equals("NL") Assert(t,"job",job).IsKindOf(new(Job)) Assert(t,"names", []string{}).Len(0) // you can negate a check Assert(t,"isOnline",isOnline).Not().IsTrue()
You can create and use your own checks by implementing the RelationalOperator.
type caseInsensitiveStringEquals struct{} func (c caseInsensitiveStringEquals) Apply(left, right interface{}) bool { s_left, ok := left.(string) if !ok { return false } s_right, ok := right.(string) if !ok { return false } return strings.EqualFold(s_left, s_right) } func TestCompareUsing(t *testing.T) { Assert(t, "insensitive", "ABC").With(caseInsensitiveStringEquals{}).Equals("abc") }
Index ¶
- Variables
- func Scolorf(syntaxCode string, format string, args ...interface{}) string
- type Operand
- func (o Operand) After(moment time.Time)
- func (o Operand) Before(moment time.Time)
- func (o Operand) Equals(want interface{})
- func (o Operand) GreaterThan(want interface{})
- func (o Operand) IsEmpty()
- func (o Operand) IsFalse()
- func (o Operand) IsKindOf(v interface{})
- func (o Operand) IsNil()
- func (o Operand) IsNotNil()
- func (o Operand) IsTrue()
- func (o Operand) Len(want int)
- func (o Operand) Not() Operand
- func (o Operand) With(r RelationalOperator) Operand
- type RelationalOperator
Constants ¶
This section is empty.
Variables ¶
var AssertColorsEnabled = true
AssertColorsEnabled can be changed to disable the use of terminal coloring. One usecase is to add a command line flag to your test that controls its value.
func init() { flag.BoolVar(&assert.AssertColorsEnabled, "color", true, "want colors?") } go test -color=false
var AssertFatalColorSyntaxCode = "@{wB}"
AssertFatalColorSyntaxCode requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go . Set to an empty string to disable coloring.
var AssertSuccessColorSyntaxCode = "@{y}"
AssertSuccessColorSyntaxCode requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go . Set to an empty string to disable coloring.
var Fatalf = func(t testingT, format string, args ...interface{}) { t.Helper() t.Fatalf("%s", Scolorf(AssertFatalColorSyntaxCode, format, args...)) }
Fatalf calls Fatalf on a test instance t. You can inject your own implementation of assert.testingT
var Log = func(t testingT, args ...interface{}) {
t.Helper()
t.Log(args...)
}
Log calls Log on a test instance t. You can inject your own implementation of assert.testingT
var Logf = func(t testingT, format string, args ...interface{}) { t.Helper() t.Logf("%s", Scolorf(AssertSuccessColorSyntaxCode, format, args...)) }
Logf calls Log on a test instance t. You can inject your own implementation of assert.testingT
Functions ¶
Types ¶
type Operand ¶
type Operand struct {
// contains filtered or unexported fields
}
Operand represent a value
func That ¶
That creates an Operand on the value we have got and describes the variable that is being testing.
func (Operand) Equals ¶
func (o Operand) Equals(want interface{})
Equals checks whether the value we have got is equal to the value we want.
func (Operand) GreaterThan ¶
func (o Operand) GreaterThan(want interface{})
GreaterThan checks whether the value we have got is greater than to the value we want.
func (Operand) IsEmpty ¶
func (o Operand) IsEmpty()
IsEmpty checks that len(value) or value.Len() is zero. It operates on Array, Chan, Map, Slice, or String and objects that implement Len() int.
func (Operand) IsKindOf ¶
func (o Operand) IsKindOf(v interface{})
IsKindOf checks whether the values are of the same type
func (Operand) Len ¶
Len checks that len(value) or value.Len() is equals to the given length. It operates on Array, Chan, Map, Slice, or String and objects that implement Len() int.
func (Operand) With ¶
func (o Operand) With(r RelationalOperator) Operand
With returns a copy Operand that will use the RelationalOperator.
type RelationalOperator ¶
type RelationalOperator interface { // Return the result of applying the two values left and right Apply(left, right interface{}) bool }
RelationalOperator specifies the function that can operate on two values