actually

package module
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 9 Imported by: 0

README

Actually

actually report card Go Reference

Yet another testing library, actually.

  • Builder interface to make test code obvious
  • Consistent method name to reduce things you have to remember
  • Specific fail report to save your time

Usage

package main // https://go.dev/play/p/d57qXq3q6dl

import (
    "testing"
    a "github.com/bayashi/actually"
)

func Test(t *testing.T) {
    love, err := getLove()

    // Assert 1 object
    a.Got(err).NoError(t)
    a.Got(love).True(t)

    // Assert 2 objects
    heart := &love
    body  := heart
    a.Got(heart).Expect(body).SamePointer(t)
}

func getLove() (bool, error) {
    return true, nil
}

Assertions

For 1 object
  • True, False, Nil, NotNil, NoError
For 2 objects
  • Same, SameConvertibleNumber, SamePointer, SameType
  • Cmp, CmpAllowUnexported, (CmpOpt)
  • NotSame, NotSameConvertibleNumber, NotSamePointer, NotSameType
For panic
  • Panic, PanicMessage, NoPanic
For string value by regexp
  • Match, NotMatch
For length of an object
  • Len

Here is a Wiki of full API documentation.


Fail reports

actually will help you with evident fail report:

builder_test.go:133:
            Test name:      TestTree
            Trace:          /path/to/src/github.com/bayashi/goverview/builder_test.go:133
            Fail reason:    Not same
            Expected:       Dump: "\n┌ 001/\n├── .gitignore\n├── LICENSE: License MIT\n├── go.mod: go 1.18\n└───+ main.go: main\n      Func: X\n      const: X\n"
            Actually got:   Dump: "\n┌ 001/\n├── .gitignore\n├── LICENSE: License MIT\n├── go.mod: go 1.19\n└──* main.go: main\n      Func: X\n      Const: X\n"
            Diff Details:   --- Expected
                            +++ Actually got
                            @@ -4,6 +4,6 @@
                             ├── LICENSE: License MIT
                            -├── go.mod: go 1.18
                            -└───+ main.go: main
                            +├── go.mod: go 1.19
                            +└──* main.go: main
                                   Func: X
                            -      const: X
                            +      Const: X
            Raw Expect:     ---
                            ┌ 001/
                            ├── .gitignore
                            ├── LICENSE: License MIT
                            ├── go.mod: go 1.18
                            └───+ main.go: main
                                  Func: X
                                  const: X
                            ---
            Raw Got:        ---
                            ┌ 001/
                            ├── .gitignore
                            ├── LICENSE: License MIT
                            ├── go.mod: go 1.19
                            └──* main.go: main
                                  Func: X
                                  Const: X

actually has the Debug("label", any_variable) method to show additional data only in fail report.

Like below, src variable will be dumped nicely with Got value res only on fail.

res := someFunc(src)
actually.Got(res).Debug("src", src).True(t)

See more details in a Wiki.


Installation

go get github.com/bayashi/actually

License

MIT License

Author

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

See Also

Special Thanks

Inspired by:

Documentation

Overview

Yet another pithy testing framework `actually`

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Actual added in v0.17.0

func Actual(g any) *testingA

Actual is an alias of Got.

func Diff added in v0.12.0

func Diff(a any, b any) string

Diff is a helper function to get a diff string of 2 objects for debugging

func Dump added in v0.23.0

func Dump(a any) string

Dump is a helper function to get a dumped string of an object for debugging

func Expect

func Expect(e any) *testingA

Expect sets the value you expect to be the same as the one you got. Expect creates *testingA and returns it.

func Expectf added in v0.26.0

func Expectf(format string, e ...any) *testingA

Expectf sets the formatted string value you expect to be the same as the one you got. Expectf creates *testingA and returns it.

func Fail added in v0.21.0

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

Fail is to show decorated fail report. (Actual shortcut to witness.Fail)

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

func FailNotNowOn added in v0.13.0

func FailNotNowOn(t *testing.T)

FailNotNowOn function turns an ENV flag off to stop further test execution immediately if one test fails. If you want to turn the ENV flag on, then you should call `FailNowOn`.

func Test(t *testing.T) {
	// turn on to fail right now
	actually.FailNowOn(t)
	actually.Got(something).Nil(t)                    // Fail Now
	actually.Got(something).Expect(something).Same(t) // Fail Now

	// turn off
	actually.FailNotNowOn(t)
	actually.Got(something).Nil(t)                    // NOT Fail Now
	actually.Got(something).Expect(something).Same(t) // NOT Fail Now

	// Fail Now by FailNow() in the chain
	actually.Got(something).FailNow().Nil(t)          // Fail Now

	// Again, turn on to fail right now
	actually.FailNowOn(t)
	actually.Got(something).Nil(t)                    // Fail Now
}

This switch is enabled within the test. Not only during function.

func FailNow added in v0.21.0

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

FailNow is to show decorated fail report by t.Fatal. (Actual shortcut to witness.FailNow)

if g != e {
	actually.FailNow(t, "Not same", g, e)
}

func FailNowOn added in v0.13.0

func FailNowOn(t *testing.T)

FailNowOn function turns an ENV flag on to stop further test execution immediately if one test fails. This switch is enabled within the test. Not only during function.

func Test(t *testing.T) {
	actually.FailNowOn(t)
	actually.Got(something).Nil(t)                    // Fail Now
	actually.Got(something).Expect(something).Same(t) // Fail Now
}

Warning: Do not use FailNowOn along with t.Parallel.

func Fatal added in v0.21.0

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

Fatal is an alias of FailNow

func FatalOn added in v0.17.0

func FatalOn(t *testing.T)

FatalOn is an alias of FailNowOn.

func Got

func Got(g any) *testingA

Got sets the value you actually got. Got() creates *testingA and returns it.

func GotError added in v0.13.0

func GotError(g error) *testingA

GotError sets the error value you actually got. GotError creates `*testingA` and returns it.

func Skip added in v0.3.0

func Skip(t *testing.T, skipReasons ...any)

Skip provides shorthand to skip further tests within the same function for `-short` option.

func Test(t *testing.T) {
	Got(1).NotNil(t) // Run
	Skip(t)
	Got(2).NotNil(t) // Skip
	Got(3).NotNil(t) // Skip Also
}

func Want added in v0.17.0

func Want(e any) *testingA

Want is an alias of Expect.

Types

This section is empty.

Jump to

Keyboard shortcuts

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