assert

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 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:

func TestExample(t *testing.T) {
  // assert equality
  assert.DeepEqual(t, actual, expect)

  // assert inequality
  assert.NotDeepEqual(t, actual, expect)
}

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

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 deeply equality between actual and expect parameters.

func NotDeepEqual

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

NotDeepEqual tests deeply inequality between actual and expected parameters.

func NotPanic

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

NotPanic asserts that the function fn does not panic.

func Panic

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

Panic expects the function fn to panic.

Types

type Assertion

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

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 deeply equality between actual and expect parameters.

func (*Assertion) NotDeepEqual

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

NotDeepEqual tests deeply inequality between actual and expected parameters.

func (*Assertion) NotPanic

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

NotPanic asserts that the function fn does not panic.

func (*Assertion) Panic

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

Panic expects the function fn to panic.

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.

Jump to

Keyboard shortcuts

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