assert

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 11 Imported by: 0

README

Assert Go

A lightweight assertion library for Go, designed for systems and real-time programming with structured logging and flush controls.

Installation

go get github.com/ZanzyTHEbar/assert-lib

Usage

package main

import (
    "context"
    "github.com/ZanzyTHEbar/assert-lib"
)

func main() {
    handler := assert.NewAssertHandler()
    
    handler.Assert(context.TODO(), false, "This should fail")
}

Check out the examples directory for usage examples.

Features

  • Assertions: Assert, Nil, NotNil, NoError, Never.
  • Flush Management: Control output flushes with AssertFlush.
  • Context-Based Logging: Attach structured logging to your assertion calls.
  • Custom Loggers: Use your own logger with the AssertHandler interface.

Examples

go run examples/basic_assertion.go
go run examples/deferred_assertions.go
go run examples/custom_exit.go
go run examples/formater.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssertData

type AssertData interface {
	Dump() string
}

Define interfaces for logging/asserting

type AssertFlush

type AssertFlush interface {
	Flush()
}

type AssertHandler

type AssertHandler struct {
	// contains filtered or unexported fields
}

Define the AssertHandler to encapsulate state

func NewAssertHandler

func NewAssertHandler() *AssertHandler

Add a constructor for the handler

func (*AssertHandler) AddAssertData

func (a *AssertHandler) AddAssertData(key string, value AssertData)

func (*AssertHandler) AddAssertFlush

func (a *AssertHandler) AddAssertFlush(flusher AssertFlush)

func (*AssertHandler) Assert

func (a *AssertHandler) Assert(ctx context.Context, truth bool, msg string, data ...any)

func (*AssertHandler) AssertWithTimeout

func (a *AssertHandler) AssertWithTimeout(ctx context.Context, timeout time.Duration, truth bool, msg string, data ...any)

func (*AssertHandler) Never

func (a *AssertHandler) Never(ctx context.Context, msg string, data ...any)

func (*AssertHandler) Nil

func (a *AssertHandler) Nil(ctx context.Context, item any, msg string, data ...any)

func (*AssertHandler) NoError

func (a *AssertHandler) NoError(ctx context.Context, err error, msg string, data ...any)

func (*AssertHandler) NotNil

func (a *AssertHandler) NotNil(ctx context.Context, item any, msg string, data ...any)

func (*AssertHandler) ProcessDeferredAssertions

func (a *AssertHandler) ProcessDeferredAssertions(ctx context.Context)

Process all deferred assertions at once, logging or exiting if needed

func (*AssertHandler) RemoveAssertData

func (a *AssertHandler) RemoveAssertData(key string)

func (*AssertHandler) SetDeferAssertions

func (a *AssertHandler) SetDeferAssertions(deferMode bool)

SetDeferAssertions allows toggling deferred assertion mode

func (*AssertHandler) SetExitFunc

func (a *AssertHandler) SetExitFunc(exitFunc func(int))

func (*AssertHandler) SetFormatter

func (a *AssertHandler) SetFormatter(formatter Formatter)

func (*AssertHandler) ToWriter

func (a *AssertHandler) ToWriter(w io.Writer)

type Formatter

type Formatter interface {
	Format(assertData map[string]interface{}, stack string) string
}

Formatter interface for structured output

type JSONFormatter

type JSONFormatter struct{}

JSONFormatter for JSON output

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(assertData map[string]interface{}, stack string) string

type TextFormatter

type TextFormatter struct{}

TextFormatter is the default plain text output format

func (*TextFormatter) Format

func (f *TextFormatter) Format(assertData map[string]interface{}, stack string) string

type YAMLFormatter

type YAMLFormatter struct{}

YAMLFormatter for YAML output

func (*YAMLFormatter) Format

func (f *YAMLFormatter) Format(assertData map[string]interface{}, stack string) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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