assert

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 4 Imported by: 0

README

Assert

Go

🍯 Requirement Go 1.18+ for Generics, lower Go version please use 0.x

Extends stretchr/testify/assert for add more useful methods.

  • assert.Equal - asserts values equal, but ignore type.
  • assert.EqualHTML - asserts HTML equal, ignore spaces.

Installation

go get github.com/longbridgeapp/assert

Usage

package some_test

import (
  "github.com/longbridgeapp/assert"
)

func TestSomeMethod(t *testing.T) {
	assert.EqualHTML(t, "<p>Hello world</p><p>This is next line</p>", `
		<p>Hello world</p>
		<p>This is next line</p>
	`)
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[A any, B any](t TestingT, s A, contains B, msgAndArgs ...any) bool

Contains asserts that the specified string, list(array, slice...) or map contains the specified substring or element.

assert.Contains(t, "Hello World", "World")
assert.Contains(t, ["Hello", "World"], "World")
assert.Contains(t, {"Hello": "World"}, "Hello")

func ElementsMatch

func ElementsMatch[T any](t TestingT, listA, listB T, msgAndArgs ...any) (ok bool)

ElementsMatch asserts that the specified listA(array, slice...) is equal to specified listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, the number of appearances of each of them in both lists should match.

assert.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2])

func Empty added in v1.1.0

func Empty(t TestingT, object any, msgAndArgs ...any) bool

Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.

assert.Empty(t, "")

func Equal

func Equal[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool

Equal asserts two values are equal, this will ignore type. (int8, int32, int64, int, uint ...)

assert.Equal(t, 123, 123) // ok
assert.Equal(t, uint32(123), int64(123)) // ok
assert.Equal(t, 123, "123") // fail

This method only detects whether the values are equal (understand by your brain). If you want to strictly check the type, please use:

assert.StrictEqual(t, 123, int64(123))

func EqualError

func EqualError(t TestingT, theError error, errString string, msgAndArgs ...any) bool

EqualError asserts that a function returned an error (i.e. not `nil`) and that it is equal to the provided error.

actualObj, err := SomeFunction()
assert.EqualError(t, err,  expectedErrorString)

func EqualHTML

func EqualHTML(t TestingT, exptected, actual string)

EqualHTML asserts two HTML equal, will ignore spaces / breaks between > <

assert.EqualHTML(t, "<p>Hello</p> <p>world<p>", "<p>Hello</p>    <p>world<p>") // ok

func Error

func Error(t TestingT, err error, msgAndArgs ...any) bool

Error asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if assert.Error(t, err) {
	   assert.Equal(t, expectedObj, actualObj)
  }

func False

func False(t TestingT, value bool, msgAndArgs ...any) bool

False asserts that the specified value is false.

assert.False(t, myBool)

func Len added in v1.1.0

func Len(t TestingT, object any, length int, msgAndArgs ...any) bool

Len asserts that the specified object has specific length. Len also fails if the object has a type that len() not accept.

assert.Len(t, mySlice, 3)

func Nil

func Nil(t TestingT, object any, msgAndArgs ...any) bool

Nil asserts that the specified object is nil.

assert.Nil(t, err)

func NoError

func NoError(t TestingT, err error, msgAndArgs ...any) bool

NoError asserts that a function returned no error (i.e. `nil`).

  actualObj, err := SomeFunction()
  if assert.NoError(t, err) {
	   assert.Equal(t, expectedObj, actualObj)
  }

func NotContains

func NotContains[A any, B any](t TestingT, s A, contains B, msgAndArgs ...any) bool

NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the specified substring or element.

assert.NotContains(t, "Hello World", "Earth")
assert.NotContains(t, ["Hello", "World"], "Earth")
assert.NotContains(t, {"Hello": "World"}, "Earth")

func NotEqual

func NotEqual[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool

NotEqual asserts value not equal, ignore type.

assert.NotEqual(t, 12, 13) // ok
assert.NotEqual(t, 12, int32(12)) // fail
assert.NotEqual(t, 12, int32(13)) // ok

func NotNil added in v1.1.0

func NotNil(t TestingT, object any, msgAndArgs ...any) bool

NotNil asserts that the specified object is not nil.

assert.NotNil(t, "hello")

func NotPanics

func NotPanics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...any) bool

NotPanics asserts that the code inside the specified PanicTestFunc panics.

assert.NotPanics(t, func(){ GoCrazy() })

func NotSame

func NotSame[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool

NotSame asserts that two pointers do not reference the same object.

assert.NotSame(t, ptr1, ptr2)

Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func Panics

func Panics(t TestingT, f testifyAssert.PanicTestFunc, msgAndArgs ...any) bool

Panics asserts that the code inside the specified PanicTestFunc panics.

assert.Panics(t, func(){ GoCrazy() })

func Same

func Same[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool

Same asserts that two pointers reference the same object.

assert.Same(t, ptr1, ptr2)

Both arguments must be pointer variables. Pointer variable sameness is determined based on the equality of both type and value.

func StrictEqual

func StrictEqual[A any, B any](t TestingT, expected A, actual B, msgAndArgs ...any) bool

StrictEqual strictly asserts two values and type are equal.

assert.StrictEqual(t, 123, 123)
assert.StrictEqual(t, 123, int64(123)) // fail

func True

func True(t TestingT, value bool, msgAndArgs ...any) bool

True asserts that the specified value is true.

assert.True(t, myBool)

func WithinDuration

func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...any) bool

WithinDuration asserts that the two times are within duration delta of each other.

assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second)

Types

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
}

Jump to

Keyboard shortcuts

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