is

package module
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2020 License: MIT Imports: 8 Imported by: 0

README

is GoDev Build Status

Is provides a quick, clean and simple framework for writing Go tests.

Installation

To install, simply execute:

go get -u github.com/tylerb/is/v3

Usage

func TestSomething(t *testing.T) {
	assert := is.New(t)

	expected := 10
	actual, _ := awesomeFunction()
	assert.Equal(actual, expected)
}

If you'd like a bit more information when a test fails, you may use the Msg() method:

func TestSomething(t *testing.T) {
	assert := is.New(t)

	expected := 10
	actual, details := awesomeFunction()
	assert.Msg("actual details: %s", details).Equal(actual, expected)
}

By default, any assertion that fails will halt termination of the test. If you would like to run a group of assertions in a row, you may use the Lax method. This is useful for asserting/printing many values at once, so you can correct all the issues between test runs.

func TestSomething(t *testing.T) {
	assert := is.New(t)

	assert.Lax(func (lax Asserter) {
		lax.Equal(1, 2)
		lax.True(false)
		lax.ShouldPanic(func(){})
	}) 
}

If any of the assertions fail inside that function, an additional error will be printed and test execution will halt.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Asserter

type Asserter interface {
	// tb returns the testing object with which this Asserter was originally
	// initialized.
	TB() testing.TB

	// Msg defines a message to print in the event of a failure. This allows you
	// to print out additional information about a failure if it happens.
	Msg(format string, args ...interface{}) Asserter

	// AddMsg appends a message to print in the event of a failure. This allows
	// you to build a failure message in multiple steps. If no message was
	// previously set, simply sets the message.
	//
	// This method is most useful as a way of setting a default error message,
	// then adding additional information to the output for specific assertions.
	// For example:
	//
	// assert := is.New(t).Msg("User ID: %d",u.ID)
	// /*do things*/
	// assert.AddMsg("Raw Response: %s",body).Equal(res.StatusCode, http.StatusCreated)
	AddMsg(format string, args ...interface{}) Asserter

	// Equal performs a deep compare of the provided objects and fails if they are
	// not equal.
	//
	// Equal does not respect type differences. If the types are different and
	// comparable (eg int32 and int64), they will be compared as though they are
	// the same type.
	Equal(actual interface{}, expected interface{})

	// NotEqual performs a deep compare of the provided objects and fails if they are
	// equal.
	//
	// NotEqual does not respect type differences. If the types are different and
	// comparable (eg int32 and int64), they will be compared as though they are
	// the same type.
	NotEqual(a interface{}, b interface{})

	// OneOf performs a deep compare of the provided object and an array of
	// comparison objects. It fails if the first object is not equal to one of the
	// comparison objects.
	//
	// OneOf does not respect type differences. If the types are different and
	// comparable (eg int32 and int64), they will be compared as though they are
	// the same type.
	OneOf(a interface{}, b ...interface{})

	// NotOneOf performs a deep compare of the provided object and an array of
	// comparison objects. It fails if the first object is equal to one of the
	// comparison objects.
	//
	// NotOneOf does not respect type differences. If the types are different and
	// comparable (eg int32 and int64), they will be compared as though they are
	// the same type.
	NotOneOf(a interface{}, b ...interface{})

	// Err checks the provided error object to determine if an error is present.
	Err(e error)

	// NotErr checks the provided error object to determine if an error is not
	// present.
	NotErr(e error)

	// Nil checks the provided object to determine if it is nil.
	Nil(o interface{})

	// NotNil checks the provided object to determine if it is not nil.
	NotNil(o interface{})

	// True checks the provided boolean to determine if it is true.
	True(b bool)

	// False checks the provided boolean to determine if is false.
	False(b bool)

	// Zero checks the provided object to determine if it is the zero value
	// for the type of that object. The zero value is the same as what the object
	// would contain when initialized but not assigned.
	//
	// This method, for example, would be used to determine if a string is empty,
	// an array is empty or a map is empty. It could also be used to determine if
	// a number is 0.
	//
	// In cases such as slice, map, array and chan, a nil value is treated the
	// same as an object with len == 0
	Zero(o interface{})

	// NotZero checks the provided object to determine if it is not the zero
	// value for the type of that object. The zero value is the same as what the
	// object would contain when initialized but not assigned.
	//
	// This method, for example, would be used to determine if a string is not
	// empty, an array is not empty or a map is not empty. It could also be used
	// to determine if a number is not 0.
	//
	// In cases such as slice, map, array and chan, a nil value is treated the
	// same as an object with len == 0
	NotZero(o interface{})

	// Len checks the provided object to determine if it is the same length as the
	// provided length argument.
	//
	// If the object is not one of type array, slice or map, it will fail.
	Len(o interface{}, l int)

	// ShouldPanic expects the provided function to panic. If the function does
	// not panic, this assertion fails.
	ShouldPanic(f func())

	// EqualType checks the type of the two provided objects and
	// fails if they are not the same.
	EqualType(expected, actual interface{})

	// WaitForTrue waits until the provided func returns true. If the timeout is
	// reached before the function returns true, the test will fail.
	WaitForTrue(timeout time.Duration, f func() bool)

	// Lax accepts a function inside which a failed assertion will not halt
	// test execution. After the function returns, if any assertion had failed,
	// an additional message will be printed and test execution will be halted.
	//
	// This is useful for running assertions on, for example, many values in a struct
	// and having all the failed assertions print in one go, rather than having to run
	// the test multiple times, correcting a single failure per run.
	Lax(fn func(lax Asserter))
}

Asserter provides methods that leverage the existing testing capabilities found in the Go test framework. The methods provided allow for a more natural, efficient and expressive approach to writing tests. The goal is to write fewer lines of code while improving communication of intent.

func New

func New(tb testing.TB) Asserter

New returns a new Asserter containing the testing object provided.

type Equaler

type Equaler interface {
	Equal(in interface{}) bool
}

Equaler is used to define equality for types.

For example, this is useful if you have a struct that includes time.Time fields. You can implement this method and use time.Time.Equal() to do the comparison.

Deprecated

type EqualityChecker added in v3.2.0

type EqualityChecker interface {
	IsEqual(in interface{}) bool
}

EqualityChecker is used to define equality for types during testing.

For example, this is useful if you have a struct that includes time.Time fields. You can implement this method and use time.Time.Equal() to do the comparison.

Jump to

Keyboard shortcuts

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