Categorygithub.com/hedzr/assert
modulepackage
0.1.3
Repository: https://github.com/hedzr/assert.git
Documentation: pkg.go.dev

# README

assert

Go GitHub tag (latest SemVer) GoDoc FOSSA Status Go Report Card Coverage Status

assert provides a set of assertion helpers for unit/bench testing in golang.

assert is inspired by these projects:

improvements

  • Can be used with both unit test and bench test.
  • Most of conventiional assertions:
    • Equal, NotEqual, EqualTrue, EqualFalse
    • Nil, NotNil
    • Error, NoError
    • PanicMatches: test for the function which might throw a panic
    • Match, NotMatch: compares a value with regexp test
  • Fresh coding in go 1.13~1.15 and later.

Short guide

package some_test

import (
	"github.com/hedzr/assert"
	"testing"
)

type Person struct {
	Name string
	Age  int
}

func TestEqual(t *testing.T) {
	expected := []*Person{{"Alec", 20}, {"Bob", 21}, {"Sally", 22}}
	actual := []*Person{{"Alex", 20}, {"Bob", 22}, {"Sally", 22}}
	assert.NotEqual(t, expected, actual)

	assert.Equal(t, actual, actual)
}

func TestEqualTrue(t *testing.T) {
	assert.EqualTrue(t, true)
	assert.EqualFalse(t, false)
}

LICENSE

MIT

# Functions

DiffValues compares 'a' and 'b' and return its differences as a text string with terminal escaped sequences.
DiffValuesDefault compares 'a' and 'b' and return its differences as a text string with terminal escaped sequences.
Equal validates that 'actual' is equal to 'expect' and throws an error with line number.
EqualFalse validates that 'actual' is false.
EqualSkip validates that 'actual' is equal to 'expect' and throws an error with line number but the skip variable tells EqualSkip how far back on the stack to report the error.
EqualTrue validates that 'actual' is true.
Error asserts that a function returned an error (i.e.
Match validates that value matches the regex, either string or *regex and throws an error with line number.
MatchSkip validates that value matches the regex, either string or *regex and throws an error with line number but the skip variable tells MatchRegexSkip how far back on the stack to report the error.
Nil asserts that the specified object is nil.
NilSkip asserts that the specified object is nil.
NoError asserts that a function returned no error (i.e.
NotEqual validates that val1 is not equal val2 and throws an error with line number.
NotEqualSkip validates that val1 is not equal to val2 and throws an error with line number but the skip variable tells NotEqualSkip how far back on the stack to report the error.
NotMatch validates that value matches the regex, either string or *regex and throws an error with line number.
NotMatchSkip validates that value matches the regex, either string or *regex and throws an error with line number but the skip variable tells NotMatchRegexSkip how far back on the stack to report the error.
NotNil asserts that the specified object is not nil.
NotNilSkip asserts that the specified object is not nil.
PanicMatches validates that the panic output of running fn matches the supplied string.
PanicMatchesSkip validates that the panic output of running fn matches the supplied string but the skip variable tells PanicMatchesSkip how far back on the stack to report the error.

# Constants

AppName const.
Version const.
VersionInt const.