assert

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: BSD-3-Clause Imports: 4 Imported by: 46

Documentation

Index

Constants

View Source
const (
	// ASSERT_PREFIX is the prefix for assertion errors.
	ASSERT_PREFIX string = "[ASSERT]: "
)
View Source
const (
	// DEFAULT_ERR_MSG is the error message used for when errors returns
	// empty messages.
	DEFAULT_ERR_MSG string = "something went wrong"
)
View Source
const (
	// MAX_UINT32 is the maximum value of a uint32.
	MAX_UINT32 uint64 = 0xFFFFFFFF
)

Variables

This section is empty.

Functions

func CastFloat added in v1.0.1

func CastFloat[E ~float32 | ~float64](v E, name string) (int32, error)

CastFloat casts a variable of a floating-point type to int32. If the variable is too large or too small, CastFloat returns an assert error with a custom error message.

Parameters:

  • v: The variable to assert.
  • name: The name of the variable.

Returns:

  • int32: The variable casted to int32.
  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the variable is too large or too small.

Format:

"[ASSERT]: <name> is too large"

Where, <name> is the name of the variable. Defaults to "var" if empty.

However, if the variable is too small, the error message is as follows:

"[ASSERT]: <name> is too small"

Example:

var x float64 = 5.5

res, err := CastFloat(x, "x")
if err != nil {
	panic(err)
}

func CastInt added in v1.0.1

func CastInt[E ~int | ~int8 | ~int16 | ~int32 | ~int64](v E, name string) (uint32, error)

CastInt casts a variable of an integer type to uint32, ensuring that it is not negative nor greater than slices.MAX_SLICE_SIZE. If the variable is negative or too big, CastInt returns an assert error with a custom error message.

Parameters:

  • v: The variable to assert.
  • name: The name of the variable.

Returns:

  • uint32: The variable casted to uint32. It is never greater than MAX_UINT32.
  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the variable is negative or too big.

Format:

"[ASSERT]: <name> <msg>"

Where:

  • <name> is the name of the variable. Defaults to "var" if empty.
  • <msg> is the error message. If the variable is negative, it is "is negative". If the variable is too big, it is "is too large".

Example:

var x int32 = 5

res, err := CastInt(x, "x")
if err != nil {
	panic(err)
}

func Cond

func Cond(cond bool, msg string) error

Cond asserts that the given condition is `true`. If not, Cond returns an assert error with the given error message.

Parameters:

  • cond: The condition to check.
  • msg: The error message to use if the condition is not `true`.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the condition is not `true`.

Format:

"[ASSERT]: <msg>"

Where, <msg> is the error message. Defaults to [DefaultCause] if empty.

Example:

const x int = 5

if err := Cond(x > 0, "x <= 0"); err != nil {
	panic(err)
}

func Deref added in v1.0.1

func Deref[E any](e *E, name string) (E, error)

Deref dereferences a pointer and returns the value it points to. If the pointer is `nil`, Deref returns an assert error with a custom error message.

Parameters:

  • e: The pointer to dereference.
  • name: The name of the pointer.

Returns:

  • E: The value the pointer points to.
  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the pointer is `nil`.

Format:

"[ASSERT]: <name> is nil"

Where, <name> is the name of the pointer. Defaults to "var" if empty.

func Equal

func Equal[E comparable](a, b E, n1, n2 string) error

Equal asserts that two variables are equal. If they are not equal, Equal returns an assert error with a custom error message.

For convenience, Equal considers NaN to never be equal to another value, not even itself.

Parameters:

  • a, b: The variables to assert.
  • n1, n2: The names of the variables `a` and `b`, respectively.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the variables are not equal.

Format:

"[ASSERT]: <n1> != <n2>"

Where, <n1> and <n2> are the names of the variables, respectively. Default to "var" if empty.

Example:

var x int = 5
var y int = 5

if err := Equal(x, y, "x", "y"); err != nil {
	panic(err)
}

func False

func False(ok bool, fcall string) error

False is the same as True but asserts that the return value of the function call is `false`. If the return value of the function call is not `false`, False returns an assert error with a custom error message.

