assert

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: MIT Imports: 8 Imported by: 0

README

Assert

Go package for writing checks in unit tests

Build Status Go Report Card GoDoc

© 2015 http://ernestmicklei.com. MIT License

Documentation

Overview

Package assert, for writing checks in unit tests

This package provides functions to reduce the amount of code needed to write simple assertions. It implements the best practice pattern where the output of a failure explains what the check "got" and what it wanted. The assert functions are defined such that writing requires less code but still are easy to understand. It works by decorating the standard testing.T type in your test and report (Fatal) the offending asserting call if a check fails.

Example

import (
	"testing.T"
	"github.com/emicklei/assert"
)

func TestShoeFits(t *testing.T) {
	shoeSize := 42
	assert.That(t,"shoeSize",shoeSize).Equals(46)
}

which will report

got [42] (int) for "shoeSize" but want [46] (int)

Examples: (using the dot import)

Assert(t,"err",err).IsNil()
Assert(t,"isOffline",isOffline).IsTrue()
Assert(t,"country",country).Equals("NL")
Assert(t,"job",job).IsKindOf(new(Job))
Assert(t,"names", []string{}).Len(0)

// you can negate a check
Assert(t,"isOnline",isOnline).Not().IsTrue()

You can create and use your own checks by implementing the RelationalOperator.

type caseInsensitiveStringEquals struct{}

func (c caseInsensitiveStringEquals) Apply(left, right interface{}) bool {
	s_left, ok := left.(string)
	if !ok {
		return false
	}
	s_right, ok := right.(string)
	if !ok {
		return false
	}
	return strings.EqualFold(s_left, s_right)
}

func TestCompareUsing(t *testing.T) {
	Assert(t, "insensitive", "ABC").With(caseInsensitiveStringEquals{}).Equals("abc")
}

Index

Constants

This section is empty.

Variables

View Source
var AssertColorsEnabled = true

AssertColorsEnabled can be changed to disable the use of terminal coloring. One usecase is to add a command line flag to your test that controls its value.

func init() {
	flag.BoolVar(&assert.AssertColorsEnabled, "color", true, "want colors?")
}

go test -color=false
View Source
var AssertFatalColorSyntaxCode = "@{wB}"

AssertFatalColorSyntaxCode requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go . Set to an empty string to disable coloring.

View Source
var AssertSuccessColorSyntaxCode = "@{y}"

AssertSuccessColorSyntaxCode requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go . Set to an empty string to disable coloring.

View Source
var Fatalf = func(t testingT, format string, args ...interface{}) {
	t.Helper()
	t.Fatalf("%s", Scolorf(AssertFatalColorSyntaxCode, format, args...))
}

Fatalf calls Fatalf on a test instance t. You can inject your own implementation of assert.testingT

View Source
var Log = func(t testingT, args ...interface{}) {
	t.Helper()
	t.Log(args...)
}

Log calls Log on a test instance t. You can inject your own implementation of assert.testingT

View Source
var Logf = func(t testingT, format string, args ...interface{}) {
	t.Helper()
	t.Logf("%s", Scolorf(AssertSuccessColorSyntaxCode, format, args...))
}

Logf calls Log on a test instance t. You can inject your own implementation of assert.testingT

Functions

func Scolorf

func Scolorf(syntaxCode string, format string, args ...interface{}) string

Scolorf returns a string colorized for terminal output using the syntaxCode (unless that's empty). Requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go .

Types

type Operand

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

Operand represent a value

func Assert

func Assert(t testingT, label string, value interface{}) Operand

Assert creates an Operand on a value that needs to be checked.

func That

func That(t testingT, label string, got interface{}) Operand

That creates an Operand on the value we have got and describes the variable that is being testing.

func (Operand) After

func (o Operand) After(moment time.Time)

After checks whether the value we have got is after a moment.

func (Operand) Before

func (o Operand) Before(moment time.Time)

Before checks whether the value we have got is before a moment.

func (Operand) Equals

func (o Operand) Equals(want interface{})

Equals checks whether the value we have got is equal to the value we want.

func (Operand) GreaterThan

func (o Operand) GreaterThan(want interface{})

GreaterThan checks whether the value we have got is greater than to the value we want.

func (Operand) IsEmpty

func (o Operand) IsEmpty()

IsEmpty checks that len(value) or value.Len() is zero. It operates on Array, Chan, Map, Slice, or String and objects that implement Len() int.

func (Operand) IsFalse

func (o Operand) IsFalse()

IsFalse checks whether the value is false

func (Operand) IsKindOf

func (o Operand) IsKindOf(v interface{})

IsKindOf checks whether the values are of the same type

func (Operand) IsNil

func (o Operand) IsNil()

IsNil checks whether the value is nil

func (Operand) IsNotNil

func (o Operand) IsNotNil()

IsNotNil checks whether the value is nil

func (Operand) IsTrue

func (o Operand) IsTrue()

IsTrue checks whether the value is true

func (Operand) Len

func (o Operand) Len(want int)

Len checks that len(value) or value.Len() is equals to the given length. It operates on Array, Chan, Map, Slice, or String and objects that implement Len() int.

func (Operand) Not

func (o Operand) Not() Operand

Not creates a new Operand with a negated version of its comparator.

func (Operand) With

With returns a copy Operand that will use the RelationalOperator.

type RelationalOperator

type RelationalOperator interface {
	// Return the result of applying the two values left and right
	Apply(left, right interface{}) bool
}

RelationalOperator specifies the function that can operate on two values

Jump to

Keyboard shortcuts

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