Help us shape the future of Go! Take the 2026 Go Developer Survey

unassert

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: MIT Imports: 0 Imported by: 10

README

unassert - providing a lack of assertions

unassert is a library that provides assertions based on the provided build tags - unassert_panic or unassert_stderr.

By default assertions are disabled (NOP implementation) to provide zero overhead in performance. The Go compiler detects when the library is not enbled and completely removes any function calls to unassert, so it behaves as if it the library were not used at all. This can be very useful if you are in a high performance scenario where you want to do assertinons in tight loops and you are reasonably sure your test cases cover any realistic scenarios.

There are multiple implementations available that can be enabled to discover issues in automated test runs or in QA environments.

GoDoc Build Status codecov Go Report License FOSSA Status

Go Get

go get -u github.com/markus-wa/go-unassert

Example

package main

import (
	"fmt"

	"github.com/markus-wa/go-unassert"
)

func main() {
	unassert.True(true)

	fmt.Println("Hello")

	unassert.Same("a", "b")

	fmt.Println("World")

	unassert.Error("you can also %s a string", "format")
	unassert.Nilf(new(struct{}), "you can also pass error messages to any other function")
	unassert.NotNilf(nil, "with or without %s", "formatting")

	lenLessThan10 := func(v interface{}) bool {
		return len(v.([]int)) < 10
	}
	unassert.Matches(lenLessThan10, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})

	complexEvaluation := func() bool {
		// add logic here
		return false
	}
	unassert.ReturnsTrue(complexEvaluation)
}
Assertions disabled
go run main.go

output:

Hello
World
unassert_panic
go run -tags unassert_panic main.go

output:

Hello
panic: UNASSERT: assertion failed: expected same values, got a != b

goroutine 1 [running]:
github.com/markus-wa/go-unassert.Same(...)
    C:/Users/mwalt/dev/go-unassert/panic.go:11
main.main()
    C:/Users/mwalt/dev/go-unassert/example/example.go:14 +0x1c3
exit status 2
unassert_stderr
go run -tags unassert_stderr example/example.go

output:

Hello
UNASSERT: assertion failed: expected same values, got a != b
goroutine 1 [running]:
runtime/debug.Stack(0x4ddcc0, 0xc00000e020, 0xc000084ea8)
    /opt/go/current/src/runtime/debug/stack.go:24 +0x9d
github.com/markus-wa/go-unassert/stderr.Stderr(0x4c8eef, 0x34, 0xc00000c060, 0x2, 0x2)
    /home/mark/dev/go-unassert/stderr/stderr.go:20 +0x1b3
github.com/markus-wa/go-unassert.Error(...)
    /home/mark/dev/go-unassert/unassert.go:18
github.com/markus-wa/go-unassert.Samef(...)
    /home/mark/dev/go-unassert/unassert.go:142
github.com/markus-wa/go-unassert.Same(...)
    /home/mark/dev/go-unassert/unassert.go:129
main.main()
    /home/mark/dev/go-unassert/example/example.go:14 +0x3c4
World
UNASSERT: you can also format a string
goroutine 1 [running]:
runtime/debug.Stack(0x4ddcc0, 0xc00000e020, 0xc000084ea8)
    /opt/go/current/src/runtime/debug/stack.go:24 +0x9d
github.com/markus-wa/go-unassert/stderr.Stderr(0x4c51f2, 0x18, 0xc000010210, 0x1, 0x1)
    /home/mark/dev/go-unassert/stderr/stderr.go:20 +0x1b3
github.com/markus-wa/go-unassert.Error(...)
    /home/mark/dev/go-unassert/unassert.go:18
main.main()
    /home/mark/dev/go-unassert/example/example.go:18 +0x18f
UNASSERT: you can also pass error messages to any other function
goroutine 1 [running]:
runtime/debug.Stack(0x4ddcc0, 0xc00000e020, 0xc000084ea8)
    /opt/go/current/src/runtime/debug/stack.go:24 +0x9d
github.com/markus-wa/go-unassert/stderr.Stderr(0x4c9136, 0x36, 0x0, 0x0, 0x0)
    /home/mark/dev/go-unassert/stderr/stderr.go:20 +0x1b3
github.com/markus-wa/go-unassert.Error(...)
    /home/mark/dev/go-unassert/unassert.go:18
github.com/markus-wa/go-unassert.Nilf(...)
    /home/mark/dev/go-unassert/unassert.go:92
main.main()
    /home/mark/dev/go-unassert/example/example.go:19 +0x1c2
UNASSERT: with or without formatting
goroutine 1 [running]:
runtime/debug.Stack(0x4ddcc0, 0xc00000e020, 0xc000084ea8)
    /opt/go/current/src/runtime/debug/stack.go:24 +0x9d
github.com/markus-wa/go-unassert/stderr.Stderr(0x4c3db5, 0x12, 0xc000010240, 0x1, 0x1)
    /home/mark/dev/go-unassert/stderr/stderr.go:20 +0x1b3
github.com/markus-wa/go-unassert.Error(...)
    /home/mark/dev/go-unassert/unassert.go:18
github.com/markus-wa/go-unassert.NotNilf(...)
    /home/mark/dev/go-unassert/unassert.go:117
main.main()
    /home/mark/dev/go-unassert/example/example.go:20 +0x224
UNASSERT: assertion failed: predicate(x) did not return true; x = [1 2 3 4 5 6 7 8 9 10]
goroutine 1 [running]:
runtime/debug.Stack(0x4ddcc0, 0xc00000e020, 0xc000084e70)
    /opt/go/current/src/runtime/debug/stack.go:24 +0x9d
