is

package module
v2.1.4+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2019 License: MIT Imports: 6 Imported by: 0

README

is GoDoc Build Status

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

Installation

To install, simply execute:

go get github.com/tylerb/is

Vendoring

Vendoring is recommended, as this library can change from time to time. The last change was updating it to use the new Helper() method in the 1.9 testing framework.

Check out the official Go dependency manager, dep. Alternatively, I also like glide.

Usage

Using Is is simple:

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

	expected := 10
	result, _ := awesomeFunction()
	is.Equal(expected,result)
}

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

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

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

By default, Is fails and stops the test immediately. If you prefer to run multiple assertions to see them all fail at once, use the Lax method:

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

	is.Equal(1,someFunc()) // if this fails, a message is printed and the test continues
	is.Equal(2,someOtherFunc()) // if this fails, a message is printed and the test continues

If you are using a relaxed instance of Is, you can switch it back to strict mode with Strict. This is useful when an assertion must be correct, or subsequent calls will panic:

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

	results := someFunc()
	is.Strict().Equal(len(results),3) // if this fails, a message is printed and testing stops
	is.Equal(results[0],1) // if this fails, a message is printed and testing continues
	is.Equal(results[1],2)
	is.Equal(results[2],3)

Strict mode, in this case, applies only to the line on which it is invoked, as we don't overwrite our copy of the is variable.

Contributing

If you would like to contribute, please:

  1. Create a GitHub issue regarding the contribution. Features and bugs should be discussed beforehand.
  2. Fork the repository.
  3. Create a pull request with your solution. This pull request should reference and close the issues (Fix #2).

All pull requests should:

  1. Pass gometalinter -t . with no warnings.
  2. Be go fmt formatted.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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.

type Is

type Is struct {
	TB testing.TB
	// contains filtered or unexported fields
}

Is 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) *Is

New creates a new instance of the Is object and stores a reference to the provided testing object.

func (*Is) AddMsg

func (is *Is) AddMsg(format string, args ...interface{}) *Is

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:

is := is.New(t).Msg("User ID: %d",u.ID) /*do things*/ is.AddMsg("Raw Response: %s",body).Equal(res.StatusCode, http.StatusCreated)

func (*Is) Equal

func (is *Is) Equal(actual interface{}, expected interface{})

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.

func (*Is) EqualType

func (is *Is) EqualType(expected, actual interface{})

EqualType checks the type of the two provided objects and fails if they are not the same.

func (*Is) Err

func (is *Is) Err(e error)

Err checks the provided error object to determine if an error is present.

func (*Is) False

func (is *Is) False(b bool)

False checks the provided boolean to determine if is false.

func (*Is) Lax added in v1.1.1

func (is *Is) Lax() *Is

Lax returns a copy of this instance of Is which does not abort the test if a failure occurs. Use this to run a set of tests and see all the failures at once.

func (*Is) Len

func (is *Is) Len(o interface{}, l int)

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.

func (*Is) Msg

func (is *Is) Msg(format string, args ...interface{}) *Is

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.

func (*Is) New

func (is *Is) New(tb testing.TB) *Is

New creates a new copy of your Is object and replaces the internal testing object with the provided testing object. This is useful for re-initializing your `is` instance inside a subtest so that it doesn't panic when using Strict mode.

For example, creating your initial instance as such

is := is.New(t)

is the convention, but this obviously shadows the `is` package namespace. Inside your subtest, you can do the exact same thing to initialize a locally scoped variable that uses the subtest's testing.T object.

func (*Is) Nil

func (is *Is) Nil(o interface{})

Nil checks the provided object to determine if it is nil.

func (*Is) NotEqual

func (is *Is) NotEqual(a interface{}, b 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.

func (*Is) NotErr

func (is *Is) NotErr(e error)

NotErr checks the provided error object to determine if an error is not present.

func (*Is) NotNil

func (is *Is) NotNil(o interface{})

NotNil checks the provided object to determine if it is not nil.

func (*Is) NotOneOf

func (is *Is) NotOneOf(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.

func (*Is) NotZero

func (is *Is) NotZero(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

func (*Is) OneOf

func (is *Is) OneOf(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.

func (*Is) ShouldPanic

func (is *Is) ShouldPanic(f func())

ShouldPanic expects the provided function to panic. If the function does not panic, this assertion fails.

func (*Is) Strict added in v1.1.1

func (is *Is) Strict() *Is

Strict returns a copy of this instance of Is which aborts the test if a failure occurs. This is the default behavior, thus this method has no effect unless it is used to reverse a previous call to Lax.

func (*Is) True

func (is *Is) True(b bool)

True checks the provided boolean to determine if it is true.

func (*Is) WaitForTrue

func (is *Is) WaitForTrue(timeout time.Duration, f func() bool)

WaitForTrue waits until the provided func returns true. If the timeout is reached before the function returns true, the test will fail.

func (*Is) Zero

func (is *Is) Zero(o interface{})

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

Jump to

Keyboard shortcuts

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