assert

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2024 License: MIT Imports: 4 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cond

func Cond(cond bool, msg string)

Cond asserts that a condition is true.

If the condition is false, it calls panic() with an error.Err with the code AssertFail and the given message.

Parameters:

  • cond: The condition to assert.
  • msg: The message to use if the condition is false.

func CondF

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

CondF asserts that a condition is true.

If the condition is false, it calls panic() with an error.Err with the code AssertFail and a message that includes the formatted string.

Parameters:

  • cond: The condition to assert.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func Conv

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

Conv asserts that the given object can be converted to type T.

If the object can be converted to type T, it returns the converted value. Otherwise, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object and its expected type.

Parameters:

  • obj: The object to convert to type T.
  • name: The name of the object to use for the error message.

Returns:

  • T: The converted value.

func Err

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

Err asserts that an error is nil.

If the error is not nil, it calls panic() with an error.Err with the code AssertFail and a message that includes the original error.

Parameters:

  • err: The error to check.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func Fix

func Fix(name string, obj Fixer, allow_nil bool)

Fix fixes the object.

Parameters:

  • name: The name of the object.
  • obj: The object to fix.
  • allow_nil: Whether to allow the object to be nil.

Panics if the object can not be fixed.

func New

func New[T gers.Pointer](obj T, inner error) T

New asserts a constructor returns a non-zero value.

If the constructor returns a zero value, it calls panic() with an error.Err with the code AssertFail and a message that includes the original error.

Parameters:

  • obj: The object returned by the constructor.
  • inner: The error returned by the constructor.

Returns:

  • T: The non-zero value returned by the constructor.

func NotNil

func NotNil(obj any, name string)

NotNil asserts that the given object is not nil.

If the object is nil, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object.

Parameters:

  • obj: The object to assert is not nil.
  • name: The name of the object to use for the error message.

func NotOk

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

NotOk asserts that a condition is false.

If the condition is true, it calls panic() with an error.Err with the code AssertFail and a message that includes the original condition.

Parameters:

  • ok: The condition to assert.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func NotZero

func NotZero[T comparable](obj T, name string)

NotZero asserts that the given object is not its zero value.

If the object is its zero value, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object and its zero value.

Parameters:

  • obj: The object to assert is not its zero value.
  • name: The name of the object to use for the error message.

func Ok

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

Ok asserts that a condition is true.

If the condition is false, it calls panic() with an error.Err with the code AssertFail and a message that includes the original condition.

Parameters:

  • ok: The condition to assert.
  • format: The format string to use for the message.
  • args: The arguments to pass to the format string.

func Type

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

Type asserts that the given object is of type T.

If the object is not of type T, it calls panic() with an error.Err with the code AssertFail and a message that includes the original object and its expected type.

Parameters:

  • obj: The object to assert is of type T.
  • name: The name of the object to use for the error message.
  • allow_nil: Whether to allow the object to be nil.

func Validate

func Validate(name string, obj Validater, allow_nil bool)

Validate validates the object.

Parameters:

  • name: The name of the object.
  • obj: The object to validate.
  • allow_nil: Whether to allow the object to be nil.

Panics if the object's internal state is invalid.

Types

type ErrorCode

type ErrorCode int

ErrorCode is the type of the error code.

const (
	// AssertFail occurs when a test or assertion fails.
	AssertFail ErrorCode = iota

	// InvalidState is a type of assertion that occurs when
	// a method is called on a struct having an invalid state.
	InvalidState

	// FailFix occurs when a struct cannot be fixed or resolved
	// due to an invalid internal state.
	FailFix
)

func (ErrorCode) Int

func (e ErrorCode) Int() int

Int implements the error.ErrorCoder interface.

func (ErrorCode) String

func (i ErrorCode) String() string

type Fixer

type Fixer interface {
	// Fix fixes the object.
	//
	// Returns:
	//   - error: An error that occurred while fixing the object.
	Fix() error
}

Fixer is defines the behavior of an object that can be fixed. This must not have a non-pointer receiver.

type Validater

type Validater interface {
	// Validate validates the object.
	//
	// Returns:
	//   - error: An error that occurred while validating the object.
	Validate() error
}

Validater is an interface that all validaters must implement.

Jump to

Keyboard shortcuts

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