Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.
/ verify Public archive

Extensible, type-safe, fluent assertion Go library. Do NOT use it (sic!).

License

Notifications You must be signed in to change notification settings

fluentassert/verify

Repository files navigation

FluentAssert

Extensible, type-safe, fluent assertion Go library.

Go Reference Keep a Changelog GitHub Release go.mod LICENSE

Build Status Go Report Card Codecov

Please ⭐ Star this repository if you find it valuable and worth maintaining.

Description

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.

Quick start

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,
                },
          }

Custom assertions

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

Supported Go versions

Minimal supported Go version is 1.18.

Contributing

Feel free to create an issue or propose a pull request.

Developing

Run ./goyek.sh (Bash) or .\goyek.ps1 (PowerShell) to execute the build pipeline.

The repository contains confiugration for Visual Studio Code.

License

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.