goassert

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2022 License: MIT Imports: 3 Imported by: 0

README

goassert

goassert is a simple assertion library for go/golang. It does not try to do anything fancy and keeps things as simple as possible. Its primary goal is to make assertions as short and simple as possible. It also takes advantage of go generics as much as possible to be able to handle as many types as possible with as fewest assertion methods as possible

Installation

go get -t -u github.com/golanglibs/goassert@latest

Usage

package yourpackage

import (
	"testing",

	"github.com/golanglibs/goassert"
)

func Test_1Plus1ShouldEqual2(t *testing.T) {
	actual := 1 + 1
	expected := 2

	goassert.Equal(t, expected, actual)
	// on assertion error
	// --- FAIL: Test_1Plus1ShouldEqual2 (0.00s)
	// module_test.go: 11: Expected 2. Actual: 1
}

Available Assertions

Truth
  • True - asserts the value is true
  • False - asserts the value is false
Equality
  • Equal - asserts two values are equal. Values must be comparable
  • NotEqual - asserts two values are not equal. Values must be comparable
  • DeepEqual - asserts two values are deeply equal. Internally uses reflect.DeepEqual. Can be used to assert equality of arrays, slices and maps
  • NotDeepEqual - asserts two values are deeply not equal. Internally uses reflect.DeepEqual. Can be used to assert inequality of arrays, slices and maps
  • Nil - asserts the value is nil
  • NotNil - asserts the value is not nil
  • SimilarSlice - asserts two slices have the same values in any order. The elements must be comparable
  • NotSimilarSlice - asserts two slices do not have the same values. The elements must be comparable
Collection
  • EmptySlice - asserts the slice is empty. The assertion will fail if the slice is nil
  • NotEmptySlice - asserts the slice is not nil or empty
  • SliceLength - asserts the slice has the specified length
  • SliceContains - asserts the slice contains the specified value. The value must be comparable
  • SliceNotContains - asserts the slice does not contain the specified value. The value must be comparable
  • EmptyMap - asserts the map is empty. The assertion will fail if the map is nil
  • NotEmptyMap - asserts the map is not nil or empty
  • MapLength - asserts the map has the specified length
  • MapContainsKey - asserts the map contains the specified key. Key must be comparable
  • MapNotContainsKey - asserts the map does not contain the specified key. Key must be comparable
  • MapContains - asserts the map contains the specified key-value pair. Key and value must be comparable
  • MapNotContains - asserts the map does not contain the specified key-value pair. Key and value must be comparable
Panic
  • Panic - asserts given function panics
  • NotPanic - asserts given function does not panic
  • PanicWithError - asserts given function panics with the specified error
  • NotPanicWithError - asserts given functoin does not panic with the specified error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual added in v0.5.0

func DeepEqual[T any](t testing.TB, expected T, actual T)

Asserts that the two given valus are deeply equal. Internally uses reflect.DeepEqual. The equality of arrays, slices and maps can be asserted with this method

func EmptyMap

func EmptyMap[K comparable, V any](t testing.TB, m map[K]V)

Asserts that the given map is empty. The assertion will fail if the given map is nil

func EmptySlice

func EmptySlice[T any](t testing.TB, s []T)

Asserts that the given slice is empty. The assertion fails if the given slice is nil

func Equal

func Equal[K comparable](t testing.TB, expected K, actual K)

Asserts that the two given values are equal. The given values must be [comparable]

func False

func False(t testing.TB, assertion bool)

Asserts that the given value is false

func MapContains

func MapContains[K, V comparable](t testing.TB, m map[K]V, k K, v V)

Asserts that the given map contains the given key-value pair. The key and value must be [comparable]

func MapContainsKey added in v0.4.0

func MapContainsKey[K comparable, V any](t testing.TB, m map[K]V, k K)

Asserts that the given map contains the given key. The key must be [comparable]

func MapLength

func MapLength[K comparable, V any](t testing.TB, m map[K]V, expectedLength int)

Asserts that the given map has length equal to the specified length

func MapNotContains

func MapNotContains[K, V comparable](t testing.TB, m map[K]V, k K, v V)

Asserts that the given map does not contain the given key-value pair. The key and value must be [comparable]

func MapNotContainsKey added in v0.4.0

func MapNotContainsKey[K comparable, V any](t testing.TB, m map[K]V, k K)

Asserts that the given map does not contain the given key. The key must be [comparable]

func Nil

func Nil(t testing.TB, actual interface{})

Asserts that the given value is nil

func NotDeepEqual added in v0.5.0

func NotDeepEqual[T any](t testing.TB, expected T, actual T)

Asserts that the two given valus are not deeply equal. Internally uses reflect.DeepEqual. The inequality of arrays, slices and maps can be asserted with this method

func NotEmptyMap

func NotEmptyMap[K comparable, V any](t testing.TB, m map[K]V)

Asserts that the given map is not nil or empty

func NotEmptySlice

func NotEmptySlice[T any](t testing.TB, s []T)

Asserts that the given slice is not nil or empty

func NotEqual

func NotEqual[K comparable](t testing.TB, expected K, actual K)

Asserts that the two given values are not equal. The given values must be [comparable]

func NotNil

func NotNil(t testing.TB, actual interface{})

Asserts that the given value is not nil

func NotPanic

func NotPanic(t testing.TB, underTest func())

Asserts that the given function does not panic

func NotPanicWithError

func NotPanicWithError[T any](t testing.TB, expectedError T, underTest func())

Asserts that the given function does not panic with the specified error. The assertion succeeds if the given function does not panic or panics with a different error

func NotSimilarSlice

func NotSimilarSlice[T any](t testing.TB, expected []T, actual []T)

Asserts that the two given slices does not have the same values. The elements must be [comparable]

func Panic

func Panic(t testing.TB, underTest func())

Asserts that the given function panics

func PanicWithError

func PanicWithError[T any](t testing.TB, expectedError T, underTest func())

Asserts that the given function panics with the specified error. The actual error must be of the same type and value as the given error

func SimilarSlice

func SimilarSlice[T any](t testing.TB, expected []T, actual []T)

Asserts that the two given slices have the same values in any order. The elements must be [comparable]

func SliceContains

func SliceContains[K comparable](t testing.TB, s []K, element K)

Asserts that the given slice contains the given element. The element must be [comparable]

func SliceLength

func SliceLength[T any](t testing.TB, s []T, expectedLength int)

Asserts that the given slice has length equal to the specified expected length

func SliceNotContains

func SliceNotContains[K comparable](t testing.TB, s []K, element K)

Asserts that the given slice does not contain the given element. The element must be [comparable]

func True

func True(t testing.TB, assertion bool)

Asserts that the given value is true

Types

This section is empty.

Jump to

Keyboard shortcuts

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