asserter

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 10 Imported by: 0

README

Build Status codecov Maintainability

asserter - Package for oneline assertions

NO LONGER MAINTAINED!!!

After some consideration and reading Assert Libraries I've decided to stop using this package and advise you the same.

Quick start

go get github.com/gregoryv/asserter

In your tests

func Test_something(t *testing.T) {
    got, err := something()

    assert := asserter.New(t)
    assert(err == nil).Fatal(err)
    assert(got == exp).Errorf("%v, expected %v", got, exp)
    // same as
    assert().Equals(got, exp)

    assert().Contains(got, "text")
    assert().Contains(got, 1)

    // Check readers content
    resp, err := http.Get("http://example.com")
    assert(err == nil).Fatal(err)
    assert().Contains(resp.Body, "<title>")
}

HTTP handler specific

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"},
})

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssertErrFunc added in v0.3.0

type AssertErrFunc func(error) T

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

type AssertFunc func(expr ...bool) Testar

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

type HttpResponse struct {
	T
	http.Handler
}

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

type MixedErrFunc func(interface{}, error) T

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 T

type T interface {
	Helper()
	Error(...interface{})
	Errorf(string, ...interface{})
	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fail()
	FailNow()
	Log(...interface{})
	Logf(string, ...interface{})
}

type Testar added in v0.4.0

type Testar interface {
	T
	Asserter
}

Testar combines testing.T with and Asserter

type WrappedT added in v0.4.0

type WrappedT struct {
	T
}

func Wrap added in v0.4.0

func Wrap(t T) *WrappedT
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) Assert added in v0.4.0

func (w *WrappedT) Assert(expr ...bool) Testar

func (*WrappedT) Bad added in v0.4.0

func (w *WrappedT) Bad(err error) T

func (*WrappedT) Contains added in v0.4.0

func (w *WrappedT) Contains(body, exp interface{}) T

Contains checks the body for the given expression. The body can be various types.

func (*WrappedT) Equals added in v0.4.0

func (w *WrappedT) Equals(got, exp interface{}) T

Equals fails with error if got is different from exp. Returns T for e.g. logging extra or calling Fatal if needed.

func (*WrappedT) Error added in v0.4.0

func (w *WrappedT) Error(args ...interface{})

func (*WrappedT) Errorf added in v0.4.0

func (w *WrappedT) Errorf(format string, args ...interface{})

func (*WrappedT) Errors added in v0.4.0

func (w *WrappedT) Errors() (ok, bad AssertErrFunc)

func (*WrappedT) Fail added in v0.4.0

func (w *WrappedT) Fail()

func (*WrappedT) FailNow added in v0.4.0

func (w *WrappedT) FailNow()

func (*WrappedT) Fatal added in v0.4.0

func (w *WrappedT) Fatal(args ...interface{})

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) Fatalf added in v0.4.0

func (w *WrappedT) Fatalf(format string, args ...interface{})

func (*WrappedT) Helper added in v0.4.0

func (w *WrappedT) Helper()

func (*WrappedT) Log added in v0.4.0

func (w *WrappedT) Log(args ...interface{})

func (*WrappedT) Logf added in v0.4.0

func (w *WrappedT) Logf(format string, args ...interface{})

func (*WrappedT) MixBad added in v0.4.0

func (w *WrappedT) MixBad(any interface{}, err error) T

func (*WrappedT) MixOk added in v0.4.0

func (w *WrappedT) MixOk(any interface{}, err error) T

func (*WrappedT) Mixed added in v0.4.0

func (w *WrappedT) Mixed() (ok, bad MixedErrFunc)

func (*WrappedT) MustBad added in v0.4.0

func (w *WrappedT) MustBad(err error) T

func (*WrappedT) MustMixBad added in v0.4.0

func (w *WrappedT) MustMixBad(any interface{}, err error) T

func (*WrappedT) MustMixOk added in v0.4.0

func (w *WrappedT) MustMixOk(any interface{}, err error) T

func (*WrappedT) MustOk added in v0.4.0

func (w *WrappedT) MustOk(err error) T

func (*WrappedT) NotEqual added in v0.4.1

func (w *WrappedT) NotEqual(a, b interface{}) T

func (*WrappedT) Ok added in v0.4.0

func (w *WrappedT) Ok(err error) T

func (*WrappedT) ResponseFrom added in v0.4.0

func (w *WrappedT) ResponseFrom(h http.Handler) *HttpResponse

Jump to

Keyboard shortcuts

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