should

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Result

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

Result holds the result of a test condition.

func BeDeepZero

func BeDeepZero(t assert.Test, value any, msg ...any) Result

BeDeepZero is asserting that a given value is the zero value for its kind.

It uses reflection to determine whether the value is zero. For comparable values it is preferable to use the should.BeZero, as it performs simple comparison using the '==' operator.

Example:
	var err error
	should.BeDeepZero(t, err, "err should be zero value")

func BeEmpty

func BeEmpty[T any](t assert.Test, slice []T, msg ...any) Result

BeEmpty is asserting that the given slice holds no items.

Nil value is considered empty.

Example:
	should.BeEmpty(t, []string{}, "should have 0 items")

func BeEmptyString

func BeEmptyString[T ~string](t assert.Test, value T, msg ...any) Result

BeEmptyString is asserting that the given string holds no characters.

Example:
	should.BeEmptyString(t, "", "should have 0 characters")

func BeError

func BeError(t assert.Test, err, targetErr error, msg ...any) Result

BeError is asserting that the given error is a target error.

The errors are compared using errors.Is, so any wrapped errors will work just fine.

Example:
	should.BeError(t, baba(), ErrBabaIsNotYou, "operation baba should have failed with error 'ErrBabaIsNotYou'")

func BeErrorContaining

func BeErrorContaining(t assert.Test, err error, value string, msg ...any) Result

BeErrorContaining is asserting that the given error is not a nil error and its value contains the provided value.

Example:
	should.BeErrorContaining(t, baba(), "baba", "operation baba should have failed with an error containing 'baba'")

func BeErrorNotContaining

func BeErrorNotContaining(t assert.Test, err error, value string, msg ...any) Result

BeErrorNotContaining is asserting that the given error is not nil and its value does not contain the provided value.

Example:
	should.BeErrorNotContaining(t, baba(), "flag", "operation baba should have failed with an error not containing 'flag'")

func BeFalse

func BeFalse(t assert.Test, value bool, msg ...any) Result

BeFalse is asserting that the given value is not true.

Example:
	should.BeFalse(t, isActive, "user should not be active")

func BeGreater

func BeGreater[T assert.Ordered](t assert.Test, value, other T, msg ...any) Result

BeGreater is asserting that a given value is greater than another value.

Example:
	should.BeGreater(t, 10, 5, "10 should be greater than 5")

func BeGreaterBytes

func BeGreaterBytes(t assert.Test, value, other []byte, msg ...any) Result

BeGreaterBytes is asserting that a given slice of bytes is greater lexicographically than another slice of bytes.

Example:
	should.BeGreaterBytes(t, []byte("abc"), []byte("abb"), "first array should be greater lexicographically")

func BeGreaterOrEqual

func BeGreaterOrEqual[T assert.Ordered](t assert.Test, value, other T, msg ...any) Result

BeGreaterOrEqual is asserting that a given value is greater than or equal to another value.

Example:
	should.BeGreaterOrEqual(t, 5, 4, "5 should be greater or equal to 4")

func BeGreaterOrEqualBytes

func BeGreaterOrEqualBytes(t assert.Test, itemA, itemB []byte, msg ...any) Result

BeGreaterOrEqualBytes is asserting that a given slice of bytes is greater or equal lexicographically to another slice of bytes.

Example:
	should.BeGreaterOrEqualBytes(t, []byte("abc"), []byte("abb"), "first should be greater or equal lexicographically")

func BeInArray

func BeInArray(t assert.Test, array, item any, msg ...any) Result

BeInArray is asserting that the given array of items is containing an item.

Items are being compared using reflection by calling reflect.DeepEqual. array must be an array or a pointer to an array, otherwise the function panics.

Example:
	should.BeInArray(t, [...]string{"baba", "is", "you"}, "is", "should contain 'is'")

func BeInRange

func BeInRange[T assert.Ordered](t assert.Test, item, min, max T, msg ...any) Result

BeInRange is asserting that a given value fits inclusively within a certain range.

Example:
	should.BeInRange(t, value, 0, 100, "value should be within range 0-100")

func BeLesser

func BeLesser[T assert.Ordered](t assert.Test, value, other T, msg ...any) Result

BeLesser is asserting that a given value is lesser than another value.

Example:
	should.BeLesser(t, 5, 10, "5 should be lesser than 10")

func BeLesserBytes

func BeLesserBytes(t assert.Test, value, other []byte, msg ...any) Result

