Documentation ¶
Overview ¶
Package test contains helper functions to remove common boilerplate to make testing easier.
The ExpectEquality() is the most basic and probably the most useful function. It compares like-typed variables for equality and returns true if they match. ExpectInequality() is the inverse function.
The ExpectFailure() and ExpectSuccess() functions test for failure and success. These two functions work with bool or error and special handling for nil.
ExpectImplements() tests whether an instance implements the specified type.
The Writer type meanwhile, implements the io.Writer interface and should be used to capture output. The Writer.Compare() function can then be used to test for equality.
Index ¶
- func DemandEquality[T comparable](t *testing.T, value T, expectedValue T)
- func ExpectEquality[T comparable](t *testing.T, value T, expectedValue T)
- func ExpectFailure(t *testing.T, v interface{}) bool
- func ExpectImplements[T comparable](t *testing.T, instance any, implements T)
- func ExpectInequality[T comparable](t *testing.T, value T, expectedValue T)
- func ExpectSuccess(t *testing.T, v interface{}) bool
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DemandEquality ¶ added in v0.33.0
func DemandEquality[T comparable](t *testing.T, value T, expectedValue T)
DemandEquility is used to test equality between one value and another. If the test fails it is a testing fatility
This is particular useful if the values being tested are used in further tests and so must be correct. For example, testing that the lengths of two slices are equal before iterating over them in unison
func ExpectEquality ¶ added in v0.25.0
func ExpectEquality[T comparable](t *testing.T, value T, expectedValue T)
ExpectEquality is used to test equality between one value and another
func ExpectFailure ¶ added in v0.25.0
ExpectFailure tests argument v for a failure condition suitable for it's type. Types bool and error are treated thus:
bool == false error != nil
If type is nil then the test will fail
func ExpectImplements ¶ added in v0.30.0
func ExpectImplements[T comparable](t *testing.T, instance any, implements T)
ExpectImplements tests whether an instance is an implementation of type T
func ExpectInequality ¶ added in v0.25.0
func ExpectInequality[T comparable](t *testing.T, value T, expectedValue T)
ExpectInequality is used to test inequality between one value and another. In other words, the test does not want to succeed if the values are equal
func ExpectSuccess ¶ added in v0.25.0
ExpectSuccess tests argument v for a success condition suitable for it's type. Types bool and error are treated thus:
bool == true error == nil
If type is nil then the test will succeed
Types ¶
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is an implementation of the io.Writer interface. It should be used to capture output and to compare with predefined strings.