assert

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 3 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.DeepEqual(t, actual, expect)

  // assert inequality
  assert.NotDeepEqual(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.

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

  // The following line will set the test result to fail and stop the execution
  assert.DeepEqualNow(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.DeepEqual(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.DeepEqual(actual, expect)

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

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 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 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) 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) 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) 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