BeLesserBytes is asserting that a given slice of bytes is lesser lexicographically than another slice of bytes.

Example:
	should.BeLesserBytes(t, []byte("abb"), []byte("abc"), "first should be lesser lexicographically")

func BeLesserOrEqual

func BeLesserOrEqual[T assert.Ordered](t assert.Test, value, other T, msg ...any) Result

BeLesserOrEqual is asserting that a given value is lesser or equal to another value.

Example:
	should.BeLesserOrEqual(t, 5, 7, "5 should be lesser than or equal to 7")

func BeLesserOrEqualBytes

func BeLesserOrEqualBytes(t assert.Test, itemA, itemB []byte, msg ...any) Result

BeLesserOrEqualBytes is asserting that a given slice of bytes is lesser or equal lexicographically to another slice of bytes.

Example:
	should.BeLesserOrEqualBytes(t, babaData, otherData, "babaData should be lesser or equal lexicographically")

func BeLonger

func BeLonger[T any](t assert.Test, slice, other []T, msg ...any) Result

BeLonger is asserting that the given slice is of greater len than another slice.

Example:
	should.BeLonger(t, []string{"baba", "is", "you"}, []string{"flag", "is"}, "should have more items")

func BeLongerString

func BeLongerString[T ~string](t assert.Test, slice, other T, msg ...any) Result

BeLongerString is asserting that the given string is of greater len than another string.

Example:
	should.BeLongerString(t, "baba is you", "flag is", "should have more characters")

func BeNil

func BeNil(t assert.Test, value any, msg ...any) Result

BeNil is asserting that a given value is nil.

Example:
	should.BeNil(t, value, "value should be nil")

func BeNilError

func BeNilError(t assert.Test, err error, msg ...any) Result

BeNilError is asserting that the given error is nil.

Example:
	should.BeNilError(t, baba(), "operation baba should have passing without an error")

func BeOfGreaterLen

func BeOfGreaterLen[T any](t assert.Test, slice []T, thresholdLen int, msg ...any) Result

BeOfGreaterLen is asserting that the given slice is of greater len than a certain threshold.

Example:
	should.BeOfGreaterLen(t, []string{"baba", "is", "you"}, 2, "should have at least 3 items")

func BeOfGreaterOrEqualLen

func BeOfGreaterOrEqualLen[T any](t assert.Test, slice []T, thresholdLen int, msg ...any) Result

BeOfGreaterOrEqualLen is asserting that the given slice is of greater or equal len to a certain threshold.

Example:
	should.BeOfGreaterOrEqualLen(t, []string{"baba", "is", "you"}, 3, "should have 3 or more items")

func BeOfLen

func BeOfLen[T any](t assert.Test, slice []T, targetLen int, msg ...any) Result

BeOfLen is asserting that the given slice is of certain length.

Example:
	should.BeOfLen(t, []string{"baba", "is", "you"}, 3, "should have 3 items")

func BeOfLesserLen

func BeOfLesserLen[T any](t assert.Test, slice []T, thresholdLen int, msg ...any) Result

BeOfLesserLen is asserting that the given slice is of lesser len than a certain threshold.

Example:
	should.BeOfLesserLen(t, []string{"baba", "is", "you"}, 5, "should have less than 5 items")

func BeOfLesserOrEqualLen

func BeOfLesserOrEqualLen[T any](t assert.Test, slice []T, thresholdLen int, msg ...any) Result

BeOfLesserOrEqualLen is asserting that the given slice is of lesser or equal len to a certain threshold.

Example:
	should.BeOfLesserOrEqualLen(t, []string{"baba", "is", "you"}, 5, "should have less or equal to 5 items")

func BeOfSameLength

func BeOfSameLength[T any](t assert.Test, slice, other []T, msg ...any) Result

BeOfSameLength is asserting that the given slice is of equal len to another slice.

Example:
	should.BeOfSameLength(t, []string{"baba", "is", "you"}, []string{"flag", "is", "win"}, "should have equal len")

func BeShorter

func BeShorter[T any](t assert.Test, slice, other []T, msg ...any) Result

BeShorter is asserting that the given slice is of lesser len than another slice.

Example:
	should.BeShorter(t, []string{"baba", "is"}, []string{"flag", "is", "win"}, "should have fewer items")

func BeShorterString

func BeShorterString[T ~string](t assert.Test, value, other T, msg ...any) Result

BeShorterString is asserting that the given string is of lesser len than another string.

