check

package
v0.5.4 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertibleTo

func ConvertibleTo[T any](t testers.Tester) (c testers.Check[any])

ConvertibleTo creates a check that the actual value is conversable to the given expected type.

Example: check.ConvertibleTo[int](t).Assert(actual)

func DescendingSorted

func DescendingSorted[TElem any](t testers.Tester, comparer ...comp.Comparer[TElem]) (c testers.Check[any])

DescendingSorted creates a check that the actual collection of values is sorted on descending order.

This may take an optional comparer. The given type parameter is the type of elements in the actual value that are being compared.

Example: check.DescendingSorted(t).Assert(actual)

func Empty

func Empty(t testers.Tester) (c testers.Check[any])

Empty creates a check that the length of the actual value is zero.

This requires that the actual value is a string, slice, array, map, or anything that has a Len, Length, or Count method.

Example: check.IsEmpty(t).Assert(actual)

func EndsWith

func EndsWith(t testers.Tester, expected any) (c testers.Check[any])

EndsWith creates a check that the given expected string or array is the suffix for the actual object.

Example: `check.EndsWith(t, "foo").Assert(actual)` Example: `check.EndsWith(t, []int{3, 4, 5}).Assert(actual)`

func Epsilon

func Epsilon[T utils.NumConstraint](t testers.Tester, expected, epsilon T) (c testers.Check[T])

Epsilon creates a check that the actual value is equal to the given expected value or very close to the given expected value. The value must be within the given epsilon to be considered equal.

The given epsilon must be greater than zero. An epsilon comparator should be used when comparing calculated floating point numbers since calculations may accrue small errors and make the actual value very close to the expected value but not exactly equal.

Example: check.Epsilon(t, 14.0, 1.0e-9).Assert(actual)

func Equal

func Equal[T any](t testers.Tester, expected T) (c testers.Check[T])

Equal creates a check that the given expected value is equal to an actual value.

This uses `utils.Equal` for the comparison.

Example: `check.Equal(t, 42).Assert(actual)`

func EqualElems

func EqualElems(t testers.Tester, expected any) (c testers.Check[any])

EqualElems creates a check that the actual slice contains all of the given expected elements and no others in any order while ignoring repeats.

This doesn't check number of occurrences in the actual slice meaning that multiple expected elements has no effect and will simply match the same value in the slice.

If a map is given to either the expected or actual values. The values being matched will be key/value tuples.

Example: check.EqualElems(t, []int{3, 7, 10}).Assert(actual)

func ErrorHas

func ErrorHas[T any](t testers.Tester) (c testers.Check[error])

ErrorHas creates a check that the given error type is contained within the error tree of the actual error.

This uses errors `As` method to find the contained type.

Example: check.ErrorHas[Stacked](t).Assert(actual)

func Excludes

func Excludes(t testers.Tester, unexpected any) (c testers.Check[any])

Excludes creates a check that the actual collection of values does not contain any of the given unexpected values.

There must be at least one unexpected element. If a map is given to either the expected or actual values. The values being matched will be key/value tuples.

Example: check.Excludes(t, []int{3, 7, 10}).Assert(actual)

func False

func False(t testers.Tester) (c testers.Check[bool])

False creates a check that the actual boolean value is false.

Example: check.False(t).Assert(actual)

func GreaterEq

func GreaterEq[T any](t testers.Tester, expected T, comparer ...comp.Comparer[T]) (c testers.Check[T])

GreaterEq creates a check that the actual value is greater than or equal to the given expected value.

May provide one optional comparer to use for this check. If the type being checked does not have a default comparer, then a comparer for that type must be provided.

Example: check.GreaterEq(t, 14).Assert(actual)

func GreaterThan

func GreaterThan[T any](t testers.Tester, expected T, comparer ...comp.Comparer[T]) (c testers.Check[T])

GreaterThan creates a check that the actual value is greater than the given expected value.

May provide one optional comparer to use for this check. If the type being checked does not have a default comparer, then a comparer for that type must be provided.

Example: check.GreaterThan(t, 14).Assert(actual)

func HasKeys

func HasKeys[M ~map[TKey]TValue, TKey comparable, TValue any](t testers.Tester, expected ...TKey) (c testers.Check[M])

