check

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 10 Imported by: 0

README

check Go

A lightweight, panic-based assertion library for Go with built-in fault injection.

Features

  • Type-safe assertions using Go generics
  • Panic-based testing for fail-fast behavior
  • Zero dependencies - uses only the Go standard library
  • Fault injection for chaos engineering and reliability testing
  • Stack trace capture when converting panics to errors

Installation

go get github.com/eliothedeman/check

Quick Start

package mypackage_test

import (
    "testing"
    "github.com/eliothedeman/check"
)

func TestMyFunction(t *testing.T) {
    result := MyFunction()

    // Assertions panic if they fail
    check.Eq(result.Status, "success")
    check.GT(result.Count, 0)
    check.NotNil(result.Data)
}

API Reference

Equality and Comparison
// Equality
check.Eq(a, b)       // Panics if a != b
check.NotEq(a, b)    // Panics if a == b

// Ordered comparisons
check.GT(a, b)       // Panics if a <= b (greater than)
check.LT(a, b)       // Panics if a >= b (less than)
check.GTE(a, b)      // Panics if a < b (greater than or equal)
check.LTE(a, b)      // Panics if a > b (less than or equal)

// Range checks
check.Between(a, low, high)           // Panics if a is not strictly between low and high
check.BetweenInclusive(a, low, high)  // Panics if a < low or a > high
Type and Nil Checks
check.Nil(x)        // Panics if x is not nil
check.NotNil(x)     // Panics if x is nil
check.Is[T](a)      // Panics if a is not of type T
Error Handling
// Error matching (uses errors.Is)
check.ErrIs(err, target)

// Verify a function panics
check.Panics(func() {
    // Code that should panic
})

// Convert panics to errors with stack traces
result, err := check.Catch(func() string {
    check.GT(0, 1)  // This will panic
    return "success"
})
// err will contain the panic with full stack trace
Fault Injection

Inject controlled failures for chaos engineering and reliability testing:

// Configure a fault point with 50% failure rate
check.ErrCfg("database_query", check.Prob[float64](0.5))

// In your code, add fault injection points
func QueryDatabase() error {
    if err := check.ErrPoint("database_query"); err != nil {
        return err
    }
    // Normal database logic
    return nil
}

Examples

Basic Assertions
func TestUserValidation(t *testing.T) {
    user := CreateUser("alice", 25)

    check.NotNil(user)
    check.Eq(user.Name, "alice")
    check.GT(user.Age, 0)
    check.Between(user.Age, 18, 120)
}
Error Checking
func TestFileOperations(t *testing.T) {
    err := OpenNonexistentFile()
    check.ErrIs(err, os.ErrNotExist)

    // Works with wrapped errors
    wrapped := fmt.Errorf("failed to open: %w", os.ErrNotExist)
    check.ErrIs(wrapped, os.ErrNotExist)
}
Panic Recovery with Stack Traces
func TestDangerousOperation(t *testing.T) {
    result, err := check.Catch(func() int {
        check.GT(0, 1)  // This panics
        return 42
    })

    if err != nil {
        // err contains the panic message and full stack trace
        t.Logf("Operation failed: %v", err)
    }
}
Fault Injection for Reliability Testing (API not stable)
func TestRetryLogic(t *testing.T) {
    // Configure fault point to fail 80% of the time
    check.ErrCfg("api_call", check.Prob[float64](0.8))

    // Test that retry logic handles failures
    var attempts int
    for attempts < 5 {
        if err := MakeAPICall(); err == nil {
            break
        }
        attempts++
    }

    check.GT(attempts, 1)  // Verify retries occurred
}

func MakeAPICall() error {
    if err := check.ErrPoint("api_call"); err != nil {
        return err
    }
    // Normal API logic
    return nil
}

Design Philosophy

Panic-based assertions: Unlike traditional assertion libraries that return booleans, check uses panics to signal failures. This provides a fail-fast approach that integrates naturally with Go's testing framework through recover().

Type safety: Leverages Go generics to provide compile-time type safety for comparisons and assertions.

Minimal dependencies: Uses only the Go standard library, keeping the dependency tree clean and reducing maintenance burden.

Chaos engineering: Built-in fault injection allows you to test your code's resilience to failures without complex mocking frameworks.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFault = errors.New("fault")

Functions

func Between

func Between[T cmp.Ordered](a, low, high T, msg ...string)

func BetweenInclusive

func BetweenInclusive[T cmp.Ordered](a, low, high T, msg ...string)

func Catch

func Catch[T any](f func() T, catchable ...error) (out T, err error)

Catch executes f and converts panics into errors. If catchable errors are specified, only panics matching those errors (via errors.Is) are caught; others are re-panicked. Returns the result of f and any caught error.

func Eq

func Eq[T comparable](a, b T, msg ...string)

Eq compares the two input values and panics if they are not equal

func ErrCfg

func ErrCfg(name string, opts ...ErrOpt)

func ErrIs

func ErrIs(e error, target error, msg ...string)

ErrIs panics if e does not match target using errors.Is

func ErrPoint

func ErrPoint(name string) error

func GT

func GT[T cmp.Ordered](a, b T, msg ...string)

GT panics if a is not greater than b

func GTE

func GTE[T cmp.Ordered](a, b T, msg ...string)

GTE panics if a is not greater than or equal to b

func Is

func Is[T any](a any, msg ...string)

Is panics if a is not of type T

func LT

func LT[T cmp.Ordered](a, b T, msg ...string)

LT panics if a is not less than b

func LTE

func LTE[T cmp.Ordered](a, b T, msg ...string)

LTE panics if a is not less than or equal to b

func Must added in v0.2.0

func Must[T any](t T, err error, msg ...string) T

func Nil

func Nil(x any, msg ...string)

Nil panics if x is not nil

func NotEq

func NotEq[T comparable](a, b T, msg ...string)

NotEq compares the two input values and panics if they are equal

func NotNil

func NotNil(x any, msg ...string)

NotNil panics if x is nil

func Panics

func Panics(f func(), msg ...string)

Panics executes f and panics if f does not panic

func SliceContains added in v0.3.0

func SliceContains[T comparable](s []T, v T, msg ...string)

SliceContains panics if v is not found in s

func SliceEq added in v0.3.0

func SliceEq[T comparable](a, b []T, msg ...string)

SliceEq panics if slices a and b are not element-wise equal

func SliceSorted added in v0.3.0

func SliceSorted[T cmp.Ordered](s []T, msg ...string)

SliceSorted panics if s is not sorted in ascending order

Types

type ErrCondition

type ErrCondition = func(string) error

type ErrOpt

type ErrOpt = func(*errCfg)

func Prob

func Prob[T float32 | float64](p T) ErrOpt

Prob takes a probability between 0.0 and 1.0 and applies it to the given error

Jump to

Keyboard shortcuts

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