must

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2023 License: Unlicense Imports: 7 Imported by: 0

README

must

Assertion utilities for testing in Go

Write your test as usual.

func TestAdd(t *testing.T) {
	a := 1
	b := 2
	actual := Add(a, b)
	expected := 3

	if actual != expected {
		t.Errorf("need %d, got %d", expected, actual)
	}

	if actual <= a {
		t.Errorf("%d < %d is false", actual, a)
	}

	if actual <= b {
		t.Errorf("%d < %d is false", actual, b)
	}
}

Run the test with -v flag, you will see the output like bellow.

=== RUN   TestAdd
    example_test.go:14: need 3, got 2
    example_test.go:22: 2 < 2 is false
--- FAIL: TestAdd (0.00s)

Refactor by using golang must!

func TestAdd(t *testing.T) {
	a := 1
	b := 2
	actual := Add(a, b)
	expected := 3

	must := must.New(t)
	must.Equal(expected, actual)
	must.True(actual <= a)
	must.True(actual <= b)
}

Run the test with -v flag, you will see the output like bellow.

=== RUN   TestAdd
          example_test.go:16: need 3, got 2
          example_test.go:17: need true, got false
--- FAIL: TestAdd (0.00s)

Your test more clean and clear, now 👍

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal(t testing.TB, a, b any)

Equal check are a and b equal, if not it will be failed

func False

func False(t testing.TB, a bool)

False check is a false, if not it will be failed

func Nil

func Nil(t testing.TB, a any)

Nil check is a nil, if not it will be failed

func NotEqual

func NotEqual(t testing.TB, a, b any)

NotEqual reverse of Equal which is failed if a and b are equal

func NotNil

func NotNil(t testing.TB, a any)

NotNil reverse of Nil which is failed if a nil

func That

func That[T any](t testing.TB, value T) that[T]

That need testing.TB and value as parameters and "that" chainning instance will be returned with the value parameter as single assertable data

func True

func True(t testing.TB, a bool)

True check is a true, if not it will be failed

Types

type Must

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

Must contains single testing.TB instance and it will be reuse in same test case

func New

func New(t testing.TB) *Must

New need testing.TB as parameter and Must instance will be returned

func (Must) Equal

func (m Must) Equal(a, b any)

Equal check are a and b equal, if not it will be failed

func (Must) False

func (m Must) False(a bool)

False check is a false, if not it will be failed

func (Must) Nil

func (m Must) Nil(a any)

Nil check is a nil, if not it will be failed

func (Must) NotEqual

func (m Must) NotEqual(a, b any)

NotEqual reverse of Equal which is failed if a and b are equal

func (Must) NotNil

func (m Must) NotNil(a any)

NotNil reverse of Nil which is failed if a nil

func (Must) True

func (m Must) True(a bool)

True check is a true, if not it will be failed

Jump to

Keyboard shortcuts

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