HasKeys creates a check that the actual map contains all of the given expected keys.

There must be at least one expected key.

Example: check.HasKeys[map[string]int](t, `Name`, `Pet`, `Car`).Assert(actual)

func HasValues

func HasValues[M ~map[TKey]TValue, TKey, TValue comparable](t testers.Tester, expected ...TValue) (c testers.Check[M])

HasValues creates a check that the actual map contains all of the given expected values.

There must be at least one expected key. This doesn't check number of occurrences in the actual map meaning that multiple expected values has no effect and will simply match the same value in the map.

Example: check.HasValues[map[string]int](t, 4, 5, 6).Assert(actual)

func Implements

func Implements[T any](t testers.Tester) (c testers.Check[any])

Implements creates a check that the actual value implements the given type.

The given target type must be an interface.

Example: check.Implements[Stringer](t).Assert(actual)

func InRange

func InRange[T any](t testers.Tester, minValue, maxValue T, comparer ...comp.Comparer[T]) (c testers.Check[T])

InRange creates a check that the actual value is between the given min and maximum inclusively.

May provide one optional comparer to use for this check. If the type being checked does not have a default comparer, then a comparer for that type must be provided.

Example: InRange.LessEq(t, 0, 359).Assert(actual)

func Includes

func Includes(t testers.Tester, expected any) (c testers.Check[any])

Includes creates a check that the actual collection of values contains all of the given expected values.

There must be at least one expected element. This doesn't check number of occurrences in the actual slice meaning that multiple expected elements has no effect and will simply match the same value in the slice.

If a map is given to either the expected or actual values. The values being matched will be key/value tuples.

Example: check.Contains(t, []int{3, 7, 10}).Assert(actual)

func Intersects

func Intersects(t testers.Tester, expected any) (c testers.Check[any])

Intersects creates a check that the actual collection of values contains at least one of the given expected values.

There must be at least one expected element. If a map is given to either the expected or actual values. The values being matched will be key/value tuples.

Example: check.Intersects(t, []int{3, 7, 10}).Assert(actual)

func Is

func Is[T any](t testers.Tester, p collections.Predicate[T]) (c testers.Check[T])

Is creates a check that the actual value causes the given predicate to return true.

Example: check.Is(t, func(x thing) bool { return thing.Valid() }).Assert(actual)

func IsNot

func IsNot[T any](t testers.Tester, p collections.Predicate[T]) (c testers.Check[T])

IsNot creates a check that the actual value causes the given predicate to return false.

Example: check.IsNot(t, func(x thing) bool { return thing.Valid() }).Assert(actual)

func Length

func Length(t testers.Tester, expected int) (c testers.Check[any])

Length creates a check that the length of the actual value is equal to the given expected length.

This requires that the actual value is a string, slice, array, map, or anything that has a Len, Length, or Count method.

Example: check.Length(t, 5).Assert(actual)

func LessEq

func LessEq[T any](t testers.Tester, expected T, comparer ...comp.Comparer[T]) (c testers.Check[T])

LessEq creates a check that the actual value is less than or equal to the given expected value.

May provide one optional comparer to use for this check. If the type being checked does not have a default comparer, then a comparer for that type must be provided.

Example: check.LessEq(t, 14).Assert(actual)

func LessThan

func LessThan[T any](t testers.Tester, expected T, comparer ...comp.Comparer[T]) (c testers.Check[T])

LessThan creates a check that the actual value is less than the given expected value.

May provide one optional comparer to use for this check. If the type being checked does not have a default comparer, then a comparer for that type must be provided.

Example: check.LessThan(t, 14).Assert(actual)

func LongerThan

func LongerThan(t testers.Tester, expected int) (c testers.Check[any])

LongerThan creates a check that the length of the actual value is longer than the given expected length.

This requires that the actual value is a string, slice, array, map, or anything that has a Len, Length, or Count method.

Example: check.LongerThan(t, 5).Assert(actual)

func Match

func Match(t testers.Tester, regex any) (c testers.Check[any])

Match creates a check that the given expected regular expression matches the actual string.

The given pattern must be a string or a `*regexp.Regexp` instance. This uses `utils.String` to get the string from the actual value.

Example: `check.Match(t, "^\w+$").Assert(actual)`

func MatchError

