fail

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2020 License: GPL-3.0 Imports: 3 Imported by: 0

README

go-fail

Latest release Build status Go Report Card Documentation

Fail is a Go package designed to easily create human-readable failure reports for different kinds of failed tests. It is not a testing or assertion framework.

go get github.com/roeldev/go-fail
import "github.com/roeldev/go-fail"

Show differences

Below examples creates a diff report that shows the difference between the actual and expected values.

func TestAnything(t *testing.T) {
    have := Anything()
    want := "expected value"

    if have != want {
        t.Error(fail.Diff{
            Func: "Anything",
            Msg: "this is a meaningful message",
            Have: have,
            Want: want,
        })
    }
}

The output result is something similar like:

Anything() this is a meaningful message
string(
- 	"actual value",
+ 	"expected value",
  )

Unexpected error

func TestDoSomething(t *testing.T) {
    if err := DoSomething(); err != nil {
        // display the error that should not occur
        t.Error(fail.Err{
            Func: "DoSomething",
            Err: err,
        })
    }
}

The output result is something similar like:

DoSomething() unexpected error:
some error occurred while doing important stuff...

Documentation

Additional detailed documentation is available at go.dev

Created with

License

GPL-3.0+ © 2019 Roel Schut

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff struct {
	Func string
	Msg  string
	Have interface{}
	Want interface{}
	Opts []cmp.Option
}

Diff is used to create a failure report for any non-matching values.

func (Diff) String

func (fd Diff) String() string

String returns a human-readable report of the differences between the actual (have) and expected (want) values. It uses `cmp.Diff()` to do so. Options added to the Opts fields are passed to `cmp.Diff()`.

type Err

type Err struct {
	Func string
	Err  error
}

Err is used to create a failure report for any unexpected encountered errors.

func (Err) String

func (fe Err) String() string

String returns a human-readable failure report of the unexpected error.

type Msg

type Msg struct {
	Func string
	Msg  string
}

Msg is used to create a simple failure report with only a message.

func (Msg) String

func (msg Msg) String() string

String returns the human-readable failure report.

type Panic added in v0.2.0

type Panic struct {
	Func  string
	Panic interface{}
}

Panic is used to create a failure report for any unexpected encountered panics.

func (Panic) String added in v0.2.0

func (fp Panic) String() string

String returns a human-readable failure report of the unexpected panic.

type RetVal

type RetVal struct {
	Func string
	Msg  string
	Have []interface{}
	Want []interface{}
	Opts []cmp.Option
}

RetVal is used to create a failure report of non-matching values from a function.

func (RetVal) String

func (rv RetVal) String() string

String returns a human-readable report of the differences between the returned value (have) and expected (want) values. It uses `cmp.Diff()` to do so. Options added to the Opts fields are passed to `cmp.Diff()`.

Jump to

Keyboard shortcuts

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