Giter VIP home page Giter VIP logo

go-assert's Introduction

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...))
}

go-assert's People

Contributors

jumpaku avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.