Parameters:

  • ok: The return value of the function call that the condition is coming from.
  • fcall: The name of the function that is being called.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the return value of the function call is not `false`.

Format:

"[ASSERT]: <fcall>() returns true"

Where, <fcall> is the function call that the condition is coming from. Defaults to "func" if empty.

Example:

const x int = 5

ok := IsEven(x)
if err := False(ok, "IsEven"); err != nil {
	panic(err)
}

func Map

func Map[K comparable, V any](m map[K]V, name string) error

Map asserts that a map is not `nil`. If it is `nil`, Map returns an assert error with a custom error message.

Parameters:

  • m: The map to assert.
  • name: The name of the map.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the map is `nil`.

Format:

"[ASSERT]: <name> is nil"

Where, <name> is the name of the map. Defaults to "map" if empty.

Example:

m := make(map[int]int)

if err := Map(m, "m"); err != nil {
	panic(err)
}

func New

func New[E any](v *E, cause error) error

New is a composite of NotNil and [NotErr] that asserts that a constructor returns a non-`nil` value when no error is returned. If the constructor returns an error or the value is `nil`, New returns an assert error with a custom error message.

Parameters:

  • v: The value returned by the constructor.
  • cause: The error returned by the constructor.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the constructor returns an error or the value is `nil`.

Format:

"[ASSERT]: new() returns <cause>"

Where, <cause> is the error message. Defaults to errors.DEFAULT_ERR_MSG if empty.

However, if the cause is `nil` but the value is not `nil`, the error message is as follows:

"[ASSERT]: new() returns nil"

Example:

res, err := NewMyObject()
if err := New(res, err); err != nil {
	panic(err)
}

func Nil

func Nil[E any](v *E, name string) error

Nil asserts that a given pointer is `nil`. If the pointer is not `nil`, Nil returns an assert error with a custom error message.

Parameters:

  • v: The pointer to the variable to assert.
  • name: The name of the variable.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the pointer is not `nil`.

Format:

"[ASSERT]: <name> not nil"

Where, <name> is the name of the variable. Defaults to "var" if empty.

Example:

var x *int

if err := Nil(x, "x"); err != nil {
	panic(err)
}

func NotEqual

func NotEqual[E comparable](a, b E, n1, n2 string) error

NotEqual is like Equal but asserts that two variables are not equal. If they are equal, NotEqual returns an assert error with a custom error message.

For convenience, NotEqual considers NaN to always be not equal to another value, including itself.

Parameters:

  • a, b: The variables to assert.
  • n1, n2: The names of the variables `a` and `b`, respectively.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the variables are equal.

Format:

"[ASSERT]: <n1> == <n2>"

Where, <n1> and <n2> are the names of the variables, respectively. Default to "var" if empty.

Example:

var x int = 5
var y int = 6

if err := NotEqual(x, y, "x", "y"); err != nil {
	panic(err)
}

func NotError added in v0.1.2

func NotError(inner error, fcall string) error

NotError is like True but asserts that a function call is successful (i.e., the error is `nil`). If the error is not `nil`, [NotErr] returns an assert error with a custom error message.

Parameters:

  • inner: The error returned by the function call.
  • fcall: The name of the function that is being called.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the error is not `nil`.

Format:

"[ASSERT]: <fcall>() returns <error>"

Where:

  • <fcall> is the function call that the error is coming from. Defaults to "func" if empty.
  • <error> is the message of the error. Defaults to errors.DEFAULT_ERR_MSG if empty. It is quoted using strconv.Quote.

Example:

const x int = 5

err := IsOdd(x)
if err := NotErr(err, "IsOdd"); err != nil {
	panic(err)
}

func NotNegative

func NotNegative[E ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64](v E, name string) error

NotNegative asserts that a variable of an integer type is not negative. If the variable is negative, NotNegative returns an assert error with a custom error message.

Parameters:

  • v: The variable to assert.
  • name: The name of the variable.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the variable is negative.

Format:

"[ASSERT]: <name> is negative"

Where, <name> is the name of the variable. Defaults to "var" if empty.

Example:

var x int32 = 5

if err := NotNegative(x, "x"); err != nil {
	panic(err)
}

func NotNil

