assert

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: MIT Imports: 5 Imported by: 6

Documentation

Index

Constants

View Source
const AssertionFailed string = "assertion failed: "

AssertionFailed is the message that is shown when an assertion fails.

Variables

This section is empty.

Functions

func Assert

func Assert(cond bool, msg string)

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 AssertConv[T any](elem any, var_name string) T

func AssertDeref added in v0.1.2

func AssertDeref[T any](elem *T, param_name string) T

func AssertErr

func AssertErr(err error, format string, args ...any)

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

func AssertF(cond bool, format string, args ...any)

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 AssertNotNil(elem any, param_name string)

func AssertOk

func AssertOk(cond bool, format string, args ...any)

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"

func AssertTypeOf added in v0.1.2

func AssertTypeOf[T any](elem any, var_name string, allow_nil bool)

Types

type Assertion

type Assertion[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

Assertion is the struct that is used to perform assertions.

func AssertThat

func AssertThat[T cmp.Ordered](name string, val T) *Assertion[T]

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

func (a *Assertion[T]) Applies(msg func() string, cond func(value T) bool) *Assertion[T]

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

func (a *Assertion[T]) Check() bool

Check checks whether the condition is met.

Returns:

  • bool: true if the condition is met. false otherwise.

func (*Assertion[T]) Equal

func (a *Assertion[T]) Equal(b T) *Assertion[T]

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

func (a *Assertion[T]) ErrorWithMessage(msg string) error

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

func (a *Assertion[T]) GreaterOrEqualThan(b T) *Assertion[T]

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

func (a *Assertion[T]) GreaterThan(b T) *Assertion[T]

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

func (a *Assertion[T]) In(values ...T) *Assertion[T]

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

func (a *Assertion[T]) InRange(min, max T) *Assertion[T]

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

func (a *Assertion[T]) LessOrEqualThan(b T) *Assertion[T]

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

func (a *Assertion[T]) LessThan(b T) *Assertion[T]

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

func (a *Assertion[T]) Not() *Assertion[T]

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

func (a *Assertion[T]) PanicWithMessage(msg string)

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.

func (*Assertion[T]) Zero

func (a *Assertion[T]) Zero() *Assertion[T]

Zero is the assertion for checking if the value is the zero value for its type.

If any other condition is specified, the furthest condition overwrites any other condition.

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

type EqualCond[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

EqualCond is the condition for the Assertion struct and checks if the value is equal to another value.

func (*EqualCond[T]) Message

func (ec *EqualCond[T]) Message() string

Message implements the Conditioner interface.

Message: "have the value <other>"

func (*EqualCond[T]) Verify

func (ec *EqualCond[T]) Verify(value T) bool

Verify implements the Conditioner interface.

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

type GreaterOrEqualThanCond[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

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

type GreaterThanCond[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

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

type InCond[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

InCond is the condition for the Assertion struct and checks if the value is in a list of values.

func (*InCond[T]) Message

func (iic *InCond[T]) Message() string

Message implements the Conditioner interface.

Message: "be one of {<value1>, <value2>, ...}"

func (*InCond[T]) Verify

func (iic *InCond[T]) Verify(value T) bool

Verify implements the Conditioner interface.

type InRangeCond

type InRangeCond[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

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

type LessOrEqualThanCond[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

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

type LessThanCond[T cmp.Ordered] struct {
	// contains filtered or unexported fields
}

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.

type ZeroCond

type ZeroCond[T cmp.Ordered] struct{}

ZeroCond is the condition for the Assertion struct and checks if the value is the zero value for its type. For example, 0 for int, 0.0 for float, "" for string, etc.

func (*ZeroCond[T]) Message

func (izc *ZeroCond[T]) Message() string

Message implements the Conditioner interface.

Message: "be its zero value"

func (*ZeroCond[T]) Verify

func (izc *ZeroCond[T]) Verify(value T) bool

Verify implements the Conditioner interface.

Jump to

Keyboard shortcuts

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