Documentation
¶
Index ¶
- Variables
- func Assert(c LikeTB, obtained any, checker Checker, args ...any)
- func Check(c LikeTB, obtained any, checker Checker, args ...any) bool
- func DeepEqual(a1, a2 any) (bool, error)
- func InternalSuite(suite any) any
- func InternalTestingT(t *testing.T)
- func List(suite any) []string
- func ListAll() []string
- func Must[T any, E error, F func() (T, E)](t LikeTB, f F) T
- func Must0[T any, E error, F func() (T, E)](t LikeTB, f F) T
- func Must0_0[E error, F func() E](t LikeTB, f F)
- func Must0_1[T any, E error, F func() (T, E)](t LikeTB, f F) T
- func Must0_2[T any, T2 any, E error, F func() (T, T2, E)](t LikeTB, f F) (T, T2)
- func Must1[T any, E error, A any, F func(A) (T, E)](t LikeTB, f F, a A) T
- func Must1_0[E error, A any, F func(A) E](t LikeTB, f F, a A)
- func Must1_1[T any, E error, A any, F func(A) (T, E)](t LikeTB, f F, a A) T
- func Must1_2[T any, T2 any, E error, A any, F func(A) (T, T2, E)](t LikeTB, f F, a A) (T, T2)
- func Must2[T any, E error, A any, B any, F func(A, B) (T, E)](t LikeTB, f F, a A, b B) T
- func Must2_0[E error, A any, B any, F func(A, B) E](t LikeTB, f F, a A, b B)
- func Must2_1[T any, E error, A any, B any, F func(A, B) (T, E)](t LikeTB, f F, a A, b B) T
- func Must2_2[T any, T2 any, E error, A any, B any, F func(A, B) (T, T2, E)](t LikeTB, f F, a A, b B) (T, T2)
- func Run(t *testing.T, suite any)
- func RunAll(t *testing.T)
- type Binding
- type C
- type Checker
- func And(checker Checker, checkers ...Checker) Checker
- func Deref(checker Checker) Checker
- func Not(checker Checker) Checker
- func Or(checker Checker, checkers ...Checker) Checker
- func OrderedLeft[T ~[]E, E any](matcher Checker) Checker
- func OrderedMatch[T ~[]E, E any](matcher Checker) Checker
- func OrderedRight[T ~[]E, E any](matcher Checker) Checker
- func TimeBetween(start, end time.Time) Checker
- func UnorderedMatch[T ~[]E, E any](matcher Checker) Checker
- type CheckerInfo
- type CommentInterface
- type CustomCheckFunc
- type ErrorStacker
- type LikeC
- type LikeTB
- type Logger
- type MultiChecker
- type TBC
Constants ¶
This section is empty.
Variables ¶
var ExpectedValue = &struct{}{}
ExpectedValue if passed to MultiChecker.AddExpr, will be substituded with the expected value.
var IsNonZeroUUID = And(Not(IsZeroUUID), IsUUID)
IsNonZeroUUID checks the obtained value is a non-zero uuid.
var IsUUID = &uuidChecker{
name: "IsUUID",
}
IsUUID checks the obtained value is a uuid.
var IsZeroUUID = &uuidChecker{
example: "00000000-0000-0000-0000-000000000000",
name: "IsZeroUUID",
}
IsZeroUUID checks the obtained value is zero a uuid.
var JSONEquals = &codecEqualChecker{ name: "JSONEquals", marshal: json.Marshal, unmarshal: json.Unmarshal, }
JSONEquals defines a checker that checks whether a byte slice, when unmarshaled as JSON, is equal to the given value. Rather than unmarshaling into something of the expected body type, we reform the expected body in JSON and back to any, so we can check the whole content. Otherwise we lose information when unmarshaling.
var YAMLEquals = &codecEqualChecker{ name: "YAMLEquals", marshal: yaml.Marshal, unmarshal: yaml.Unmarshal, }
YAMLEquals defines a checker that checks whether a byte slice, when unmarshaled as YAML, is equal to the given value. Rather than unmarshaling into something of the expected body type, we reform the expected body in YAML and back to any, so we can check the whole content. Otherwise we lose information when unmarshaling.
Functions ¶
func DeepEqual ¶
DeepEqual tests for deep equality. It uses normal == equality where possible but will scan elements of arrays, slices, maps, and fields of structs. In maps, keys are compared with == but elements use deep equality. DeepEqual correctly handles recursive types. Functions are equal only if they are both nil.
DeepEqual differs from reflect.DeepEqual in two ways: - an empty slice is considered equal to a nil slice. - two time.Time values that represent the same instant but with different time zones are considered equal.
If the two values compare unequal, the resulting error holds the first difference encountered.
func InternalSuite ¶
InternalSuite registers the given value as a test suite to be run. Any methods starting with the Test prefix in the given value will be considered as a test method. Deprecated: prefer to write a standard test function per suite and call Run.
func InternalTestingT ¶
InternalTestingT runs all test suites registered with the Suite function, printing results to stdout, and reporting any failures back to the "testing" package. Deprecated: prefer to write a standard test function per suite and call Run.
func List ¶
List returns the names of the test functions in the given suite that will be run with the provided run configuration.
func ListAll ¶
func ListAll() []string
ListAll returns the names of all the test functions registered with the Suite function that will be run with the provided run configuration.
func Must ¶
Must calls f with no arguments and asserts that the error return is nil and returns the remaining single return value. Must is an alias of Must0_1.
func Must0 ¶
Must0 calls f with no arguments and asserts that the error return is nil and returns the remaining single return value. Must0 is an alias of Must0_1.
func Must0_0 ¶
Must0_0 calls f with no arguments and asserts that the error return is nil and returns no values.
func Must0_1 ¶
Must0_1 calls f with no arguments and asserts that the error return is nil and returns the remaining single return value.
func Must0_2 ¶
Must0_2 calls f with no arguments and asserts that the error return is nil and returns the remaining two return values.
func Must1 ¶
Must1 calls f with one argument and asserts that the error return is nil and returns the remaining single return value. Must1 is an alias of Must1_1.
func Must1_0 ¶
Must1_0 calls f with one argument and asserts that the error return is nil and returns no values.
func Must1_1 ¶
Must1_1 calls f with one argument and asserts that the error return is nil and returns the remaining single return value.
func Must1_2 ¶
Must1_2 calls f with one argument and asserts that the error return is nil and returns the remaining two return values.
func Must2 ¶
Must2 calls f with two arguments and asserts that the error return is nil and returns the remaining single return value. Must2 is an alias of Must2_1.
func Must2_0 ¶
Must2_0 calls f with two arguments and asserts that the error return is nil and returns no values.
func Must2_1 ¶
Must2_1 calls f with two arguments and asserts that the error return is nil and returns the remaining single return value.
func Must2_2 ¶
func Must2_2[T any, T2 any, E error, A any, B any, F func(A, B) (T, T2, E)](t LikeTB, f F, a A, b B) (T, T2)
Must2_2 calls f with two arguments and asserts that the error return is nil and returns the remaining two return values.
Types ¶
type Binding ¶
type Binding interface {
Checker
// Matches returns true if x passes the checker.
Matches(x any) bool
// Got returns the error from checking x.
Got(x any) string
// String returns the name of the sub-checker and the bound arguments.
String() string
}
Binding provides a way to bind arguments to a checker. You can use it as a checker or as a matcher.
type C ¶
func (*C) Assert ¶
Assert ensures that the first value matches the expected value according to the provided checker. If they do not match, an error is logged, the test is marked as failed, and the test execution stops.
Some checkers may not need the expected argument (e.g. IsNil).
If the last value in args implements CommentInterface, it is used to log additional information instead of being passed to the checker (see Commentf for an example).
func (*C) Check ¶
Check verifies if the first value matches the expected value according to the provided checker. If they do not match, an error is logged, the test is marked as failed, and the test execution continues.
Some checkers may not need the expected argument (e.g. IsNil).
If the last value in args implements CommentInterface, it is used to log additional information instead of being passed to the checker (see Commentf for an example).
func (*C) Logger ¶
Logger enables *C to be used as a logger in functions that require only the minimum interface of *log.Logger.
type Checker ¶
type Checker interface {
Info() *CheckerInfo
Check(params []any, names []string) (result bool, error string)
}
The Checker interface must be provided by checkers used with the Assert and Check verification methods.
var After Checker = &timeCompareChecker{ CheckerInfo: &CheckerInfo{Name: "After", Params: []string{"obtained", "want"}}, compareFunc: func(t1, t2 time.Time) bool { return t1.After(t2) }, }
After checks whether the obtained time.Time is After the want time.Time.
var Almost Checker = &timeCompareChecker{ CheckerInfo: &CheckerInfo{Name: "Almost", Params: []string{"obtained", "want"}}, compareFunc: func(t1, t2 time.Time) bool { return math.Abs(t1.Sub(t2).Seconds()) <= 1.0 }, }
Almost checks whether the obtained time.Time is within 1s of the want time.Time.
var Before Checker = &timeCompareChecker{ CheckerInfo: &CheckerInfo{Name: "Before", Params: []string{"obtained", "want"}}, compareFunc: func(t1, t2 time.Time) bool { return t1.Before(t2) }, }
Before checks whether the obtained time.Time is Before the want time.Time.
var Contains Checker = &containsChecker{ &CheckerInfo{Name: "Contains", Params: []string{"obtained", "expected"}}, }
var DeepEquals Checker = &deepEqualsChecker{ &CheckerInfo{Name: "DeepEquals", Params: []string{"obtained", "expected"}}, }
The DeepEquals checker verifies that the obtained value is deep-equal to the expected value. The check will work correctly even when facing slices, interfaces, and values of different types (which always fail the test).
For example:
c.Assert(value, DeepEquals, 42)
c.Assert(array, DeepEquals, []string{"hi", "there"})
This checker differs from gocheck.DeepEquals in that it will compare a nil slice equal to an empty slice, and a nil map equal to an empty map.
var DoesNotExist Checker = &doesNotExistChecker{ &CheckerInfo{Name: "DoesNotExist", Params: []string{"obtained"}}, }
var DurationLessThan Checker = &durationLessThanChecker{ &CheckerInfo{Name: "DurationLessThan", Params: []string{"obtained", "expected"}}, }
var Equals Checker = &equalsChecker{ &CheckerInfo{Name: "Equals", Params: []string{"obtained", "expected"}}, }
The Equals checker verifies that the obtained value is equal to the expected value, according to usual Go semantics for ==.
For example:
c.Assert(value, Equals, 42)
var ErrorIs Checker = &errorIsChecker{ CheckerInfo: &CheckerInfo{ Name: "ErrorIs", Params: []string{"obtained", "error"}, }, }
ErrorIs checks whether a value is an error that matches the other argument.
var ErrorIsNil Checker = &errorIsNilChecker{ &CheckerInfo{Name: "ErrorIsNil", Params: []string{"value"}}, }
The ErrorIsNil checker tests whether the obtained value is nil. Explicitly tests against only `nil`.
For example:
c.Assert(err, ErrorIsNil)
var ErrorMatches Checker = errorMatchesChecker{ &CheckerInfo{Name: "ErrorMatches", Params: []string{"value", "regex"}}, }
The ErrorMatches checker verifies that the error value is non nil and matches the regular expression provided.
For example:
c.Assert(err, ErrorMatches, "perm.*denied")
var FitsTypeOf Checker = &fitsTypeChecker{ &CheckerInfo{Name: "FitsTypeOf", Params: []string{"obtained", "sample"}}, }
The FitsTypeOf checker verifies that the obtained value is assignable to a variable with the same type as the provided sample value.
For example:
c.Assert(value, FitsTypeOf, int64(0)) c.Assert(value, FitsTypeOf, os.Error(nil))
var GreaterThan Checker = &greaterThanChecker{ &CheckerInfo{Name: "GreaterThan", Params: []string{"obtained", "expected"}}, }
var HasLen Checker = &hasLenChecker{ &CheckerInfo{Name: "HasLen", Params: []string{"obtained", "n"}}, }
The HasLen checker verifies that the obtained value has the provided length. In many cases this is superior to using Equals in conjunction with the len function because in case the check fails the value itself will be printed, instead of its length, providing more details for figuring the problem.
For example:
c.Assert(list, HasLen, 5)
var HasPrefix Checker = &hasPrefixChecker{ &CheckerInfo{Name: "HasPrefix", Params: []string{"obtained", "expected"}}, }
var HasSuffix Checker = &hasSuffixChecker{ &CheckerInfo{Name: "HasSuffix", Params: []string{"obtained", "expected"}}, }
var Ignore Checker = &ignoreChecker{ &CheckerInfo{Name: "Ignore", Params: []string{"obtained"}}, }
Ignore always succeeds.
var Implements Checker = &implementsChecker{ &CheckerInfo{Name: "Implements", Params: []string{"obtained", "ifaceptr"}}, }
The Implements checker verifies that the obtained value implements the interface specified via a pointer to an interface variable.
For example:
var e os.Error c.Assert(err, Implements, &e)
var IsDirectory Checker = &isDirectoryChecker{ &CheckerInfo{Name: "IsDirectory", Params: []string{"obtained"}}, }
IsTrue checks whether a value has an underlying boolean type and is false.
var IsNil Checker = &isNilChecker{ &CheckerInfo{Name: "IsNil", Params: []string{"value"}}, }
The IsNil checker tests whether the obtained value is nil.
For example:
c.Assert(err, IsNil)
var IsNonEmptyFile Checker = &isNonEmptyFileChecker{ &CheckerInfo{Name: "IsNonEmptyFile", Params: []string{"obtained"}}, }
var IsSymlink Checker = &isSymlinkChecker{ &CheckerInfo{Name: "IsSymlink", Params: []string{"obtained"}}, }
var IsTrue Checker = &isTrueChecker{ &CheckerInfo{Name: "IsTrue", Params: []string{"obtained"}}, }
IsTrue checks whether a value has an underlying boolean type and is true.
var IsZero Checker = &zeroChecker{ &CheckerInfo{Name: "IsZero", Params: []string{"obtained"}}, }
IsZero is a Checker that ensures the obtained value is the zero value for the obtained type.
var LessThan Checker = &lessThanChecker{ &CheckerInfo{Name: "LessThan", Params: []string{"obtained", "expected"}}, }
var Matches Checker = &matchesChecker{ &CheckerInfo{Name: "Matches", Params: []string{"value", "regex"}}, }
The Matches checker verifies that the string provided as the obtained value (or the string resulting from obtained.String()) matches the regular expression provided.
For example:
c.Assert(err, Matches, "perm.*denied")
var NotNil Checker = ¬NilChecker{ &CheckerInfo{Name: "NotNil", Params: []string{"value"}}, }
The NotNil checker verifies that the obtained value is not nil.
For example:
c.Assert(iface, NotNil)
This is an alias for Not(IsNil), made available since it's a fairly common check.
NotZero is a Checker that ensures the obtained value is not the zero value for the obtained type.
var PanicMatches Checker = &panicMatchesChecker{ &CheckerInfo{Name: "PanicMatches", Params: []string{"function", "expected"}}, }
The PanicMatches checker verifies that calling the provided zero-argument function will cause a panic with an error value matching the regular expression provided.
For example:
c.Assert(func() { f(1, 2) }, PanicMatches, `open.*: no such file or directory`).
var Panics Checker = &panicsChecker{ &CheckerInfo{Name: "Panics", Params: []string{"function", "expected"}}, }
The Panics checker verifies that calling the provided zero-argument function will cause a panic which is deep-equal to the provided value.
For example:
c.Assert(func() { f(1, 2) }, Panics, &SomeErrorType{"BOOM"}).
var SameContents Checker = &sameContents{ &CheckerInfo{Name: "SameContents", Params: []string{"obtained", "expected"}}, }
SameContents checks that the obtained slice contains all the values (and same number of values) of the expected slice and vice versa, without respect to order or duplicates. Uses DeepEquals on contents to compare. Content types do not need to be hashable, but must satisfy reflect.DeepEquals.
var SamePath Checker = &samePathChecker{ &CheckerInfo{Name: "SamePath", Params: []string{"obtained", "expected"}}, }
SamePath checks paths to see whether they're the same, can follow symlinks and is OS independent
var Satisfies Checker = &satisfiesChecker{ &CheckerInfo{ Name: "Satisfies", Params: []string{"obtained", "func(T) bool"}, }, }
Satisfies checks whether a value causes the argument function to return true. The function must be of type func(T) bool where the value being checked is assignable to T.
var SymlinkDoesNotExist Checker = &symlinkDoesNotExistChecker{ &CheckerInfo{Name: "SymlinkDoesNotExist", Params: []string{"obtained"}}, }
func OrderedLeft ¶
OrderedLeft checks the left/obtained value is a slice and all of its values appear in the right/expected slice in the same order. A matcher is passed to match values between the slices.
func OrderedMatch ¶
OrderedMatch checks the obtained slice contains the same values as the expected, in the same order.
func OrderedRight ¶
OrderedRight checks the left/obtained value is a slice and all of its values appear in the right/expected slice in the same order. A matcher is passed to match values between the slices.
func TimeBetween ¶
func UnorderedMatch ¶
UnorderedMatch checks the obtained slice contains the same values as the expected, but in any order.
type CheckerInfo ¶
See the Checker interface.
func (*CheckerInfo) Info ¶
func (info *CheckerInfo) Info() *CheckerInfo
type CommentInterface ¶
type CommentInterface interface {
CheckCommentString() string
}
CommentInterface must be implemented by types that attach extra information to failed checks. See the Commentf function for details.
func Commentf ¶
func Commentf(format string, args ...any) CommentInterface
Commentf returns an infomational value to use with Assert or Check calls. If the checker test fails, the provided arguments will be passed to fmt.Sprintf, and will be presented next to the logged failure.
For example:
c.Assert(v, Equals, 42, Commentf("Iteration #%d failed.", i))
Note that if the comment is constant, a better option is to simply use a normal comment right above or next to the line, as it will also get printed with any errors:
c.Assert(l, Equals, 8192) // Ensure buffer size is correct (bug #123)
type CustomCheckFunc ¶
CustomCheckFunc should return true for useDefault if DeepEqualWithCustomCheck should behave like DeepEqual. Otherwise the result of the CustomCheckFunc is used.
type ErrorStacker ¶
type LikeC ¶
type LikeC interface {
LikeTB
TestName() string
Logger() Logger
Check(obtained any, checker Checker, args ...any) bool
Assert(obtained any, checker Checker, args ...any)
MkDir() string
}
LikeC offers almost the same as C but only has the common methods between testing.T and testing.B.
type LikeTB ¶
type LikeTB interface {
Attr(key, value string)
Cleanup(func())
Error(args ...any)
Errorf(format string, args ...any)
Fail()
FailNow()
Failed() bool
Fatal(args ...any)
Fatalf(format string, args ...any)
Helper()
Log(args ...any)
Logf(format string, args ...any)
Name() string
Setenv(key, value string)
Chdir(dir string)
Skip(args ...any)
SkipNow()
Skipf(format string, args ...any)
Skipped() bool
TempDir() string
Context() context.Context
Output() io.Writer
}
LikeTB is a copy of testing.TB without the private method.
type MultiChecker ¶
type MultiChecker struct {
*CheckerInfo
// contains filtered or unexported fields
}
MultiChecker is a deep checker that by default matches for equality. But checks can be overriden based on path (either explicit match or regexp)
func NewMultiChecker ¶
func NewMultiChecker() *MultiChecker
NewMultiChecker creates a MultiChecker which is a deep checker that by default matches for equality. But checks can be overriden based on path (either explicit match or regexp)
func NewMultiCheckerWithDefault ¶
func NewMultiCheckerWithDefault(c Checker, args ...any) *MultiChecker
SetDefault changes the default equality checking to another checker. Using tc.Ignore or tc.Equals (with tc.ExpectedValue) is currently the only reasonable checkers.
func (*MultiChecker) AddExpr ¶
func (checker *MultiChecker) AddExpr( expr string, c Checker, args ...any, ) *MultiChecker
AddExpr exception which matches path with go expression. Use _ for wildcard. The top level or root value must be a _ when using expression. Use `len(_.x.y.z)` to override length checking for the path.