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
- func (a ErrorType) I(info any, other ...any) ErrorType
- func (a ErrorType) Info(info any, other ...any) ErrorType
- func (a ErrorType) Not() ErrorType
- func (a ErrorType) ToBeNil(t Tester)
- func (a ErrorType) ToContain(t Tester, substring string)
- func (a ErrorType) ToHaveOccurred(t Tester)
- func (a ErrorType) ToMatch(t Tester, pattern *regexp.Regexp)
- 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 OrderedOr
- 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) *OrderedOr[O]
- func (a *OrderedType[O]) ToBeBetween(t Tester, minimum, maximum O) *OrderedOr[O]
- func (a *OrderedType[O]) ToBeBetweenOrEqual(t Tester, minimum, maximum O) *OrderedOr[O]
- func (a *OrderedType[O]) ToBeGreaterThan(t Tester, threshold O) *OrderedOr[O]
- func (a *OrderedType[O]) ToBeGreaterThanOrEqual(t Tester, threshold O) *OrderedOr[O]
- func (a *OrderedType[O]) ToBeLessThan(t Tester, threshold O) *OrderedOr[O]
- func (a *OrderedType[O]) ToBeLessThanOrEqual(t Tester, threshold O) *OrderedOr[O]
- func (a *OrderedType[O]) ToEqual(t Tester, expected any) *OrderedOr[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]) ToContain(t Tester, expected T)
- 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 StringOr
- 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) *StringOr[S]
- func (a *StringType[S]) ToBeEmpty(t Tester) *StringOr[S]
- func (a *StringType[S]) ToContain(t Tester, substring S) *StringOr[S]
- func (a *StringType[S]) ToEqual(t Tester, expected string) *StringOr[S]
- func (a *StringType[S]) ToHaveLength(t Tester, expected int) *StringOr[S]
- func (a *StringType[S]) ToMatch(t Tester, pattern *regexp.Regexp) *StringOr[S]
- func (a *StringType[S]) Trim(at int) *StringType[S]
- type Stringy
- type Tester
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ApproximateFloatFraction = 1e-6
ApproximateFloatFraction provides an option that compares any (a, b float32) or (a, b float64) pair. This is initialised to 1e-6, which means that a pair of floats are considered effectively equal if their fractional difference is less than one part in a million. 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, MapType.Using and SliceType.Using instead.
var JustLogIt = SimpleTester(log.Fatal, log.Fatal)
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 Value ¶ added in v1.0.0
Value 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.
AnyType uses gocmp.Equal so the manner of comparison can be tweaked using that API (see 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 or even a struct. If info 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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
type pair struct {
a, b int
}
// Any matching is most useful for structs (but can test anything)
v := pair{1, 2}
expect.Any(v).ToBe(t, pair{1, 2})
expect.Any(v).Not().ToBe(t, pair{3, 4})
var i int // some loop counter
// Info gives more information when the test fails, such as within a loop
expect.Any(v).Info("loop %d", i).ToBe(t, pair{1, 2})
}
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 actual value must be a type that is convertible to the type of the expected value. 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 or even a struct. If info 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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
var v bool
expect.Bool(v).ToBe(t, false) // or ToBeFalse(t)
var i int // some loop counter
// Info gives more information when the test fails, such as within a loop
expect.Bool(v).Info("loop %d", i).Not().ToBeTrue(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. At least one of the parameters must be an error. 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 or even a struct. If info 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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
var err error
// ... something under test goes here
expect.Error(err).ToBeNil(t)
expect.Error(err).Not().ToHaveOccurred(t)
// if there's a function that returns various results and an error...
thingUnderTest := func() (string, error) { return "", nil }
// ...the function return parameters can be passed straight in
expect.Error(thingUnderTest()).Not().ToHaveOccurred(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 or even a struct. If info 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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
expect.Func(func() { panic(101) }).ToPanic(t)
expect.Func(func() {}).Not().ToPanic(t)
}
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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
expect.Func(func() { panic("boo") }).ToPanicWithMessage(t, "boo")
}
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 or even a struct. If info 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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
m := map[string]int{"a": 1, "b": 2, "c": 3}
// ToBe verifies all the keys and values match
expect.Map(m).ToBe(t, map[string]int{"a": 1, "b": 2, "c": 3})
}
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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
m := map[string]int{"a": 1, "b": 2, "c": 3}
// verify one key is present
expect.Map(m).ToContain(t, "b")
// verify one key and its value match
expect.Map(m).ToContain(t, "b", 2)
}
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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
m := map[string]int{"a": 1, "b": 2, "c": 3}
expect.Map(m).ToContainAll(t, "a", "b")
}
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.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
m := map[string]int{"a": 1, "b": 2, "c": 3}
expect.Map(m).ToContainAny(t, "z", "b")
}
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 OrderedOr ¶ added in v0.16.0
OrderedOr is only used for conjunction concatenation (see OrderedOr.Or).
func (*OrderedOr[O]) Or ¶ added in v0.16.0
func (or *OrderedOr[O]) Or() *OrderedType[O]
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 or even a struct. If info is a format string, more parameters can follow and will be formatted accordingly (see fmt.Sprintf).
func (*OrderedType[O]) Not ¶
func (a *OrderedType[O]) Not() *OrderedType[O]
Not inverts the assertion.
func (*OrderedType[O]) ToBe ¶
func (a *OrderedType[O]) ToBe(t Tester, expected O) *OrderedOr[O]
ToBe asserts that the actual and expected numbers have the same values and types. The tester is normally *testing.T.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
// Number matching can use any size of int or uint or float, or subtype of any of them.
// This example allows either of the two expected values.
v := 123
expect.Number(v).ToBe(nil, 123).Or().ToBe(t, 125)
// The `Info` method can be helpful when testing inside a loop, for example.
var i int // some loop counter
expect.Number(v).Info("loop %d", i).Not().ToBe(t, 321)
}
func (*OrderedType[O]) ToBeBetween ¶ added in v0.12.0
func (a *OrderedType[O]) ToBeBetween(t Tester, minimum, maximum O) *OrderedOr[O]
ToBeBetween asserts that the actual values is between two threshold values. The assertion succeeds if minimum < value < maximum. The tester is normally *testing.T.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
// number matching can use any size of int or uint or float, or subtype of any of them
v := 123
expect.Number(v).ToBeBetween(t, 100, 200)
var i int // some loop counter
expect.Number(v).Info("loop %d", i).Not().ToBeBetween(t, 1, 10)
}
func (*OrderedType[O]) ToBeBetweenOrEqual ¶ added in v0.12.0
func (a *OrderedType[O]) ToBeBetweenOrEqual(t Tester, minimum, maximum O) *OrderedOr[O]
ToBeBetweenOrEqual asserts that the actual values is between two threshold values. The assertion succeeds if minimum <= value <= maximum. The tester is normally *testing.T.
func (*OrderedType[O]) ToBeGreaterThan ¶
func (a *OrderedType[O]) ToBeGreaterThan(t Tester, threshold O) *OrderedOr[O]
ToBeGreaterThan asserts that the actual values is greater than the threshold value. The tester is normally *testing.T.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
// number matching can use any size of int or uint or float, or subtype of any of them
v := 123
expect.Number(v).ToBeGreaterThan(t, 100)
var i int // some loop counter
expect.Number(v).Info("loop %d", i).Not().ToBeGreaterThan(t, 200)
}
func (*OrderedType[O]) ToBeGreaterThanOrEqual ¶ added in v0.12.0
func (a *OrderedType[O]) ToBeGreaterThanOrEqual(t Tester, threshold O) *OrderedOr[O]
ToBeGreaterThanOrEqual 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) *OrderedOr[O]
ToBeLessThan asserts that the actual values is less than the threshold value. The tester is normally *testing.T.
func (*OrderedType[O]) ToBeLessThanOrEqual ¶ added in v0.12.0
func (a *OrderedType[O]) ToBeLessThanOrEqual(t Tester, threshold O) *OrderedOr[O]
ToBeLessThanOrEqual asserts that the actual values is less than or equal to the threshold value. The tester is normally *testing.T.
func (*OrderedType[O]) ToEqual ¶ added in v0.21.0
func (a *OrderedType[O]) ToEqual(t Tester, expected any) *OrderedOr[O]
ToEqual asserts that the actual and expected numbers have the same values and similar types. The expected value must be some numeric type. The tester is normally *testing.T.
type SliceType ¶
type SliceType[T any] struct { // contains filtered or unexported fields }
SliceType is used for assertions about slices.
func Slice ¶
Slice creates an assertion for deep value comparison of slices of any 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 or even a struct. If info 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. If you pass the expected values in a slice, don't forget the ellipsis. 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]) ToContain ¶ added in v0.18.0
ToContain asserts that the slice contains the expected value. 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 StringOr ¶ added in v0.15.0
type StringOr[S Stringy] struct { // contains filtered or unexported fields }
StringOr is only used for conjunction concatenation (see StringOr.Or).
func (*StringOr[S]) Or ¶ added in v0.15.0
func (or *StringOr[S]) Or() *StringType[S]
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 or even a struct. If info 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) *StringOr[S]
ToBe asserts that the actual and expected strings have the same values and types. The tester is normally *testing.T.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
// string matching can use any string, or subtype of string
s := "hello"
expect.String(s).ToBe(t, "hello")
var i int // some loop counter
expect.String(s).Info("loop %d", i).Not().ToBe(t, "goodbye")
}
func (*StringType[S]) ToBeEmpty ¶ added in v0.8.0
func (a *StringType[S]) ToBeEmpty(t Tester) *StringOr[S]
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) *StringOr[S]
ToContain asserts that the actual string contains the substring. The tester is normally *testing.T.
Example ¶
package main
import (
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
s := "Once more unto the breach"
expect.String(s).ToContain(t, "unto")
var i int // some loop counter
expect.String(s).Info("loop %d", i).Not().ToContain(t, "foobar")
}
func (*StringType[S]) ToEqual ¶
func (a *StringType[S]) ToEqual(t Tester, expected string) *StringOr[S]
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) *StringOr[S]
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) *StringOr[S]
ToMatch asserts that the actual string matches a regular expression. The tester is normally *testing.T.
Example ¶
package main
import (
"regexp"
"testing"
"github.com/rickb777/expect"
)
func main() {
var t *testing.T
s := "hello"
expect.String(s).ToMatch(t, regexp.MustCompile("^he"))
var i int // some loop counter
expect.String(s).Info("loop %d", i).Not().ToMatch(t, regexp.MustCompile(".*bye$"))
}
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.