must

package module
v0.0.0-...-492b25f Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2018 License: MIT Imports: 4 Imported by: 0

README

must

Build Status GoDoc Go Report Card

A collection of helper functions for checking unit test output in Go

Documentation

Overview

Package must provides helper functions for validating output in unit tests. The output "must" be what we expect.

Must does not provide assertions, but follows a similar syntax that you might be used to from unit testing in other languages. You provide a testing.T, objects to be tested and an error message, and the must functions will raise a testing error if the expectations on the objects are not met. This error will contain additional context (such as an object diff) to help you identify the nature of the test failure.

For example:

result := must.BeEqual(t, expected, got, "expectation not met")

Will trigger an error in t if got and expected are not the same. The message "expectation not met" will be included in the error along with a diff of expected and got.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeEqual

func BeEqual(t TestingT, expected, got interface{}, a ...interface{}) bool

BeEqual compares the expected and got interfaces, triggering an error on t if they are not equal. This error will include a diff of the two objects.

The return value will be true if the interfaces are equal.

Additional output for any error message can be provided as additional parameters, as with fmt.Print.

func BeEqualErrors

func BeEqualErrors(t TestingT, expected, got error, a ...interface{}) bool

BeEqualErrors compares two errors to determine if they are considered equal. The errors expected and got are considered equal if they are both nil, or both are non-nil and their error messsages (from their Error() functions) match.

This ignores the actual type of these errors, so two errors created with different struct types, but the same message will still be equal.

Should the errors not be considered equal, an error will be raised in t including both messages and false will be returned.

Additional output for any error message can be provided as additional parameters, as with fmt.Print.

func BeError

func BeError(t TestingT, got error, a ...interface{}) bool

BeError checks that the provided error is not nil

The return value will be true iff error is not nil

Additional output for any error message can be provided as additional parameters, as with fmt.Print.

func BeErrorIf

func BeErrorIf(t TestingT, errorExpected bool, got error, a ...interface{}) bool

BeErrorIf checks that the errorExpected flag corresponds to the provided error: error should be nil iff errorExpected flag is false

The return value will be true iff error value corresponds to the errorExpected flag

Additional output for any error message can be provided as additional parameters, as with fmt.Print.

func BeNoError

func BeNoError(t TestingT, got error, a ...interface{}) bool

BeNoError checks whether or not the got value is an error.

The return value will be true if got is nil.

Additional output for any error message can be provided as additional parameters, as with fmt.Print.

func BeSameLength

func BeSameLength(t TestingT, expected, got interface{}, a ...interface{}) bool

BeSameLength checks whether the two inputs have the same length according to the len function.

The return value will be true if their lengths match.

Additional output for any error message can be provided as additional parameters, as with fmt.Print.

Types

type MustTester

type MustTester interface {
	BeEqual(expected, got interface{}, a ...interface{}) bool
	BeEqualErrors(expected, got error, a ...interface{}) bool
	BeNoError(got error, a ...interface{}) bool
	BeError(got error, a ...interface{}) bool
	BeErrorIf(errorExpected bool, got error, a ...interface{}) bool
	BeSameLength(expected, got interface{}, a ...interface{}) bool
}

MustTester defines an interface with functions matching the package level check functions, without the requirement to specify a TestingT.

type Tester

type Tester struct {
	T                   TestingT                               // *testing.T or equivalent
	InterfaceComparison func(expected, got interface{}) bool   // Optional custom interface comparison function
	InterfaceDiff       func(expected, got interface{}) string // Optional custom interace diff function
}

Tester implements MustTester and provides a TestingT to be used for all check functions.

func (Tester) BeEqual

func (tester Tester) BeEqual(expected, got interface{}, a ...interface{}) bool

BeEqual compares the expected and got interfaces, triggering an error on the Tester's T if they are not equal.

This corresponds to the function BeEqual

func (Tester) BeEqualErrors

func (tester Tester) BeEqualErrors(expected, got error, a ...interface{}) bool

BeEqualErrors compares the expected and got errors, triggering an error on the Tester's T if they are not equal.

This corresponds to the function BeEqualErrors

func (Tester) BeError

func (tester Tester) BeError(got error, a ...interface{}) bool

BeError checks that the received error is not nil

func (Tester) BeErrorIf

func (tester Tester) BeErrorIf(errorExpected bool, got error, a ...interface{}) bool

BeErrorIf checks that the received error corresponds to the errorExpected flag

func (Tester) BeNoError

func (tester Tester) BeNoError(got error, a ...interface{}) bool

BeNoError checks whether got is set, triggering an error on the Tester's T if it is non-nil.

This corresponds to the function BeNoError

func (Tester) BeSameLength

func (tester Tester) BeSameLength(expected, got interface{}, a ...interface{}) bool

BeSameLength checks whether the two inputs have the same length according to the len function.

This corresponds to the function BeSameLength

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	Helper()
}

TestingT is an interface wrapper around *testing.T

Jump to

Keyboard shortcuts

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