Documentation ¶
Overview ¶
Package terst is a terse (terst = test + terse), easy-to-use testing library for Go.
terst is compatible with (and works via) the standard testing package: http://golang.org/pkg/testing
var is = terst.Is func Test(t *testing.T) { terst.Terst(t, func() { is("abc", "abc") is(1, ">", 0) var abc []int is(abc, nil) } }
Do not import terst directly, instead use `terst-import` to copy it into your testing environment:
https://github.com/robertkrimen/terst/tree/master/terst-import
$ go get github.com/robertkrimen/terst/terst-import $ terst-import
Index ¶
- func Is(arguments ...interface{}) bool
- func IsErr(arguments ...interface{}) error
- func Terst(t *testing.T, arguments ...func())
- type Call
- func (cl *Call) Error(arguments ...interface{})
- func (cl *Call) Errorf(format string, arguments ...interface{})
- func (cl *Call) Log(arguments ...interface{})
- func (cl *Call) Logf(format string, arguments ...interface{})
- func (cl *Call) Skip(arguments ...interface{})
- func (cl *Call) Skipf(format string, arguments ...interface{})
- func (cl *Call) T() *testing.T
- func (cl *Call) TestFunc() *runtime.Func
- type ErrFail
- type ErrInvalid
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Is ¶
func Is(arguments ...interface{}) bool
Is compares two values (got & expect) and returns true if the comparison is true, false otherwise. In addition, if the comparison is false, Is will report the error in a manner similar to testing.T.Error(...). Is also takes an optional argument, a comparator, that changes how the comparison is made. The following comparators are available:
== # got == expect (default) != # got != expect > # got > expect (float32, uint, uint16, int, int64, ...) >= # got >= expect < # got < expect <= # got <= expect =~ # regexp.MustCompile(expect).Match{String}(got) !~ # !regexp.MustCompile(expect).Match{String}(got)
Basic usage with the default comparator (==):
Is(<got>, <expect>)
Specifying a different comparator:
Is(<got>, <comparator>, <expect>)
A simple comparison:
Is(2 + 2, 4)
A bit trickier:
Is(1, ">", 0) Is(2 + 2, "!=", 5) Is("Nothing happens.", "=~", `ing(\s+)happens\.$`)
Is should only be called under a Terst(t, ...) call. For a standalone version, use IsErr. If no scope is found and the comparison is false, then Is will panic the error.
func IsErr ¶
func IsErr(arguments ...interface{}) error
IsErr compares two values (got & expect) and returns nil if the comparison is true, an ErrFail if the comparison is false, or an ErrInvalid if the comparison is invalid. IsErr also takes an optional argument, a comparator, that changes how the comparison is made.
Is & IsErr are similar but different:
Is(...) // Should only be called within a Terst(...) call IsErr(...) // A standalone comparator, the same as Is, just without the automatic reporting
func Terst ¶
Terst creates a testing scope, where Is can be called and errors will be reported according to the top-level location of the comparison, and not where the Is call actually takes place. For example:
func test(value int) { Is(value, 5) // <--- This failure is reported below. } Terst(t, func(){ Is(2, ">", 3) // <--- An error is reported here. test(5) // <--- An error is reported here. })
Types ¶
type Call ¶
type Call struct {
// contains filtered or unexported fields
}
Call is a reference to a line immediately under a Terst testing scope.
func Caller ¶
func Caller() *Call
Caller will search the stack, looking for a Terst testing scope. If a scope is found, then Caller returns a Call for logging errors, accessing testing.T, etc. If no scope is found, Caller returns nil.
func (*Call) Error ¶
func (cl *Call) Error(arguments ...interface{})
Error is the terst version of `testing.T.Error`
func (*Call) Log ¶
func (cl *Call) Log(arguments ...interface{})
Log is the terst version of `testing.T.Log`
func (*Call) Skip ¶
func (cl *Call) Skip(arguments ...interface{})
Skip is the terst version of `testing.T.Skip`
type ErrInvalid ¶
type ErrInvalid error
ErrInvalid indicates an invalid comparison (e.g. bool == string).