github.com/markus-wa/go-unassert/stderr.Stderr(0x4c93d2, 0x3a, 0xc000010260, 0x1, 0x1)
    /home/mark/dev/go-unassert/stderr/stderr.go:20 +0x1b3
github.com/markus-wa/go-unassert.Error(...)
    /home/mark/dev/go-unassert/unassert.go:18
github.com/markus-wa/go-unassert.Matchesf(0x4c9920, 0x49fb40, 0xc00000c080, 0x4c93d2, 0x3a, 0xc000010260, 0x1, 0x1)
    /home/mark/dev/go-unassert/unassert.go:200 +0x88
github.com/markus-wa/go-unassert.Matches(...)
    /home/mark/dev/go-unassert/unassert.go:186
main.main()
    /home/mark/dev/go-unassert/example/example.go:25 +0x328
UNASSERT: assertion failed: evaluator() did not return true
goroutine 1 [running]:
runtime/debug.Stack(0x4ddcc0, 0xc00000e020, 0xc000084e70)
    /opt/go/current/src/runtime/debug/stack.go:24 +0x9d
github.com/markus-wa/go-unassert/stderr.Stderr(0x4c8c09, 0x31, 0x0, 0x0, 0x0)
    /home/mark/dev/go-unassert/stderr/stderr.go:20 +0x1b3
github.com/markus-wa/go-unassert.Error(...)
    /home/mark/dev/go-unassert/unassert.go:18
github.com/markus-wa/go-unassert.ReturnsTruef(0x4c9928, 0x4c8c09, 0x31, 0x0, 0x0, 0x0)
    /home/mark/dev/go-unassert/unassert.go:233 +0x74
github.com/markus-wa/go-unassert.ReturnsTrue(...)
    /home/mark/dev/go-unassert/unassert.go:219
main.main()
    /home/mark/dev/go-unassert/example/example.go:31 +0x35e

License

This project is licensed under the MIT license.

FOSSA Status

Documentation

Overview

Package unassert provides assertions based on the provided build tags. Using -tags unassert_panics panics if an assertion fails. Using -tags unassert_stderr prints an error to stdout if an assertion fails.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error added in v0.1.1

func Error(format string, v ...interface{})

Error promotes an error according to 'unassert_' build tags. Formats according to a format specifier.

func False added in v0.1.2

func False(b bool)

False checks if b is false. Behaves according to 'unassert_' build tags.

func Falsef added in v0.1.2

func Falsef(b bool, format string, v ...interface{})

Falsef checks if b is false. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func Matches added in v0.1.3

func Matches(predicate Predicate, x interface{})

Matches checks if predicate(v) is true. Useful for lazy evaluation of complex assertions. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func Matchesf added in v0.1.3

func Matchesf(predicate Predicate, x interface{}, format string, v ...interface{})

Matchesf checks if predicate(v) is true. Useful for lazy evaluation of complex assertions. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func Nil added in v0.1.1

func Nil(o interface{})

Nil checks if v is nil. Behaves according to 'unassert_' build tags.

func Nilf added in v0.1.1

func Nilf(o interface{}, format string, v ...interface{})

Nilf checks if v is nil. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func NotNil added in v0.1.1

func NotNil(o interface{})

NotNil checks if v is not nil. Behaves according to 'unassert_' build tags.

func NotNilf added in v0.1.1

func NotNilf(o interface{}, format string, v ...interface{})

NotNilf checks if v is not nil. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func NotSame added in v0.1.2

func NotSame(a interface{}, b interface{})

NotSame checks if a != b. Behaves according to 'unassert_' build tags.

func NotSamef added in v0.1.2

func NotSamef(a interface{}, b interface{}, format string, v ...interface{})

NotSamef checks if a != b. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func ReturnsTrue added in v0.1.3

func ReturnsTrue(evaluator Evaluator)

ReturnsTrue checks if evaluator() returns true. Useful for lazy evaluation of complex assertions. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func ReturnsTruef added in v0.1.3

func ReturnsTruef(evaluator Evaluator, format string, v ...interface{})

ReturnsTrue checks if evaluator() returns true. Useful for lazy evaluation of complex assertions. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func Same

func Same(a interface{}, b interface{})

Same checks if a == b. Behaves according to 'unassert_' build tags.

func Samef added in v0.1.1

func Samef(a interface{}, b interface{}, format string, v ...interface{})

Samef checks if a == b. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

func True

func True(b bool)

True checks if b is true. Behaves according to 'unassert_' build tags.

func Truef added in v0.1.1

func Truef(b bool, format string, v ...interface{})

Truef checks if b is true. Behaves according to 'unassert_' build tags. Formats according to a format specifier.

Types

type ErrorHandler added in v0.1.1

type ErrorHandler func(format string, v ...interface{})

ErrorHandler handles an error. See unassert_panic & unassert_stderr

type Evaluator added in v0.1.3

type Evaluator func() bool

Evaluator is a function type used for lazy evaluation in assertions. Mostly used for function closures. See ReturnsTrue().

type Predicate added in v0.1.3

type Predicate func(interface{}) bool

Predicate is a function type used for lazy evaluation in assertions. It takes one argument and returns true if that argument matches a given predicate. See Matches().

Directories

Path Synopsis
Package dopanic implements unassert.ErrorHandler by panicking.
Package dopanic implements unassert.ErrorHandler by panicking.
Package stderr implements unassert.ErrorHandler by printing to stderr.
Package stderr implements unassert.ErrorHandler by printing to stderr.

Jump to

Keyboard shortcuts

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