func MatchError(t testers.Tester, regex any) (c testers.Check[error])

MatchError creates a check that the given expected regular expression matches the actual error's Error() string.

Example: check.MatchError(t, `^\w+$`).Assert(actual)

func Negative

func Negative[T any](t testers.Tester, comparer ...comp.Comparer[T]) (c testers.Check[T])

Negative creates a check that the actual value is less than zero.

May provide one optional comparer to use for this check. If the type being checked does not have a default comparer, then a comparer for that type must be provided.

Example: check.Negative(t).Assert(actual)

func Nil

func Nil(t testers.Tester) (c testers.Check[any])

Nil creates a check that the actual type is nil.

The actual value must a type which can be nil.

Example: check.Nil(t).Assert(actual)

func NoError

func NoError(t testers.Tester) (c testers.Check[error])

NoError creates a check that the actual error is not nil.

Example: check.NoError(t).Assert(actual)

func NotDescendingSorted

func NotDescendingSorted[TElem any](t testers.Tester, comparer ...comp.Comparer[TElem]) (c testers.Check[any])

NotDescendingSorted creates a check that the actual collection of values is not sorted in descending order.

This may take an optional comparer. The given type parameter is the type of elements in the actual value that are being compared.

Example: check.NotDescendingSorted(t).Assert(actual)

func NotEmpty

func NotEmpty(t testers.Tester) (c testers.Check[any])

NotEmpty creates a check that the length of the actual value is not zero.

This requires that the actual value is a string, slice, array, map, or anything that has a Len, Length, or Count method.

Example: check.IsNotEmpty(t).Assert(actual)

func NotEpsilon

func NotEpsilon[T utils.NumConstraint](t testers.Tester, unexpected, epsilon T) (c testers.Check[T])

NotEpsilon creates a check that the actual value is not equal to the given unexpected value and not very close to the given unexpected value. The value must not be within the given epsilon to be considered not equal.

The given epsilon must be greater than zero. An epsilon comparator should be used when comparing calculated floating point numbers since calculations may accrue small errors and make the actual value very close to the expected value but not exactly equal.

Example: check.NotEpsilon(t, 14.0, 1.0e-9).Assert(actual)

func NotEqual

func NotEqual[T any](t testers.Tester, unexpected T) (c testers.Check[T])

NotEqual creates a check that the given expected value is not equal to an actual value.

This uses `utils.Equal` for the comparison.

Example: `check.NotEqual(t, 42).Assert(actual)`

func NotNil

func NotNil(t testers.Tester) (c testers.Check[any])

NotNil creates a check that the actual type is not nil.

The actual value must a type which can be nil.

Example: check.NotNil(t).Assert(actual)

func NotSame

func NotSame[T comparable](t testers.Tester, expected T) (c testers.Check[T])

NotSame creates a check that the given expected type is not equal to the actual type using the `==` comparison. This can be used to ensure that two pointers point to different objects.

Example: check.NotSame(t, expected).Assert(actual)

func NotSameType

func NotSameType(t testers.Tester, unexpected any) (c testers.Check[any])

NotSameType creates a check that the actual value is not the same type as the unexpected type.

Example: check.NotSameType(t, 3.2).Assert(actual)

func NotSorted

func NotSorted[TElem any](t testers.Tester, comparer ...comp.Comparer[TElem]) (c testers.Check[any])

NotSorted creates a check that the actual collection of values is not sorted.

This may take an optional comparer. The given type parameter is the type of elements in the actual value that are being compared.

Example: check.NotSorted(t).Assert(actual)

func NotType

func NotType[T any](t testers.Tester) (c testers.Check[any])

NotType creates a check that the actual value is not the given type.

Example: check.Type[int](t).Assert(actual)

func NotUnique

func NotUnique[TElem comparable](t testers.Tester) (c testers.Check[any])

NotUnique creates a check that the actual collection does not have all unique values.

Example: check.NotUnique(t).Assert(actual)

func NotZero

func NotZero(t testers.Tester) (c testers.Check[any])

NotZero creates a check that the actual value is not the zero value of that type.

Example: check.NotZero(t).Assert(actual)

func OneOf

func OneOf(t testers.Tester, expected any) (c testers.Check[any])

OneOf creates a check that the actual value is one of the values in the given expected values.

