assert

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: MIT Imports: 1 Imported by: 5

README

go-verify

A small Go package that contains helping functions for testing, assertions, and coded errors.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cond

func Cond(cond bool, msg string)

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

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

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

func Conv[T any](v any, name string) T

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

func Deref[T any](v any, name string) T

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

func Err(inner error, format string, args ...any)

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

func False(ok bool, format string, args ...any)

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

func NewErrAssertFail(msg string) error

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

func True(ok bool, format string, args ...any)

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

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

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.

Directories

Path Synopsis
OLD

Jump to

Keyboard shortcuts

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