Documentation ¶
Index ¶
- func Cond(cond bool, msg string)
- func Condf(cond bool, format string, args ...any)
- func Conv[T any](v any, name string) T
- func Deref[T any](v any, name string) T
- func Err(inner error, format string, args ...any)
- func False(ok bool, format string, args ...any)
- func NewErrAssertFail(msg string) error
- func NotZero[T comparable](v T, name string)
- func True(ok bool, format string, args ...any)
- func Type[T any](v any, name string, allow_nil bool)
- type ErrAssertFail
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cond ¶
Cond checks a condition and if it is not true, panics with ErrAssertFail.
Parameters:
- cond: The condition to check.
- msg: The error message to use if the condition is not true.
Panics:
- ErrAssertFail: If the condition is not true.
func Condf ¶
Condf checks a condition and if it is not true, panics with ErrAssertFail.
Parameters:
- cond: The condition to check.
- format: The format string for the error message to use if the condition is not true.
- args: The arguments to use with the format string.
Panics:
- ErrAssertFail: If the condition is not true.
func Conv ¶ added in v0.1.1
Conv asserts whether the variable is of type T. If not, it panics with an ErrAssertFailed error. Unlike with Type(), this returns the converted type as well.
Parameters:
- v: The variable to assert.
- name: The name of the variable. If empty, the name "variable" is used.
Example:
v := "foo" res := Conv[int](v, "v") // Panics: v = string, want int
func Deref ¶
Deref asserts whether the variable is both non-nil and is of type T. If not, it panics with an ErrAssertFailed error.
Parameters:
- v: The variable to assert.
- name: The name of the variable. If empty, the name "variable" is used.
Returns:
- T: The dereferenced variable.
Example:
var v *int _ = Deref[string](v, "v") // Panics: v = *int, want string
func Err ¶
Err panics with ErrAssertFail if the given inner error is not nil. The error message will be the given format string with the given arguments followed by " = " and the error message of the inner error. If the given format string is empty, the error message will be "func() = " followed by the error message of the inner error.
Parameters:
- inner: The inner error to check.
- format: The format string for the error message.
- args: The arguments to use with the format string.
Panics:
- ErrAssertFail: If the inner error is not nil.
func False ¶
False checks whether a boolean condition is false. If not, it panics with ErrAssertFail using the given format string and arguments. If the format string is empty, "func() = true" is used as the error message.
Parameters:
- ok: The boolean condition to check.
- format: The format string for the error message if the condition is true.
- args: The arguments to use with the format string.
Panics:
- ErrAssertFail: If the condition is true.
func NewErrAssertFail ¶ added in v0.1.3
NewErrAssertFail creates and returns a new ErrAssertFail error with the specified error message.
Parameters:
- msg: The error message.
Returns:
- error: A pointer to the newly created ErrAssertFail. Never returns nil.
Format:
"[ASSERT FAIL]: <msg>"
Where:
- <msg> is the error message. If empty, defaults to "an assertion was not met".
func NotZero ¶
func NotZero[T comparable](v T, name string)
NotZero asserts whether the variable is not its zero value. If not, it panics with an ErrAssertFailed error.
Parameters:
- v: The variable to assert.
- name: The name of the variable. If empty, "variable" is used.
Example:
v := 0 NotZero[int](v, "v") // Panics: v is zero
func True ¶
True checks whether a boolean condition is true. If not, it panics with ErrAssertFail using the given format string and arguments. If the format string is empty, "func() = false" is used as the error message.
Parameters:
- ok: The boolean condition to check.
- format: The format string for the error message if the condition is false.
- args: The arguments to use with the format string.
Panics:
- ErrAssertFail: If the condition is false.
func Type ¶
Type asserts whether the variable is of type T. If not, it panics with an ErrAssertFailed error.
Parameters:
- v: The variable to assert.
- name: The name of the variable. If empty, the name "variable" is used.
- allow_nil: Whether to allow the variable to be nil.
Example:
v := "foo" Type[int](v, "v", false) // Panics: v = string, want int
Types ¶
type ErrAssertFail ¶ added in v0.1.3
type ErrAssertFail struct { // Msg is the error message. Msg string }
ErrAssertFail occurs when an assertion is not met.
func (ErrAssertFail) Error ¶ added in v0.1.3
func (e ErrAssertFail) Error() string
Error implements error.