Documentation
¶
Overview ¶
Package assert is a testing library that provides many assertions. It utilizes only one dependency, in order to manage dependency creep within a consuming application.
All assertions in this library are paired with expectations (implementations of Expectation); in fact, every assertion provided by this library is a thin wrapper that calls an expectation constructor, then passes it to That to perform the test and report the result. This has a distinct benefit for table-driven tests, as the table may contain arbitrary expectations. Expectations may be useful in other instances, as well; for instance, a mock library could be written that utilizes expectations for mock function parameters.
Another feature of this library is the use of generics. This has a particular benefit for cases such as the Equal assertion applied against integer types that are not "int": if an assertion uses "any" for its argument types, then it becomes necessary to explicitly cast an integer literal used as an expected value to the correct integer type. Because Equal uses generic syntax, the go compiler is able to automatically make the correct conversion. (For those cases where "any" is needed, EqualObj is provided.)
Finally, the package provides several building blocks for constructing custom assertions with the same reporting and option processing used by the library. All a consumer needs to do is embed BaseExpectation within their Expectation implementation to have access to all of the features the package provides to all internal expectation constructors and assertion functions. Other types exist which provide additional functionality, such as ChainExpectation.
Index ¶
- Variables
- func AllOf[T any](t TB, exps []Expectation[T], actual T, opts ...Option) bool
- func AnyOf[T any](t TB, exps []Expectation[T], actual T, opts ...Option) bool
- func Cap(t TB, expected int, actual any, opts ...Option) bool
- func CapGreater(t TB, expected int, actual any, opts ...Option) bool
- func CapGreaterEqual(t TB, expected int, actual any, opts ...Option) bool
- func CapLess(t TB, expected int, actual any, opts ...Option) bool
- func CapLessEqual(t TB, expected int, actual any, opts ...Option) bool
- func ChanClosed[T any](t TB, actual chan T, opts ...Option) bool
- func ChanContent[T any](t TB, actual chan T, opts ...Option) bool
- func ChanOpen[T any](t TB, actual chan T, opts ...Option) bool
- func Compare(t TB, cmpr CompareType, expected, actual any, opts ...Option) bool
- func CompareBytes(cmpr CompareType, x, y []byte) bool
- func CompareFloat(cmpr CompareType, x, y float64) bool
- func CompareStrings(cmpr CompareType, x, y string) bool
- func CompareTimes(cmpr CompareType, x, y time.Time) bool
- func Contains(t TB, expected, actual any, opts ...Option) bool
- func DefaultCmpOption(exp CmpableExpectation)
- func Empty(t TB, actual any, opts ...Option) bool
- func Equal[T any](t TB, expected, actual T, opts ...Option) bool
- func EqualFold(t TB, expected, actual any, opts ...Option) bool
- func EqualObj(t TB, expected, actual any, opts ...Option) bool
- func Error(t TB, actual error, opts ...Option) bool
- func ErrorAs[T error](t TB, actual error, opts ...Option) bool
- func ErrorIs(t TB, expected, actual error, opts ...Option) bool
- func ErrorIsNot(t TB, expected, actual error, opts ...Option) bool
- func ErrorNotAs[T error](t TB, actual error, opts ...Option) bool
- func ExpectedType(e any) reflect.Type
- func False(t TB, actual bool, opts ...Option) bool
- func Func[T any](t TB, fn func(actual T) error, actual T, opts ...Option) bool
- func GetAttrOrder(e error) []string
- func GetAttrs(e error) map[string]string
- func GetMessage(e error) string
- func Greater(t TB, expected, actual any, opts ...Option) bool
- func GreaterEqual(t TB, expected, actual any, opts ...Option) bool
- func InitExpectation(exp TentamenExpectation, opts []Option, extra ...Option)
- func IsA[T any](t TB, actual any, opts ...Option) bool
- func IsFatal(e error) bool
- func IsNotA[T any](t TB, actual any, opts ...Option) bool
- func IsSkip(e error) bool
- func Len(t TB, expected int, actual any, opts ...Option) bool
- func LenGreater(t TB, expected int, actual any, opts ...Option) bool
- func LenGreaterEqual(t TB, expected int, actual any, opts ...Option) bool
- func LenLess(t TB, expected int, actual any, opts ...Option) bool
- func LenLessEqual(t TB, expected int, actual any, opts ...Option) bool
- func Less(t TB, expected, actual any, opts ...Option) bool
- func LessEqual(t TB, expected, actual any, opts ...Option) bool
- func Negative(t TB, actual any, opts ...Option) bool
- func Nil(t TB, actual any, opts ...Option) bool
- func NoError(t TB, actual error, opts ...Option) bool
- func NoPanics(t TB, actual func(), opts ...Option) bool
- func NoPrefix(t TB, prefix, actual any, opts ...Option) bool
- func NoSuffix(t TB, suffix, actual any, opts ...Option) bool
- func Not[T any](t TB, exp Expectation[T], actual T, opts ...Option) bool
- func NotCap(t TB, expected int, actual any, opts ...Option) bool
- func NotContains(t TB, expected, actual any, opts ...Option) bool
- func NotEmpty(t TB, actual any, opts ...Option) bool
- func NotEqual[T any](t TB, expected, actual T, opts ...Option) bool
- func NotEqualFold(t TB, expected, actual any, opts ...Option) bool
- func NotEqualObj(t TB, expected, actual any, opts ...Option) bool
- func NotLen(t TB, expected int, actual any, opts ...Option) bool
- func NotNil(t TB, actual any, opts ...Option) bool
- func NotRegexp(t TB, re string, actual any, opts ...Option) bool
- func NotSame[T any](t TB, expected, actual *T, opts ...Option) bool
- func NotSetsRelation(t TB, rel SetRelation, expected, actual any, opts ...Option) bool
- func NotTimeDelta(t TB, expected time.Time, delta time.Duration, actual time.Time, ...) bool
- func NotTimeRange(t TB, start, end, actual time.Time, opts ...Option) bool
- func NotValuesContain(t TB, expected, actual any, opts ...Option) bool
- func NotWithinDelta(t TB, expected any, delta float64, actual any, opts ...Option) bool
- func NotWithinEpsilon(t TB, expected any, epsilon float64, actual any, opts ...Option) bool
- func NotZero(t TB, actual any, opts ...Option) bool
- func OneOf[T any](t TB, exps []Expectation[T], actual T, opts ...Option) bool
- func Panics(t TB, actual func(), opts ...Option) bool
- func Positive(t TB, actual any, opts ...Option) bool
- func Prefix(t TB, prefix, actual any, opts ...Option) bool
- func Regexp(t TB, re string, actual any, opts ...Option) bool
- func Same[T any](t TB, expected, actual *T, opts ...Option) bool
- func SetsDisjoint(t TB, expected, actual any, opts ...Option) bool
- func SetsEqual(t TB, expected, actual any, opts ...Option) bool
- func SetsIntersect(t TB, expected, actual any, opts ...Option) bool
- func SetsNotDisjoint(t TB, expected, actual any, opts ...Option) bool
- func SetsNotEqual(t TB, expected, actual any, opts ...Option) bool
- func SetsNotIntersect(t TB, expected, actual any, opts ...Option) bool
- func SetsNotSubset(t TB, expected, actual any, opts ...Option) bool
- func SetsNotSuperset(t TB, expected, actual any, opts ...Option) bool
- func SetsRelation(t TB, rel SetRelation, expected, actual any, opts ...Option) bool
- func SetsSubset(t TB, expected, actual any, opts ...Option) bool
- func SetsSuperset(t TB, expected, actual any, opts ...Option) bool
- func Suffix(t TB, suffix, actual any, opts ...Option) bool
- func That[T any](t TB, e Expectation[T], actual T, opts ...ThatOption) bool
- func TimeDelta(t TB, expected time.Time, delta time.Duration, actual time.Time, ...) bool
- func TimeRange(t TB, start, end, actual time.Time, opts ...Option) bool
- func Transform[S any, T any](t TB, xform func(S) (T, error), actual S, opts ...Option) bool
- func True(t TB, actual bool, opts ...Option) bool
- func UnexportedFieldsEqualCmpOption(pkgPath string, typs ...reflect.Type) cmp.Option
- func ValuesContain(t TB, expected, actual any, opts ...Option) bool
- func WithinDelta(t TB, expected any, delta float64, actual any, opts ...Option) bool
- func WithinEpsilon(t TB, expected any, epsilon float64, actual any, opts ...Option) bool
- func Zero(t TB, actual any, opts ...Option) bool
- type AllOfExpectation
- type AnyExpectation
- type AnyOfExpectation
- type AttrOption
- type BaseExpectation
- func (be BaseExpectation) Fatal() bool
- func (be BaseExpectation) Location() Location
- func (be BaseExpectation) Message() string
- func (be BaseExpectation) Report(opts ...ReportOption) *Report
- func (be *BaseExpectation) SetFatal(fatal bool)
- func (be *BaseExpectation) SetLocation(loc Location)
- func (be *BaseExpectation) SetMessage(msg string)
- func (be *BaseExpectation) SetSkip(skip bool)
- func (be *BaseExpectation) SetSuppressLoc(suppressLoc bool)
- func (be *BaseExpectation) SetXFail(xfail bool)
- func (be BaseExpectation) Skip() bool
- func (be BaseExpectation) SuppressLoc() bool
- func (be *BaseExpectation) XFail() bool
- type BoolExpectation
- type CapExpectation
- func ExpectCap(expected int, opts ...Option) *CapExpectation
- func ExpectCapGreater(expected int, opts ...Option) *CapExpectation
- func ExpectCapGreaterEqual(expected int, opts ...Option) *CapExpectation
- func ExpectCapLess(expected int, opts ...Option) *CapExpectation
- func ExpectCapLessEqual(expected int, opts ...Option) *CapExpectation
- func ExpectNotCap(expected int, opts ...Option) *CapExpectation
- type ChainExpectation
- type ChainableExpectation
- type ChanClosedExpectation
- type ChanContentExpectation
- type CmpExpectation
- type CmpOption
- type CmpableExpectation
- type CompareBytesExpectation
- type CompareComparableExpectation
- type CompareFloatExpectation
- type CompareStringsExpectation
- type CompareTimesExpectation
- type CompareType
- type ContainsExpectation
- type EqualExpectation
- type EqualFoldExpectation
- type ErrorAsExpectation
- type ErrorExpectation
- type ErrorIsExpectation
- type ErrorOption
- type Expectation
- func ExpectCompare(cmpr CompareType, expected any, opts ...Option) Expectation[any]
- func ExpectGreater(expected any, opts ...Option) Expectation[any]
- func ExpectGreaterEqual(expected any, opts ...Option) Expectation[any]
- func ExpectLess(expected any, opts ...Option) Expectation[any]
- func ExpectLessEqual(expected any, opts ...Option) Expectation[any]
- type ExpectationOption
- type FuncExpectation
- type HasFixExpectation
- type IsAExpectation
- type LenExpectation
- func ExpectEmpty(opts ...Option) *LenExpectation
- func ExpectLen(expected int, opts ...Option) *LenExpectation
- func ExpectLenGreater(expected int, opts ...Option) *LenExpectation
- func ExpectLenGreaterEqual(expected int, opts ...Option) *LenExpectation
- func ExpectLenLess(expected int, opts ...Option) *LenExpectation
- func ExpectLenLessEqual(expected int, opts ...Option) *LenExpectation
- func ExpectNotEmpty(opts ...Option) *LenExpectation
- func ExpectNotLen(expected int, opts ...Option) *LenExpectation
- type Location
- type NilExpectation
- type NoneExpectation
- type NotExpectation
- type OneOfExpectation
- type Option
- type PanicsExpectation
- type PositiveExpectation
- type RegexpExpectation
- type Report
- func (r *Report) Error() string
- func (r *Report) SetAttr(name, value string) *Report
- func (r *Report) SetError(e error) *Report
- func (r *Report) SetFatal(fatal bool) *Report
- func (r *Report) SetLocation(loc Location) *Report
- func (r *Report) SetMessage(msg string) *Report
- func (r *Report) SetSkip(skip bool) *Report
- func (r *Report) SetXFail(xfail bool) *Report
- func (r *Report) Unwrap() error
- type ReportOption
- type SameExpectation
- type SaveToOption
- type SetRelation
- type SetsExpectation
- func ExpectNotSetsRelation(rel SetRelation, expected any, opts ...Option) *SetsExpectation
- func ExpectSetsDisjoint(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsEqual(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsIntersect(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsNotDisjoint(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsNotEqual(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsNotIntersect(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsNotSubset(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsNotSuperset(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsRelation(rel SetRelation, expected any, opts ...Option) *SetsExpectation
- func ExpectSetsSubset(expected any, opts ...Option) *SetsExpectation
- func ExpectSetsSuperset(expected any, opts ...Option) *SetsExpectation
- type TB
- type TentamenExpectation
- type ThatOption
- type TimeRangeExpectation
- func ExpectNotTimeDelta(expected time.Time, delta time.Duration, opts ...Option) *TimeRangeExpectation
- func ExpectNotTimeRange(start, end time.Time, opts ...Option) *TimeRangeExpectation
- func ExpectTimeDelta(expected time.Time, delta time.Duration, opts ...Option) *TimeRangeExpectation
- func ExpectTimeRange(start, end time.Time, opts ...Option) *TimeRangeExpectation
- type TransformExpectation
- type WithMessage
- type WithinExpectation
- func ExpectNotWithinDelta(expected any, delta float64, opts ...Option) *WithinExpectation
- func ExpectNotWithinEpsilon(expected any, epsilon float64, opts ...Option) *WithinExpectation
- func ExpectWithinDelta(expected any, delta float64, opts ...Option) *WithinExpectation
- func ExpectWithinEpsilon(expected any, epsilon float64, opts ...Option) *WithinExpectation
- type ZeroExpectation
Constants ¶
This section is empty.
Variables ¶
var AnError = errors.New("an error") //nolint:revive,staticcheck
AnError is an error that may be used when an arbitrary error is required.
var Fatal = withFatal(true)
Fatal is an option that may be passed to indicate that an expectation failure should be treated as fatal. This will report the failure, but then skip subsequent assertions. Note that this is mutually exclusive with the Skip option; if both are given, the one given last wins.
var NoSkip = withSkip(false)
NoSkip is an option that may be passed to indicate that an expectation failure should not be treated as skipping the test. This is the default.
var NoSuppressLoc = withSuppressLoc(false)
NoSuppressLoc is an option that may be passed to indicate that the location that constructed an Expectation should be reported when generating a Report. This is the default.
var NonFatal = withFatal(false)
NonFatal is an option that may be passed to indicate that an expectation failure should be treated as non-fatal. This is the default.
var Skip = withSkip(true)
Skip is an option that may be passed to indicate that an expectation failure should be treated as skipping the test. This will report the failure message but not fail the test, and the remaining assertions will be skipped. Note that this is mutually exclusive with the Fatal option; if both are given, the one given last wins.
var SuppressLoc = withSuppressLoc(true)
SuppressLoc is an option that may be passed to indicate that the location that constructed an Expectation should not be reported when generating a Report. This is intended for use within an assertion function, which constructs the Expectation implementation, then immediately passes it to That.
var XFail = withXFail(true)
XFail is an option that may be passed to indicate that an expectation failure should be treated as success. This will log the failure but allow the testing to succeed.
var XSuccess = withXFail(false)
XSuccess is an option that may be passed to indicate that an expectation failure should be treated as a failure. This is the default.
Functions ¶
func AllOf ¶
func AllOf[T any](t TB, exps []Expectation[T], actual T, opts ...Option) bool
AllOf is an assertion that requires that all of a list of expectations accept an actual value.
func AnyOf ¶
func AnyOf[T any](t TB, exps []Expectation[T], actual T, opts ...Option) bool
AnyOf is an assertion that requires that at least one of a list of expectations accept an actual value.
func CapGreater ¶
CapGreater is an assertion that checks that a capacity is greater than an expected value.
func CapGreaterEqual ¶
CapGreaterEqual is an assertion that checks that a capacity is greater than or equal to an expected value.
func CapLessEqual ¶
CapLessEqual is an assertion that checks that a capacity is less than or equal to an expected value.
func ChanClosed ¶
ChanClosed is an assertion that checks that a channel is closed.
func ChanContent ¶
ChanContent is an assertion that checks that a channel has at least one item, which it then consumes from the channel. The WithExpectation and WithSaveTo options may be used to specify an additional expectation to perform on the item. To assert that a channel have no items, use Empty on the channel.
func Compare ¶
func Compare(t TB, cmpr CompareType, expected, actual any, opts ...Option) bool
Compare is an assertion that checks that an actual value has a specific relationship to an expected value, as expressed by the CompareType argument.
func CompareBytes ¶
func CompareBytes(cmpr CompareType, x, y []byte) bool
CompareBytes is similar to Compare, but for byte slices.
func CompareFloat ¶
func CompareFloat(cmpr CompareType, x, y float64) bool
CompareFloat is a comparison function for floating point numbers. It performs the desired comparison, returning true or false.
func CompareStrings ¶
func CompareStrings(cmpr CompareType, x, y string) bool
CompareStrings is similar to Compare, but for strings.
func CompareTimes ¶
func CompareTimes(cmpr CompareType, x, y time.Time) bool
CompareTimes is similar to Compare, but for times.
func Contains ¶
Contains is an assertion that traverses an actual value, which must be a collection or an iterator, looking for an expected element. For strings or byte slices, it checks that a substring is contained; for types implementing a "Contains" method, it checks that that method returns true. If the "collection" is an integer, it asserts that the range [0..n) contains the element. For functions that are iterators yielding one value, it checks that one of those values matches the expectation. For functions that are iterators yielding two values, it checks that one of the first yielded values matches the expectation. For arrays, it checks that one of the available values matches the expectation, while for maps, it checks that one of the keys matches the expectation. Finally, for channels, it checks that the channel contains the expected value, discarding elements from the channel until it finds the expected one. (Channels should be closed before calling the ContainsExpectation.Check method.)
func DefaultCmpOption ¶
func DefaultCmpOption(exp CmpableExpectation)
DefaultCmpOption is a helper function that sets a default cmp.Option for use with cmp.Equal, if no alternatives have been set using WithCmp. Expectations that embed CmpExpectation should call this function after InitExpectation.
func EqualFold ¶
EqualFold is an assertion that checks that two strings (or byte slices, or byte arrays, or any combination of those types) are equal, accounting for case-folding. This is more general than just doing strings.ToLower to both inputs.
func Error ¶
Error is an assertion that validates that an error is not nil. The WithExpectation and WithSaveTo options are accepted for further operations on the error string.
func ErrorAs ¶
ErrorAs is an assertion that checks if errors.As returns true for the specified type. The WithExpectation and WithSaveTo options are accepted for further operations on the value.
func ErrorIs ¶
ErrorIs is an assertion that checks that an error satisfies errors.Is against another error.
func ErrorIsNot ¶
ErrorIsNot is an assertion that checks that an error does not satisfy errors.Is against another error.
func ErrorNotAs ¶
ErrorNotAs is an assertion that checks if errors.As returns false for the specified type. The WithExpectation and WithSaveTo options are accepted but ignored.
func ExpectedType ¶
ExpectedType is a utility that takes an implementation of Expectation and returns the type of the expected actual value. The return value will be nil if the provided value is not an implementation of Expectation.
func Func ¶
Func is an assertion that performs its check by calling a function. The error returned by the function (if any) will always be wrapped in a Report.
func GetAttrOrder ¶
GetAttrOrder returns the order the attributes were added in. This will be nil if there are no associated attributes. Note that this is a clone of the attribute order; changes to the returned slice will not affect the attributes associated with the error.
func GetAttrs ¶
GetAttrs returns the map of attributes associated with the error. This will be nil if there are no associated attributes. Note that this is a clone of the attribute map; changes to the returned map will not affect the attributes associated with the error.
func GetMessage ¶
GetMessage returns the message associated with an error, if any.
func Greater ¶
Greater is an assertion that checks that an actual value is greater than an expected value.
func GreaterEqual ¶
GreaterEqual is an assertion that checks that an actual value is greater than or equal to an expected value.
func InitExpectation ¶
func InitExpectation(exp TentamenExpectation, opts []Option, extra ...Option)
InitExpectation initializes a TentamenExpectation, parsing the options passed to an Expectation constructor. It must be passed the list of options that were passed to the constructor. Any additional options passed to InitExpectation are processed before that list, to allow for defaulting behavior.
func IsA ¶
IsA is an assertion that validates that a type assertion for the specified type can be applied to a value. The WithExpectation and WithSaveTo options may be used to specify an additional expectation to perform on the asserted value.
func IsNotA ¶
IsNotA is an assertion that validate that a type assertion for the specified type cannot be applied to a value. The WithExpectation and WithSaveTo options are accepted but ignored.
func LenGreater ¶
LenGreater is an assertion that checks that a length is greater than an expected value.
func LenGreaterEqual ¶
LenGreaterEqual is an assertion that checks that a length is greater than or equal to an expected value.
func LenLessEqual ¶
LenLessEqual is an assertion that checks that a length is less than or equal to an expected value.
func LessEqual ¶
LessEqual is an assertion that checks that an actual value is less than or equal to an expected value.
func Negative ¶
Negative is an assertion that checks that a number is negative. For integers, 0 is considered positive. For floats, this assertion distinguishes between 0.0 and -0.0.
func NoError ¶
NoError is an assertion that validates that no error is present. The WithExpectation and WithSaveTo options are accepted but ignored.
func NoPanics ¶
NoPanics is an assertion that invokes a given function and validates that no panic occurs. The WithExpectation and WithSaveTo options are accepted but ignored.
func NoPrefix ¶
NoPrefix is an assertion that checks that a string, byte slice, or byte array does not have a specified prefix.
func NoSuffix ¶
NoSuffix is an assertion that checks that a string, byte slice, or byte array does not have a specified suffix.
func Not ¶
func Not[T any](t TB, exp Expectation[T], actual T, opts ...Option) bool
Not is an assertion that requires that another expectation not be met. Most expectations have built-in "not" handling; this is intended for use with expectations like AllOfExpectation.
func NotContains ¶
NotContains is an assertion that traverses an actual value, which must be a collection or an iterator, looking for an expected element. For strings or byte slices, it checks that a substring is not contained; for types implementing a "Contains" method, it checks that that method returns false. If the "collection" is an integer, it asserts that the range [0..n) does not contain the element. For functions that are iterators yielding one value, it checks that none of those values matches the expectation. For functions that are iterators yielding two values, it checks that none of the first yielded values matches the expectation. For arrays, it checks that none of the available indexes matches the expectation, while for maps, it checks that none of the keys matches the expectation. Finally, for channels, it checks that the channel does not contain the expected value, discarding elements from the channel until it finds the expected one. (Channels should be closed before calling the ContainsExpectation.Check method.)
func NotEqualFold ¶
NotEqualFold is an assertion that checks that two strings (or byte slices, or byte arrays, or any combination of those types) are not equal, accounting for case-folding. This is more general than just doing strings.ToLower to both inputs.
func NotEqualObj ¶
NotEqual is an assertion that checks that two values are not equal.
func NotRegexp ¶
NotRegexp is an assertion that checks that a string, byte slice, or byte array does not match a regular expression.
func NotSame ¶
NotSame is an assertion that validates that two pointers do not point to the same object.
func NotSetsRelation ¶
func NotSetsRelation(t TB, rel SetRelation, expected, actual any, opts ...Option) bool
NotSetsRelation is an assertion that compares two objects that can be interpreted as sets and validates that actual does not have the specified relation to expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func NotTimeDelta ¶
func NotTimeDelta(t TB, expected time.Time, delta time.Duration, actual time.Time, opts ...Option) bool
NotTimeDelta is an assertion that checks that a given actual time is not within a delta, expressed as a time.Duration, of an expected time; that is, it checks that expected-delta < actual < expected+delta is not true.
func NotTimeRange ¶
NotTimeRange is an assertion that checks that a given actual time is not within a specified range of an expected time. That is, it checks that start < actual < end is not true.
func NotValuesContain ¶
NotValuesContain is an assertion that traverses an actual value, which must be a collectior or an iterator, looking for an expected value. For functions that are iterators yielding two values, it checks that none of the second yielded values matches the expectation. For arrays, it checks that none of the available array elements matches the expectation, while for maps, it checks that none of the values matches the expectation.
func NotWithinDelta ¶
NotWithinDelta is an assertion that checks that an actual number is not within a delta of another number; that is, it asserts that expected-delta < actual < expected+delta is not true. The delta must be positive, or the function will panic.
func NotWithinEpsilon ¶
NotWithinEpsilon is an assertion that checks that an actual number is not within epsilon of another number; that is, it verifies that |(expected-actual)/expected| < epsilon is not true. The epsilon must be positive and the expected value must not be zero, or the function will panic.
func NotZero ¶
NotZero is an assertion that checks that an actual value does not represent the zero value for its type.
func OneOf ¶
func OneOf[T any](t TB, exps []Expectation[T], actual T, opts ...Option) bool
OneOf is an assertion that requires that exactly one of a list of expectations accept an actual value.
func Panics ¶
Panics is an assertion that invokes a given function and validates that a panic occurs. The WithExpectation and WithSaveTo options are accepted for further operations on the panic; the type will be "any".
func Positive ¶
Positive is an assertion that checks that a number is positive. For integers, 0 is considered positive. For floats, this assertion distinguishes between 0.0 and -0.0.
func Prefix ¶
Prefix is an assertion that checks that a string, byte slice, or byte array has a specified prefix.
func Regexp ¶
Regexp is an assertion that checks that a string, byte slice, or byte array matches a regular expression.
func SetsDisjoint ¶
SetsDisjoint is an assertion that compares two objects that can be interpreted as sets and requires that they are disjoint. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsEqual ¶
SetsEqual is an assertion that compares two objects that can be interpreted as sets and requires that they are equal. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsIntersect ¶
SetsIntersect is an assertion that compares two objects that can be interpreted as sets and requires that they intersect. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsNotDisjoint ¶
SetsNotDisjoint is an assertion that compares two objects that can be interpreted as sets and requires that they are disjoint. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsNotEqual ¶
SetsNotEqual is an assertion that compares two objects that can be interpreted as sets and requires that they are not equal. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsNotIntersect ¶
SetsNotIntersect is an assertion that compares two objects that can be interpreted as sets and requires that they do not intersect. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsNotSubset ¶
SetsNotSubset is an assertion that compares two objects that can be interpreted as sets and requires that actual not be a subset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsNotSuperset ¶
SetsNotSuperset is an assertion that compares two objects that can be interpreted as sets and requires that actual not be a superset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsRelation ¶
func SetsRelation(t TB, rel SetRelation, expected, actual any, opts ...Option) bool
SetsRelation is an assertion that compares two objects that can be interpreted as sets and validates that actual has the specified relation to expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsSubset ¶
SetsSubset is an assertion that compares two objects that can be interpreted as sets and requires that actual be a subset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func SetsSuperset ¶
SetsSuperset is an assertion that compares two objects that can be interpreted as sets and requires that actual be a superset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func Suffix ¶
Suffix is an assertion that checks that a string, byte slice, or byte array has a specified suffix.
func That ¶
func That[T any](t TB, e Expectation[T], actual T, opts ...ThatOption) bool
That is the core assertion function; all other assertion functions in this library end up calling That. Given an expectation and an actual value, That checks that the expectation is met and acts accordingly.
For convenience, That returns true if the expectation was met, and false otherwise.
func TimeDelta ¶
func TimeDelta(t TB, expected time.Time, delta time.Duration, actual time.Time, opts ...Option) bool
TimeDelta is an assertion that checks that a given actual time is within a delta, expressed as a time.Duration, of an expected time; that is, it checks that expected-delta < actual < expected+delta.
func TimeRange ¶
TimeRange is an assertion that checks that a given actual time is within a specified range of an expected time. That is, it checks that start < actual < end.
func Transform ¶
Transform is an assertion that validates that a transformation function executes without errors when applied to an actual value. The transformation function takes the original value and returns either a transformed value or an error. An example of a transformation would be a function that accesses an attribute of a struct.
The WithExpectation option may be provided to check an expectation on the transformed value, or WithSaveTo may be given to save the transformed value to a variable.
func UnexportedFieldsEqualCmpOption ¶
UnexportedFieldsEqualCmpOption returns a cmp.Option implementation that will ignore unexported fields except in the named package. This option is used by default by the EqualExpectation implementation, but may be overridden if custom options are provided via CmpOption.
func ValuesContain ¶
ValuesContain is an assertion that traverses an actual value, which must be a collectior or an iterator, looking for an expected value. For functions that are iterators yielding two values, it checks that one of the second yielded values matches the expectation. For arrays, it checks that one of the available array elements matches the expectation, while for maps, it checks that one of the values matches the expectation.
func WithinDelta ¶
WithinDelta is an assertion that checks that an actual number is within a delta of another number; that is, it asserts that expected-delta < actual < expected+delta. The delta must be positive, or the function will panic.
func WithinEpsilon ¶
WithinEpsilon is an assertion that checks that an actual number is within epsilon of another number; that is, it verifies that |(expected-actual)/expected| < epsilon. The epsilon must be positive and the expected value must not be zero, or the function will panic.
Types ¶
type AllOfExpectation ¶
type AllOfExpectation[T any] struct { BaseExpectation // contains filtered or unexported fields }
AllOfExpectation is an Expectation implementation that requires that all of a list of other expectations accept a given input.
func ExpectAllOf ¶
func ExpectAllOf[T any](exps []Expectation[T], opts ...Option) *AllOfExpectation[T]
ExpectAllOf returns an Expectation implementation that requires all of a list of expectations accept an actual value.
func (AllOfExpectation[T]) Check ¶
func (e AllOfExpectation[T]) Check(actual T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type AnyExpectation ¶
type AnyExpectation[T any] struct { BaseExpectation }
AnyExpectation is an Expectation implementation that accepts any value and always accepts it. This is a singleton expectation.
func ExpectAny ¶
func ExpectAny[T any](opts ...Option) *AnyExpectation[T]
ExpectAny returns an Expectation implementation that accepts any value.
func (AnyExpectation[T]) Check ¶
func (e AnyExpectation[T]) Check(_ T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type AnyOfExpectation ¶
type AnyOfExpectation[T any] struct { BaseExpectation // contains filtered or unexported fields }
AnyOfExpectation is an Expectation implementation that requires that any of a list of other expectations accept a given input.
func ExpectAnyOf ¶
func ExpectAnyOf[T any](exps []Expectation[T], opts ...Option) *AnyOfExpectation[T]
ExpectAnyOf returns an Expectation implementation that requires at least one of a list of expectations accept an actual value.
func (AnyOfExpectation[T]) Check ¶
func (e AnyOfExpectation[T]) Check(actual T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type AttrOption ¶
type AttrOption struct {
// contains filtered or unexported fields
}
AttrOption is an option that specifies a single attribute and its associated value to set on the Report.
func WithAttr ¶
func WithAttr(name, value string) AttrOption
WithAttr returns an option that may be passed to a Report to set a named attribute to a given value. Note that this will override any previously set value for that attribute.
type BaseExpectation ¶
type BaseExpectation struct {
// contains filtered or unexported fields
}
BaseExpectation is intended to provide utilities for Expectation implementations. These utilities include tracking the locations of where expectations are created, generating user-friendly reports when expectations are not met, etc. Although not required for implementing an Expectation, it is recommended to embed a BaseExpectation.
func (BaseExpectation) Fatal ¶
func (be BaseExpectation) Fatal() bool
Fatal returns true if the BaseExpectation is set to treat failures as fatal.
func (BaseExpectation) Location ¶
func (be BaseExpectation) Location() Location
Location returns the location associated with the BaseExpectation.
func (BaseExpectation) Message ¶
func (be BaseExpectation) Message() string
Message returns the message associated with the BaseExpectation.
func (BaseExpectation) Report ¶
func (be BaseExpectation) Report(opts ...ReportOption) *Report
Report constructs a Report based on the settings of BaseExpectation.
func (*BaseExpectation) SetFatal ¶
func (be *BaseExpectation) SetFatal(fatal bool)
SetFatal sets or clears the fatal flag, based on the provided boolean argument.
func (*BaseExpectation) SetLocation ¶
func (be *BaseExpectation) SetLocation(loc Location)
SetLocation sets the location at which the expectation was created. This location is normally initialized by InitExpectation.
func (*BaseExpectation) SetMessage ¶
func (be *BaseExpectation) SetMessage(msg string)
SetMessage sets the message that the expectation will use in the event of an expectation failure.
func (*BaseExpectation) SetSkip ¶
func (be *BaseExpectation) SetSkip(skip bool)
SetSkip sets or clears the skip flag, based on the provided boolean argument.
func (*BaseExpectation) SetSuppressLoc ¶
func (be *BaseExpectation) SetSuppressLoc(suppressLoc bool)
SetSuppressLoc sets or cleas the suppress location flag, based on the provided boolean argument.
func (*BaseExpectation) SetXFail ¶
func (be *BaseExpectation) SetXFail(xfail bool)
SetXFail sets or clears the xfail flag, based on the provided boolean argument.
func (BaseExpectation) Skip ¶
func (be BaseExpectation) Skip() bool
Skip returns true if the BaseExpectation is set to treat failures as a test skip.
func (BaseExpectation) SuppressLoc ¶
func (be BaseExpectation) SuppressLoc() bool
SuppressLoc returns true if the BaseExpectation is set to suppress reporting its location.
func (*BaseExpectation) XFail ¶
func (be *BaseExpectation) XFail() bool
XFail returns true if the BaseExpectation is set to treat failures as expected.
type BoolExpectation ¶
type BoolExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
BoolExpectation is an Expectation implementation that checks if a boolean value is true or false.
func ExpectFalse ¶
func ExpectFalse(opts ...Option) *BoolExpectation
ExpectFalse returns an Expectation implementation that checks that the provided boolean value is false.
func ExpectTrue ¶
func ExpectTrue(opts ...Option) *BoolExpectation
ExpectTrue returns an Expectation implementation that checks that the provided boolean value is true.
func (BoolExpectation) Check ¶
func (e BoolExpectation) Check(actual bool) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type CapExpectation ¶
type CapExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
CapExpectation is an Expectation implementation that compares the capacity of an array, channel, slice, or pointer to an array to an expected value. Note that Check always fails if the actual value is not one of these types.
func ExpectCap ¶
func ExpectCap(expected int, opts ...Option) *CapExpectation
ExpectCap returns an Expectation implementation that checks that a capacity matches an expected value.
func ExpectCapGreater ¶
func ExpectCapGreater(expected int, opts ...Option) *CapExpectation
ExpectCapGreater returns an Expectation implementation that checks that a capacity is greater than an expected value.
func ExpectCapGreaterEqual ¶
func ExpectCapGreaterEqual(expected int, opts ...Option) *CapExpectation
ExpectCapGreaterEqual returns an Expectation implementation that checks that a capacity is greater than or equal to an expected value.
func ExpectCapLess ¶
func ExpectCapLess(expected int, opts ...Option) *CapExpectation
ExpectCapLess returns an Expectation implementation that checks that a capacity is less than an expected value.
func ExpectCapLessEqual ¶
func ExpectCapLessEqual(expected int, opts ...Option) *CapExpectation
ExpectCapLessEqual returns an Expectation implementation that checks that a capacity is less than or equal to an expected value.
func ExpectNotCap ¶
func ExpectNotCap(expected int, opts ...Option) *CapExpectation
ExpectNotCap returns an Expectation implementation that checks that a capacity does not match an expected value.
func (CapExpectation) Check ¶
func (e CapExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type ChainExpectation ¶
type ChainExpectation[T any] struct { // contains filtered or unexported fields }
ChainExpectation implements ChainableExpectation, and can be used as an embed within an Expectation implementation.
func (*ChainExpectation[T]) Chain ¶
func (e *ChainExpectation[T]) Chain(val T) error
Chain must be called after [Expectation.Check] has been evaluated successfully, with the value. This will call the chain-to expectation (if any) and save the value, if desired. An error will be returned if the chain-to expectation fails.
func (*ChainExpectation[T]) ChainTo ¶
func (e *ChainExpectation[T]) ChainTo(exp any)
ChainTo is used to save another expectation that will be checked after the primary expectation has completed its check successfully.
func (*ChainExpectation[T]) SaveTo ¶
func (e *ChainExpectation[T]) SaveTo(dest any)
SaveTo is used to save the value that would be passed to a chained expectation to a variable. It takes a pointer, and will ensure that the value is saved to that pointer at the successful completion of [Expectation.Check].
type ChainableExpectation ¶
type ChainableExpectation interface {
// ChainTo is used to save another expectation that will be
// checked after the primary expectation has completed its check
// successfully.
ChainTo(e any)
// SaveTo is used to save the value that would be passed to a
// chained expectation to a variable. It takes a pointer, and
// will ensure that the value is saved to that pointer at the
// successful completion of [Expectation.Check].
SaveTo(dest any)
}
ChainableExpectation is an interface that is satisfied by chainable expectations. A chainable expectation is an expectation that checks one thing, then passes a value of a different type to another expectation, or stores that value into a specified pointer.
type ChanClosedExpectation ¶
type ChanClosedExpectation[T any] struct { BaseExpectation // contains filtered or unexported fields }
ChanClosedExpectation is an implementation of Expectation that applies to channels. It asserts that the channel is closed. Note that if the channel has any content, this expectation will fail; the channel must be drained before applying this expectation to the channel.
func ExpectChanClosed ¶
func ExpectChanClosed[T any](opts ...Option) *ChanClosedExpectation[T]
ExpectChanClosed returns an Expectation implementation that requires that a channel be closed.
func ExpectChanOpen ¶
func ExpectChanOpen[T any](opts ...Option) *ChanClosedExpectation[T]
ExpectChanOpen returns an Expectation implementation that requires that a channel be open.
func (ChanClosedExpectation[T]) Check ¶
func (e ChanClosedExpectation[T]) Check(actual chan T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type ChanContentExpectation ¶
type ChanContentExpectation[T any] struct { BaseExpectation ChainExpectation[T] }
ChanContentExpectation is an implementation of Expectation that applies to channels. It asserts that the channel contains an item, then chains to any expectations for the item itself.
func ExpectChanContent ¶
func ExpectChanContent[T any](opts ...Option) *ChanContentExpectation[T]
ExpectChanContent returns an Expectation implementation that requires that a channel have at least one item, which the expectation's Check method will consume. The WithExpectation and WithSaveTo options may be used to specify an additional expectation to perform on the item. To require that a channel have no items, use ExpectEmpty on the channel.
func (ChanContentExpectation[T]) Check ¶
func (e ChanContentExpectation[T]) Check(actual chan T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type CmpExpectation ¶
type CmpExpectation struct {
// contains filtered or unexported fields
}
CmpExpectation is an implementation of CmpableExpectation that can be used as an embed within an Expectation implementation.
func (*CmpExpectation) CmpOptions ¶
func (ce *CmpExpectation) CmpOptions() cmp.Options
CmpOptions returns the options to pass to cmp.Equal or cmp.Diff.
func (*CmpExpectation) SetCmpOption ¶
func (ce *CmpExpectation) SetCmpOption(opt cmp.Option)
SetCmpOption sets a cmp.Option to pass to cmp.Equal or cmp.Diff.
type CmpOption ¶
type CmpOption struct {
// contains filtered or unexported fields
}
CmpOption is an option that may be applied to any expectation that implements CmpableExpectation to record options to pass to cmp.Equal or cmp.Diff.
func WithCmp ¶
WithCmp returns an option that may be passed to options that utilize cmp.Equal to specify the options to utilize with that function. If no options are utilized, typically, the default will be that provided by [UnexpertedFieldsEqualCmpOption].
func (CmpOption) ApplyOption ¶
func (o CmpOption) ApplyOption(exp TentamenExpectation) bool
ApplyOption applies the option to the BaseExpectation. It should return true if the option applies to the Expectation implementation (expressed via the "typ" parameter), or false if not.
type CmpableExpectation ¶
type CmpableExpectation interface {
// SetCmpOption sets a [cmp.Option] to pass to [cmp.Equal] or
// [cmp.Diff].
SetCmpOption(opt cmp.Option)
// CmpOptions returns the options to pass to [cmp.Equal] or
// [cmp.Diff].
CmpOptions() cmp.Options
}
CmpableExpectation is an interface describing expectations that utilize cmp.Equal and/or cmp.Diff
type CompareBytesExpectation ¶
type CompareBytesExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
CompareBytesExpectation is an Expectation implementation that compares two byte slices and checks if they have a specified relation, e.g., less than; or greater than or equal; etc.
func (CompareBytesExpectation) Check ¶
func (e CompareBytesExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type CompareComparableExpectation ¶
type CompareComparableExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
CompareComparableExpectation is an Expectation implementation that compares two objects having an Equal or Compare method.
func (CompareComparableExpectation) Check ¶
func (e CompareComparableExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type CompareFloatExpectation ¶
type CompareFloatExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
CompareFloatExpectation is an Expectation implementation that compares two byte slices and checks if they have a specified relation, e.g., less than; or greater than or equal; etc.
func (CompareFloatExpectation) Check ¶
func (e CompareFloatExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type CompareStringsExpectation ¶
type CompareStringsExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
CompareStringsExpectation is an Expectation implementation that compares two byte slices and checks if they have a specified relation, e.g., less than; or greater than or equal; etc.
func (CompareStringsExpectation) Check ¶
func (e CompareStringsExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type CompareTimesExpectation ¶
type CompareTimesExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
CompareTimesExpectation is an Expectation implementation that compares two times and checks if they have a specified relation, e.g., less than; or greater than or equal; etc.
func (CompareTimesExpectation) Check ¶
func (e CompareTimesExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type CompareType ¶
type CompareType string
CompareType describes the type of comparison. This encodes comparisons such as greater-than or less-than-or-equal.
const ( CompareEqual CompareType = "==" // Check for equality CompareLess CompareType = "<" // Check for less-than CompareLessEqual CompareType = "<=" // Check for less-than-or-equal CompareGreater CompareType = ">" // Check for greater-than CompareGreaterEqual CompareType = ">=" // Check for greater-than-or-equal CompareNotEqual CompareType = "!=" // Check for not equal )
The valid comparisons. In most instances, CompareEqual is not used, preferring the Equal assertion, but it is provided for completeness.
type ContainsExpectation ¶
type ContainsExpectation struct {
BaseExpectation
CmpExpectation
// contains filtered or unexported fields
}
ContainsExpectation is an Expectation implementation that checks that a specific value exists within a collection.
func ExpectContains ¶
func ExpectContains(expected any, opts ...Option) *ContainsExpectation
ExpectContains returns an Expectation implementation that traverses an actual value, which must be a collection or an iterator, looking for an expected element. For strings or byte slices, it checks that a substring is contained; for types implementing a "Contains" method, it checks that that method returns true. If the "collection" is an integer, it asserts that the range [0..n) contains the element. For functions that are iterators yielding one value, it checks that one of those values matches the expectation. For functions that are iterators yielding two values, it checks that one of the first yielded values matches the expectation. For arrays, it checks that one of the available indexes matches the expectation, while for maps, it checks that one of the keys matches the expectation. Finally, for channels, it checks that the channel contains the expected value, discarding elements from the channel until it finds the expected one. (Channels should be closed before calling the ContainsExpectation.Check method.)
func ExpectNotContains ¶
func ExpectNotContains(expected any, opts ...Option) *ContainsExpectation
ExpectNotContains returns an Expectation implementation that traverses an actual value, which must be a collection or an iterator, looking for an expected element. For strings or byte slices, it checks that a substring is not contained; for types implementing a "Contains" method, it checks that that method returns false. If the "collection" is an integer, it asserts that the range [0..n) does not contain the element. For functions that are iterators yielding one value, it checks that none of those values matches the expectation. For functions that are iterators yielding two values, it checks that none of the first yielded values matches the expectation. For arrays, it checks that none of the available values matches the expectation, while for maps, it checks that none of the keys matches the expectation. Finally, for channels, it checks that the channel does not contain the expected value, discarding elements from the channel until it finds the expected one. (Channels should be closed before calling the ContainsExpectation.Check method.)
func ExpectNotValuesContain ¶
func ExpectNotValuesContain(expected any, opts ...Option) *ContainsExpectation
ExpectNotValuesContain returns an Expectation implementation that traverses an actual value, which must be a collectior or an iterator, looking for an expected value. For functions that are iterators yielding two values, it checks that none of the second yielded values matches the expectation. For arrays, it checks that none of the available array elements matches the expectation, while for maps, it checks that none of the values matches the expectation.
func ExpectValuesContain ¶
func ExpectValuesContain(expected any, opts ...Option) *ContainsExpectation
ExpectValuesContain returns an Expectation implementation that traverses an actual value, which must be a collectior or an iterator, looking for an expected value. For functions that are iterators yielding two values, it checks that one of the second yielded values matches the expectation. For arrays, it checks that one of the available array elements matches the expectation, while for maps, it checks that one of the values matches the expectation.
func (ContainsExpectation) Check ¶
func (e ContainsExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type EqualExpectation ¶
type EqualExpectation[T any] struct { BaseExpectation CmpExpectation // contains filtered or unexported fields }
EqualExpectation is an Expectation implementation that checks if two values are equal to each other. It uses cmp.Equal to perform this comparison.
func ExpectEqual ¶
func ExpectEqual[T any](expected T, opts ...Option) *EqualExpectation[T]
ExpectEqual returns an Expectation implementation that checks that two values are equal.
func ExpectEqualObj ¶
func ExpectEqualObj(expected any, opts ...Option) *EqualExpectation[any]
ExpectEqualObj returns an Expectation implementation that checks that two values are equal. This variant specifically accepts "any" type.
func ExpectNotEqual ¶
func ExpectNotEqual[T any](expected T, opts ...Option) *EqualExpectation[T]
ExpectNotEqual returns an Expectation implementation that checks that two values are not equal.
func ExpectNotEqualObj ¶
func ExpectNotEqualObj(expected any, opts ...Option) *EqualExpectation[any]
ExpectNotEqualObj returns an Expectation implementation that checks that two values are not equal. This variant specifically accepts "any" type.
func (EqualExpectation[T]) Check ¶
func (e EqualExpectation[T]) Check(actual T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type EqualFoldExpectation ¶
type EqualFoldExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
EqualFoldExpectation is an expectation that checks to see if two strings (or two byte slices, or a string and a byte slice) are equal, accounting for case-folding. This is more general than just doing strings.ToLower to both strings.
This expectation works by converting strings into byte slices
func ExpectEqualFold ¶
func ExpectEqualFold(expected any, opts ...Option) *EqualFoldExpectation
ExpectEqualFold returns an Expectation implementation that compares two strings (or byte slices, or byte arrays, or any combination of those types) and returns whether they are equal, accounting for case-folding. This is more general than just doing strings.ToLower to both inputs.
func ExpectNotEqualFold ¶
func ExpectNotEqualFold(expected any, opts ...Option) *EqualFoldExpectation
ExpectNotEqualFold returns an Expectation implementation that compares two strings (or byte slices, or byte arrays, or any combination of those types) and returns whether they are not equal, accounting for case-folding. This is more general than just doing strings.ToLower to both inputs.
func (EqualFoldExpectation) Check ¶
func (e EqualFoldExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type ErrorAsExpectation ¶
type ErrorAsExpectation[T error] struct { BaseExpectation ChainExpectation[T] // contains filtered or unexported fields }
ErrorAsExpectation is an Expectation implementation that checks that an error satisfies errors.As for the specified type. It is a ChainExpectation that captures the value saved by errors.As.
func ExpectErrorAs ¶
func ExpectErrorAs[T error](opts ...Option) *ErrorAsExpectation[T]
ExpectErrorAs returns an Expectation implementation that checks that errors.As returns true with the specified type. The function accepts the WithExpectation and WithSaveTo options for further operations against the value saved by errors.As.
func ExpectErrorNotAs ¶
func ExpectErrorNotAs[T error](opts ...Option) *ErrorAsExpectation[T]
ExpectErrorNotAs returns an Expectation implementation that checks that errors.As does not return true with the specified type. The function accepts but ignores WithExpectation and WithSaveTo options.
func (ErrorAsExpectation[T]) Check ¶
func (e ErrorAsExpectation[T]) Check(actual error) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type ErrorExpectation ¶
type ErrorExpectation struct {
BaseExpectation
ChainExpectation[string]
// contains filtered or unexported fields
}
ErrorExpectation is an Expectation implementation that determines if an error occurred. It is a ChainExpectation that captures the error text as reported by the [error.Error] method.
func ExpectError ¶
func ExpectError(opts ...Option) *ErrorExpectation
ExpectError returns an Expectation implementation that checks that an error is not nil. The function accepts the WithExpectation and WithSaveTo options for further operations on the error string.
func ExpectNoError ¶
func ExpectNoError(opts ...Option) *ErrorExpectation
ExpectNoError returns an Expectation implementation that checks that no error is present. The function accepts the WithExpectation and WithSaveTo options, but will not utilize them.
func (ErrorExpectation) Check ¶
func (e ErrorExpectation) Check(actual error) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type ErrorIsExpectation ¶
type ErrorIsExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
ErrorIsExpectation is an Expectation implementation that checks if an error satisfies the errors.Is check against another error.
func ExpectErrorIs ¶
func ExpectErrorIs(expected error, opts ...Option) *ErrorIsExpectation
ExpectErrorIs returns an Expectation implementation that checks that an error satisfies errors.Is against another error.
func ExpectErrorIsNot ¶
func ExpectErrorIsNot(expected error, opts ...Option) *ErrorIsExpectation
ExpectErrorIsNot returns an Expectation implementation that checks that an error does not satisfy errors.Is against another error.
func (ErrorIsExpectation) Check ¶
func (e ErrorIsExpectation) Check(actual error) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type ErrorOption ¶
type ErrorOption struct {
// contains filtered or unexported fields
}
ErrorOption is a report option that sets the error associated with a Report.
func WithError ¶
func WithError(e error) ErrorOption
WithError returns a report option that sets the error associated with a Report.
type Expectation ¶
type Expectation[T any] interface { // Check checks that the given actual value matches the // expectation. If the actual value does not match the // expectation, an error should be returned. Check(actual T) error }
Expectation describes an expectation about an actual value. The expectation is checked by calling the Check method with the actual value; if an error is returned, then the expectation was not met.
Any error is acceptable as a return value for the Check method, but consumers may want to embed BaseExpectation and use the BaseExpectation.Report method for generating a more user-friendly report about the error.
func ExpectCompare ¶
func ExpectCompare(cmpr CompareType, expected any, opts ...Option) Expectation[any]
ExpectCompare returns an Expectation implementation that compares two objects that can be ordered. It is compatible with any numbers, strings, byte slices, or time.Time.
func ExpectGreater ¶
func ExpectGreater(expected any, opts ...Option) Expectation[any]
ExpectGreater returns an Expectation implementation that checks that an actual value is greater than an expected value.
func ExpectGreaterEqual ¶
func ExpectGreaterEqual(expected any, opts ...Option) Expectation[any]
ExpectGreaterEqual returns an Expectation implementation that checks that an actual value is greater than or equal to an expected value.
func ExpectLess ¶
func ExpectLess(expected any, opts ...Option) Expectation[any]
ExpectLess returns an Expectation implementation that checks that an actual value is less than an expected value.
func ExpectLessEqual ¶
func ExpectLessEqual(expected any, opts ...Option) Expectation[any]
ExpectLessEqual returns an Expectation implementation that checks that an actual value is less than or equal to an expected value.
type ExpectationOption ¶
type ExpectationOption struct {
// contains filtered or unexported fields
}
ExpectationOption is an option that may be applied to any expectation that implements ChainableExpectation to record another expectation that should be executed upon a value after the primary expectation has been checked.
func WithExpectation ¶
func WithExpectation(exp any) ExpectationOption
WithExpectation allows chaining to another expectation. This may be used with expectations such as ErrorAsExpectation to perform additional checks on the recovered value.
func (ExpectationOption) ApplyOption ¶
func (o ExpectationOption) ApplyOption(exp TentamenExpectation) bool
ApplyOption applies the option to the BaseExpectation. It should return true if the option applies to the Expectation implementation (expressed via the "typ" parameter), or false if not.
type FuncExpectation ¶
type FuncExpectation[T any] struct { BaseExpectation // contains filtered or unexported fields }
FuncExpectation is an Expectation implementation that performs its check by calling a function with the same signature as [Expectation.Check].
func ExpectFunc ¶
func ExpectFunc[T any](fn func(actual T) error, opts ...Option) *FuncExpectation[T]
ExpectFunc returns an Expectation implementation that performs a check by calling a function. The error returned by the function (if any) will always be wrapped in a Report.
func (FuncExpectation[T]) Check ¶
func (e FuncExpectation[T]) Check(actual T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type HasFixExpectation ¶
type HasFixExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
HasFixExpectation is an expectation that checks to see if a string has a specified prefix or suffix. The inputs may be strings, byte slices, or byte arrays, and may even be of different types.
func ExpectNoPrefix ¶
func ExpectNoPrefix(prefix any, opts ...Option) *HasFixExpectation
ExpectNoPrefix returns an Expectation implementation that checks that a string (or byte slice, or byte array) does not have a designated prefix.
func ExpectNoSuffix ¶
func ExpectNoSuffix(suffix any, opts ...Option) *HasFixExpectation
ExpectNoSuffix returns an Expectation implementation that checks that a string (or byte slice, or byte array) does not have a designated suffix.
func ExpectPrefix ¶
func ExpectPrefix(prefix any, opts ...Option) *HasFixExpectation
ExpectPrefix returns an Expectation implementation that checks that a string (or byte slice, or byte array) has a designated prefix.
func ExpectSuffix ¶
func ExpectSuffix(suffix any, opts ...Option) *HasFixExpectation
ExpectSuffix returns an Expectation implementation that checks that a string (or byte slice, or byte array) has a designated suffix.
func (HasFixExpectation) Check ¶
func (e HasFixExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type IsAExpectation ¶
type IsAExpectation[T any] struct { BaseExpectation ChainExpectation[T] // contains filtered or unexported fields }
IsAExpectation is an Expectation implementation that checks that a value can be asserted to a specific type. This can be done to recover underlying structs, or it can be used to check if something implements a particular interface. The underlying code is a type assertion.
IsAExpectation is a ChainExpectation and accepts the WithExpectation and WithSaveTo options for the value as asserted.
func ExpectIsA ¶
func ExpectIsA[T any](opts ...Option) *IsAExpectation[T]
ExpectIsA returns an Expectation implementation that applies a type assertion to a value. The expectation is met if the type assertion succeeds. This function accepts the WithExpectation and WithSaveTo options applied against the specified type for further checks on the converted value.
func ExpectIsNotA ¶
func ExpectIsNotA[T any](opts ...Option) *IsAExpectation[T]
ExpectIsNotA returns an Expectation implementation that applies a type assertion to a value. The expectation is met if the type assertion fails. This function accepts the WithExpectation and WithSaveTo options, but ignores them.
func (IsAExpectation[T]) Check ¶
func (e IsAExpectation[T]) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type LenExpectation ¶
type LenExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
LenExpectation is an Expectation implementation that compares the length of an array, channel, map, slice, string, or pointer to an array, or any object implementing a Len method returning int, to an expected value. Note that Check always fails if the actual value is not one of these types.
func ExpectEmpty ¶
func ExpectEmpty(opts ...Option) *LenExpectation
ExpectEmpty returns an Expectation implementation that checks that a length is 0.
func ExpectLen ¶
func ExpectLen(expected int, opts ...Option) *LenExpectation
ExpectLen returns an Expectation implementation that checks that a length matches an expected value.
func ExpectLenGreater ¶
func ExpectLenGreater(expected int, opts ...Option) *LenExpectation
ExpectLenGreater returns an Expectation implementation that checks that a length is greater than an expected value.
func ExpectLenGreaterEqual ¶
func ExpectLenGreaterEqual(expected int, opts ...Option) *LenExpectation
ExpectLenGreaterEqual returns an Expectation implementation that checks that a length is greater than or equal to an expected value.
func ExpectLenLess ¶
func ExpectLenLess(expected int, opts ...Option) *LenExpectation
ExpectLenLess returns an Expectation implementation that checks that a length is less than an expected value.
func ExpectLenLessEqual ¶
func ExpectLenLessEqual(expected int, opts ...Option) *LenExpectation
ExpectLenLessEqual returns an Expectation implementation that checks that a length is less than or equal to an expected value.
func ExpectNotEmpty ¶
func ExpectNotEmpty(opts ...Option) *LenExpectation
ExpectNotEmpty returns an Expectation implementation that checks that a length is not 0.
func ExpectNotLen ¶
func ExpectNotLen(expected int, opts ...Option) *LenExpectation
ExpectNotLen returns an Expectation implementation that checks that a length does not match an expected value.
func (LenExpectation) Check ¶
func (e LenExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type Location ¶
type Location struct {
File string // File name
Line int // Line number
Func string // Function name
}
Location describes a single location. This will include the filename, the line number, and, if known, the function name.
Location directly implements the ReportOption type and may be passed directly to NewReport.
func CalledFrom ¶
CalledFrom returns a Location describing where the function that called CalledFrom was called. If it is unable to determine the calling point, it returns the empty Location. The "skip" argument normally should be 0, but higher numbers allow skipping more calls in the stack.
func CallerLocations ¶
CallerLocations returns the stack of locations as a list of Location instances. Autogenerated files are excluded. The stack only extends to the test function and will not extend beyond that. The "skip" argument indicates the number of frames to skip. If 0, then the first entry in the returned list will be for the function that calls CallerLocations. The "depth" argument specifies how deep in the call stack to return.
func GetLocation ¶
GetLocation returns the location associated with an error, if any. This will return a pointer to Location, or nil. Note that this is a clone of the location; changes to the returned location will not affect the location associated with the error.
func (Location) ApplyOption ¶
func (l Location) ApplyOption(exp TentamenExpectation) bool
ApplyOption applies the option to the BaseExpectation. It should return true if the option applies to the Expectation implementation (expressed via the "typ" parameter), or false if not.
type NilExpectation ¶
type NilExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
NilExpectation is an Expectation implementation that checks if a value represents a nil value.
func ExpectNil ¶
func ExpectNil(opts ...Option) *NilExpectation
ExpectNil returns an Expectation implementation that checks that the provided value is nil.
func ExpectNotNil ¶
func ExpectNotNil(opts ...Option) *NilExpectation
ExpectNotNil returns an Expectation implementation that checks that the provided value is not nil.
func (NilExpectation) Check ¶
func (e NilExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type NoneExpectation ¶
type NoneExpectation[T any] struct { BaseExpectation }
NoneExpectation is an Expectation implementation that rejects any value. It always returns an empty Report.
func ExpectNone ¶
func ExpectNone[T any](opts ...Option) *NoneExpectation[T]
ExpectNone returns an Expectation implementation that rejects any value.
func (NoneExpectation[T]) Check ¶
func (e NoneExpectation[T]) Check(_ T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type NotExpectation ¶
type NotExpectation[T any] struct { BaseExpectation // contains filtered or unexported fields }
NotExpectation is an Expectation implementation that requires that another expectation is not met. Most expectations have built-in "not" handling; this is intended for use with expectations like AllOfExpectation.
func ExpectNot ¶
func ExpectNot[T any](exp Expectation[T], opts ...Option) *NotExpectation[T]
ExpectNot returns an Expectation implementation that requires that another expectation not be met. Most expectations have built-in "not" handling; this is intended for use with expectations like AllOfExpectation.
func (NotExpectation[T]) Check ¶
func (e NotExpectation[T]) Check(actual T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type OneOfExpectation ¶
type OneOfExpectation[T any] struct { BaseExpectation // contains filtered or unexported fields }
OneOfExpectation is an Expectation implementation that requires that exactly one of a list of other expectations accept a given input.
func ExpectOneOf ¶
func ExpectOneOf[T any](exps []Expectation[T], opts ...Option) *OneOfExpectation[T]
ExpectOneOf returns an Expectation implementation that requires exactly one of a list of expectations accept an actual value.
func (OneOfExpectation[T]) Check ¶
func (e OneOfExpectation[T]) Check(actual T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type Option ¶
type Option interface {
// ApplyOption applies the option to the specified expectation,
// which must implement [TentamenExpectation]. It should return
// "false" if and only if the option cannot be applied to this
// expectation.
ApplyOption(exp TentamenExpectation) bool
}
Option describes an option that may be passed to any Expectation constructor or assertion function in this package.
type PanicsExpectation ¶
type PanicsExpectation struct {
BaseExpectation
ChainExpectation[any]
// contains filtered or unexported fields
}
PanicsExpectation is an Expectation implementation that executes a function and validates that a panic occurred. It is a ChainExpectation that captures the reported panic.
func ExpectNoPanics ¶
func ExpectNoPanics(opts ...Option) *PanicsExpectation
ExpectNoPanics returns an Expectation implementation that checks that a function does not produce a panic when invoked. The constructor accepts the WithExpectation and WithSaveTo options, but they are ignored.
func ExpectPanics ¶
func ExpectPanics(opts ...Option) *PanicsExpectation
ExpectPanics returns an Expectation implementation that checks that a function produces a panic when invoked. The constructor accepts the WithExpectation and WithSaveTo options for further operations on the panic; the type of the expectation must be "any".
func (PanicsExpectation) Check ¶
func (e PanicsExpectation) Check(actual func()) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type PositiveExpectation ¶
type PositiveExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
PositiveExpectation is an Expectation implementation that checks to see if a number is positive or negative. This is more than just using ExpectGreater with 0, as floats can represent a negative 0. Note that, with the exception of floats, 0 is considered positive.
func ExpectNegative ¶
func ExpectNegative(opts ...Option) *PositiveExpectation
ExpectNegative returns an Expectation implementation that checks that a number is negative. For integers, 0 is considered positive. For floats, this expectation distinguishes between 0.0 and -0.0.
func ExpectPositive ¶
func ExpectPositive(opts ...Option) *PositiveExpectation
ExpectPositive returns an Expectation implementation that checks that a number is positive. For integers, 0 is considered positive. For floats, this expectation distinguishes between 0.0 and -0.0.
func (PositiveExpectation) Check ¶
func (e PositiveExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type RegexpExpectation ¶
type RegexpExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
RegexpExpectation is an expectation that checks that a string, byte slice, or byte array matches a regular expression.
func ExpectNotRegexp ¶
func ExpectNotRegexp(re string, opts ...Option) *RegexpExpectation
ExpectNotRegexp returns an Expectation implementation that checks that a string, byte slice, or byte array does not match the specified regular expression.
func ExpectRegexp ¶
func ExpectRegexp(re string, opts ...Option) *RegexpExpectation
ExpectRegexp returns an Expectation implementation that checks that a string, byte slice, or byte array matches the specified regular expression.
func (RegexpExpectation) Check ¶
func (e RegexpExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type Report ¶
type Report struct {
// contains filtered or unexported fields
}
Report is a type that implements the [error] interface, along with additional functionality to make user-friendly expectation failure reports.
func NewReport ¶
func NewReport(opts ...ReportOption) *Report
NewReport constructs and returns a Report with the specified information.
func (*Report) SetAttr ¶
SetAttr sets an individual attribute in the report. This will replace any existing attribute of the same name. To replace all the attributes, use [Report.SetAttrs]; to merge a map of attributes, use [Report.MergeAttrs]. For convenience, this method returns the Report.
func (*Report) SetError ¶
SetError sets the error associated with the report. For convenience, it returns the Report.
func (*Report) SetFatal ¶
SetFatal sets the value of the "fatal" flag in the error. For convenience, it returns the Report.
func (*Report) SetLocation ¶
SetLocation sets the location that generated the report. For convenience, it returns the Report.
func (*Report) SetMessage ¶
SetMessage sets the message for the report to the specified string. For convenience, it returns the Report.
func (*Report) SetSkip ¶
SetSkip sets the value of the "skip" flag in the error. For convenience, it returns the Report.
type ReportOption ¶
type ReportOption interface {
// contains filtered or unexported methods
}
ReportOption is an option that may be passed to NewReport to attach data to the report.
type SameExpectation ¶
type SameExpectation[T any] struct { BaseExpectation // contains filtered or unexported fields }
SameExpectation is an Expectation implementation that checks if two pointers represent the same object.
func ExpectNotSame ¶
func ExpectNotSame[T any](expected *T, opts ...Option) *SameExpectation[T]
ExpectNotSame returns an Expectation implementation that checks that two pointers do not refer to the same object.
func ExpectSame ¶
func ExpectSame[T any](expected *T, opts ...Option) *SameExpectation[T]
ExpectSame returns an Expectation implementation that checks that two pointers refer to the same object.
func (SameExpectation[T]) Check ¶
func (e SameExpectation[T]) Check(actual *T) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type SaveToOption ¶
type SaveToOption struct {
// contains filtered or unexported fields
}
SaveToOption is an option that may be applied to any expectation that implements ChainableExpectation to save a recovered value to a specified target. The specified target must be a pointer to the expected type.
func WithSaveTo ¶
func WithSaveTo(dest any) SaveToOption
WithSaveTo allows saving a recovered value to a specified variable. This may be used with expectations such as ErrorAsExpectation to save the value to perform additional checks on the recovered value.
func (SaveToOption) ApplyOption ¶
func (o SaveToOption) ApplyOption(exp TentamenExpectation) bool
ApplyOption applies the option to the BaseExpectation. It should return true if the option applies to the Expectation implementation (expressed via the "typ" parameter), or false if not.
type SetRelation ¶
type SetRelation string
SetRelation describes the relationship between two sets.
const ( SetEqual SetRelation = "equal" // The sets are equal SetSub SetRelation = "subset" // The set on the left is a subset SetSuper SetRelation = "superset" // The set on the left is a superset SetIntersect SetRelation = "intersection" // The two sets intersect SetDisjoint SetRelation = "disjoint" // The two sets are disjoint )
The valid set relationships.
type SetsExpectation ¶
type SetsExpectation struct {
BaseExpectation
CmpExpectation
// contains filtered or unexported fields
}
SetsExpectation is an Expectation implementation that performs comparisons on sets. A set is the values in a slice or array, or the keys in a map; this expectation compares two such sets and determines if they are equal or if one is a subset of the other.
func ExpectNotSetsRelation ¶
func ExpectNotSetsRelation(rel SetRelation, expected any, opts ...Option) *SetsExpectation
ExpectNotSetsRelation returns an Expectation implementation that compares two objects that can be interpreted as sets and validates that actal does not have the specified relation to expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsDisjoint ¶
func ExpectSetsDisjoint(expected any, opts ...Option) *SetsExpectation
ExpectSetsDisjoint returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that they are disjoint. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsEqual ¶
func ExpectSetsEqual(expected any, opts ...Option) *SetsExpectation
ExpectSetsEqual returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that they are equal. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsIntersect ¶
func ExpectSetsIntersect(expected any, opts ...Option) *SetsExpectation
ExpectSetsIntersect returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that they intersect. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsNotDisjoint ¶
func ExpectSetsNotDisjoint(expected any, opts ...Option) *SetsExpectation
ExpectSetsNotDisjoint returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that they are not disjoint. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsNotEqual ¶
func ExpectSetsNotEqual(expected any, opts ...Option) *SetsExpectation
ExpectSetsNotEqual returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that they are not equal. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsNotIntersect ¶
func ExpectSetsNotIntersect(expected any, opts ...Option) *SetsExpectation
ExpectSetsNotIntersect returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that they do not intersect. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsNotSubset ¶
func ExpectSetsNotSubset(expected any, opts ...Option) *SetsExpectation
ExpectSetsNotSubset returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that actual not be a subset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsNotSuperset ¶
func ExpectSetsNotSuperset(expected any, opts ...Option) *SetsExpectation
ExpectSetsNotSuperset returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that actual not be a superset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsRelation ¶
func ExpectSetsRelation(rel SetRelation, expected any, opts ...Option) *SetsExpectation
ExpectSetsRelation returns an Expectation implementation that compares two objects that can be interpreted as sets and validates that actual has the specified relation to expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsSubset ¶
func ExpectSetsSubset(expected any, opts ...Option) *SetsExpectation
ExpectSetsSubset returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that actual be a subset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func ExpectSetsSuperset ¶
func ExpectSetsSuperset(expected any, opts ...Option) *SetsExpectation
ExpectSetsSuperset returns an Expectation implementation that compares two objects that can be interpreted as sets and requires that actual be a superset of expected. These include maps (for which keys are considered), slices, or iterators. The comparisons are performed without regard to ordering within the inputs, and duplicate values can be handled without issue.
func (SetsExpectation) Check ¶
func (e SetsExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type TB ¶
type TB interface {
Log(args ...any)
Fail()
FailNow()
Helper()
SkipNow()
}
TB is the interface containing the methods of testing.TB that are used by the library.
type TentamenExpectation ¶
type TentamenExpectation interface {
// SetMessage sets the message that the expectation will use in
// the event of an expectation failure.
SetMessage(msg string)
Message() string
// SetLocation sets the location at which the expectation was
// created. This location is normally initialized by
// [InitExpectation].
SetLocation(loc Location)
Location() Location
// SetFatal sets or clears the fatal flag, based on the provided
// boolean argument.
SetFatal(fatal bool)
Fatal() bool
// SetSkip sets or clears the skip flag, based on the provided
// boolean argument.
SetSkip(skip bool)
Skip() bool
// SetXFail sets or clears the xfail flag, based on the provided
// boolean argument.
SetXFail(xfail bool)
XFail() bool
// SetSuppressLoc sets or cleas the suppress location flag, based
// on the provided boolean argument.
SetSuppressLoc(suppressLoc bool)
SuppressLoc() bool
// Report constructs a [Report] based on the settings of the
// expectation.
Report(opts ...ReportOption) *Report
}
TentamenExpectation is an additional interface expectations may implement to allow use of the option processing system made available by this assertion library. The simplest way of implementing this interface is to embed BaseExpectation.
type ThatOption ¶
type ThatOption interface {
// contains filtered or unexported methods
}
ThatOption is an option that may be passed to That to control how failures are reported.
type TimeRangeExpectation ¶
type TimeRangeExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
TimeRangeExpectation is an Expectation implementation that checks that a time is within a desired range.
func ExpectNotTimeDelta ¶
func ExpectNotTimeDelta(expected time.Time, delta time.Duration, opts ...Option) *TimeRangeExpectation
ExpectNotTimeDelta returns an Expectation implementation that checks that a given actual time is not within a delta, expressed as a time.Duration, of an expected time; that is, it checks that expected-delta < actual < expected+delta is not true.
func ExpectNotTimeRange ¶
func ExpectNotTimeRange(start, end time.Time, opts ...Option) *TimeRangeExpectation
ExpectNotTimeRange returns an Expectation implementation that checks that a given actual time is not within a specified range of an expected time. That is, it checks that start < actual < end is not true.
func ExpectTimeDelta ¶
ExpectTimeDelta returns an Expectation implementation that checks that a given actual time is within a delta, expressed as a time.Duration, of an expected time; that is, it checks that expected-delta < actual < expected+delta.
func ExpectTimeRange ¶
func ExpectTimeRange(start, end time.Time, opts ...Option) *TimeRangeExpectation
ExpectTimeRange returns an Expectation implementation that checks that a given actual time is within a specified range of an expected time. That is, it checks that start < actual < end.
type TransformExpectation ¶
type TransformExpectation[S any, T any] struct { BaseExpectation ChainExpectation[T] // contains filtered or unexported fields }
TransformExpectation is an Expectation implementation that applies a transformation to an actual value. The transformation is simply a function that takes the original value and returns either the transformed value or an error. An example of a transformation would be a function that accesses an attribute of a struct.
TransformExpectation is a ChainExpectation and accepts the WithExpectation and WithSaveTo options for the return type of the transformation function.
func ExpectTransform ¶
func ExpectTransform[S any, T any](xform func(S) (T, error), opts ...Option) *TransformExpectation[S, T]
ExpectTransform returns an Expectation implementation that applies a transformation to an actual value. The transformation is simply a function that takes the original value and returns either the transformed value or an error. An example of a transformation would be a function that accesses an attribute of a struct.
This function accepts the WithExpectation and WithSaveTo options for further operations on the transformed value.
func (TransformExpectation[S, T]) Check ¶
func (e TransformExpectation[S, T]) Check(actual S) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type WithMessage ¶
type WithMessage string
WithMessage is an option that sets the message set on a Report if the expectation is not met.
func (WithMessage) ApplyOption ¶
func (m WithMessage) ApplyOption(exp TentamenExpectation) bool
ApplyOption applies the option to the BaseExpectation. It should return true if the option applies to the Expectation implementation (expressed via the "typ" parameter), or false if not.
type WithinExpectation ¶
type WithinExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
WithinExpectation is an Expectation implementation that checks to see if a number is within a specified range of another number.
func ExpectNotWithinDelta ¶
func ExpectNotWithinDelta(expected any, delta float64, opts ...Option) *WithinExpectation
ExpectNotWithinDelta returns an Expectation implementation that checks that an actual number is not within a delta of another number; that is, it verifies that expected-delta < actual < expected+delta is not true. The delta must be positive, or the function will panic.
func ExpectNotWithinEpsilon ¶
func ExpectNotWithinEpsilon(expected any, epsilon float64, opts ...Option) *WithinExpectation
ExpectNotWithinEpsilon returns an Expectation implementation that checks that an actual number is not within epsilon of another number; that is, it verifies that |(expected-actual)/expected| < epsilon is not true. The epsilon must be positive and the expected value must not be zero, or the function will panic.
func ExpectWithinDelta ¶
func ExpectWithinDelta(expected any, delta float64, opts ...Option) *WithinExpectation
ExpectWithinDelta returns an Expectation implementation that checks that an actual number is within a delta of another number; that is, it verifies that expected-delta < actual < expected+delta. The delta must be positive, or the function will panic.
func ExpectWithinEpsilon ¶
func ExpectWithinEpsilon(expected any, epsilon float64, opts ...Option) *WithinExpectation
ExpectWithinEpsilon returns an Expectation implementation that checks that an actual number is within epsilon of another number; that is, it verifies that |(expected-actual)/expected| < epsilon. The epsilon must be positive and the expected value must not be zero, or the function will panic.
func (WithinExpectation) Check ¶
func (e WithinExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.
type ZeroExpectation ¶
type ZeroExpectation struct {
BaseExpectation
// contains filtered or unexported fields
}
ZeroExpectation is an Expectation implementation that checks if a value represents a zero value for its type.
func ExpectNotZero ¶
func ExpectNotZero(opts ...Option) *ZeroExpectation
ExpectNotZero returns an Expectation implementation that checks that the provided value does not represent a zero value for its type.
func ExpectZero ¶
func ExpectZero(opts ...Option) *ZeroExpectation
ExpectZero returns an Expectation implementation that checks that the provided value represents a zero value for its type.
func (ZeroExpectation) Check ¶
func (e ZeroExpectation) Check(actual any) error
Check checks that the given actual value matches the expectation. If the actual value does not match the expectation, an error should be returned.