assertion

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Assertion is handled by two main interfaces that act as a Fluent API.

Assert interface for value holding.

Expectation interface to apply transformations, expectations, matches. etc on the held value.

Example
package main

import (
	"net/http/httptest"
	"testing"

	"github.com/elethoughts-code/goasserts/assertion"
)

func main() {
	// Testing variable
	t := &testing.T{}

	// Examples
	assert := assertion.New(t)
	assert.That(1).IsEq(1)
	assert.That("123").Not().IsNil()
	assert.That(httptest.NewRecorder()).Cookie("my-cookie").IsNil()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotOfErrorType = errors.New("value is not of type error")
View Source
var ErrNotOfLenType = errors.New("value type should be Array, Slice, String or Map")
View Source
var ErrNotOfMapType = errors.New("value should be a map")
View Source
var ErrNotOfResponseRecorderType = errors.New("value is not of type *httptest.ResponseRecorder")
View Source
var ErrNotOfSliceType = errors.New("value should be a slice")
View Source
var ErrNotOfStringType = errors.New("value should be a string")

Functions

This section is empty.

Types

type Assert

type Assert interface {
	That(v interface{}) Expectation
}

Assert is the assertion entry point. It takes a variable as a value and hold it to apply transformations and expectations to it.

That should takes a value and return a wrapped Expectation around it.

func New

func New(t PublicTB) Assert

New is an Assert builder. it takes a *testing.T like variable (uses PublicTB interface).

type AttributeParser

type AttributeParser interface {
	Attr(key interface{}) Expectation
	Index(i int) Expectation
}

AttributeParser interface encloses attribute/index access transformations. All transformations return the same expectation interface to pile in calls (Fluent API).

Attr(key interface{}) change value to the corresponding attribute. Value should be a struct, a map or a ptr/interface to struct or map.

Index(i int) change value to the corresponding index. Value should be a slice, an array or a ptr/interface to slice or array.

type CommonExpectation

type CommonExpectation interface {
	Matches(m Matcher)
	IsEq(e interface{})
	IsDeepEq(e interface{})
	NoDiff(e interface{})
	IsNil()
	HaveKind(k reflect.Kind)
	IsError(target error)
	AsError(target interface{})
}

CommonExpectation interface hold commonly used expectations. It also exposes Matches(m Matcher) as a generic expectation method. Nearly all expectations interfaces are shortcuts for Matches(m Matcher).

IsEq(e interface{}) expects simple "==" equality.

IsDeepEq(e interface{}) expects value and expectation to be deep equal (reflect.DeepEq).

IsNil() expects value to be nil.

HaveKind(k reflect.Kind) expects value to be of the given reflection Kind.

IsError(target error) and AsError(target interface{}) apply errors.IsError and errors.AsError checks/

NoDiff(e interface{}) uses diff.Diffs(v, e) to check equality. When the expectation fails, it log the deltas detected between the value and the expectation.

type Expectation

Expectation interface have three roles. It changes Expectation state by setting negation, changing expectation result message, etc. It apply transformations on assertion value. It executes expectations.

Not() sets current expectation state to negation. thus `assert.That("123").Not().IsNil()` will pass and assert.That(1).Not().IsEq(1) will fail.

OrFatal() tell the expectation execution to fail with t.Fatal instead of t.Error.

Silent() tell the expectation to fail without logging its message.

Logf() and Log() set custom failing message.

type HTTPRecorderParser

type HTTPRecorderParser interface {
	BodyToString() Expectation
	JSONBodyToMap() Expectation
	JSONBodyToSlice() Expectation
	DecodeBody(decoder func(*bytes.Buffer) (interface{}, error)) Expectation
	Response() Expectation
	Status() Expectation
	Headers() Expectation
	Header(header string) Expectation
	Cookies() Expectation
	Cookie(cookie string) Expectation
}

HTTPRecorderParser interface encloses *httptest.ResponseRecorder value transformations. All transformations return the same expectation interface to pile in calls (Fluent API).

Note: all body transformations do not drain the response buffer. Which means that multiple transformations on the same *httptest.ResponseRecorder value can be executed.

DecodeBody(decoder func(*bytes.Buffer) (interface{}, error)) Changes value to the body using a custom Buffer decoder.

BodyToString() Changes value to the body as String.

JSONBodyToMap() Changes value to the body as map from JSON decoding.

JSONBodyToSlice() Changes value to the body as slice from JSON decoding.

Response() Changes value to the response value. (*httptest.ResponseRecorder response attribute)

Status() Changes value to the response status code.

Headers() Changes value to the response Headers map.

Header(header string) Changes value to a specific Header.

Cookies() Changes value to the response Cookies slice.

Cookie(cookie string) Changes value to a specific Cookie.

type LengthExpectation

type LengthExpectation interface {
	HasLen(len int)
	HasMaxLen(len int)
	HasMinLen(len int)
	IsEmpty()
}

LengthExpectation apply on Array, Slice, Map and String values. It adds expectations related to the value length.

HasLen(len int) check if the value length is equal to the len parameter.

HasMaxLen(len int) check if the value length have not exceeded the len parameter.

HasMinLen(len int) check if the value length is greater than the len parameter.

IsEmpty() check if the value is empty.

type MapEntry

type MapEntry struct {
	Key   interface{}
	Value interface{}
}

MapEntry is the struct used by Entries() transformation to change a map to a slice of entries.

type MapExpectation

type MapExpectation interface {
	ContainsValue(e interface{})
	ContainsKey(e interface{})
}

MapExpectation interface encloses map related expectations.

ContainsValue(e interface{}) check if the map have an equal to e parameter value.

ContainsKey(e interface{}) check if the map have an equal to e parameter key.

type MapTransformer

type MapTransformer interface {
	Values() Expectation
	Keys() Expectation
	Entries() Expectation
}

MapTransformer interface encloses map related transformations. All transformations return the same expectation interface to pile in calls (Fluent API).

Values() Transform the assert value from map to a slice of its values.

Keys() Transform the assert value from map to a slice of its keys.

Entries() Transform the assert value from map to a slice of its entries (MapEntry).

type MatchResult

type MatchResult struct {
	Matches bool
	Log     string
	NLog    string
}

MatchResult is returned by Matcher based expectations.

Log attribute is the straight message to log when match fails

NLog attribute is the negation message to log when match succeed but negation attribute is set.

type Matcher

type Matcher func(v interface{}) (MatchResult, error)

Matcher is a function used on related expectations.

func All

func All(matcher Matcher) Matcher

func AsError

func AsError(target interface{}) Matcher

func AtLeast

func AtLeast(n int, matcher Matcher) Matcher

func Contains

func Contains(e interface{}) Matcher

func ContainsKey

func ContainsKey(e interface{}) Matcher

func ContainsValue

func ContainsValue(e interface{}) Matcher

func HasLen

func HasLen(len int) Matcher

func HasMaxLen

func HasMaxLen(len int) Matcher

func HasMinLen

func HasMinLen(len int) Matcher

func HaveKind

func HaveKind(k reflect.Kind) Matcher

func IsBlank

func IsBlank() Matcher

func IsDeepEq

func IsDeepEq(e interface{}) Matcher

func IsEmpty

func IsEmpty() Matcher

func IsEq

func IsEq(e interface{}) Matcher

func IsError

func IsError(target error) Matcher

func IsNil

func IsNil() Matcher

func MatchRe

func MatchRe(reg string) Matcher

func NoDiff

func NoDiff(e interface{}) Matcher

func Unordered

func Unordered(e interface{}) Matcher

type PublicTB

type PublicTB interface {
	Cleanup(func())
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fail()
	FailNow()
	Failed() bool
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
	Helper()
	Log(args ...interface{})
	Logf(format string, args ...interface{})
	Name() string
	Skip(args ...interface{})
	SkipNow()
	Skipf(format string, args ...interface{})
	Skipped() bool
}

PublicTB interface is used to abstract *testing.T variables.

type SliceExpectation

type SliceExpectation interface {
	Contains(e interface{})
	Unordered(e interface{})
	All(m Matcher)
	AtLeast(n int, m Matcher)
	Any(m Matcher)
}

SliceExpectation interface encloses slice related expectations.

Contains(e interface{}) check if e parameter is into the slice.

Unordered(e interface{}) check if all elements into the e parameter (should be a slice) are into the slice regardless of the order.

All(m Matcher) check if all elements of the value slice matches.

AtLeast(n int, m Matcher) check if at least n elements from the value slice matches.

Any(m Matcher) check if at any element from the value slice matches.

type StringExpectation

type StringExpectation interface {
	IsBlank()
	MatchRe(reg string)
}

StringExpectation interface encloses string related expectations.

IsBlank() check if the value is a blank string.

MatchRe(reg string) applies regex on the value string.

Jump to

Keyboard shortcuts

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