Extensible, type-safe, fluent assertion Go library.
Please ⭐ Star
this repository if you find it valuable and worth maintaining.
The fluent API makes the assertion code easier to read and write (more).
The generics (type parameters) make the usage type-safe.
The library is extensible.
package test
import (
"testing"
"github.com/pellared/fluentassert/f"
)
type A struct {
Str string
Bool bool
Slice []int
}
func Foo() (A, error) {
return A{Str: "wrong", Slice: []int{1, 4}}, nil
}
func TestFoo(t *testing.T) {
got, err := Foo()
f.Obj(err).Zero().Require(t, "should be no error") // uses t.Fatal, stops execution if fails
f.Obj(got).DeepEqual(
A{Str: "string", Bool: true, Slice: []int{1, 2}},
).Assert(t) // uses t.Error, continues execution if fails
}
$ go test
--- FAIL: TestFoo (0.00s)
foo_test.go:24:
mismatch (-want +got):
test.A{
- Str: "string",
+ Str: "wrong",
- Bool: true,
+ Bool: false,
Slice: []int{
1,
- 2,
+ 4,
},
}
You can take advantage of the f.FailureMessage
and f.Fluent*
types
to create your own fluent assertions.
For reference, take a look at the implementation of existing fluent assertions in this repository (for example comparable.go).
Minimal supported Go version is 1.18.
Feel free to create an issue or propose a pull request.
Run ./goyek.sh
(Bash) or .\goyek.ps1
(PowerShell)
to execute the build pipeline.
The repository contains confiugration for Visual Studio Code.
fluentassert is licensed under the terms of the MIT license.
github.com/google/go-cmp
(license: BSD-3-Clause)
is the only third-party dependency.