witness

package module
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 7 Imported by: 1

README

witness

witness CI witness report card

witness is a test helper to make an evident report on a fail of your test.

Usage

Simple case.

package main

import (
    "testing"

    w "github.com/bayashi/witness"
)

func TestExample(t *testing.T) {
    g := "a\nb\nc"
    e := "a\nd\nc"

    if g != e {
        w.Fail(t, "Not same", g, e)
    }
}

below result will be shown:

Test name:      TestExample
Trace:          /home/usr/go/src/github.com/bayashi/witness/witness_test.go:14
Fail reason:    Not same
Type:           Expect:string, Got:string
Expected:       "a\nd\nc"
Actually got:   "a\nb\nc"

There is a builder interface to specify waht you report.

w.Got(g).Expect(e).Fail(t, "Not same")

There are switches to show more additional info:

w.Got(g).Expect(e).ShowAll().Fail(t, "Not same")

And then,

Test name:      TestExample
Trace:          /home/usr/go/src/github.com/bayashi/witness/witness_test.go:14
Fail reason:    Not same
Type:           Expect:string, Got:string
Expected:       "a\nd\nc"
Actually got:   "a\nb\nc"
Diff details:   --- Expected
                +++ Actually got
                @@ -1,3 +1,3 @@
                 a
                -d
                +b
                 c
Raw Expect:     ---
                a
                d
                c
                ---
Raw Got:        ---
                a
                b
                c
                ---

See witness-showcase for actual outputs on fail.

Also see Witness Package reference for more details.

Installation

go get github.com/bayashi/witness

License

MIT License

Author

Dai Okabayashi: https://github.com/bayashi

See Also

https://github.com/bayashi/actually

Special Thanks To

https://github.com/stretchr/testify

Documentation

Index

Constants

View Source
const ShowDiff, ShowRaw, NotShowDiff, NotShowRaw = true, true, false, false

You can write "witness.New(witness.ShowDiff, witness.NotShowRaw)" instead of raw boolean

Variables

This section is empty.

Functions

func Diff

func Diff(a any, b any) string

Diff is to get a diff string of 2 objects for debugging in test Two args should be same type. Otherwise, diff string will be a blank string.

func Dump

func Dump(v any) string

Dump is to get a dumped string by `spew.Sdump` for debugging in test

func Fail

func Fail(t *testing.T, reason string, got any, expect ...any)

Fail is shortcut method. These are same expression.

witness.Got(got).Fail(t, reason)
witness.Fail(t, reason, got)

Fail with 2 values cases are below

witness.Got(got).Expect(expect).Fail(t, reason)
witness.Fail(t, reason, got, expect)

func FailNow

func FailNow(t *testing.T, reason string, got any, expect ...any)

FailNow is shortcut method. There are same expression.

witness.Got(got).FailNow(t, reason)
witness.FailNow(t, reason, got)

FailNow with 2 values cases are below

witness.Got(got).Expect(expect).FailNow(t, reason)
witness.FailNow(t, reason, got, expect)

func Fatal added in v0.0.15

func Fatal(t *testing.T, reason string, got any, expect ...any)

Do fail with report and stop running test right now. The `Fatal` method is an alias of the `FailNow` method.

Types

type Witness

type Witness struct {
	// contains filtered or unexported fields
}

Witness is a context of the fail report

func Actual added in v0.0.15

func Actual(v any) *Witness

Set a value you actually got. The `Actual` method is an alias of the `Got` method.

func Debug added in v0.0.18

func Debug(label string, info ...any) *Witness

Set debug information to show on fail

func Expect

func Expect(v any) *Witness

Set a value you expect

func Got

func Got(v any) *Witness

Set a value you got

func Message added in v0.0.13

func Message(label string, msg string) *Witness

Set additional messages to show

func Name

func Name(n string) *Witness

Set test name

func Namef

func Namef(format string, a ...any) *Witness

Set test name by format

func New

func New(showFlag ...bool) *Witness

You don't need to call `New`. You can call `Got` or `Expect` directly without calling `New` like below.

witness.Got("abc").Fail(t, "somehow")

You should call `New` when you need to set options for several reports in order to avoid calling `ShowDiff` or `ShowRaw` for each report.

w := witness.New(witness.ShowDiff, witness.ShowRaw)
w.Got(123).Fail(t, "Not expected")
w.Got("c").Fail(t, "Expected d")

func Want added in v0.0.15

func Want(v any) *Witness

Set a value you want. The `Want` method is an alias of the `Expect` method.

func (*Witness) Actual added in v0.0.15

func (w *Witness) Actual(v any) *Witness

Set a value you actually got. The `Actual` method is an alias of the `Got` method.

func (*Witness) Debug added in v0.0.18

func (w *Witness) Debug(label string, info ...any) *Witness

Set debug information to show on fail

func (*Witness) Expect

func (w *Witness) Expect(v any) *Witness

Set a value you expect

func (*Witness) Fail

func (w *Witness) Fail(t *testing.T, reason string)

Do fail with report

func (*Witness) FailNow

func (w *Witness) FailNow(t *testing.T, reason string)

Do fail with report and stop running test right now

func (*Witness) Fatal added in v0.0.15

func (w *Witness) Fatal(t *testing.T, reason string)

Do fail with report and stop running test right now. The `Fatal` method is an alias of the `FailNow` method.

func (*Witness) Got

func (w *Witness) Got(v any) *Witness

Set a value you got

func (*Witness) Message

func (w *Witness) Message(label string, msg string) *Witness

Set additional messages to show

func (*Witness) Name

func (w *Witness) Name(n string) *Witness

Set test name

func (*Witness) Namef

func (w *Witness) Namef(format string, a ...any) *Witness

Set test name by format

func (*Witness) ShowAll added in v0.0.7

func (w *Witness) ShowAll() *Witness

Turn on flags for both showDiff and showRaw

func (*Witness) ShowDiff

func (w *Witness) ShowDiff() *Witness

Turn on a flag to show diff details

func (*Witness) ShowRaw

func (w *Witness) ShowRaw() *Witness

Turn on a flag to show raw values

func (*Witness) Want added in v0.0.15

func (w *Witness) Want(v any) *Witness

Set a value you want. The `Want` method is an alias of the `Expect` method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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