assert

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 7 Imported by: 0

README

Assert

Example Output

Note: The display of the source code is heavily inspired by the ztrue/tracerr module. Its dependent on running on the machine the code was compiled on. Asserts shouldn't be used in production code anyway.

Usage

Add the assert tag to the build command to include the asserts in the code. If the tag is not present, all assert turn into no-ops.

All asserts are self describing, apart from Always and Never, which are mean to be used like in the SQLite codebase.

  • Equals[T comparable](a, b T)
  • NotEquals[T comparable](a, b T)
  • LessThan[T cmp.Ordered](a, b T)
  • MoreThan[T cmp.Ordered](a, b T)
  • LessOrEquals[T cmp.Ordered](a, b T)
  • MoreOrEquals[T cmp.Ordered](a, b T)
  • Nil(v any)
  • NotNil(v any)
  • Always(v bool) bool
  • Never(v bool) bool

Configuration

The following global variables can be set to change the behavior of the module:

Writer

The io.Writer to write the output to. Default is os.Stderr.

ContextWindow

The number of source code lines to display around the assert. Default is 5.

MaxTraceDepth

The maximum number of stack frames to display. Default is 20.

ReturnValue

The return value of the program when an assert fails. Default is 1.

SectionTitleColor

ANSI escape code for the section title. Default is bold underlined.

ContextColor

ANSI escape code for the context lines. Default is light gray.

LineColor

ANSI escape code for the line with the assert. Default is red.

ArgumentColor

ANSI escape code for the Argument X part in the Values section.

StackColor

ANSI escape code for the stack frames. Default is gray.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ArgumentColor = ansiCyan
View Source
var ContextColor = ansiGray
View Source
var ContextWindow int = 5

ContextWindow represents the number of source code lines to display above and below the line that caused the assertion failure.

View Source
var LineColor = ansiRed
View Source
var MaxTraceDepth int = 20

MaxTraceDepth represents the maximum number of stack frames to display in the stack trace.

View Source
var ReturnValue int = 1

ReturnValue represents the exit code to use when an assertion fails.

View Source
var SectionTitleColor = ansiUnderline + ansiBold
View Source
var StackColor = ansiGray
View Source
var Writer io.Writer = os.Stderr

Functions

func Always

func Always(v bool) bool

func Equals

func Equals[T comparable](a, b T, context ...any)

func LessOrEquals

func LessOrEquals[T cmp.Ordered](a, b T, context ...any)

func LessThan

func LessThan[T cmp.Ordered](a, b T, context ...any)

func MoreOrEquals

func MoreOrEquals[T cmp.Ordered](a, b T, context ...any)

func MoreThan

func MoreThan[T cmp.Ordered](a, b T, context ...any)

func Never

func Never(v bool) bool

func Nil

func Nil(v any, context ...any)

func NotEquals

func NotEquals[T comparable](a, b T, context ...any)

func NotNil

func NotNil(v any, context ...any)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