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 ¶
- Variables
- type Assert
- type AttributeParser
- type CommonExpectation
- type Expectation
- type HTTPRecorderParser
- type LengthExpectation
- type MapEntry
- type MapExpectation
- type MapTransformer
- type MatchResult
- type Matcher
- func All(matcher Matcher) Matcher
- func AsError(target interface{}) Matcher
- func AtLeast(n int, matcher Matcher) Matcher
- func Contains(e interface{}) Matcher
- func ContainsKey(e interface{}) Matcher
- func ContainsValue(e interface{}) Matcher
- func HasLen(len int) Matcher
- func HasMaxLen(len int) Matcher
- func HasMinLen(len int) Matcher
- func HaveKind(k reflect.Kind) Matcher
- func IsBlank() Matcher
- func IsDeepEq(e interface{}) Matcher
- func IsEmpty() Matcher
- func IsEq(e interface{}) Matcher
- func IsError(target error) Matcher
- func IsNil() Matcher
- func MatchRe(reg string) Matcher
- func NoDiff(e interface{}) Matcher
- func Unordered(e interface{}) Matcher
- type PublicTB
- type SliceExpectation
- type StringExpectation
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotOfErrorType = errors.New("value is not of type error")
var ErrNotOfLenType = errors.New("value type should be Array, Slice, String or Map")
var ErrNotOfMapType = errors.New("value should be a map")
var ErrNotOfResponseRecorderType = errors.New("value is not of type *httptest.ResponseRecorder")
var ErrNotOfSliceType = errors.New("value should be a slice")
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.
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 ¶
type Expectation interface { Not() Expectation OrFatal() Expectation Silent() Expectation Logf(format string, args ...interface{}) Expectation Log(log string) Expectation CommonExpectation LengthExpectation StringExpectation SliceExpectation MapExpectation MapTransformer AttributeParser HTTPRecorderParser }
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 ¶
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 ContainsKey ¶
func ContainsKey(e interface{}) Matcher
func ContainsValue ¶
func ContainsValue(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.