unassert

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2020 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.

By default assertions are disabled (NOP implementation) to provide the best possible performance. 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")
}
Assertions disabled
$ go run main.go
Hello
World
unassert_panic
$ go run -tags unassert_panic main.go
Hello
panic: UNASSERT: assertion failed: 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
Hello
UNASSERT: assertion failed: a != b
goroutine 1 [running]:
runtime/debug.Stack(0x4eb280, 0xc000006020, 0xc00009de90)
    C:/Users/mwalt/scoop/apps/go/current/src/runtime/debug/stack.go:24 +0xa4
github.com/markus-wa/go-unassert/stderr.Stderr(0x4d4798, 0x1a, 0xc0000044a0, 0x2, 0x2)
    C:/Users/mwalt/dev/go-unassert/stderr/stderr.go:19 +0x1ba
github.com/markus-wa/go-unassert.Same(...)
    C:/Users/mwalt/dev/go-unassert/unassert.go:30
main.main()
    C:/Users/mwalt/dev/go-unassert/example/example.go:14 +0x27a
World
UNASSERT: you can also format a string
goroutine 1 [running]:
runtime/debug.Stack(0x4eb280, 0xc000006020, 0xc00009de90)
    C:/Users/mwalt/scoop/apps/go/current/src/runtime/debug/stack.go:24 +0xa4
github.com/markus-wa/go-unassert/stderr.Stderr(0x4d40fc, 0x18, 0xc000058200, 0x1, 0x1)
    C:/Users/mwalt/dev/go-unassert/stderr/stderr.go:19 +0x1ba
github.com/markus-wa/go-unassert.Error(...)
    C:/Users/mwalt/dev/go-unassert/unassert.go:18
main.main()
    C:/Users/mwalt/dev/go-unassert/example/example.go:18 +0x16d
UNASSERT: you can also pass error messages to any other function
goroutine 1 [running]:
runtime/debug.Stack(0x4eb280, 0xc000006020, 0xc00009de90)
    C:/Users/mwalt/scoop/apps/go/current/src/runtime/debug/stack.go:24 +0xa4
github.com/markus-wa/go-unassert/stderr.Stderr(0x4d84ee, 0x36, 0x0, 0x0, 0x0)
    C:/Users/mwalt/dev/go-unassert/stderr/stderr.go:19 +0x1ba
github.com/markus-wa/go-unassert.Nilf(...)
    C:/Users/mwalt/dev/go-unassert/unassert.go:98
main.main()
    C:/Users/mwalt/dev/go-unassert/example/example.go:19 +0x19f
UNASSERT: with or without formatting
goroutine 1 [running]:
runtime/debug.Stack(0x4eb280, 0xc000006020, 0xc00009de90)
    C:/Users/mwalt/scoop/apps/go/current/src/runtime/debug/stack.go:24 +0xa4
github.com/markus-wa/go-unassert/stderr.Stderr(0x4d24a6, 0x12, 0xc000058230, 0x1, 0x1)
    C:/Users/mwalt/dev/go-unassert/stderr/stderr.go:19 +0x1ba
github.com/markus-wa/go-unassert.NotNilf(...)
    C:/Users/mwalt/dev/go-unassert/unassert.go:125
main.main()
    C:/Users/mwalt/dev/go-unassert/example/example.go:20 +0x200

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 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 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

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