is

package module
v0.0.0-...-68e9c06 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2015 License: MIT Imports: 6 Imported by: 19

README

is

GoDoc

A mini testing helper for Go.

  • Simple interface (is.OK and is.Equal)
  • Plugs into existing Go toolchain (uses testing.T)
  • Obvious for newcomers and newbs
  • Also gives you is.Panic and is.PanicWith helpers - because testing panics is ugly
Usage
  1. Write test functions as usual
  2. Add is := is.New(t) at top of your test functions
  3. Call target code
  4. Make assertions using new is object
func TestSomething(t *testing.T) {
  is := is.New(t)

  // ensure not nil
  obj := SomeFunc()
  is.OK(obj)

  // ensure no error
  obj, err := SomeFunc()
  is.NoErr(err)

  // ensure not false
  b := SomeBool()
  is.OK(b)

  // ensure not ""
  s := SomeString()
  is.OK(s)

  // ensure not zero
  is.OK(len(something))

  // ensure doesn't panic
  is.OK(func(){
    MethodShouldNotPanic()
  })

  // ensure many things in one go
  is.OK(b, err, obj, "something")

  // ensure something does panic
  is.Panic(func(){
    MethodShouldPanic(1)
  })
  is.PanicWith("package: arg must be >0", func(){
    MethodShouldPanicWithSpecificMessage(0)
  })

  // make sure two values are equal
  is.Equal(1, 2)
  is.Equal(err, ErrSomething)
  is.Equal(a, b)

}
Get started

Get it:

go get github.com/cheekybits/is

Then import it:

import (
  "testing"
  "github.com/cheekybits/is"
)

Documentation

Overview

Package is is a mini testing helper.

func TestSomething(t *testing.T) {
  is := is.New(t)
  obj, err := MethodBeingTested()
  is.OK(obj, err) // list of objects, all must be OK
  is.Equal(obj, "Hello world")
}

OK

is.OK asserts that the specified object is OK, which means different things for different types:

bool  - OK means not false
int   - OK means not zero
error - OK means nil
string - OK means not ""
func() - OK means does not panic
everything else - OK means not nil

Equality

is.Equal asserts that two objects are effectively equal.

Panics

is.Panic and is.PanicWith asserts that the func() will panic. PanicWith specifies the panic text that is expected:

func TestInvalidArgs(t *testing.T) {
  is := is.New(t)
  is.Panic(func(){
    SomeMethod(1)
  })
  is.PanicWith("invalid args, both cannot be nil", func(){
    OtherMethod(nil, nil)
  })
}

Relaxed

To prevent is from stopping when the first assertion fails, you can use is.Relaxed(t), rather than is.New(t).

func TestManyThings(t *testing.T) {
  is := is.Relaxed(t)
  thing, err := Something();
  is.OK(thing)
  is.Nil(err)
  another, err := Another();
  is.Nil(another)
  is.Err(err)
  // all assertions will be tried
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type I

type I interface {
	// OK asserts that the specified objects are all OK.
	OK(o ...interface{})
	// Equal asserts that the two values are
	// considered equal. Non strict.
	Equal(a, b interface{})
	// NotEqual asserts that the two values are not
	// considered equal. Non strict.
	NotEqual(a, b interface{})
	// NoErr asserts that the value is not an
	// error.
	NoErr(err ...error)
	// Err asserts that the value is an error.
	Err(err ...error)
	// Nil asserts that the specified objects are
	// all nil.
	Nil(obj ...interface{})
	// NotNil asserts that the specified objects are
	// all nil.
	NotNil(obj ...interface{})
	// True asserts that the specified objects are
	// all true
	True(obj ...interface{})
	// False asserts that the specified objects are
	// all true
	False(obj ...interface{})
	// Panic asserts that the specified function
	// panics.
	Panic(fn func())
	// PanicWith asserts that the specified function
	// panics with the specific message.
	PanicWith(m string, fn func())
	// Fail indicates that the test has failed with the
	// specified formatted arguments.
	Fail(args ...interface{})
	// Failf indicates that the test has failed with the
	// formatted arguments.
	Failf(format string, args ...interface{})
}

I represents the is interface.

func New

func New(t T) I

New creates a new I capable of making assertions.

func Relaxed

func Relaxed(t T) I

Relaxed creates a new I capable of making assertions, but will not fail immediately allowing all assertions to run.

type T

type T interface {
	FailNow()
}

T represents the an interface for reporting failures. testing.T satisfied this interface.

Jump to

Keyboard shortcuts

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