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 ¶
This library is heavily inspired by features of ScalaTest, see http://www.scalatest.org. It tries to adapt similar syntax for Golang. 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.
It Ok If /* actual */ Should /* expected */ It Ok If f() Should Equal 5 It Ok If f() Should Not Less 5 It Ok If f() Must Less 5
Syntax at Glance ¶
Each assertion begins with phrase:
it Ok If ...
Continues with one of imperative keyword as defined by RFC 2119
Must, Must Not, Should, Should Not, May
Assertions with user-defined functions is a technique to define arbitrary boolean expression.
it.Ok(t).If(three). Should().Assert(func(be interface{}) bool { (be > 1) && (be < 10) && (be != 5) })
Intercept any failures in target features
it.Ok(t).If(refToCodeBlock). Should().Intercept(/* ... */)
Matches equality and identity
it.Ok(t). If(one).Should().Equal(1). If(one).Should().Be().A(1)
Matches type
it.Ok(t).If(one). Should().Be().Like(1)
Matches Order and Ranges
it.Ok(t). If(three).Should().Be().Less(10). If(three).Should().Be().LessOrEqual(3). If(three).Should().Be().Greater(1). If(three).Should().Be().GreaterOrEqual(3). If(three).Should().Be().In(1, 10)
Index ¶
- type Be
- func (t Be) A(expect interface{}) Expr
- func (t Be) Eq(expect interface{}) Expr
- func (t Be) Greater(expect interface{}) Expr
- func (t Be) GreaterOrEqual(expect interface{}) Expr
- func (t Be) In(a, b interface{}) Expr
- func (t Be) Less(expect interface{}) Expr
- func (t Be) LessOrEqual(expect interface{}) Expr
- func (t Be) Like(expect interface{}) Expr
- type Expr
- type Fail
- type Imperative
- func (t Imperative) Assert(f func(interface{}) bool) Expr
- func (t Imperative) Be() Be
- func (t Imperative) Eq(expect interface{}) Expr
- func (t Imperative) Equal(expect interface{}) Expr
- func (t Imperative) Equiv(expect interface{}) Expr
- func (t Imperative) Fail() Expr
- func (t Imperative) Intercept(err error) Expr
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Be ¶
type Be struct {
// contains filtered or unexported fields
}
Be is a term of assert expression See Be keyword
func (Be) A ¶
A matches expected values against actual
it.Ok(t).If(actual).Should().Be().A(expected)
func (Be) Greater ¶
Greater compares actual against expected value.
it.Ok(t).If(actual).Should().Be().Greater(expected)
func (Be) GreaterOrEqual ¶
GreaterOrEqual compares actual against expected value.
it.Ok(t).If(actual).Should().Be().Greater(expected)
func (Be) In ¶
In checks that actual value fits to range
it.Ok(t).If(actual).Should().Be().In(from, to)
func (Be) Less ¶
Less compares actual against expected value.
it.Ok(t).If(actual).Should().Be().Less(expected)
func (Be) LessOrEqual ¶
LessOrEqual compares actual against expected value.
it.Ok(t).If(actual).Should().Be().LessOrEqual(expected)
type Expr ¶
type Expr struct {
// contains filtered or unexported fields
}
Expr is a term of assert expression See Ok keyword
func Ok ¶
Ok creates assertion expression, it takes a reference to standard *testing.T interface to setup the evaluation context.
type Fail ¶
type Fail struct {
// contains filtered or unexported fields
}
Fail is a term is assert expression See Fail keyword
type Imperative ¶
type Imperative struct {
// contains filtered or unexported fields
}
Imperative is a term of assert expression See imperative keywords (Must, MustNot, Should, ShouldNot, May)
func (Imperative) Assert ¶
func (t Imperative) Assert(f func(interface{}) bool) Expr
Assert with user-defined functions is a technique to define arbitrary boolean expression. The stashed value is fed as argument to the function. It fails if the function return false.
it.Ok(t).If(x). Should().Assert(func(be interface{}) bool { (be > 1) && (be < 10) && (be != 5) })
func (Imperative) Equal ¶
func (t Imperative) Equal(expect interface{}) Expr
Equal compares left and right sides. The assert fails if values and types are not equal.
it.Ok(t).If(1). Should().Equal(1)
func (Imperative) Equiv ¶
func (t Imperative) Equiv(expect interface{}) Expr
Equiv compares equivalence (same value) of left and right sides. The assert fails if values are not equal but it relaxes types requirements
it.Ok(t).If(1). Should().Equiv(1)
func (Imperative) Fail ¶
func (t Imperative) Fail() Expr
Fail catches any errors caused by the function under the test. The assert fails if error is not nil.
func (Imperative) Intercept ¶
func (t Imperative) Intercept(err error) Expr
Intercept catches any errors caused by the function under the test. The assert fails if expected failure do not match.
it.Ok(t).If(refToCodeBlock). Should().Intercept(/* ... */)
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a term of assert expression See If keyword
func (Value) May ¶
func (t Value) May() Imperative
May is an optional imperative constrain. Its violation do not impact on testing flow in anyway. The informative warning message is produced to console. Error message will be printed only if the test fails or the -test.v
func (Value) Must ¶
func (t Value) Must() Imperative
Must is imperative keyword. The assert definition is an absolute requirement. It terminates execution of tests if assert is false.
func (Value) MustNot ¶
func (t Value) MustNot() Imperative
MustNot is imperative keyword. The assert definition is an absolute prohibition. It terminates execution of tests if assert is true.
func (Value) Should ¶
func (t Value) Should() Imperative
Should is imperative keyword. The assert definition is a strongly recommended requirement. However it's violation do not block continuation of testing.
func (Value) ShouldNot ¶
func (t Value) ShouldNot() Imperative
ShouldNot is imperative keyword. The assert definition is prohibited. However it's violation do not block continuation of testing.