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.
ExpectFailure(), ExpectSuccess() and ExpectImplements() all return a boolean to indicate whether the test has passed. This allows the user to control larger test procedures that have several stages
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, v T, expectedValue T)
- func DemandFailure(t *testing.T, v any)
- func DemandImplements[T comparable](t *testing.T, instance any, implements T) bool
- func DemandSuccess(t *testing.T, v any)
- func ExpectApproximate[T Approximte](t *testing.T, value T, expectedValue T, tolerance float64) bool
- func ExpectEquality[T comparable](t *testing.T, value T, expectedValue T) bool
- func ExpectFailure(t *testing.T, v any) bool
- func ExpectInequality[T comparable](t *testing.T, value T, expectedValue T) bool
- func ExpectSuccess(t *testing.T, v any) bool
- type Approximte
- 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, v 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 DemandFailure ¶ added in v0.35.0
DemandFailure is used to test for a value which indicates an 'unsuccessful' value for the type. See ExpectFailure() for more information on failure values
func DemandImplements ¶ added in v0.35.0
func DemandImplements[T comparable](t *testing.T, instance any, implements T) bool
DemandImplements tests whether an instance is an implementation of type T
func DemandSuccess ¶ added in v0.35.0
DemandSucess is used to test for a value which indicates an 'successful' value for the type. See ExpectSucess() for more information on success values
func ExpectApproximate ¶ added in v0.35.2
func ExpectApproximate[T Approximte](t *testing.T, value T, expectedValue T, tolerance float64) bool
ExpectApproximate is used to test approximate equality between one value and another.
Tolerance represents a percentage. For example, 0.5 is tolerance of +/- 50%. If the tolerance value is negative then the positive equivalent is used.
func ExpectEquality ¶ added in v0.25.0
func ExpectEquality[T comparable](t *testing.T, value T, expectedValue T) bool
ExpectEquality is used to test equality between one value and another
func ExpectFailure ¶ added in v0.25.0
ExpectFailure tests for an 'unsucessful value for the value's type.
Types bool and error are treated thus:
bool == false error != nil
If type is nil then the test will fail
func ExpectInequality ¶ added in v0.25.0
func ExpectInequality[T comparable](t *testing.T, value T, expectedValue T) bool
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
Types ¶
type Approximte ¶ added in v0.35.2
Approximate constraint used by ExpectApproximate() function
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.