https://github.com/icza/mighty
Lightweight extension to Go's testing package.
https://github.com/icza/mighty
library lightweight-extension testing
Last synced: about 1 year ago
JSON representation
Lightweight extension to Go's testing package.
- Host: GitHub
- URL: https://github.com/icza/mighty
- Owner: icza
- License: apache-2.0
- Created: 2016-09-08T11:08:52.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-03-30T13:32:00.000Z (about 3 years ago)
- Last Synced: 2025-03-24T10:45:47.270Z (over 1 year ago)
- Topics: library, lightweight-extension, testing
- Language: Go
- Homepage:
- Size: 33.2 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# mighty

[](https://pkg.go.dev/github.com/icza/mighty)
[](https://goreportcard.com/report/github.com/icza/mighty)
[](https://codecov.io/gh/icza/mighty)
Package `mighty` is a lightweight extension to Go's [testing](https://golang.org/pkg/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](https://golang.org/ref/spec#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:
- [github.com/icza/bitio](https://github.com/icza/bitio)
- [github.com/icza/session](https://github.com/icza/session)
- [github.com/icza/kvcache](https://github.com/icza/kvcache)