Skip to content

icza/mighty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

About

Lightweight extension to Go's testing package.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Languages