Documentation
¶
Index ¶
- Constants
- func Assert(cond bool, msg string)
- func AssertConv[T any](elem any, var_name string) T
- func AssertDeref[T any](elem *T, param_name string) T
- func AssertErr(err error, format string, args ...any)
- func AssertF(cond bool, format string, args ...any)
- func AssertNotNil(elem any, param_name string)
- func AssertOk(cond bool, format string, args ...any)
- func AssertTypeOf[T any](elem any, var_name string, allow_nil bool)
- type Assertion
- func (a *Assertion[T]) Applies(msg func() string, cond func(value T) bool) *Assertion[T]
- func (a *Assertion[T]) Check() bool
- func (a *Assertion[T]) Equal(b T) *Assertion[T]
- func (a *Assertion[T]) Error() *ErrAssertionFailed
- func (a *Assertion[T]) ErrorWithMessage(msg string) error
- func (a *Assertion[T]) GreaterOrEqualThan(b T) *Assertion[T]
- func (a *Assertion[T]) GreaterThan(b T) *Assertion[T]
- func (a *Assertion[T]) In(values ...T) *Assertion[T]
- func (a *Assertion[T]) InRange(min, max T) *Assertion[T]
- func (a *Assertion[T]) LessOrEqualThan(b T) *Assertion[T]
- func (a *Assertion[T]) LessThan(b T) *Assertion[T]
- func (a *Assertion[T]) Not() *Assertion[T]
- func (a *Assertion[T]) Panic()
- func (a *Assertion[T]) PanicWithMessage(msg string)
- func (a *Assertion[T]) Satisfies(cond Conditioner[T]) *Assertion[T]
- func (a *Assertion[T]) Zero() *Assertion[T]
- type Conditioner
- type EqualCond
- type ErrAssertionFailed
- type GenericCond
- type GreaterOrEqualThanCond
- type GreaterThanCond
- type InCond
- type InRangeCond
- type LessOrEqualThanCond
- type LessThanCond
- type ZeroCond
Constants ¶
const AssertionFailed string = "assertion failed: "
AssertionFailed is the message that is shown when an assertion fails.
Variables ¶
This section is empty.
Functions ¶
func Assert ¶
Assert panics iff the condition is false. The panic is not a string but an error of type *ErrAssertionFailed.
Parameters:
- cond: the condition to check.
- msg: the message to show if the condition is not met.
Example:
foo := "foo" Assert(foo == "bar", "foo is not bar") // panics: "assertion failed: foo is not bar"
func AssertConv ¶ added in v0.1.2
func AssertDeref ¶ added in v0.1.2
func AssertErr ¶
AssertErr is the same as Assert but for errors. Best used for ensuring that a function does not return an unexpected error.
Parameters:
- err: the error to check.
- format: the format describing the function's signature.
- args: the arguments passed to the function.
Example:
foo := "foo" err := my_function(foo, "bar") AssertErr(err, "my_function(%s, %s)", foo, "bar") // panics: "assertion failed: function my_function(foo, bar) returned the error: <err>"
func AssertF ¶
AssertF same as Assert but with a format string and arguments that are in accordance with fmt.Printf.
Parameters:
- cond: the condition to check.
- format: the format string to show if the condition is not met.
- args: the arguments to pass to the format string.
Example:
foo := "foo" bar := "bar" AssertF(foo == bar, "%s is not %s", foo, bar) // panics: "assertion failed: foo is not bar"
func AssertNotNil ¶ added in v0.1.2
func AssertOk ¶
AssertOk is the same as Assert but for booleans. Best used for ensuring that a function that are supposed to return the boolean `true` does not return `false`.
Parameters:
- cond: the result of the function.
- format: the format describing the function's signature.
- args: the arguments passed to the function.
Example:
foo := "foo" ok := my_function(foo, "bar") AssertOk(ok, "my_function(%s, %s)", foo, "bar") // panics: "assertion failed: function my_function(foo, bar) returned false while true was expected"
Types ¶
type Assertion ¶
Assertion is the struct that is used to perform assertions.
func AssertThat ¶
AssertThat is a constructor for the Assertion struct.
Parameters:
- name: the name of the parameter (or variable) to assert.
- val: the value of the parameter (or variable) to assert.
Returns:
- *Assertion: the assertion. Never returns nil.
A normal construction is a chain of AssertThat() function followed by the conditions and the action to perform.
Example:
foo := "foo"
AssertThat("foo", foo).Not().In("bar", "fooo", "baz").Panic()
// Does not panic since foo is not in ["bar", "fooo", "baz"]
func (*Assertion[T]) Applies ¶
Applies is the same as Satisfies but without needing to provide a custom definition that implements Conditioner. Best used for checks that are only done once.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- msg: the message of the condition.
- cond: the condition to check.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
func (*Assertion[T]) Check ¶
Check checks whether the condition is met.
Returns:
- bool: true if the condition is met. false otherwise.
func (*Assertion[T]) Equal ¶
Equal is the assertion for checking if the value is equal to another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
func (*Assertion[T]) Error ¶
func (a *Assertion[T]) Error() *ErrAssertionFailed
Error same as Panic but returns the *ErrAssertionFailed error instead of a panic.
The error message is "expected <name> to <message>; got <value> instead" where <name> is the name of the assertion, <message> is the message of the condition and <value> is the value of the assertion.
Returns:
- *ErrAssertionFailed: the error. Nil iff the condition is met.
func (*Assertion[T]) ErrorWithMessage ¶
ErrorWithMessage is the same as PanicWithMessage but returns the *ErrAssertionFailed error instead of a panic. This error message is overrides the default error message of the Assertion.
Of course, the message is still used within the *ErrAssertionFailed error.
Returns:
- *ErrAssertionFailed: the error. Nil iff the condition is met.
func (*Assertion[T]) GreaterOrEqualThan ¶
GreaterOrEqualThan is the assertion for checking if the value is greater or equal than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
func (*Assertion[T]) GreaterThan ¶
GreaterThan is the assertion for checking if the value is greater than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
func (*Assertion[T]) In ¶
In is the assertion for checking if the value is in a list of values.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- values: the list of values to check against.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
The list is sorted in ascending order and duplicates are removed. As a special case, if only one value is provided, the assertion will be equal to the EqualCond[T] with that value.
func (*Assertion[T]) InRange ¶
InRange is the assertion for checking if the value is in a range.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- min: the minimum value of the range.
- max: the maximum value of the range.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
If min is greater than max, the min and max values will be swapped. Moreover, if min is equal to max, the assertion will be equal to the EqualCond[T] with the min value.
func (*Assertion[T]) LessOrEqualThan ¶
LessOrEqualThan is the assertion for checking if the value is less or equal than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
func (*Assertion[T]) LessThan ¶
LessThan is the assertion for checking if the value is less than another value.
If any other condition is specified, the furthest condition overwrites any other condition.
Parameters:
- b: the other value to compare with.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
func (*Assertion[T]) Not ¶
Not negates the assertion. Useful for checking the negation of an assertion.
However, if the positive check is more expensive than its negative counterpart, it is suggested to create the negative assertion rather than negating the positive one.
Furthermore, if more than one Not() function is called on the same assertion, then if the count of the Not() functions is odd, the assertion will be negated. Otherwise, the assertion will be positive.
For example, doing .Not().Not().Not() is the same as .Not().
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
func (*Assertion[T]) Panic ¶
func (a *Assertion[T]) Panic()
Panic will panic if the condition is not met.
The error message is "expected <name> to <message>; got <value> instead" where <name> is the name of the assertion, <message> is the message of the condition and <value> is the value of the assertion. Finally, this error message is used within the *ErrAssertionFailed error.
func (*Assertion[T]) PanicWithMessage ¶
PanicWithMessage is the same as Panic but with a custom error message. This error message is overrides the default error message of the Assertion.
Of course, the message is still used within the *ErrAssertionFailed error.
func (*Assertion[T]) Satisfies ¶
func (a *Assertion[T]) Satisfies(cond Conditioner[T]) *Assertion[T]
Satisfies is the assertion for checking custom conditions.
If any other condition is specified, the furthest condition overwrites any other condition. However, if cond is nil, this function will be a no-op.
Parameters:
- cond: the condition to check.
Returns:
- *Assertion: the assertion for chaining. Never returns nil.
type Conditioner ¶
type Conditioner[T cmp.Ordered] interface { // Message returns the message that is shown when the condition is not met. // // Returns: // - string: the message. Message() string // Verify checks if the condition is met. In other words, whether the value // satisfies the condition. // // Parameters: // - value: the value to check. // // Returns: // - bool: true if the condition is met. false otherwise. Verify(value T) bool }
Conditioner is an interface that describes the behavior of the condition for the Assertion struct.
Since all messages are preceded by the "expected <value> to", it is important to make sure that the grammar is correct. Thus, a message of "be true" is shown as "expected <value> to be true; got <value> instead".
type EqualCond ¶
EqualCond is the condition for the Assertion struct and checks if the value is equal to another value.
type ErrAssertionFailed ¶
type ErrAssertionFailed struct {
// Msg is the message that is shown when the assertion fails.
Msg string
}
ErrAssertionFailed is the error returned when an assertion fails.
func NewErrAssertionFailed ¶
func NewErrAssertionFailed(msg string) *ErrAssertionFailed
NewErrAssertionFailed is a constructor for ErrAssertionFailed.
Parameters:
- msg: the message that is shown when the assertion fails.
Returns:
- *ErrAssertionFailed: the error. Never returns nil.
func (*ErrAssertionFailed) Error ¶
func (e *ErrAssertionFailed) Error() string
Error implements the error interface.
The message is prefixed with the AssertionFailed constant.
type GenericCond ¶
type GenericCond[T any] struct { // contains filtered or unexported fields }
GenericCond is the condition for the Assertion struct and is used for specifying custom conditions without having to write a custom Conditioner.
func (*GenericCond[T]) Message ¶
func (gc *GenericCond[T]) Message() string
Message implements the Conditioner interface.
The returned message is the result of the message function and, if no message function was provided, an empty string is returned.
func (*GenericCond[T]) Verify ¶
func (gc *GenericCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
The returned verification is the result of the verify function and, if no verify function was provided, true is returned.
type GreaterOrEqualThanCond ¶
GreaterOrEqualThanCond is the condition for the Assertion struct and checks if the value is greater or equal than another value.
func (*GreaterOrEqualThanCond[T]) Message ¶
func (gtec *GreaterOrEqualThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be greater or equal than <other>"
func (*GreaterOrEqualThanCond[T]) Verify ¶
func (gtec *GreaterOrEqualThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type GreaterThanCond ¶
GreaterThanCond is the condition for the Assertion struct and checks if the value is greater than another value.
func (*GreaterThanCond[T]) Message ¶
func (gtc *GreaterThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be greater than <other>"
func (*GreaterThanCond[T]) Verify ¶
func (gtc *GreaterThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type InCond ¶
InCond is the condition for the Assertion struct and checks if the value is in a list of values.
type InRangeCond ¶
InRangeCond is the condition for the Assertion struct and checks if the value is in a range. Both bounds are included in the range.
func (*InRangeCond[T]) Message ¶
func (irc *InRangeCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be in range [<min> : <max>]"
func (*InRangeCond[T]) Verify ¶
func (irc *InRangeCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type LessOrEqualThanCond ¶
LessOrEqualThanCond is the condition for the Assertion struct and checks if the value is less or equal than another value.
func (*LessOrEqualThanCond[T]) Message ¶
func (letc *LessOrEqualThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be less or equal than <other>"
func (*LessOrEqualThanCond[T]) Verify ¶
func (letc *LessOrEqualThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.
type LessThanCond ¶
LessThanCond is the condition for the Assertion struct and checks if the value is less than another value.
func (*LessThanCond[T]) Message ¶
func (ltc *LessThanCond[T]) Message() string
Message implements the Conditioner interface.
Message: "be less than <other>"
func (*LessThanCond[T]) Verify ¶
func (ltc *LessThanCond[T]) Verify(value T) bool
Verify implements the Conditioner interface.