Documentation
¶
Overview ¶
Package it implements a human-friendly syntax for assertions to validates correctness of your code. It's style allows to write BDD-like specifications: "X should Y", "A equals to B", etc.
Inspiration ¶
There is a vision that change of code style in testing helps developers to "switch gears". It's sole purpose to write unit tests assertions in natural language. The library is inspired by features of http://www.scalatest.org and tries to adapt similar syntax for Golang.
It Should / actual / Be / expected / It Should X Equal Y It Should X Less Y It Should String X Contain Y It Should Seq X Equal Y¹, ... Yⁿ
The approximation of the style into Golang syntax:
it.Then(t). Should(it.Equal(x, y)). Should(it.Less(x, y)). Should(it.String(x).Contain(y)). Should(it.Seq(x).Equal(/ ... /))
Syntax at Glance ¶
Each assertion begins with phrase:
it Then ...
Continues with one of imperative keyword as defined by RFC 2119
Must, Must Not, Should, Should Not, May, May Not
Assertions with inline closure is a technique to define arbitrary boolean expression.
it.Then(t). Should(it.True(be > 1 && be < 10 && be != 5))
Intercept any failures in target features
it.Then(t). ShouldNot(it.Fail(refToCodeBlock))
Matches equality and identity
it.Then(t). Should(it.Equal(x, y))
Matches type
it.Then(t). Should(it.SameAs(x, y))
Matches Order and Ranges
it.Ok(t). Should(it.Less(x, 10)). Should(it.LessOrEqual(x, 3)). Should(it.Greater(x, 1)). Should(it.GreaterOrEqual(x, 3)).
Index ¶
- func Be(f func() bool) error
- func Equal[T comparable](x, y T) error
- func Equiv[T any](x, y T) error
- func Greater[T Orderable](x, y T) error
- func GreaterOrEqual[T Orderable](x, y T) error
- func Less[T Orderable](x, y T) error
- func LessOrEqual[T Orderable](x, y T) error
- func Like[T any](x any, y T) error
- func Nil(x interface{}) error
- func SameAs[T any](x, y T) error
- func True(x bool) error
- func TypeOf[T any](x any) error
- type Callable
- type Check
- func (check *Check) May(errs ...error) *Check
- func (check *Check) MayNot(errs ...error) *Check
- func (check *Check) Must(errs ...error) *Check
- func (check *Check) MustNot(errs ...error) *Check
- func (check *Check) Should(errs ...error) *Check
- func (check *Check) ShouldNot(errs ...error) *Check
- func (check *Check) Skip(errs ...error) *Check
- type FailIt
- type JsonOf
- type MapOf
- type Orderable
- type SeqContainIt
- type SeqOf
- type String
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Equal ¶
func Equal[T comparable](x, y T) error
Equal check equality (x = y) of two scalar variables
it.Should(it.Equal(x, y))
func GreaterOrEqual ¶
Greater compares (x >= y) of two scalar variables
it.Should(it.GreaterOrEqual(x, y))
func Like ¶ added in v2.1.0
Equiv check equality (x ≈ y) of two non scalar variables
it.Should(it.Like(x, y))
func Nil ¶
func Nil(x interface{}) error
Nil asserts the variable for the nil value
it.Should(it.Nil(x))
Types ¶
type Callable ¶
type Callable interface {
~func() error | ~func()
}
Callable type constraint for scope of Intercepts
type Check ¶
type Check struct {
// contains filtered or unexported fields
}
Check type is constructor of imperative testing expressions.
it.Then(t).Should( ... )
func Then ¶
Then creates assertion expression, it takes a reference to standard *testing.T interface to setup the evaluation context.
func (*Check) May ¶
May is an optional imperative constrain. Its violation do not impact on testing flow in anyway. The informative warning message is produced to console if assert is failed. Error message will be printed only if the test fails or the -test.v
func (*Check) MayNot ¶
MayNot is an optional imperative constrain. Its violation do not impact on testing flow in anyway. The informative warning message is produced to console if assert is not failed. Error message will be printed only if the test fails or the -test.v
func (*Check) Must ¶
Must is imperative keyword. The assert definition is an absolute requirement. It terminates execution of tests if assert is failed.
func (*Check) MustNot ¶
MustNot is imperative keyword. The definition is an absolute prohibition It terminates execution of tests if assert is not failed.
func (*Check) Should ¶
Should is imperative keyword. The assert definition is a strongly recommended requirement. However it's violation do not block continuation of testing. The test fails if assert is failed.
type FailIt ¶
type FailIt struct {
// contains filtered or unexported fields
}
FailIt extend Fail assert
func Error ¶
Error checks return values of function on the error cases
it.Should(it.Error(refToCodeBlock()))
func Fail ¶
Fail catches any errors caused by the function under the test.
it.Should(it.Fail(refToCodeBlock))
type JsonOf ¶ added in v2.2.0
type JsonOf[A any] struct { // contains filtered or unexported fields }
type MapOf ¶
type MapOf[K comparable, V any] map[K]V
func Map ¶
func Map[K comparable, V any](val map[K]V) MapOf[K, V]
type Orderable ¶
type Orderable interface {
~string |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64
}
Orderable type constraint
type SeqContainIt ¶
type SeqContainIt[A any] struct { // contains filtered or unexported fields }
FailIt extend Fail assert
func (SeqContainIt[A]) AllOf ¶
func (x SeqContainIt[A]) AllOf(ys ...A) error
func (SeqContainIt[A]) As ¶
func (x SeqContainIt[A]) As(target any) bool
func (SeqContainIt[A]) Error ¶
func (x SeqContainIt[A]) Error() string
func (SeqContainIt[A]) OneOf ¶
func (x SeqContainIt[A]) OneOf(ys ...A) error