Example:
	should.BeShorterString(t, "baba is", "flag is win", "should have fewer characters")

func BeStringOfGreaterOrEqualLen

func BeStringOfGreaterOrEqualLen[T ~string](t assert.Test, value T, thresholdLen int, msg ...any) Result

BeStringOfGreaterOrEqualLen is asserting that the given string is of greater or equal len to a certain threshold.

Example:
	should.BeStringOfGreaterOrEqualLen(t, "baba", 4, "should have 4 or more characters")

func BeStringOfLen

func BeStringOfLen[T ~string](t assert.Test, value T, targetLen int, msg ...any) Result

BeStringOfLen is asserting that the given string is of certain length.

Example:
	should.BeStringOfLen(t, "baba is you", 11, "should have 11 characters")

func BeStringOfLenGreaterThan

func BeStringOfLenGreaterThan[T ~string](t assert.Test, value T, thresholdLen int, msg ...any) Result

BeStringOfLenGreaterThan is asserting that the given string is of greater len than a certain threshold.

Example:
	should.BeStringOfLenGreaterThan(t, "baba", 3, "should have at least 4 characters")

func BeStringOfLesserLen

func BeStringOfLesserLen[T ~string](t assert.Test, value T, thresholdLen int, msg ...any) Result

BeStringOfLesserLen is asserting that the given string is of lesser len than a certain threshold.

Example:
	should.BeStringOfLesserLen(t, "baba is you", 12, "should have less than 12 characters")

func BeStringOfLesserOrEqualLen

func BeStringOfLesserOrEqualLen[T ~string](t assert.Test, value T, thresholdLen int, msg ...any) Result

BeStringOfLesserOrEqualLen is asserting that the given string is of lesser or equal len to a certain threshold.

Example:
	should.BeStringOfLesserOrEqualLen(t, "baba is you", 11, "should have at most 11 characters")

func BeStringOfSameLength

func BeStringOfSameLength[T ~string](t assert.Test, value, other T, msg ...any) Result

BeStringOfSameLength is asserting that the given string is of equal len to another string.

Example:
	should.BeOfSameLength(t, "baba is you", "flag is win", "should have equal len")

func BeTrue

func BeTrue(t assert.Test, value bool, msg ...any) Result

BeTrue is asserting that the given value is true.

Example:
	should.BeTrue(t, isActive, "user should be active")

func BeZero

func BeZero[T comparable](t assert.Test, item T, msg ...any) Result

BeZero is asserting that a given value is the zero value for its kind.

Example:
	var num int
	should.BeZero(t, num, "num should be zero")

func Contain

func Contain[T comparable](t assert.Test, slice []T, item T, msg ...any) Result

Contain is asserting that the given slice is containing an item.

Items are being compared using the '==' operator. For slices of non-comparable items use should.DeepContain. It relies on reflection to perform comparison on the items' values using reflect.DeepEqual.

Example:
	should.Contain(t, []string{"baba", "is", "you"}, "is", "should contain 'is'")

func ContainKey

func ContainKey[K comparable, V any](t assert.Test, m map[K]V, key K, msg ...any) Result

ContainKey is asserting that the given map contains a certain key.

Example:
	should.ContainKey(t, map[string]any{"baba": "is you"}, "baba", "should contain key 'baba'")

func ContainString

func ContainString[T ~string](t assert.Test, value, substr T, msg ...any) Result

ContainString is asserting that the given value is containing a substring.

Example:
	should.ContainString(t, "baba is you", "baba", "should contain 'baba'")

func DeepContain

func DeepContain[T any](t assert.Test, slice []T, value T, msg ...any) Result

DeepContain is asserting that the given slice contains a deeply equal value.

Items are being compared using reflection by calling reflect.DeepEqual. For checking slices of items that are comparable it is preferable to use should.Contain which performs simple equality check using the '==' operator.

Example:
	should.DeepContain(t, []any{"baba", "is", 5}, "is", "should contain 'is'")

func DeepEqual

func DeepEqual[T any](t assert.Test, value, other T, msg ...any) Result

DeepEqual is asserting that the given values are deeply equal.

Items are being compared using reflect.DeepEqual. For items that are comparable is it preferable to use should.Equal, as it uses simple comparison with the '==' operator.

Example:
	should.DeepEqual(t, nums, []any{"1", 2, 3}, "nums should be deeply equal to ["1", 2, 3]")

func Equal

