Documentation
¶
Index ¶
- func Contain(tb testing.TB, haystack, needle any, msg ...any)
- func ContainExactly[T any](tb testing.TB, expected, actual T, msg ...any)
- func Empty(tb testing.TB, v any, msg ...any)
- func Equal[T any](tb testing.TB, expected, actually T, msg ...any)
- func ErrorIs(tb testing.TB, expected, actual error, msg ...any)
- func False(tb testing.TB, v bool, msg ...any)
- func Nil(tb testing.TB, v any, msg ...any)
- func NotContain(tb testing.TB, haystack, v any, msg ...any)
- func NotEmpty(tb testing.TB, v any, msg ...any)
- func NotEqual[T any](tb testing.TB, expected, actually T, msg ...any)
- func NotNil(tb testing.TB, v any, msg ...any)
- func NotPanic(tb testing.TB, blk func(), msg ...any)
- func Panic(tb testing.TB, blk func(), msg ...any) any
- func True(tb testing.TB, v bool, msg ...any)
- type AnyOf
- type Asserter
- func (a Asserter) AnyOf(blk func(a *AnyOf), msg ...any)
- func (a Asserter) Contain(haystack, needle any, msg ...any)
- func (a Asserter) ContainExactly(expected, actual any, msg ...any)
- func (a Asserter) Empty(v any, msg ...any)
- func (a Asserter) Equal(expected, actually any, msg ...any)
- func (a Asserter) ErrorIs(expected, actual error, msg ...any)
- func (a Asserter) False(v bool, msg ...any)
- func (a Asserter) Nil(v any, msg ...any)
- func (a Asserter) NotContain(haystack, v any, msg ...any)
- func (a Asserter) NotEmpty(v any, msg ...any)
- func (a Asserter) NotEqual(v, oth any, msg ...any)
- func (a Asserter) NotNil(v any, msg ...any)
- func (a Asserter) NotPanic(blk func(), msg ...any)
- func (a Asserter) Panic(blk func(), msg ...any) (panicValue any)
- func (a Asserter) True(v bool, msg ...any)
- type It
Examples ¶
- AnyOf (FanOutPublishing)
- AnyOf (ListOfCompositedStructuresWhereOnlyTheEmbededValueIsRelevant)
- AnyOf (ListOfInterface)
- AnyOf (ListOfStructuresWithIrrelevantValues)
- AnyOf (StructWithManyAcceptableState)
- Asserter.AnyOf
- Asserter.Contain
- Asserter.ContainExactly
- Asserter.Empty
- Asserter.Equal
- Asserter.Equal (IsEqualFunctionThatSupportsErrorReturning)
- Asserter.Equal (IsEqualFunctionUsedForComparison)
- Asserter.ErrorIs
- Asserter.False
- Asserter.Nil
- Asserter.NotContain
- Asserter.NotEmpty
- Asserter.NotEqual
- Asserter.NotNil
- Asserter.NotPanic
- Asserter.Panic
- Asserter.True
- Contain
- ContainExactly
- Empty
- Equal
- ErrorIs
- False
- Must
- Nil
- NotContain
- NotEmpty
- NotEqual
- NotNil
- NotPanic
- Panic
- Should
- True
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contain ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).Contain(tb, []int{1, 2, 3}, 3, "optional assertion explanation") assert.Must(tb).Contain(tb, []int{1, 2, 3}, []int{1, 2}, "optional assertion explanation") assert.Must(tb).Contain(tb, map[string]int{"The Answer": 42, "oth": 13}, map[string]int{"The Answer": 42}, "optional assertion explanation") }
Output:
func ContainExactly ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.ContainExactly(tb, []int{1, 2, 3}, []int{2, 3, 1}, "optional assertion explanation") // true assert.ContainExactly(tb, []int{1, 2, 3}, []int{1, 42, 2}, "optional assertion explanation") // false }
Output:
func Empty ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Empty(tb, "") // ok assert.Empty(tb, "oh no!") // Fatal }
Output:
func Equal ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Equal(tb, "a", "a") assert.Equal(tb, 42, 42) assert.Equal(tb, []int{42}, []int{42}) assert.Equal(tb, map[int]int{24: 42}, map[int]int{24: 42}) }
Output:
func ErrorIs ¶ added in v0.80.0
Example ¶
package main import ( "errors" "fmt" "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB actualErr := errors.New("boom") assert.ErrorIs(tb, errors.New("boom"), actualErr) // passes for equality assert.ErrorIs(tb, errors.New("boom"), fmt.Errorf("wrapped error: %w", actualErr)) // passes for wrapped errors }
Output:
func False ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.False(tb, false) // ok assert.False(tb, true) // Fatal }
Output:
func Nil ¶ added in v0.80.0
Example ¶
package main import ( "errors" "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Nil(tb, nil) // ok assert.Nil(tb, errors.New("boom")) // Fatal }
Output:
func NotContain ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).NotContain(tb, []int{1, 2, 3}, 42) assert.Must(tb).NotContain(tb, []int{1, 2, 3}, []int{1, 2, 42}) assert.Must(tb).NotContain(tb, map[string]int{"The Answer": 42, "oth": 13}, map[string]int{"The Answer": 41}) }
Output:
func NotEmpty ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.NotEmpty(tb, "huh...") // ok assert.NotEmpty(tb, "") // Fatal }
Output:
func NotEqual ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.NotEqual(tb, "a", "b") assert.Equal(tb, 13, 42) }
Output:
func NotNil ¶ added in v0.80.0
Example ¶
package main import ( "errors" "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.NotNil(tb, errors.New("boom")) // ok assert.NotNil(tb, nil) // Fatal }
Output:
func NotPanic ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.NotPanic(tb, func() {}) // ok assert.NotPanic(tb, func() { panic("oh no!") }) // Fatal }
Output:
func Panic ¶ added in v0.80.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB panicValue := assert.Panic(tb, func() { panic("at the disco") }) // ok assert.Equal(tb, "some expected panic value", panicValue) assert.Panic(tb, func() {}) // Fatal }
Output:
Types ¶
type AnyOf ¶ added in v0.60.0
type AnyOf struct { TB testing.TB Fn func(...interface{}) // contains filtered or unexported fields }
AnyOf is an assertion helper that allows you run AnyOf.Test assertion blocks, that can fail, as lone at least one of them succeeds. common usage use-cases:
- list of interface, where test order, or the underlying structure's implementation is irrelevant for the behavior.
- list of big structures, where not all field value relevant, only a subset, like a structure it wraps under a field.
- list of structures with fields that has dynamic state values, which is irrelevant for the given test.
- structure that can have various state scenario, and you want to check all of them, and you expect to find one match with the input.
- fan out scenario, where you need to check in parallel that at least one of the worker received the event.
Example (FanOutPublishing) ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) type ExamplePublisherEvent struct{ V int } type ExamplePublisher struct{} func (ExamplePublisher) Publish(event ExamplePublisherEvent) {} func (ExamplePublisher) Subscribe(func(event ExamplePublisherEvent)) {} func (ExamplePublisher) Wait() {} func (ExamplePublisher) Close() error { return nil } func main() { var tb testing.TB publisher := ExamplePublisher{} anyOf := &assert.AnyOf{TB: tb, Fn: tb.Fatal} for i := 0; i < 42; i++ { publisher.Subscribe(func(event ExamplePublisherEvent) { anyOf.Test(func(it assert.It) { it.Must.Equal(42, event.V) }) }) } publisher.Publish(ExamplePublisherEvent{V: 42}) publisher.Wait() assert.Must(tb).Nil(publisher.Close()) anyOf.Finish() }
Output:
Example (ListOfCompositedStructuresWhereOnlyTheEmbededValueIsRelevant) ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB type BigStruct struct { ID string // not relevant for the test A, B, C, D, E int // not relevant data as well WrappedStruct struct { A, B, C int // relevant data for the test } } anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal} for _, v := range []BigStruct{} { anyOf.Test(func(it assert.It) { it.Must.Equal(42, v.WrappedStruct.A) it.Must.Equal(1, v.WrappedStruct.B) it.Must.Equal(2, v.WrappedStruct.C) }) } anyOf.Finish() }
Output:
Example (ListOfInterface) ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB type ExampleInterface interface { Foo() int Bar() bool Baz() string } anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal} for _, v := range []ExampleInterface{} { anyOf.Test(func(it assert.It) { it.Must.True(v.Bar()) }) } anyOf.Finish() }
Output:
Example (ListOfStructuresWithIrrelevantValues) ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB type StructWithDynamicValues struct { IrrelevantStateValue int // not relevant data for the test ImportantValue int } anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal} for _, v := range []StructWithDynamicValues{} { anyOf.Test(func(it assert.It) { it.Must.Equal(42, v.ImportantValue) }) } anyOf.Finish() }
Output:
Example (StructWithManyAcceptableState) ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB type ExampleStruct struct { Type string A, B, C int } var es ExampleStruct anyOf := assert.AnyOf{TB: tb, Fn: tb.Fatal} anyOf.Test(func(it assert.It) { it.Must.Equal(`foo`, es.Type) it.Must.Equal(1, es.A) it.Must.Equal(2, es.B) it.Must.Equal(3, es.C) }) anyOf.Test(func(it assert.It) { it.Must.Equal(`foo`, es.Type) it.Must.Equal(3, es.A) it.Must.Equal(2, es.B) it.Must.Equal(1, es.C) }) anyOf.Test(func(it assert.It) { it.Must.Equal(`bar`, es.Type) it.Must.Equal(11, es.A) it.Must.Equal(12, es.B) it.Must.Equal(13, es.C) }) anyOf.Test(func(it assert.It) { it.Must.Equal(`baz`, es.Type) it.Must.Equal(21, es.A) it.Must.Equal(22, es.B) it.Must.Equal(23, es.C) }) anyOf.Finish() }
Output:
func (*AnyOf) Finish ¶ added in v0.60.0
func (ao *AnyOf) Finish(msg ...interface{})
Finish will check if any of the assertion succeeded.
type Asserter ¶
func Must ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB // create an assertion helper which will fail the testing context with .Fatal(...) in case of a failed assert. assert.Must(tb).True(true) }
Output:
func Should ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB // create an assertion helper which will fail the testing context with .Error(...) in case of a failed assert. assert.Should(tb).True(true) }
Output:
func (Asserter) AnyOf ¶ added in v0.60.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB var list []interface { Foo() int Bar() bool Baz() string } assert.Must(tb).AnyOf(func(anyOf *assert.AnyOf) { for _, testingCase := range list { anyOf.Test(func(it assert.It) { it.Must.True(testingCase.Bar()) }) } }) }
Output:
func (Asserter) Contain ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).Contain([]int{1, 2, 3}, 3, "optional assertion explanation") assert.Must(tb).Contain([]int{1, 2, 3}, []int{1, 2}, "optional assertion explanation") assert.Must(tb).Contain(map[string]int{"The Answer": 42, "oth": 13}, map[string]int{"The Answer": 42}, "optional assertion explanation") }
Output:
func (Asserter) ContainExactly ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).ContainExactly([]int{1, 2, 3}, []int{2, 3, 1}, "optional assertion explanation") // true assert.Must(tb).ContainExactly([]int{1, 2, 3}, []int{1, 42, 2}, "optional assertion explanation") // false }
Output:
func (Asserter) Empty ¶ added in v0.61.0
Empty gets whether the specified value is considered empty.
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).Empty([]int{}) // pass assert.Must(tb).Empty([]int{42}) // fail assert.Must(tb).Empty([42]int{}) // pass assert.Must(tb).Empty([42]int{42}) // fail assert.Must(tb).Empty(map[int]int{}) // pass assert.Must(tb).Empty(map[int]int{42: 24}) // fail assert.Must(tb).Empty("") // pass assert.Must(tb).Empty("42") // fail }
Output:
func (Asserter) Equal ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).Equal(true, true, "optional assertion explanation") }
Output:
Example (IsEqualFunctionThatSupportsErrorReturning) ¶
package main import ( "errors" "testing" "github.com/adamluzsi/testcase/assert" ) type ExampleEqualableWithError struct { IrrelevantExportedField int relevantUnexportedValue int IsEqualErr error } func (es ExampleEqualableWithError) IsEqual(oth ExampleEqualableWithError) (bool, error) { return es.relevantUnexportedValue == oth.relevantUnexportedValue, es.IsEqualErr } func main() { var tb testing.TB expected := ExampleEqualableWithError{ IrrelevantExportedField: 42, relevantUnexportedValue: 24, IsEqualErr: errors.New("sadly something went wrong"), } actual := ExampleEqualableWithError{ IrrelevantExportedField: 42, relevantUnexportedValue: 24, } assert.Must(tb).Equal(expected, actual) // fails because the error returned from the IsEqual function. }
Output:
Example (IsEqualFunctionUsedForComparison) ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) type ExampleEqualable struct { IrrelevantExportedField int relevantUnexportedValue int } func (es ExampleEqualable) IsEqual(oth ExampleEqualable) bool { return es.relevantUnexportedValue == oth.relevantUnexportedValue } func main() { var tb testing.TB expected := ExampleEqualable{ IrrelevantExportedField: 42, relevantUnexportedValue: 24, } actual := ExampleEqualable{ IrrelevantExportedField: 4242, relevantUnexportedValue: 24, } assert.Must(tb).Equal(expected, actual) // passes as by IsEqual terms the two value is equal }
Output:
func (Asserter) ErrorIs ¶ added in v0.68.0
ErrorIs allows you to assert an error value by an expectation. if the implementation of the test subject later changes, and for example, it starts to use wrapping, this should not be an issue as the IsEqualErr's error chain is also matched against the expectation.
Example ¶
package main import ( "errors" "fmt" "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB actualErr := errors.New("boom") assert.Must(tb).ErrorIs(errors.New("boom"), actualErr) // passes for equality assert.Must(tb).ErrorIs(errors.New("boom"), fmt.Errorf("wrapped error: %w", actualErr)) // passes for wrapped errors }
Output:
func (Asserter) False ¶ added in v0.61.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).False(false, "optional assertion explanation") }
Output:
func (Asserter) Nil ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).Nil(nil, "optional assertion explanation") }
Output:
func (Asserter) NotContain ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).NotContain([]int{1, 2, 3}, 42, "optional assertion explanation") assert.Must(tb).NotContain([]int{1, 2, 3}, []int{42}, "optional assertion explanation") assert.Must(tb).NotContain(map[string]int{"The Answer": 42, "oth": 13}, map[string]int{"The Answer": 13}, "optional assertion explanation") }
Output:
func (Asserter) NotEmpty ¶ added in v0.61.0
NotEmpty gets whether the specified value is considered empty.
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).NotEmpty([]int{42}, "optional assertion explanation") assert.Must(tb).NotEmpty([]int{}) // fail assert.Must(tb).NotEmpty([]int{42}) // pass assert.Must(tb).NotEmpty([42]int{}) // fail assert.Must(tb).NotEmpty([42]int{42}) // pass assert.Must(tb).NotEmpty(map[int]int{}) // fail assert.Must(tb).NotEmpty(map[int]int{42: 24}) // pass assert.Must(tb).NotEmpty("") // fail assert.Must(tb).NotEmpty("42") // pass }
Output:
func (Asserter) NotEqual ¶ added in v0.58.0
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).NotEqual(true, false, "optional assertion explanation") }
Output:
func (Asserter) NotNil ¶
Example ¶
package main import ( "errors" "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).NotNil(errors.New("42"), "optional assertion explanation") }
Output:
func (Asserter) NotPanic ¶
Example ¶
package main import ( "testing" "github.com/adamluzsi/testcase/assert" ) func main() { var tb testing.TB assert.Must(tb).NotPanic(func() { /* no boom */ }, "optional assertion explanation") }
Output:
Click to show internal directories.
Click to hide internal directories.