Documentation ¶
Overview ¶
Package must provides helper functions for validating output in unit tests. The output "must" be what we expect.
Must does not provide assertions, but follows a similar syntax that you might be used to from unit testing in other languages. You provide a testing.T, objects to be tested and an error message, and the must functions will raise a testing error if the expectations on the objects are not met. This error will contain additional context (such as an object diff) to help you identify the nature of the test failure.
For example:
result := must.BeEqual(t, expected, got, "expectation not met")
Will trigger an error in t if got and expected are not the same. The message "expectation not met" will be included in the error along with a diff of expected and got.
Index ¶
- func BeEqual(t TestingT, expected, got interface{}, a ...interface{}) bool
- func BeEqualErrors(t TestingT, expected, got error, a ...interface{}) bool
- func BeError(t TestingT, got error, a ...interface{}) bool
- func BeErrorIf(t TestingT, errorExpected bool, got error, a ...interface{}) bool
- func BeNoError(t TestingT, got error, a ...interface{}) bool
- func BeSameLength(t TestingT, expected, got interface{}, a ...interface{}) bool
- type MustTester
- type Tester
- func (tester Tester) BeEqual(expected, got interface{}, a ...interface{}) bool
- func (tester Tester) BeEqualErrors(expected, got error, a ...interface{}) bool
- func (tester Tester) BeError(got error, a ...interface{}) bool
- func (tester Tester) BeErrorIf(errorExpected bool, got error, a ...interface{}) bool
- func (tester Tester) BeNoError(got error, a ...interface{}) bool
- func (tester Tester) BeSameLength(expected, got interface{}, a ...interface{}) bool
- type TestingT
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BeEqual ¶
BeEqual compares the expected and got interfaces, triggering an error on t if they are not equal. This error will include a diff of the two objects.
The return value will be true if the interfaces are equal.
Additional output for any error message can be provided as additional parameters, as with fmt.Print.
func BeEqualErrors ¶
BeEqualErrors compares two errors to determine if they are considered equal. The errors expected and got are considered equal if they are both nil, or both are non-nil and their error messsages (from their Error() functions) match.
This ignores the actual type of these errors, so two errors created with different struct types, but the same message will still be equal.
Should the errors not be considered equal, an error will be raised in t including both messages and false will be returned.
Additional output for any error message can be provided as additional parameters, as with fmt.Print.
func BeError ¶
BeError checks that the provided error is not nil
The return value will be true iff error is not nil ¶
Additional output for any error message can be provided as additional parameters, as with fmt.Print.
func BeErrorIf ¶
BeErrorIf checks that the errorExpected flag corresponds to the provided error: error should be nil iff errorExpected flag is false
The return value will be true iff error value corresponds to the errorExpected flag ¶
Additional output for any error message can be provided as additional parameters, as with fmt.Print.
func BeNoError ¶
BeNoError checks whether or not the got value is an error.
The return value will be true if got is nil.
Additional output for any error message can be provided as additional parameters, as with fmt.Print.
func BeSameLength ¶
BeSameLength checks whether the two inputs have the same length according to the len function.
The return value will be true if their lengths match.
Additional output for any error message can be provided as additional parameters, as with fmt.Print.
Types ¶
type MustTester ¶
type MustTester interface { BeEqual(expected, got interface{}, a ...interface{}) bool BeEqualErrors(expected, got error, a ...interface{}) bool BeNoError(got error, a ...interface{}) bool BeError(got error, a ...interface{}) bool BeErrorIf(errorExpected bool, got error, a ...interface{}) bool BeSameLength(expected, got interface{}, a ...interface{}) bool }
MustTester defines an interface with functions matching the package level check functions, without the requirement to specify a TestingT.
type Tester ¶
type Tester struct { T TestingT // *testing.T or equivalent InterfaceComparison func(expected, got interface{}) bool // Optional custom interface comparison function InterfaceDiff func(expected, got interface{}) string // Optional custom interace diff function }
Tester implements MustTester and provides a TestingT to be used for all check functions.
func (Tester) BeEqual ¶
BeEqual compares the expected and got interfaces, triggering an error on the Tester's T if they are not equal.
This corresponds to the function BeEqual
func (Tester) BeEqualErrors ¶
BeEqualErrors compares the expected and got errors, triggering an error on the Tester's T if they are not equal.
This corresponds to the function BeEqualErrors
func (Tester) BeErrorIf ¶
BeErrorIf checks that the received error corresponds to the errorExpected flag
func (Tester) BeNoError ¶
BeNoError checks whether got is set, triggering an error on the Tester's T if it is non-nil.
This corresponds to the function BeNoError
func (Tester) BeSameLength ¶
BeSameLength checks whether the two inputs have the same length according to the len function.
This corresponds to the function BeSameLength