assert

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2023 License: BSD-2-Clause Imports: 1 Imported by: 10

README

go-assert

A lightweight Go library for asserting conditions related to parameters and object state.

Overview

go-assert is a lightweight Go library designed to clearly declare assertions for the conditions related to parameters and object state within your codebase.

The library provides several key functions:

  1. assert.Params: This function is used to assert conditions on input parameters. If a provided condition is not met, the function panics with an error message indicating invalid parameters.

  2. assert.State: This function is used to assert conditions related to object state, such as, object properties, constraints, or invariants. If the condition is false, the function panics with an error message indicating invalid state.

  3. assert.Unexpected: This function is used to assert unexpected code paths. If the function is called, it panics with an error message indicating that the execution has reached an unexpected point. This is useful for signaling unexpected situations during program execution.

  4. assert.UnexpectedN: These functions are variations of the "Unexpected" function, but they can also be used at return statements of functions.

The "go-assert" library promotes code quality by ensuring that parameters and object states meet expected conditions. It provides concise and expressive syntax for asserting conditions, helping developers enforce best practices and reduce the likelihood of bugs.

Functions

package assert

import "fmt"

// Params panics with an error containing a formatted message if the condition is false.
// This assertion ensures that the parameters satisfy the condition.
func Params(condition bool, format string, args ...any) {
	if !condition {
		panic(fmt.Errorf(`invalid params: `+format, args...))
	}
}

// State panics with an error containing a formatted message if the condition is false.
// This assertion ensures that the state of an object satisfies the condition.
func State(condition bool, format string, args ...any) {
	if !condition {
		panic(fmt.Errorf(`invalid state: `+format, args...))
	}
}

// Unexpected panics with an error containing a formatted message when it is called.
// This assertion represents that reaching this code is unexpected.
func Unexpected(format string, args ...any) {
	panic(fmt.Errorf(`unexpected code execution: `+format, args...))
}

// Unexpected1 panics with an error containing a formatted message when it is called.
// This assertion represents that reaching this code is unexpected.
// This function call can be used as a value to be returned.
func Unexpected1[T any](format string, args ...any) T {
	panic(fmt.Errorf(`unexpected code execution: `+format, args...))
}

// Unexpected2 panics with an error containing a formatted message when it is called.
// This assertion represents that reaching this code is unexpected.
// This function call can be used as values to be returned.
func Unexpected2[T1, T2 any](format string, args ...any) (T1, T2) {
	panic(fmt.Errorf(`unexpected code execution: `+format, args...))
}

// Unexpected3 panics with an error containing a formatted message when it is called.
// This assertion represents that reaching this code is unexpected.
// This function call can be used as values to be returned.
func Unexpected3[T1, T2, T3 any](format string, args ...any) (T1, T2, T3) {
	panic(fmt.Errorf(`unexpected code execution: `+format, args...))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Params

func Params(condition bool, format string, args ...any)

Params panics with an error containing a formatted message if the condition is false. This assertion ensures that the parameters satisfy the condition.

func State

func State(condition bool, format string, args ...any)

State panics with an error containing a formatted message if the condition is false. This assertion ensures that the state of an object satisfies the condition.

func Unexpected

func Unexpected(format string, args ...any)

Unexpected panics with an error containing a formatted message when it is called. This assertion represents that reaching this code is unexpected.

func Unexpected1

func Unexpected1[T any](format string, args ...any) T

Unexpected1 panics with an error containing a formatted message when it is called. This assertion represents that reaching this code is unexpected. This function call can be used as a value to be returned, for example: ```go type Enum int

const (

Enum0 Enum = iota
Enum1

)

func F(enum Enum) string {
    switch enum {
    case Enum0:
         return "A"
    case Enum1:
        return "B"
    default:
        return Unexpected1[string]("unexpected enum value")
    }
}

func Unexpected2

func Unexpected2[T1, T2 any](format string, args ...any) (T1, T2)

Unexpected2 panics with an error containing a formatted message when it is called. This assertion represents that reaching this code is unexpected. This function call can be used as values to be returned, for example: ```go type Enum int

const (

Enum0 Enum = iota
Enum1

)

func F(enum Enum) (string, int) {
    switch enum {
    case Enum0:
        return "A", int(enum)
    case Enum1:
        return "B", int(enum)
    default:
        return Unexpected2[string, int]("unexpected enum value")
    }
}

func Unexpected3

func Unexpected3[T1, T2, T3 any](format string, args ...any) (T1, T2, T3)

Unexpected3 panics with an error containing a formatted message when it is called. This assertion represents that reaching this code is unexpected. This function call can be used as values to be returned, for example: ```go type Enum int

const (

Enum0 Enum = iota
Enum1

)

func F(enum Enum) (string, int, bool) {
    switch enum {
    case Enum0:
        return "A", int(enum), true
    case Enum1:
        return "B", int(enum), false
    default:
        return Unexpected3[string, int, bool]("unexpected enum value")
    }
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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