mighty

package module
v0.0.0-...-c4b03a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

mighty

Build Status Go Reference Go Report Card codecov

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:

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

- https://github.com/icza/session

- https://github.com/icza/kvcache

Index

Constants

This section is empty.

Variables

View Source
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

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

func NearFunc(a, b, eps float64) bool

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:

  1. if a==b, result is true (eps will not be checked, may be NaN)
  2. Inf is near to Inf (even if eps=NaN; consequence of 1.)
  3. -Inf is near to -Inf (even if eps=NaN; consequence of 1.)
  4. NaN is not near to anything (not even to NaN)
  5. 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

type Func2FloatsFunc1FloatErr func(float64, float64) func(float64, ...error)

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

ExpNear returns a method value of Myt{tb}.ExpNear. tb may be a *testing.T or *testing.B value.

type Func3FloatsErr

type Func3FloatsErr func(float64, float64, float64, ...error)

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

type Myt struct {
	testing.TB
}

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

func (m Myt) Deq(exp, got interface{}, errs ...error)

Deq reports an error if !reflect.DeepEqual(exp, got), or an optional non-nil error is provided.

func (Myt) Eq

func (m Myt) Eq(exp, got interface{}, errs ...error)

Eq reports an error if exp != got, or an optional non-nil error is provided.

func (Myt) ExpDeq

func (m Myt) ExpDeq(exp interface{}) func(got interface{}, errs ...error)

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

func (m Myt) ExpEq(exp interface{}) func(got interface{}, errs ...error)

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

func (m Myt) ExpNear(exp, eps float64) func(got float64, errs ...error)

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

func (m Myt) ExpNeq(v1 interface{}) func(v2 interface{}, errs ...error)

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())

func (Myt) Near

func (m Myt) Near(exp, got, eps float64, errs ...error)

Near reports an error if the float64 exp is not "near" to got, or an optional non-nil error is provided. "near" is defined by the NearLogic function variable.

func (Myt) Neq

func (m Myt) Neq(v1, v2 interface{}, errs ...error)

Neq reports an error if exp == got, or an optional non-nil error is provided.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL