assert

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2023 License: MIT Imports: 4 Imported by: 0

README

Assertion for Golang

test codecov Version Badge License Badge Go Reference

A collection of Golang assertion functions for verifying invariants.

Installation

To install this library, just use go get command like the following line:

go get -u github.com/ghosind/go-assert

Getting Started

This library provided assertion functions to verify the equality of values, or assert for nil:

func TestExample(t *testing.T) {
  // var actual
  // var expect

  // assert equality
  assert.Equal(t, actual, expect)

  // assert inequality
  assert.NotEqual(t, actual, expect)

  // var object

  // assert for nil
  assert.Nil(t, object)

  // assert for not nil
  assert.NotNil(t, object)
}

It also provided assertion functions to verify a function will panic or not:

func TestPanic(t *testing.T) {
  // assert panic
  assert.Panic(t, func () {
    // do something

    panic()
  })

  // assert no panic
  assert.NotPanic(t, func () {
    // do something

    // panic()
  })
}

For every assertion functions, it also provided XXXNow functions to stop the execution if the test is failed.

func TestExample(t *testing.T) {
  // var actual
  // var expect

  // The following line will set the test result to fail and stop the execution
  assert.EqualNow(t, actual, expect)

  // The following lines will never execute if they are not deep equal.
  // ...
}

Every assertion will not terminate the testing workflow. However, they'll return an error if the verification failed, and you can check the return value to get the verification result.

func TestExample(t *testing.T) {
  if err := assert.Equal(t, actual, expect); err != nil {
    // terminate test
    t.Fail()
  }
}

If you need to assert many times, you can also create an Assertion instance:

func TestExample(t *testing.T) {
  assertion := assert.New(t)

  // test equality
  assertion.Equal(actual, expect)

  // Test inequality
  assertion.NotEqual(actual, expect)
}

Available Assertions

License

This project was published under the MIT license, you can see LICENSE file to get more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual

func DeepEqual(t *testing.T, actual, expect any, message ...string) error

DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to fail if they are not deeply equal, and it doesn't stop the execution.

func DeepEqualNow added in v0.1.2

func DeepEqualNow(t *testing.T, actual, expect any, message ...string) error

DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the execution if they are not deeply equal.

func Equal added in v0.1.5

func Equal(t *testing.T, actual, expect any, message ...string) error

Equal tests the equality between actual and expect parameters. It'll set the result to fail if they are not equal, and it doesn't stop the execution.

func EqualNow added in v0.1.5

func EqualNow(t *testing.T, actual, expect any, message ...string) error

EqualNow tests the equality between actual and expect parameters, and it'll stop the execution if they are not equal.

func Match added in v0.1.5

func Match(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error

Match tests whether the string matches the regular expression or not.

func MatchNow added in v0.1.5

func MatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error

MatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if it does not match.

func MatchString added in v0.1.5

func MatchString(t *testing.T, val, pattern string, message ...string) error

MatchString will compile the pattern and test whether the string matches the regular expression or not. It will panic if the pattern is not a valid regular expression.

func MatchStringNow added in v0.1.5

func MatchStringNow(t *testing.T, val, pattern string, message ...string) error

MatchStringNow will compile the pattern and test whether the string matches the regular expression or not. It will terminate the execution if it does not match, and it will panic if the pattern is not a valid regular expression.

func Nil added in v0.1.1

func Nil(t *testing.T, val any, message ...string) error

Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.

func NilNow added in v0.1.2

func NilNow(t *testing.T, val any, message ...string) error

NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.

This function will set the result to fail, and stop the execution if the value is not nil.

func NotDeepEqual

func NotDeepEqual(t *testing.T, actual, expect any, message ...string) error

NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the result to fail if they are deeply equal, but it doesn't stop the execution.

func NotDeepEqualNow added in v0.1.2

func NotDeepEqualNow(t *testing.T, actual, expect any, message ...string) error

NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop the execution if they are deeply equal.

func NotEqual added in v0.1.5

func NotEqual(t *testing.T, actual, expect any, message ...string) error

NotEqual tests the inequality between actual and expected parameters. It'll set the result to fail if they are equal, but it doesn't stop the execution.

func NotEqualNow added in v0.1.5

func NotEqualNow(t *testing.T, actual, expect any, message ...string) error

NotEqualNow tests the inequality between actual and expected parameters, and it'll stop the execution if they are equal.

func NotMatch added in v0.1.5

func NotMatch(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error

NotMatch tests whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern.

func NotMatchNow added in v0.1.5

func NotMatchNow(t *testing.T, val string, pattern *regexp.Regexp, message ...string) error

NotMatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if the string matches the pattern.

func NotMatchString added in v0.1.5

func NotMatchString(t *testing.T, val, pattern string, message ...string) error

MatchString will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will also panic if the pattern is not a valid regular expression.

func NotMatchStringNow added in v0.1.5

func NotMatchStringNow(t *testing.T, val, pattern string, message ...string) error

NotMatchStringNow will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will terminate the execution if the string matches the pattern, and it will panic if the pattern is not a valid regular expression.

func NotNil added in v0.1.1

func NotNil(t *testing.T, val any, message ...string) error

NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.

func NotNilNow added in v0.1.2

func NotNilNow(t *testing.T, val any, message ...string) error

NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.

This function will set the result to fail, and stop the execution if the value is nil.

func NotPanic

func NotPanic(t *testing.T, fn func(), message ...string) error

NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the function panic.

func NotPanicNow added in v0.1.2

func NotPanicNow(t *testing.T, fn func(), message ...string) error

NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the function panic, and it also stops the execution.

func NotTrue added in v0.1.4

func NotTrue(t *testing.T, val any, message ...string) error

NotTrue tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

func NotTrueNow added in v0.1.4

func NotTrueNow(t *testing.T, val any, message ...string) error

NotTrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

The function will stop the execution if the value is truthy.

func Panic

func Panic(t *testing.T, fn func(), message ...string) error

Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't panic.

func PanicNow added in v0.1.2

func PanicNow(t *testing.T, fn func(), message ...string) error

PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't panic, and stop the execution.

func True added in v0.1.4

func True(t *testing.T, val any, message ...string) error

True tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

func TrueNow added in v0.1.4

func TrueNow(t *testing.T, val any, message ...string) error

TrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

The function will stop the execution if the value is falsy.

Types

type Assertion

type Assertion struct {
	*testing.T
}

func New

func New(t *testing.T) *Assertion

New returns an assertion instance for verifying invariants.

func (*Assertion) DeepEqual

func (a *Assertion) DeepEqual(actual, expect any, message ...string) error

DeepEqual tests the deep equality between actual and expect parameters. It'll set the result to fail if they are not deeply equal, and it doesn't stop the execution.

func (*Assertion) DeepEqualNow added in v0.1.2

func (a *Assertion) DeepEqualNow(actual, expect any, message ...string) error

DeepEqualNow tests the deep equality between actual and expect parameters, and it'll stop the execution if they are not deeply equal.

func (*Assertion) Equal added in v0.1.5

func (a *Assertion) Equal(actual, expect any, message ...string) error

Equal tests the equality between actual and expect parameters. It'll set the result to fail if they are not equal, and it doesn't stop the execution.

func (*Assertion) EqualNow added in v0.1.5

func (a *Assertion) EqualNow(actual, expect any, message ...string) error

EqualNow tests the equality between actual and expect parameters, and it'll stop the execution if they are not equal.

func (*Assertion) Match added in v0.1.5

func (a *Assertion) Match(val string, pattern *regexp.Regexp, message ...string) error

Match tests whether the string matches the regular expression or not.

func (*Assertion) MatchNow added in v0.1.5

func (a *Assertion) MatchNow(val string, pattern *regexp.Regexp, message ...string) error

MatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if it does not match.

func (*Assertion) MatchString added in v0.1.5

func (a *Assertion) MatchString(val, pattern string, message ...string) error

MatchString will compile the pattern and test whether the string matches the regular expression or not. It will panic if the pattern is not a valid regular expression.

func (*Assertion) MatchStringNow added in v0.1.5

func (a *Assertion) MatchStringNow(val, pattern string, message ...string) error

MatchStringNow will compile the pattern and test whether the string matches the regular expression or not. It will terminate the execution if it does not match, and it will panic if the pattern is not a valid regular expression.

func (*Assertion) Nil added in v0.1.1

func (a *Assertion) Nil(val any, message ...string) error

Nil tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.

func (*Assertion) NilNow added in v0.1.2

func (a *Assertion) NilNow(val any, message ...string) error

NilNow tests whether a value is nil or not, and it'll fail when the value is not nil. It will always return false if the value is a bool, an integer, a floating number, a complex, or a string.

This function will set the result to fail, and stop the execution if the value is not nil.

func (*Assertion) NotDeepEqual

func (a *Assertion) NotDeepEqual(actual, expect any, message ...string) error

NotDeepEqual tests the deep inequality between actual and expected parameters. It'll set the result to fail if they are deeply equal, but it doesn't stop the execution.

func (*Assertion) NotDeepEqualNow added in v0.1.2

func (a *Assertion) NotDeepEqualNow(actual, expect any, message ...string) error

NotDeepEqualNow tests the deep inequality between actual and expected parameters, and it'll stop the execution if they are deeply equal.

func (*Assertion) NotEqual added in v0.1.5

func (a *Assertion) NotEqual(actual, expect any, message ...string) error

NotEqual tests the inequality between actual and expected parameters. It'll set the result to fail if they are equal, but it doesn't stop the execution.

func (*Assertion) NotEqualNow added in v0.1.5

func (a *Assertion) NotEqualNow(actual, expect any, message ...string) error

NotEqualNow tests the inequality between actual and expected parameters, and it'll stop the execution if they are equal.

func (*Assertion) NotMatch added in v0.1.5

func (a *Assertion) NotMatch(val string, pattern *regexp.Regexp, message ...string) error

NotMatch tests whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern.

func (*Assertion) NotMatchNow added in v0.1.5

func (a *Assertion) NotMatchNow(val string, pattern *regexp.Regexp, message ...string) error

NotMatchNow tests whether the string matches the regular expression or not, and it will terminate the execution if the string matches the pattern.

func (*Assertion) NotMatchString added in v0.1.5

func (a *Assertion) NotMatchString(val, pattern string, message ...string) error

MatchString will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will also panic if the pattern is not a valid regular expression.

func (*Assertion) NotMatchStringNow added in v0.1.5

func (a *Assertion) NotMatchStringNow(val, pattern string, message ...string) error

NotMatchStringNow will compile the pattern and test whether the string matches the regular expression or not, and it set the result to fail if the string matches the pattern. It will terminate the execution if the string matches the pattern, and it will panic if the pattern is not a valid regular expression.

func (*Assertion) NotNil added in v0.1.1

func (a *Assertion) NotNil(val any, message ...string) error

NotNil tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.

func (*Assertion) NotNilNow added in v0.1.2

func (a *Assertion) NotNilNow(val any, message ...string) error

NotNilNow tests whether a value is nil or not, and it'll fail when the value is nil. It will always return true if the value is a bool, an integer, a floating number, a complex, or a string.

This function will set the result to fail, and stop the execution if the value is nil.

func (*Assertion) NotPanic

func (a *Assertion) NotPanic(fn func(), message ...string) error

NotPanic asserts that the function fn does not panic, and it'll set the result to fail if the function panic.

func (*Assertion) NotPanicNow added in v0.1.2

func (a *Assertion) NotPanicNow(fn func(), message ...string) error

NotPanicNow asserts that the function fn does not panic. It'll set the result to fail if the function panic, and it also stops the execution.

func (*Assertion) NotTrue added in v0.1.4

func (a *Assertion) NotTrue(val any, message ...string) error

NotTrue tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

func (*Assertion) NotTrueNow added in v0.1.4

func (a *Assertion) NotTrueNow(val any, message ...string) error

NotTrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a truthy value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

The function will stop the execution if the value is truthy.

func (*Assertion) Panic

func (a *Assertion) Panic(fn func(), message ...string) error

Panic expects the function fn to panic, and it'll set the result to fail if the function doesn't panic.

func (*Assertion) PanicNow added in v0.1.2

func (a *Assertion) PanicNow(fn func(), message ...string) error

PanicNow expects the function fn to panic. It'll set the result to fail if the function doesn't panic, and stop the execution.

func (*Assertion) Run added in v0.1.1

func (assertion *Assertion) Run(name string, f func(a *Assertion)) bool

Run runs f as a subtest of a called name. It runs f in a separate goroutine and blocks until f returns or calls a.Parallel to become a parallel test. Run reports whether f succeeded (or at least did not fail before calling t.Parallel).

Run may be called simultaneously from multiple goroutines, but all such calls must return before the outer test function for a returns.

func (*Assertion) True added in v0.1.4

func (a *Assertion) True(val any, message ...string) error

True tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

func (*Assertion) TrueNow added in v0.1.4

func (a *Assertion) TrueNow(val any, message ...string) error

TrueNow tests whether a value is truthy or not. It'll set the result to fail if the value is a false value. For most types of value, a falsy value is the zero value for its type. For a slice, a truthy value should not be nil, and its length must be greater than 0. For nil, the value is always falsy.

The function will stop the execution if the value is falsy.

type AssertionError

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

AssertionError indicates the failure of an assertion.

func (AssertionError) Error

func (err AssertionError) Error() string

Error returns the message of the error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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