Documentation
¶
Index ¶
- Variables
- type AnyType
- func (a AnyType[T]) I(info any, other ...any) AnyType[T]
- func (a AnyType[T]) Info(info any, other ...any) AnyType[T]
- func (a AnyType[T]) Not() AnyType[T]
- func (a AnyType[T]) ToBe(t Tester, expected T)
- func (a AnyType[T]) ToBeNil(t Tester)
- func (a AnyType[T]) ToEqual(t Tester, expected any)
- func (a AnyType[T]) Using(opt ...gocmp.Option) AnyType[T]
- type BoolType
- func (a BoolType[B]) I(info any, other ...any) BoolType[B]
- func (a BoolType[B]) Info(info any, other ...any) BoolType[B]
- func (a BoolType[B]) Not() BoolType[B]
- func (a BoolType[B]) ToBe(t Tester, expected B)
- func (a BoolType[B]) ToBeFalse(t Tester)
- func (a BoolType[B]) ToBeTrue(t Tester)
- func (a BoolType[B]) ToEqual(t Tester, expected bool)
- type ErrorType
- type FuncType
- type MapType
- func (a MapType[K, V]) I(info any, other ...any) MapType[K, V]
- func (a MapType[K, V]) Info(info any, other ...any) MapType[K, V]
- func (a MapType[K, V]) Not() MapType[K, V]
- func (a MapType[K, V]) ToBe(t Tester, expected map[K]V)
- func (a MapType[K, V]) ToBeEmpty(t Tester)
- func (a MapType[K, V]) ToBeNil(t Tester)
- func (a MapType[K, V]) ToContain(t Tester, expectedKey K, expectedValue ...V)
- func (a MapType[K, V]) ToContainAll(t Tester, expectedKey ...K)
- func (a MapType[K, V]) ToContainAny(t Tester, expectedKey ...K)
- func (a MapType[K, V]) ToHaveLength(t Tester, expected int)
- func (a MapType[K, V]) ToHaveSize(t Tester, expected int)
- func (a MapType[K, V]) Using(opt ...gocmp.Option) MapType[K, V]
- type OrderedType
- func (a OrderedType[O]) I(info any, other ...any) OrderedType[O]
- func (a OrderedType[O]) Info(info any, other ...any) OrderedType[O]
- func (a OrderedType[O]) Not() OrderedType[O]
- func (a OrderedType[O]) ToBe(t Tester, expected O)
- func (a OrderedType[O]) ToBeGreaterThan(t Tester, threshold O)
- func (a OrderedType[O]) ToBeGreaterThanOrEqualTo(t Tester, threshold O)
- func (a OrderedType[O]) ToBeLessThan(t Tester, threshold O)
- func (a OrderedType[O]) ToBeLessThanOrEqualTo(t Tester, threshold O)
- type SliceType
- func (a SliceType[T]) I(info any, other ...any) SliceType[T]
- func (a SliceType[T]) Info(info any, other ...any) SliceType[T]
- func (a SliceType[T]) Not() SliceType[T]
- func (a SliceType[T]) ToBe(t Tester, expected ...T)
- func (a SliceType[T]) ToBeEmpty(t Tester)
- func (a SliceType[T]) ToBeNil(t Tester)
- func (a SliceType[T]) ToContainAll(t Tester, expected ...T)
- func (a SliceType[T]) ToContainAny(t Tester, expected ...T)
- func (a SliceType[T]) ToHaveLength(t Tester, expected int)
- func (a SliceType[T]) Using(opt ...gocmp.Option) SliceType[T]
- type StringType
- func (a StringType[S]) I(info any, other ...any) StringType[S]
- func (a StringType[S]) Info(info any, other ...any) StringType[S]
- func (a StringType[S]) Not() StringType[S]
- func (a StringType[S]) ToBe(t Tester, expected S)
- func (a StringType[S]) ToBeEmpty(t Tester)
- func (a StringType[S]) ToContain(t Tester, substring S)
- func (a StringType[S]) ToEqual(t Tester, expected string)
- func (a StringType[S]) ToHaveLength(t Tester, expected int)
- func (a StringType[S]) ToMatch(t Tester, pattern *regexp.Regexp)
- func (a StringType[S]) Trim(at int) StringType[S]
- type Stringy
- type Tester
Constants ¶
This section is empty.
Variables ¶
var ApproximateFloatFraction = 1e-4
ApproximateFloatFraction provides an option that compares any (a, b float32) or (a, b float64) pair. Change this if needed. See cmpopts.EquateApprox and DefaultOptions.
If more than one argument is passed, all subsequent arguments will be required to be nil/zero. This is convenient if you want to make an assertion on a method/function that returns a value and an error, a common pattern in Go.
var DefaultOptions = func() gocmp.Options { return gocmp.Options{cmpopts.EquateApprox(ApproximateFloatFraction, 0), cmpopts.EquateEmpty()} }
DefaultOptions returns options used by gocmp.Equal for comparing values. The default options
- sets the threshold for float comparison to ApproximateFloatFraction
- sets empty and nil maps or slices to be treated the same
You can also use AnyType.Using instead.
var JustLogIt = SimpleTester(log.Fatalf, log.Fatalf)
JustLogIt is a tester that calls log.Fatalf on all test errors and failures.
Functions ¶
This section is empty.
Types ¶
type AnyType ¶
type AnyType[T any] struct { // contains filtered or unexported fields }
AnyType is used for equality assertions for any type.
func Any ¶
Any creates an assertion for deep value comparison of any type. This is very flexible but only provides methods to determine whether a value is equal (or not equal) to what's expected (see AnyType.ToBe, AnyType.ToBeNil and AnyType.ToEqual).
For alternative comparisons, see the more-specialized String, Number, Bool, Slice, Map, Error and Func functions.
Any uses gocmp.Equal so the manner of comparison can be tweaked using that API - see also AnyType.Using
If the values have an Equal method of the form "(T) Equal(T) bool" or "(T) Equal(I) bool" where T is assignable to I, then it uses the result of x.Equal(y) even if x or y is nil.
Lastly, it tries to compare x and y based on their basic kinds. Simple kinds like booleans, integers, floats, complex numbers, strings, and channels are compared using the equivalent of the == operator in Go. Functions are only equal if they are both nil, otherwise they are unequal.
Structs are equal if recursively calling Equal on all fields report equal. All struct fields are compared and this is repeated recursively. Unless the compare options are changed, it does not matter whether fields exported on unexported.
Slices are equal if they are both nil or both non-nil, where recursively calling Equal on all non-ignored slice or array elements report equal. Unless the compare options are changed, empty non-nil slices and nil slices are equal.
Maps are equal if they are both nil or both non-nil, where recursively calling Equal on all non-ignored map entries report equal. Map keys are equal according to the == operator. To use custom comparisons for map keys, consider using github.com/google/go-cmp/cmp/cmpopts.SortMaps. Unless the compare options are changed, empty non-nil maps and nil maps are equal.
Pointers and interfaces are equal if they are both nil or both non-nil, where they have the same underlying concrete type and recursively calling Equal on the underlying values reports equal.
Before recursing into a pointer, slice element, or map, the current path is checked to detect whether the address has already been visited. If there is a cycle, then the pointed at values are considered equal only if both addresses were previously visited in the same path step.
func (AnyType[T]) Info ¶
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (AnyType[T]) ToBe ¶
ToBe asserts that the actual and expected data have the same values and types. The tester is normally *testing.T.
func (AnyType[T]) ToBeNil ¶ added in v0.7.0
ToBeNil asserts that the actual value is nil / is not nil. The tester is normally *testing.T.
func (AnyType[T]) ToEqual ¶
ToEqual asserts that the actual and expected data have the same values and similar types. The tester is normally *testing.T.
type BoolType ¶
type BoolType[B ~bool] struct { // contains filtered or unexported fields }
BoolType is used for assertions about bools.
func Bool ¶
Bool creates a boolean assertion.
If more than one argument is passed, all subsequent arguments will be required to be nil/zero. This is convenient if you want to make an assertion on a method/function that returns a value and an error, a common pattern in Go.
func (BoolType[B]) Info ¶
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (BoolType[B]) ToBe ¶
ToBe asserts that the actual and expected items have the same values and types. The tester is normally *testing.T.
func (BoolType[B]) ToBeFalse ¶
ToBeFalse asserts that the actual value is true. The tester is normally *testing.T.
func (BoolType[B]) ToBeTrue ¶
ToBeTrue asserts that the actual value is true. The tester is normally *testing.T.
type ErrorType ¶
type ErrorType struct {
// contains filtered or unexported fields
}
ErrorType is used for assertions about errors.
func Error ¶
Error creates an error assertion. This considers the last error it finds in the supplied parameters. All other parameters are ignored.
func (ErrorType) Info ¶
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (ErrorType) ToBeNil ¶
ToBeNil asserts that the error did not occur. The tester is normally *testing.T.
func (ErrorType) ToContain ¶
ToContain asserts that the error occurred and its message contains the substring. The tester is normally *testing.T.
func (ErrorType) ToHaveOccurred ¶
ToHaveOccurred asserts that the error occurred. The tester is normally *testing.T.
type FuncType ¶ added in v0.5.0
type FuncType struct {
// contains filtered or unexported fields
}
FuncType is used for assertions about functions.
func Func ¶ added in v0.5.0
func Func(value func()) FuncType
Func wraps a function that can test for panics etc.
func (FuncType) Info ¶ added in v0.5.0
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (FuncType) ToPanic ¶ added in v0.5.0
ToPanic asserts that the function did / did not panic. The tester is normally *testing.B.
func (FuncType) ToPanicWithMessage ¶ added in v0.5.0
ToPanicWithMessage asserts that the function did panic. It is not useful to use FuncType.Not with this. The substring is used to check that the panic passed a string containing that value. The tester is normally *testing.B.
type MapType ¶
type MapType[K comparable, V any] struct { // contains filtered or unexported fields }
MapType is used for assertions about maps.
func Map ¶
func Map[K comparable, V any](value map[K]V, other ...any) MapType[K, V]
Map creates an assertion for deep value comparison of maps of any type.
This uses gocmp.Equal so the manner of comparison can be tweaked using that API - see also MapType.Using
func (MapType[K, V]) Info ¶
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (MapType[K, V]) ToBe ¶
ToBe asserts that the actual and expected maps have the same values and types. The tester is normally *testing.T.
func (MapType[K, V]) ToBeEmpty ¶ added in v0.8.0
ToBeEmpty asserts that the map has zero length. The tester is normally *testing.T.
func (MapType[K, V]) ToBeNil ¶ added in v0.10.0
ToBeNil asserts that the actual value is nil / is not nil. The tester is normally *testing.T.
func (MapType[K, V]) ToContain ¶
ToContain asserts that the map contains a particular key. If present, the expected value must also match. The tester is normally *testing.T.
func (MapType[K, V]) ToContainAll ¶ added in v0.11.0
ToContainAll asserts that the map contains all the expected keys. The tester is normally *testing.T.
func (MapType[K, V]) ToContainAny ¶ added in v0.11.0
ToContainAny asserts that the map contains any the expected keys. The tester is normally *testing.T.
func (MapType[K, V]) ToHaveLength ¶
ToHaveLength asserts that the map has the expected length. The tester is normally *testing.T.
func (MapType[K, V]) ToHaveSize ¶
ToHaveSize is a synonym for ToHaveLength. The tester is normally *testing.T.
type OrderedType ¶
OrderedType is used for assertions about numbers and other ordered types.
func Number ¶
func Number[O cmp.Ordered](value O, other ...any) OrderedType[O]
Number creates an ordering assertion. It accepts all numbers, and also coincidentally accepts strings. Its methods are the full set of ordering comparisons, i.e. >, >=, <, <=, ==, and !=.
If more than one argument is passed, all subsequent arguments will be required to be nil/zero. This is convenient if you want to make an assertion on a method/function that returns a value and an error, a common pattern in Go.
func (OrderedType[O]) I ¶
func (a OrderedType[O]) I(info any, other ...any) OrderedType[O]
I is a synonym for [Info].
func (OrderedType[O]) Info ¶
func (a OrderedType[O]) Info(info any, other ...any) OrderedType[O]
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (OrderedType[O]) ToBe ¶
func (a OrderedType[O]) ToBe(t Tester, expected O)
ToBe asserts that the actual and expected numbers have the same values and types. The tester is normally *testing.T.
func (OrderedType[O]) ToBeGreaterThan ¶
func (a OrderedType[O]) ToBeGreaterThan(t Tester, threshold O)
ToBeGreaterThan asserts that the actual values is greater than the threshold value. The tester is normally *testing.T.
func (OrderedType[O]) ToBeGreaterThanOrEqualTo ¶
func (a OrderedType[O]) ToBeGreaterThanOrEqualTo(t Tester, threshold O)
ToBeGreaterThanOrEqualTo asserts that the actual values is greater than or equal to the threshold value. The tester is normally *testing.T.
func (OrderedType[O]) ToBeLessThan ¶
func (a OrderedType[O]) ToBeLessThan(t Tester, threshold O)
ToBeLessThan asserts that the actual values is less than the threshold value. The tester is normally *testing.T.
func (OrderedType[O]) ToBeLessThanOrEqualTo ¶
func (a OrderedType[O]) ToBeLessThanOrEqualTo(t Tester, threshold O)
ToBeLessThanOrEqualTo asserts that the actual values is less than or equal to the threshold value. The tester is normally *testing.T.
type SliceType ¶
type SliceType[T comparable] struct { // contains filtered or unexported fields }
SliceType is used for assertions about slices.
func Slice ¶
func Slice[T comparable](value []T, other ...any) SliceType[T]
Slice creates an assertion for deep value comparison of slices of any comparable type.
This uses gocmp.Equal so the manner of comparison can be tweaked using that API - see also SliceType.Using
func (SliceType[T]) Info ¶
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (SliceType[T]) ToBe ¶
ToBe asserts that the actual and expected slices have the same values and types. The values must be in the same order. The tester is normally *testing.T.
func (SliceType[T]) ToBeEmpty ¶ added in v0.7.0
ToBeEmpty asserts that the slice has zero length. The tester is normally *testing.T.
func (SliceType[T]) ToBeNil ¶ added in v0.10.0
ToBeNil asserts that the actual value is nil / is not nil. The tester is normally *testing.T.
func (SliceType[T]) ToContainAll ¶
ToContainAll asserts that the slice contains all of the values listed. The tester is normally *testing.T.
func (SliceType[T]) ToContainAny ¶
ToContainAny asserts that the slice contains any of the values listed. The tester is normally *testing.T.
func (SliceType[T]) ToHaveLength ¶
ToHaveLength asserts that the slice has the expected length. The tester is normally *testing.T.
type StringType ¶
type StringType[S Stringy] struct { // contains filtered or unexported fields }
StringType is used for assertions about strings.
func String ¶
func String[S Stringy](value S, other ...any) StringType[S]
String creates a string assertion. Strings must contain valid UTF8 encodings.
It accepts all string subtypes and []byte, []rune.
If more than one argument is passed, all subsequent arguments will be required to be nil/zero. This is convenient if you want to make an assertion on a method/function that returns a value and an error, a common pattern in Go.
func (StringType[S]) I ¶
func (a StringType[S]) I(info any, other ...any) StringType[S]
I is a synonym for [Info].
func (StringType[S]) Info ¶
func (a StringType[S]) Info(info any, other ...any) StringType[S]
Info adds a description of the assertion to be included in any error message. The first parameter should be some information such as a string or a number. If this is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (StringType[S]) ToBe ¶
func (a StringType[S]) ToBe(t Tester, expected S)
ToBe asserts that the actual and expected strings have the same values and types. The tester is normally *testing.T.
func (StringType[S]) ToBeEmpty ¶ added in v0.8.0
func (a StringType[S]) ToBeEmpty(t Tester)
ToBeEmpty asserts that the string has zero length. The tester is normally *testing.T.
func (StringType[S]) ToContain ¶
func (a StringType[S]) ToContain(t Tester, substring S)
ToContain asserts that the actual string contains the substring. The tester is normally *testing.T.
func (StringType[S]) ToEqual ¶
func (a StringType[S]) ToEqual(t Tester, expected string)
ToEqual asserts that the actual and expected strings have the same values and similar types. Unlike StringType.ToBe, the concrete type may differ. The tester is normally *testing.T.
func (StringType[S]) ToHaveLength ¶ added in v0.8.0
func (a StringType[S]) ToHaveLength(t Tester, expected int)
ToHaveLength asserts that the string has the expected length. The tester is normally *testing.T.
func (StringType[S]) ToMatch ¶ added in v0.11.0
func (a StringType[S]) ToMatch(t Tester, pattern *regexp.Regexp)
ToMatch asserts that the actual string matches a regular expression. The tester is normally *testing.T.
func (StringType[S]) Trim ¶
func (a StringType[S]) Trim(at int) StringType[S]
Trim shortens the error message for very long strings. Trimming is disabled by default.
type Tester ¶
Tester reports test errors and failures. Notably, testing.T implements this interface.
func SimpleTester ¶
SimpleTester is a tester that calls errorf on test errors and fatalf on test failures.