func Equal[T comparable](t assert.Test, value, other T, msg ...any) Result

Equal is asserting that the given values are equal.

Items are being compared using the '==' operator. For non-comparable values use should.DeepEqual, which uses reflection to compare the values of the items.

Example:
	should.Equal(t, sentence, "baba is you", "sentence should be equal to 'baba is you'")

func EqualError

func EqualError(t assert.Test, err error, value string, msg ...any) Result

EqualError is asserting that the given error is not nil and its value is equal to the provided value.

Example:
	should.EqualError(t, baba(), "baba is not you", "operation baba should have failed with an error 'baba is not you'")

func EqualWithinTolerance

func EqualWithinTolerance[T assert.Float](t assert.Test, value, other, tolerance T, msg ...any) Result

EqualWithinTolerance is asserting that a given float value is equal to another float value within a certain tolerance.

Example:
	should.EqualWithinTolerance(t, value, 5.2, 0.001, "value should be equal to 5.2 with tolerance of 0.001")

func NewResult

func NewResult(t assert.Test, isPassing bool) Result

NewResult creates a new result.

func NotBeDeepZero

func NotBeDeepZero(t assert.Test, value any, msg ...any) Result

NotBeDeepZero is asserting that a given value is not the zero value for its kind.

It uses reflection to determine whether the value is zero. For comparable values it is

preferable to use the should.NotBeZero, as it performs simple comparison using the '==' operator.

Example:
	err := errors,New("baba")
	should.NotBeDeepZero(t, err, "err should not be zero value")

func NotBeEmpty

func NotBeEmpty[T any](t assert.Test, slice []T, msg ...any) Result

NotBeEmpty is asserting that the given slice holds at least one item.

Example:
	should.NotBeEmpty(t, []string{"baba"}, "should have at least 1 item")

func NotBeEmptyString

func NotBeEmptyString[T ~string](t assert.Test, value T, msg ...any) Result

NotBeEmptyString is asserting that the given string holds at least one character.

Example:
	should.NotBeEmptyString(t, "baba", "should have at least 1 character")

func NotBeError

func NotBeError(t assert.Test, err, targetErr error, msg ...any) Result

NotBeError is asserting that the given error is not a target error.

The errors are compared using errors.Is, so any wrapped errors will work just fine.

Example:
	should.NotBeError(t, baba(), ErrBabaIsNotYou, "operation baba should have failed with error other than 'ErrBabaIsNotYou'")

func NotBeInArray

func NotBeInArray(t assert.Test, array, item any, msg ...any) Result

NotBeInArray is asserting that the given array of items is not containing an item.

Items are being compared using reflection by calling reflect.DeepEqual. array must be an array or a pointer to an array, otherwise the function panics.

Example:
	should.NotBeInArray(t, [...]string{"baba", "is", "you"}, "flag", "should not contain 'flag'")

func NotBeInRange

func NotBeInRange[T assert.Ordered](t assert.Test, item, min, max T, msg ...any) Result

NotBeInRange is asserting that a given value falls non-inclusively outside a certain range.

Example:
	should.NotBeInRange(t, value, 0, 100, "value should be outside of range 0-100")

func NotBeNil

func NotBeNil(t assert.Test, value any, msg ...any) Result

NotBeNil is asserting that a given value is not nil.

Example:
	should.NotBeNil(t, value, "value should not be nil")

func NotBeNilError

func NotBeNilError(t assert.Test, err error, msg ...any) Result

NotBeNilError is asserting that the given error is not nil.

Example:
	should.NotBeNilError(t, baba(), "operation baba should have failed with an error")

func NotBeOfLen

func NotBeOfLen[T any](t assert.Test, slice []T, targetLen int, msg ...any) Result

NotBeOfLen is asserting that the given slice is not of certain length.

Example:
	should.NotBeOfLen(t, []string{"baba", "is", "you"}, 5, "should not have 5 items")

func NotBeOfSameLength

func NotBeOfSameLength[T any](t assert.Test, slice, other []T, msg ...any) Result

NotBeOfSameLength is asserting that the given slice is not of equal len to another slice.

Example:
	should.NotBeOfSameLength(t, []string{"baba", "is", "you"}, []string{"flag", "is"}, "should have different lengths")

func NotBeSameLengthString

func NotBeSameLengthString[T ~string](t assert.Test, value, other T, msg ...any) Result

NotBeSameLengthString is asserting that the given string is not of equal len to another string.