There must be at least one expected element. If a map is given to either the expected or actual values. The values being matched will be key/value tuples.

Example: check.OneOf(t, []int{3, 7, 10}).Assert(actual)

func Positive

func Positive[T any](t testers.Tester, comparer ...comp.Comparer[T]) (c testers.Check[T])

Positive creates a check that the actual value is greater than zero.

May provide one optional comparer to use for this check. If the type being checked does not have a default comparer, then a comparer for that type must be provided.

Example: check.Positive[int](t).Assert(actual)

func Same

func Same[T comparable](t testers.Tester, expected T) (c testers.Check[T])

Same creates a check that the given expected type is equal to the actual type using the `==` comparison. This can be used to ensure that two pointers point to the same object.

Example: check.Same(t, expected).Assert(actual)

func SameElems

func SameElems(t testers.Tester, expected any) (c testers.Check[any])

SameElems creates a check that the actual slice contains all of the given expected elements and no others in any order and in the same number. The types being compared to not need to match but the elements do.

There must be at least one expected element. This doesn't check number of occurrences in the actual slice meaning that multiple expected elements has no effect and will simply match the same value in the slice.

If a map is given to either the expected or actual values. The values being matched will be key/value tuples.

Example: check.HasElems(t, []int{3, 7, 10}).Assert(actual)

func SameType

func SameType(t testers.Tester, expected any) (c testers.Check[any])

SameType creates a check that the actual value is the same type as the expected type.

Example: check.SameType(t, 3.2).Assert(actual)

func ShorterThan

func ShorterThan(t testers.Tester, expected int) (c testers.Check[any])

ShorterThan creates a check that the length of the actual value is shorter than the given expected length.

This requires that the actual value is a string, slice, array, map, or anything that has a Len, Length, or Count method.

Example: check.ShorterThan(t, 5).Assert(actual)

func Single

func Single(t testers.Tester) (c testers.Check[any])

Single creates a check that the length of the actual value is one.

This requires that the actual value is a string, slice, array, map, or anything that has a Len, Length, or Count method.

Example: check.Single(t).Assert(actual)

func Sorted

func Sorted[TElem any](t testers.Tester, comparer ...comp.Comparer[TElem]) (c testers.Check[any])

Sorted creates a check that the actual collection of values is sorted.

This may take an optional comparer. The given type parameter is the type of elements in the actual value that are being compared.

Example: check.Sorted(t).Assert(actual)

func StartsWith

func StartsWith(t testers.Tester, expected any) (c testers.Check[any])

StartsWith creates a check that the given expected string or array is the prefix for the actual object.

Example: `check.StartsWith(t, "foo").Assert(actual)` Example: `check.StartsWith(t, []int{3, 4, 5}).Assert(actual)`

func String

func String(t testers.Tester, expected string) (c testers.Check[any])

String creates a check that the given expected string is the same of the string from the actual object.

This uses `utils.String` to get the string.

Example: `check.String(t, "foo").Assert(actual)`

func StringAndReset

func StringAndReset(t testers.Tester, expected string) (c testers.Check[any])

StringAndReset creates a check that the given expected string is the same of the string from the actual object then calls `Reset` on the actual object if it exists.

This uses `utils.String` to get the string. This is designed to work with `bytes.Buffer` so that a buffer can be used to collect changes, event, calls, etc then checked that those occurred. Resetting the buffer prepares it for the next collection of changes, etc.

Example: `check.StringAndReset(t, "foo").Assert(actual)`

func True

func True(t testers.Tester) (c testers.Check[bool])

True creates a check that the actual boolean value is true.

Example: check.True(t).Assert(actual)

func Type

func Type[T any](t testers.Tester) (c testers.Check[any])

Type creates a check that the actual value is the given type.

Example: check.Type[int](t).Assert(actual)

func Unique

func Unique[TElem comparable](t testers.Tester) (c testers.Check[any])

Unique creates a check that the actual collection has all unique values.

Example: check.Unique(t).Assert(actual)

func Zero

func Zero(t testers.Tester) (c testers.Check[any])

Zero creates a check that the actual value is the zero value of that type.

Example: check.Zero(t).Assert(actual)

Types

This section is empty.

Jump to

Keyboard shortcuts

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