Documentation ¶
Overview ¶
Package subtest provides a way of intializing small test functions suitable for use as with the (*testing.T).Run method. Tests using package-defined check functions can generally be initialized in two main ways. here is an example using DeepEquals:
// Short-hand syntax for built-in check functions. t.Run("got==expect", subtest.Value(got).DeepEquals(expect)) // Long syntax. t.Run("got==expect", subtest.Value(got).Test(subtest.DeepEquals(expect)))
Custom CheckFunc implementations can also be turned into tests:
t.Run("got==expect", subtest.Value(got).Test(func(got interface{}) error { if got != expect { return subtest.FailExpect("not equal", got, expect) } }))
Experimentally, any function that takes no parameter and returns an error can also be converted to a test:
t.Run("got==expect", subtest.Test(func() error { if got != expect { return subtest.FailExpect("not plain equal", got, expect) } }))
When necessary, either CheckFunc middleware or a custom ValueFunc instances can be used to prepare or transform the test value before evaluation.
PS! Note that the all experimental syntax may be removed in a later version.
Index ¶
- func FormatType(v interface{}) string
- func KeyError(key interface{}, err error) error
- func SetIndent(s string)
- func SetTypeFormatter(f func(...interface{}) string)
- func Test(f func() error) func(t *testing.T)
- type AllOff
- type Check
- type CheckFunc
- func Any() CheckFunc
- func Before(expect time.Time) CheckFunc
- func Contains(v interface{}) CheckFunc
- func ContainsMatch(c Check) CheckFunc
- func DeepEqual(expect interface{}) CheckFunc
- func Error() CheckFunc
- func ErrorIs(target error) CheckFunc
- func ErrorIsNot(target error) CheckFunc
- func GreaterThan(expect float64) CheckFunc
- func GreaterThanOrEqual(expect float64) CheckFunc
- func LessThan(expect float64) CheckFunc
- func LessThanOrEqual(expect float64) CheckFunc
- func MatchPattern(pattern string) CheckFunc
- func MatchRegexp(r *regexp.Regexp) CheckFunc
- func NoError() CheckFunc
- func NotBefore(expect time.Time) CheckFunc
- func NotDeepEqual(reject interface{}) CheckFunc
- func NotNumericEqual(expect float64) CheckFunc
- func NotReflectNil() CheckFunc
- func NotTimeEqual(expect time.Time) CheckFunc
- func NumericEqual(expect float64) CheckFunc
- func OnCap(c Check) CheckFunc
- func OnFloat64(c Check) CheckFunc
- func OnIndex(i int, c Check) CheckFunc
- func OnLen(c Check) CheckFunc
- func ReflectNil() CheckFunc
- func TimeEqual(expect time.Time) CheckFunc
- type Errors
- type Failure
- type Fields
- type Schema
- type ValueFunc
- func (vf ValueFunc) Before(v time.Time) func(t *testing.T)
- func (vf ValueFunc) Contains(v interface{}) func(t *testing.T)
- func (vf ValueFunc) ContainsMatch(c Check) func(t *testing.T)
- func (vf ValueFunc) DeepEqual(v interface{}) func(t *testing.T)
- func (vf ValueFunc) Error() func(t *testing.T)
- func (vf ValueFunc) ErrorIs(target error) func(t *testing.T)
- func (vf ValueFunc) ErrorIsNot(target error) func(t *testing.T)
- func (vf ValueFunc) GreaterThan(v float64) func(t *testing.T)
- func (vf ValueFunc) GreaterThanOrEqual(v float64) func(t *testing.T)
- func (vf ValueFunc) LessThan(v float64) func(t *testing.T)
- func (vf ValueFunc) LessThanOrEqual(v float64) func(t *testing.T)
- func (vf ValueFunc) MatchPattern(pattern string) func(t *testing.T)
- func (vf ValueFunc) MatchRegexp(r *regexp.Regexp) func(t *testing.T)
- func (vf ValueFunc) NoError() func(t *testing.T)
- func (vf ValueFunc) NotBefore(v time.Time) func(t *testing.T)
- func (vf ValueFunc) NotDeepEqual(v interface{}) func(t *testing.T)
- func (vf ValueFunc) NotNumericEqual(v float64) func(t *testing.T)
- func (vf ValueFunc) NotReflectNil() func(t *testing.T)
- func (vf ValueFunc) NotTimeEqual(v time.Time) func(t *testing.T)
- func (vf ValueFunc) NumericEqual(v float64) func(t *testing.T)
- func (vf ValueFunc) ReflectNil() func(t *testing.T)
- func (vf ValueFunc) Test(c Check) func(t *testing.T)
- func (vf ValueFunc) TimeEqual(v time.Time) func(t *testing.T)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatType ¶
func FormatType(v interface{}) string
FormatType formats a type using the configured type formatter for the package.
func SetIndent ¶
func SetIndent(s string)
SetIndent sets a string to use in package error and type formatting. The default is four spaces, as that's what's used by the Go test-runner. This function is not thread-safe, and should be called as part of initialization only. E.g. in a test package init function.
func SetTypeFormatter ¶
func SetTypeFormatter(f func(...interface{}) string)
SetTypeFormatter replaces the type formatter used by the package. This function is not thread-safe, and should be called as part of initialization only. E.g. in a test package init function.
Types ¶
type CheckFunc ¶
type CheckFunc func(got interface{}) error
CheckFunc is a function that return an error on failure.
func Before ¶
Before returns a check function that fails when the test value is not before expect. Accepts type time.Time and *time.Time.
func Contains ¶
func Contains(v interface{}) CheckFunc
Contains returns a check function that fails if the test value does not contain the input. Accepts input of type array and slice.
func ContainsMatch ¶
ContainsMatch returns a check function that fails if the test value does not contain the check. Accepts input of type array and slice.
func DeepEqual ¶
func DeepEqual(expect interface{}) CheckFunc
DeepEqual returns a check function that fails when the test value does not deep equals to expect.
func Error ¶
func Error() CheckFunc
Error returns a check function that fails if the test value is nil or not an error type.
func ErrorIs ¶
ErrorIs returns a check function that fails if the test value is not an error matching target, or not an error type.
func ErrorIsNot ¶
ErrorIsNot returns a check function that fails if the test value is an error matching target, or not an error type.
func GreaterThan ¶
GreaterThan returns a check function that fails when the test value is not a numeric value greater than expect.
func GreaterThanOrEqual ¶
GreaterThanOrEqual returns a check function that fails when the test value is not a numeric value greater than or equal to expect.
func LessThan ¶
LessThan returns a check function that fails when the test value is not a numeric value less than expect.
func LessThanOrEqual ¶
LessThanOrEqual returns a check function that fails when the test value is not a numeric value less than or equal to expect.
func MatchPattern ¶
MatchPattern is a short-hand for MatchRegexp(regexp.MustCompile(pattern)).
func MatchRegexp ¶
MatchRegexp returns a check function that fails if the test value does not match r. Allowed test value types are string, []byte, json.RawMessage, io.RuneReader and error.
func NoError ¶
func NoError() CheckFunc
NoError returns a check function that fails when the test value is a non-nill error, or if it's not an error type.
func NotBefore ¶
NotBefore returns a check function that fails when the test value is before expect. Accepts type time.Time and *time.Time.
func NotDeepEqual ¶
func NotDeepEqual(reject interface{}) CheckFunc
NotDeepEqual returns a check function that fails when the test value deep equals to reject.
func NotNumericEqual ¶
NotNumericEqual returns a check function that fails when the test value is a numeric value equal to expect.
func NotReflectNil ¶
func NotReflectNil() CheckFunc
NotReflectNil returns a check function that fails when the test value is either an untyped nil value or reflects to a pointer with a nil value.
func NotTimeEqual ¶
NotTimeEqual returns a check function that fails when the test value is a time semantically equal to expect. Accepts type time.Time and *time.Time.
func NumericEqual ¶
NumericEqual returns a check function that fails when the test value is not a numeric value equal to expect.
func OnCap ¶
OnCap returns a check function where the capacity of the test value is extracted and passed to c. Accepted input types are arrays, slices and channels.
func OnFloat64 ¶
OnFloat64 returns a check function where the test value is converted to float64 before it's passed to c.
func OnIndex ¶
OnIndex returns a check function where the item at index i of the test value is passed on to c. Accepted input types are arrays, slices and strings.
func OnLen ¶
OnLen returns a check function where the length of the test value is extracted and passed to c. Accepted input types are arrays, slices, maps, channels and strings.
func ReflectNil ¶
func ReflectNil() CheckFunc
ReflectNil returns a check function that fails when the test value is neither an untyped nil value nor reflects to a pointer with a nil value.
type Failure ¶
type Failure struct { Prefix string Got string Expect string Reject string // contains filtered or unexported fields }
Failure is an error type that aid with consistent formatting of test failures. In error matching, two Failure instances are considered equal when their formattet content is the same.
func FailExpect ¶
FailExpect formats a failure for content that is not matching some expected value. The package type formatter is used.
func FailGot ¶
FailGot formats a failure for some unexpected content. The package type formatter is used.
func FailReject ¶
FailReject formats a failure for content that is matching some rejected value. The package type formatter is used.
type Fields ¶
type Fields map[interface{}]Check
Fields provides a map of check functions.
func (Fields) OrderedKeys ¶
func (m Fields) OrderedKeys() []interface{}
OrderedKeys returns all keys in m in alphanumerical order.
type Schema ¶
type Schema struct { // Fields map keys to checks. Fields Fields // Required, if set, contain a list of required keys. When the list is // explicitly defined as an empty list, no keys will be considered required. // When the field holds a nil value, all keys present in Fields will be // considered required. Required []interface{} // AdditionalFields if set, contain a check used to validate all fields // where the key is not present in Fields. When the field holds a nil // value, no additional keys are allowed. To skip validation of additional // keys, the Any() check can be used. AdditionalFields Check }
Schema allows simple validation of fields. Currently support only maps.
type ValueFunc ¶
type ValueFunc func() (interface{}, error)
ValueFunc is a function returning a value. The main purpose of a ValueFunc instance is to initialize tests against the the returned value.
func Float64 ¶
func Float64(v interface{}) ValueFunc
Float64 returns a new ValueFunc that parses v into a float64. Accepts numeric kinds and string kinds as input.
func Index ¶
Index returns a new ValueFunc for the value at index v[i]. Accepts input of type array, slice and string.
func Value ¶
func Value(v interface{}) ValueFunc
Value returns a new ValueFunc for a static value v.
func (ValueFunc) ContainsMatch ¶
ContainsMatch is equivalent to vf.Test(ContainsMatch{c}).
func (ValueFunc) ErrorIsNot ¶
ErrorIsNot is equivalent to vf.Test(ErrorIsNot(v)). wrapped by vf.
func (ValueFunc) GreaterThan ¶
GreaterThan is equivalent to vf.Test(GreaterThan(v)).
func (ValueFunc) GreaterThanOrEqual ¶
GreaterThanOrEqual is equivalent to vf.Test(GreaterThanOrEqual(v)).
func (ValueFunc) LessThanOrEqual ¶
LessThanOrEqual is equivalent to vf.Test(LessThanOrEqual(v)).
func (ValueFunc) MatchPattern ¶
MatchPattern is equivalent to vf.Test(s.MatchPattern(pattern)).
func (ValueFunc) MatchRegexp ¶
MatchRegexp is equivalent to vf.Test(s.MatchRegexp(r)).
func (ValueFunc) NotDeepEqual ¶
NotDeepEqual is equivalent to vf.Test(NotDeepEqual(v)).
func (ValueFunc) NotNumericEqual ¶
NotNumericEqual is equivalent to vf.Test(NotNumericEqual(v)).
func (ValueFunc) NotReflectNil ¶
NotReflectNil is equivalent to vf.Test(NotReflectNil(v)).
func (ValueFunc) NotTimeEqual ¶
NotTimeEqual is equivalent to vf.Test(NotTimeEqual(v)).
func (ValueFunc) NumericEqual ¶
NumericEqual is equivalent to vf.Test(NumericEqual(v)).
func (ValueFunc) ReflectNil ¶
ReflectNil is equivalent to vf.Test(ReflectNil(v)).
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
module
|
|
internal
|
|
Package subjson contains check function middelware that validates and decodes / JSON before passing it on to another check function.
|
Package subjson contains check function middelware that validates and decodes / JSON before passing it on to another check function. |