Example:
	should.NotBeSameLengthString(t, "baba is you", "flag is", "should have different lengths")

func NotBeStringOfLen

func NotBeStringOfLen[T ~string](t assert.Test, value T, targetLen int, msg ...any) Result

NotBeStringOfLen is asserting that the given string is not of certain length.

Example:
	should.NotBeStringOfLen(t, "baba is you", 5, "should not have 5 characters")

func NotBeZero

func NotBeZero[T comparable](t assert.Test, item T, msg ...any) Result

NotBeZero is asserting that a given value is not the zero value for its kind.

Example:
	num := 5
	should.NotBeZero(t, num, "num should not be zero")

func NotContain

func NotContain[T comparable](t assert.Test, slice []T, item T, msg ...any) Result

NotContain is asserting that the given slice is not containing an item.

Items are being compared using the '==' operator. For slices of non-comparable items use should.NotDeepContain. It relies on reflection to perform comparison on the items' values using reflect.DeepEqual.

Example:
	should.NotContain(t, []string{"baba", "is", "you"}, "flag", "should not contain 'flag'")

func NotContainKey

func NotContainKey[K comparable, V any](t assert.Test, m map[K]V, key K, msg ...any) Result

NotContainKey is asserting that the given map does not contain a certain key.

Example:
	should.NotContainKey(t, map[string]any{"baba": "is you"}, "flag", "should not contain key 'flag'")

func NotContainString

func NotContainString[T ~string](t assert.Test, value, substr T, msg ...any) Result

NotContainString is asserting that the given value is not containing a substring.

Example:
	should.NotContainString(t, "baba is you", "flag", "should not contain 'baba'")

func NotDeepContain

func NotDeepContain[T any](t assert.Test, slice []T, item T, msg ...any) Result

NotDeepContain is asserting that the given slice is not containing a deeply equal item.

Items are being compared using reflection by calling reflect.DeepEqual. For checking slices of items that are comparable it is preferable to use should.NotContain which performs simple equality check using the '==' operator.

Example:
	should.NotDeepContain(t, []any{"baba", "is", 5}, "flag", "should not contain 'flag'")

func NotDeepEqual

func NotDeepEqual[T any](t assert.Test, value, other T, msg ...any) Result

NotDeepEqual is asserting that the given values are not deeply equal.

Items are being compared using reflect.DeepEqual. For items that are comparable is it preferable to use should.NotEqual, as it uses simple comparison with the '==' operator.

Example:
	should.NotDeepEqual(t, nums, []any{"1", 2, 3}, "nums should not be deeply equal to ["1", 2, 3]")

func NotEqual

func NotEqual[T comparable](t assert.Test, value, other T, msg ...any) Result

NotEqual is asserting that the given values are not equal.

Items are being compared using the '==' operator. For non-comparable values use should.NotDeepEqual, which uses reflection to compare the values of the items.

Example:
	should.NotEqual(t, sentence, "baba is you", "sentence should not be equal to 'baba is you'")

func NotEqualError

func NotEqualError(t assert.Test, err error, value string, msg ...any) Result

NotEqualError is asserting that the given error is not nil and its value is not equal to the provided value.

Example:
	should.NotEqualError(t, baba(), "baba is not you", "operation baba should have failed with error different from 'baba is not you'")

func NotPanic

func NotPanic(t assert.Test, closure func(), msg ...any) (result Result)

NotPanic is asserting that a given closure function will not panic.

Example:
	closure := func () {}

	should.NotPanic(t, closure, "closure function should not panic")

func Panic

func Panic(t assert.Test, closure func(), msg ...any) (result Result)

Panic is asserting that a given closure function will panic.

Example:
	closure := func () {
		panic("kaboom")
	}

	should.Panic(t, closure, "closure function should panic")

func PanicWith

func PanicWith(t assert.Test, closure func(), value any, msg ...any) (result Result)

PanicWith is asserting that a given closure function will panic with specific value.

Example:
	closure := func () {
		panic("kaboom")
	}

	should.PanicWith(t, closure, "kaboom", "closure function should panic with 'kaboom'")

func (Result) IsPassing

func (r Result) IsPassing() bool

IsPassing returns a boolean stating whether the test condition was successful or not.

func (Result) OtherwiseBail

func (r Result) OtherwiseBail()

OtherwiseBail will cause the current test to fail immediately and will prevent any later test conditions from executing.

Jump to

Keyboard shortcuts

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