Documentation ¶
Overview ¶
Package mighty is a lightweight extension to Go's testing package.
With this utility, you can make your Go test files super clean but still intuitive. This package doesn't want to hide complex things from you, but wants to eliminate repetitive, long code from your tests.
Using mighty ¶
You could create a value of mighty.Myt and use its methods like m.Eq(), m.Neq(), ... etc. as seen below:
m := mighty.Myt{t} // t is of type *testing.T m.Eq(6, len("mighty")) // Expect len("mighty") to be 6 r := bytes.NewBuffer([]byte{'a'}) m.ExpEq(byte('a'))(r.ReadByte()) // Expect to read 'a' and no error
But the recommended, more intuitive and more compact way is to acquire and use method values returned by functions of mighty:
eq, expEq := mighty.EqExpEq(t) // t is of type *testing.T eq(6, len("mighty")) // Expect len("mighty") to be 6 r := bytes.NewBuffer([]byte{'a'}) expEq(byte('a'))(r.ReadByte()) // Expect to read 'a' and no error
Example 1 testing math Abs ¶
Without mighty it could look like this:
cases := []struct{ in, exp float64 }{{1, 1}, {-1, 1}} for _, c := range cases { if got := math.Abs(c.in); got != c.exp { t.Errorf("Expected: %v, got: %v", c.exp, got) } }
Using mighty:
cases := []struct{ in, exp float64 }{{1, 1}, {-1, 1}} eq := mighty.Eq(t) for _, c := range cases { eq(c.exp, math.Abs(c.in)) }
Example 2 testing reading from bytes Buffer ¶
Without mighty it could look like this:
r := bytes.NewBufferString("test-data") if b, err := r.ReadByte(); b != 't' || err != nil { t.Errorf("Expected: %v, got: %v, error: %v", 't', b, err) } if line, err := r.ReadString('-'); line != "est-" || err != nil { t.Errorf("Expected: %v, got: %v, error: %v", "est", line, err) } p := make([]byte, 4) if n, err := r.Read(p); n != 4 || string(p) != "data" || err != nil { t.Errorf("Expected: n=%v, p=%v; got: n=%v, p=%v; error: %v", 4, "data", n, string(p), err) }
Using mighty:
eq, expEq := mighty.EqExpEq(t) r := bytes.NewBufferString("test-data") expEq(byte('t'))(r.ReadByte()) expEq("est-")(r.ReadString('-')) p := make([]byte, 4) expEq(4)(r.Read(p)) eq("data", string(p))
More examples ¶
For more usage examples, check out the following packages which use mighty extensively in their testing code:
- https://github.com/icza/bitio
Index ¶
- Variables
- func EqDeq(tb testing.TB) (Func2ArgsErr, Func2ArgsErr)
- func EqExpEq(tb testing.TB) (Func2ArgsErr, Func1ArgFunc1ArgErr)
- func EqNeq(tb testing.TB) (Func2ArgsErr, Func2ArgsErr)
- func NearFunc(a, b, eps float64) bool
- type Func1ArgFunc1ArgErr
- type Func2ArgsErr
- type Func2FloatsFunc1FloatErr
- type Func3FloatsErr
- type Myt
- func (m Myt) Deq(exp, got interface{}, errs ...error)
- func (m Myt) Eq(exp, got interface{}, errs ...error)
- func (m Myt) ExpDeq(exp interface{}) func(got interface{}, errs ...error)
- func (m Myt) ExpEq(exp interface{}) func(got interface{}, errs ...error)
- func (m Myt) ExpNear(exp, eps float64) func(got float64, errs ...error)
- func (m Myt) ExpNeq(v1 interface{}) func(v2 interface{}, errs ...error)
- func (m Myt) Near(exp, got, eps float64, errs ...error)
- func (m Myt) Neq(v1, v2 interface{}, errs ...error)
Constants ¶
This section is empty.
Variables ¶
var NearLogic = NearFunc
NearLogic is a variable holding a function which is responsible to decide if 2 float64 numbers are near to each other (given an epsilon). It is used by the Myt.Near() and Myt.ExpNear() functions. Default value is NearFunc, but you may set your own function.
Functions ¶
func EqDeq ¶
func EqDeq(tb testing.TB) (Func2ArgsErr, Func2ArgsErr)
EqDeq returns 2 method values: Myt{tb}.Eq and Myt{tb}.Deq. tb may be a *testing.T or *testing.B value.
func EqExpEq ¶
func EqExpEq(tb testing.TB) (Func2ArgsErr, Func1ArgFunc1ArgErr)
EqExpEq returns 2 method values: Myt{tb}.Eq and Myt{tb}.ExpEq. tb may be a *testing.T or *testing.B value.
func EqNeq ¶
func EqNeq(tb testing.TB) (Func2ArgsErr, Func2ArgsErr)
EqNeq returns 2 method values: Myt{tb}.Eq and Myt{tb}.Neq. tb may be a *testing.T or *testing.B value.
func NearFunc ¶
NearFunc checks if 2 float64 numbers are "near" to each other. The caller is responsible to provide a sensible epsilon. This is the default NearLogic, but you may set your own function.
"near" is defined as the following:
near := Math.Abs(a - b) < eps
Corner cases:
- if a==b, result is true (eps will not be checked, may be NaN)
- Inf is near to Inf (even if eps=NaN; consequence of 1.)
- -Inf is near to -Inf (even if eps=NaN; consequence of 1.)
- NaN is not near to anything (not even to NaN)
- eps=Inf results in true (unless any of a or b is NaN)
Types ¶
type Func1ArgFunc1ArgErr ¶
type Func1ArgFunc1ArgErr func(interface{}) func(interface{}, ...error)
Func1ArgFunc1ArgErr is a type describing a function which takes 1 interface{} argument and returns a function which takes 1 interface{} argument and an optional error (in the form of a variadic parameter).
func ExpDeq ¶
func ExpDeq(tb testing.TB) Func1ArgFunc1ArgErr
ExpDeq returns a method value of Myt{tb}.ExpDeq. tb may be a *testing.T or *testing.B value.
func ExpEq ¶
func ExpEq(tb testing.TB) Func1ArgFunc1ArgErr
ExpEq returns a method value of Myt{tb}.ExpEq. tb may be a *testing.T or *testing.B value.
func ExpNeq ¶
func ExpNeq(tb testing.TB) Func1ArgFunc1ArgErr
ExpNeq returns a method value of Myt{tb}.ExpNeq. tb may be a *testing.T or *testing.B value.
type Func2ArgsErr ¶
type Func2ArgsErr func(interface{}, interface{}, ...error)
Func2ArgsErr is a type describing a function which takes 2 interface{} arguments and an optional error (in the form of a variadic parameter).
func Deq ¶
func Deq(tb testing.TB) Func2ArgsErr
Deq returns a method value of Myt{tb}.Deq. tb may be a *testing.T or *testing.B value.
func Eq ¶
func Eq(tb testing.TB) Func2ArgsErr
Eq returns a method value of Myt{tb}.Eq. tb may be a *testing.T or *testing.B value.
func Neq ¶
func Neq(tb testing.TB) Func2ArgsErr
Neq returns a method value of Myt{tb}.Neq. tb may be a *testing.T or *testing.B value.
type Func2FloatsFunc1FloatErr ¶
Func2FloatsFunc1FloatErr is a type describing a function which takes 2 float64 arguments and returns a function which takes 1 float64 argument and an optional error (in the form of a variadic parameter).
func ExpNear ¶
func ExpNear(tb testing.TB) Func2FloatsFunc1FloatErr
ExpNear returns a method value of Myt{tb}.ExpNear. tb may be a *testing.T or *testing.B value.
type Func3FloatsErr ¶
Func3FloatsErr is a type describing a function which takes 3 float64 arguments and an optional error (in the form of a variadic parameter).
func Near ¶
func Near(tb testing.TB) Func3FloatsErr
Near returns a method value of Myt{tb}.Near. tb may be a *testing.T or *testing.B value.
type Myt ¶
Myt is a wrapper around a testing.TB value which is usually a *testing.T or a *testing.B, arming it with short utility methods.
func (Myt) Deq ¶
Deq reports an error if !reflect.DeepEqual(exp, got), or an optional non-nil error is provided.
func (Myt) ExpDeq ¶
ExpDeq takes the expected value and returns a function which only takes the 'got' value and an optional error.
The following multiline code:
got, err := SomeFunc() Deq(exp, got, err)
Is equivalent to this single line:
ExpDeq(exp)(SomeFunc())
func (Myt) ExpEq ¶
ExpEq takes the expected value and returns a function which only takes the 'got' value and an optional error.
The following multiline code:
got, err := SomeFunc() Eq(exp, got, err)
Is equivalent to this single line:
ExpEq(exp)(SomeFunc())
func (Myt) ExpNear ¶
ExpNear takes the expected and epsilon values and returns a function which only takes the 'got' value and an optional error.
The following multiline code:
got, err := SomeFunc() Near(exp, got, eps, err)
Is equivalent to this single line:
ExpNear(exp, eps)(SomeFunc())
func (Myt) ExpNeq ¶
ExpNeq takes one value and returns a function which takes only the 2nd value and an optional error.
The following multiline code:
v2, err := SomeFunc() Neq(v1, v2, err)
Is equivalent to this single line:
ExpNeq(v1)(SomeFunc())