func NotNil[E any](v *E, name string) error

NotNil asserts that a given pointer is not `nil`. If the pointer is `nil`, NotNil returns an assert error with a custom error message.

Parameters:

  • v: The pointer to the variable to assert.
  • name: The name of the variable.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the pointer is `nil`.

Format:

"[ASSERT]: <name> is nil"

Where, <name> is the name of the variable. Defaults to "var" if empty.

Example:

var x int = 5

if err := NotNil(&x, "x"); err != nil {
	panic(err)
}

func NotZero

func NotZero[E comparable](v E, name string) error

NotZero asserts that a variable of a comparable type is not its zero value. If the variable is zero, NotZero returns an assert error with a custom error message.

For convenience, NotZero considers NaN to be the zero value of a floating-point type.

Parameters:

  • v: The variable to assert.
  • name: The name of the variable.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the variable is zero.

Format:

"[ASSERT]: <name> is zero"

Where, <name> is the name of the variable. Defaults to "var" if empty.

Example:

var x int = 5

if err := NotZero(x, "x"); err != nil {
	panic(err)
}

func Seq

func Seq[E any](seq iter.Seq[E], name string) error

Seq is like NotNil but for sequences. If the sequence is `nil`, Seq returns an assert error with a custom error message.

Parameters:

  • seq: The sequence to assert.
  • name: The name of the sequence.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the sequence is `nil`.

Format:

"[ASSERT]: <name> is nil"

Where, <name> is the name of the sequence. Defaults to "seq" if empty.

Example:

seq := MyObject.Forward()
if err := Seq(seq, "seq"); err != nil {
	panic(err)
}

func Seq2

func Seq2[A, B any](seq iter.Seq2[A, B], name string) error

Seq2 is like Seq but for sequences having two elements. If the sequence is `nil`, Seq2 returns an assert error with a custom error message.

Parameters:

  • seq: The sequence to assert.
  • name: The name of the sequence.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the sequence is `nil`.

Format:

"[ASSERT]: <name> is nil"

Where, <name> is the name of the sequence. Defaults to "seq" if empty.

Example:

seq := MyObject.Forward2()
if err := Seq2(seq, "seq"); err != nil {
	panic(err)
}

func True

func True(ok bool, fcall string) error

True is like Cond but for return values of function calls. If the return value of the function call is not `true`, True returns an assert error with a custom error message.

Parameters:

  • ok: The return value of the function call that the condition is coming from.
  • fcall: The name of the function that is being called.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the return value of the function call is not `true`.

Format:

"[ASSERT]: <fcall>() returns false"

Where, <fcall> is the function call that the condition is coming from. Defaults to "func" if empty.

Example:

const x int = 5

ok := IsOdd(x)
if err := True(ok, "IsOdd"); err != nil {
	panic(err)
}

func Type

func Type[T any](v any, name string) error

Type asserts that a variable is not `nil` and of the specified type. If the variable is not of the specified type or `nil`, Type returns an assertion error with a custom error message.

Parameters:

  • v: The variable to assert.
  • name: The name of the variable.

Returns:

  • error: An error if the assertion fails.

Errors:

  • *ErrAssert: If the variable is not of the specified type or `nil`.

Format:

"[ASSERT]: <name> not <type>"

Where:

  • <name> is the name of the variable. Defaults to "var" if empty.
  • <type> is the type of the variable.

Example:

var x int = 5

if err := Type[int](x, "x"); err != nil {
	panic(err)
}

Types

type ErrAssert

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

ErrAssert is the error type for assertions. It is almost always panic'd. However, when callees return it as value, it is up to the callers to panic it. In such cases, the callee MUST specify in the code documentation that an assertion error can be returned.

func NewErrAssert

func NewErrAssert(cause string) *ErrAssert

NewErrAssert creates assertion errors with the given error message.

Parameters:

  • message: The reason the assertion failed.

Returns:

  • *ErrAssert: The newly created assertion error. It is never `nil`.

Format:

"[ASSERT]: <cause>"

Where, <cause> is the given message. Defaults to DEFAULT_ERR_MSG if empty.

func (ErrAssert) Error

func (e ErrAssert) Error() string

Error implements error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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