Documentation
¶
Index ¶
- Constants
- func CastFloat[E ~float32 | ~float64](v E, name string) (int32, error)
- func CastInt[E ~int | ~int8 | ~int16 | ~int32 | ~int64](v E, name string) (uint32, error)
- func Cond(cond bool, msg string) error
- func Deref[E any](e *E, name string) (E, error)
- func Equal[E comparable](a, b E, n1, n2 string) error
- func False(ok bool, fcall string) error
- func Map[K comparable, V any](m map[K]V, name string) error
- func New[E any](v *E, cause error) error
- func Nil[E any](v *E, name string) error
- func NotEqual[E comparable](a, b E, n1, n2 string) error
- func NotError(inner error, fcall string) error
- func NotNegative[E ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64](v E, name string) error
- func NotNil[E any](v *E, name string) error
- func NotZero[E comparable](v E, name string) error
- func Seq[E any](seq iter.Seq[E], name string) error
- func Seq2[A, B any](seq iter.Seq2[A, B], name string) error
- func True(ok bool, fcall string) error
- func Type[T any](v any, name string) error
- type ErrAssert
Constants ¶
const ( // ASSERT_PREFIX is the prefix for assertion errors. ASSERT_PREFIX string = "[ASSERT]: " )
const ( // DEFAULT_ERR_MSG is the error message used for when errors returns // empty messages. DEFAULT_ERR_MSG string = "something went wrong" )
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
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
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 ¶
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
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 ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.