Documentation ¶
Overview ¶
Package asserter defines helpers for asserting various outputs.
Online assertions are done by wrapping the T in a test
func TestSomething(t *testing.T) { assert := asserter.New(t) got, err := Something() t.Logf("%v, %v := Something()", got, err) assert(err == nil).Fail() // Special case used very often is check equality assert().Equals(got, 1) }
Index ¶
- type AssertErrFunc
- type AssertFunc
- type Asserter
- type HttpResponse
- func (t *HttpResponse) BodyEquals(exp []byte, m, p string, opt ...interface{})
- func (t *HttpResponse) BodyIs(exp string, m, p string, opt ...interface{})
- func (t *HttpResponse) Contains(exp string, m, p string, opt ...interface{})
- func (t *HttpResponse) Header(k, exp string, m, p string, opt ...interface{})
- func (t *HttpResponse) StatusCode(exp int, m, p string, opt ...interface{})
- type MixedErrFunc
- type T
- type Testar
- type WrappedT
- func (w *WrappedT) Assert(expr ...bool) Testar
- func (w *WrappedT) Bad(err error) T
- func (w *WrappedT) Contains(body, exp interface{}) T
- func (w *WrappedT) Equals(got, exp interface{}) T
- func (w *WrappedT) Error(args ...interface{})
- func (w *WrappedT) Errorf(format string, args ...interface{})
- func (w *WrappedT) Errors() (ok, bad AssertErrFunc)
- func (w *WrappedT) Fail()
- func (w *WrappedT) FailNow()
- func (w *WrappedT) Fatal(args ...interface{})
- func (w *WrappedT) FatalErrors() (ok, bad AssertErrFunc)
- func (w *WrappedT) FatalMixed() (ok, bad MixedErrFunc)
- func (w *WrappedT) Fatalf(format string, args ...interface{})
- func (w *WrappedT) Helper()
- func (w *WrappedT) Log(args ...interface{})
- func (w *WrappedT) Logf(format string, args ...interface{})
- func (w *WrappedT) MixBad(any interface{}, err error) T
- func (w *WrappedT) MixOk(any interface{}, err error) T
- func (w *WrappedT) Mixed() (ok, bad MixedErrFunc)
- func (w *WrappedT) MustBad(err error) T
- func (w *WrappedT) MustMixBad(any interface{}, err error) T
- func (w *WrappedT) MustMixOk(any interface{}, err error) T
- func (w *WrappedT) MustOk(err error) T
- func (w *WrappedT) NotEqual(a, b interface{}) T
- func (w *WrappedT) Ok(err error) T
- func (w *WrappedT) ResponseFrom(h http.Handler) *HttpResponse
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AssertErrFunc ¶ added in v0.3.0
func NewErrors ¶ added in v0.4.0
func NewErrors(t T) (ok, bad AssertErrFunc)
func NewFatalErrors ¶ added in v0.4.0
func NewFatalErrors(t T) (ok, bad AssertErrFunc)
type AssertFunc ¶
func New ¶
func New(t T) AssertFunc
New returns a WrappedT.Assert func for online assertions.
Example ¶
package main import ( "net/http" "testing" "github.com/gregoryv/asserter" ) var t = &testing.T{} func main() { assert := asserter.New(t) assert(1 != 2).Errorf("...") got, exp := 1, 1 assert(got == exp).Fail() assert().Equals(got, exp) }
Output:
type Asserter ¶ added in v0.4.0
type Asserter interface { Assert(...bool) Testar Equals(got, exp interface{}) T NotEqual(a, b interface{}) T Contains(body, exp interface{}) T ResponseFrom(http.Handler) *HttpResponse Errors() (ok, bad AssertErrFunc) Mixed() (ok, bad MixedErrFunc) }
type HttpResponse ¶ added in v0.2.0
func (*HttpResponse) BodyEquals ¶ added in v0.3.0
func (t *HttpResponse) BodyEquals(exp []byte, m, p string, opt ...interface{})
func (*HttpResponse) BodyIs ¶ added in v0.3.0
func (t *HttpResponse) BodyIs(exp string, m, p string, opt ...interface{})
BodyIs the same as BodyEquals
func (*HttpResponse) Contains ¶ added in v0.3.0
func (t *HttpResponse) Contains(exp string, m, p string, opt ...interface{})
Contains fails if body does not contain exp
func (*HttpResponse) Header ¶ added in v0.3.0
func (t *HttpResponse) Header(k, exp string, m, p string, opt ...interface{})
Header checks if the k header matches exp value
func (*HttpResponse) StatusCode ¶ added in v0.2.0
func (t *HttpResponse) StatusCode(exp int, m, p string, opt ...interface{})
StatusCode checks the expected status code. Options can be io.Reader for body, http.Header for headers or string for an error message.
Example ¶
package main import ( "net/http" "strings" "testing" "github.com/gregoryv/asserter" ) var ( t = &testing.T{} handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) ) func main() { assert := asserter.New(t) exp := assert().ResponseFrom(handler) // io.Reader option means body exp.StatusCode(200, "POST", "/", strings.NewReader("the body")) // string option means error message exp.StatusCode(200, "GET", "/", "should be ok") // http.Header additional headers exp.StatusCode(200, "GET", "/", http.Header{ "Content-Type": []string{"text/plain"}, }) }
Output:
type MixedErrFunc ¶ added in v0.3.0
func NewFatalMixed ¶ added in v0.4.0
func NewFatalMixed(t T) (ok, bad MixedErrFunc)
func NewMixed ¶ added in v0.4.0
func NewMixed(t T) (ok, bad MixedErrFunc)
type WrappedT ¶ added in v0.4.0
type WrappedT struct {
T
}
func Wrap ¶ added in v0.4.0
Example ¶
package main import ( "net/http" "testing" "github.com/gregoryv/asserter" ) var t = &testing.T{} func main() { w := asserter.Wrap(t) w.MixOk(something()) got := 2 exp := 1 w.Assert().Equals(got, exp) // Same as w.Assert(got == exp).Errorf("got %v, expected %v", got, exp) resp, _ := http.Get("http://example.com") w.Assert().Contains(resp.Body, "</html>") } func something() (int, error) { return 1, nil }
Output:
func (*WrappedT) Contains ¶ added in v0.4.0
Contains checks the body for the given expression. The body can be various types.
func (*WrappedT) Equals ¶ added in v0.4.0
Equals fails with error if got is different from exp. Returns T for e.g. logging extra or calling Fatal if needed.
func (*WrappedT) Errors ¶ added in v0.4.0
func (w *WrappedT) Errors() (ok, bad AssertErrFunc)
func (*WrappedT) FatalErrors ¶ added in v0.4.0
func (w *WrappedT) FatalErrors() (ok, bad AssertErrFunc)
func (*WrappedT) FatalMixed ¶ added in v0.4.0
func (w *WrappedT) FatalMixed() (ok, bad MixedErrFunc)
func (*WrappedT) Mixed ¶ added in v0.4.0
func (w *WrappedT) Mixed() (ok, bad MixedErrFunc)
func (*WrappedT) MustMixBad ¶ added in v0.4.0
func (*WrappedT) ResponseFrom ¶ added in v0.4.0
func (w *WrappedT) ResponseFrom(h http.Handler) *HttpResponse