Documentation
¶
Overview ¶
Package test provides convenient testing routines in form of high-level generic functions Eq and NotEq, which check equality and inequality of the provided values and message on failures of the check with help of the provided instance of interface Failer. The interface is compatible with types from the package testing of the standard library of Go. The functions report in a fixed formats, which also include line numbers of their callers. They're supposed to be called from testing functions Test*.
The package provides more low-level layer in form of type T and its generic methods. The type allows to set up the depth of the unrolling of the caller stack and format templates of the messages. Also, its methods are variadic and allow to specify additional parameters, which could be supplied to the format template.
It should be mentioned, that the functions and the methods of the type also report on inability to unroll the stack. In such a case, they ignore the parameters.
Index ¶
Constants ¶
const EqFormat = "test.Eq failed:%d: got %v, should be %v"
EqFormat is the format string, used by the function Eq
const NotEqFormat = "test.NotEq failed:%d: got %v and %[2]v"
NotEqFormat is the format string, used by the function NotEq
const Version = "v1.0.0"
Version of the library
Variables ¶
This section is empty.
Functions ¶
func Eq ¶
func Eq[V comparable](f Failer, got, shouldBe V)
Eq is a generic function of checking on equality between two provided values. It reports on failure of the condition through the provided instance of Failer, using (in exactly this order) line of the caller, first value (which is supposed to be obtained through the tested calculation) and second value (which the first value should be equal to). If the information on the caller is impossible to obtain, the function reports on that through a distinct message, ignoring the parameters.
func NotEq ¶
func NotEq[V comparable](f Failer, got, shouldNotBe V)
NotEq is a generic function of checking on inequality between two provided values. It reports on failure of the condition through the provided instance of Failer, using (in exactly this order) line of the caller and first value. If the information on the caller is impossible to obtain, the function reports on that through a distinct message, ignoring the parameters.
Types ¶
type Failer ¶
type Failer interface {
// Fatalf should report on a fatal situation in formatted way, which is
// similar, for example, to [testing.T.Fatalf]
Fatalf(string, ...any)
}
Failer is an abstract type of instances, which report on fatal situations
type T ¶
type T struct {
// CallerSkip is the parameter to the function [runtime.Caller], which
// obtains data on files and lines of callers of the testing methods
CallerSkip int
// EqFormat is a format string, formed with the syntax of [fmt.Printf].
// It is used to create a signal string, reporting on the fact, that
// the provided value is not that should be (see method [T.Eq]). The
// format should support the following parameters in the following
// order:
// 1) integer (%d) line of the caller;
// 2) the provided value (%v);
// 3) the value, that should be (%v).
//
// As the method supports supplying of additional arguments, they
// should appear after the parameters above.
//
// Examples:
//
// "failure at %[1]d: got %[2]v, should be %[3]v"
// "%d: test %[4]d: %[2]v != %[3]v
//
// In the last example an additional parameter has been mentioned
// (which seems like a test's order number). A caller of the method
// [T.Eq] should provide a corresponding argument for such a format.
EqFormat string
// NotEqFormat is a format string, formed with the syntax of
// [fmt.Printf]. It is used to create a signal string, reporting on the
// fact, that the provided value is that should not be (see method
// [T.NotEq]). The format should support the following parameters in
// the following order:
// 1) integer (%d) line of the caller;
// 2) the provided value (%v), which is equal to the value, that
// should not be.
//
// As the method supports supplying of additional arguments, they
// should appear after the parameters above.
NotEqFormat string
}
T is a type of testing contexts. It provides testing methods T.Eq and T.NotEq, and encapsulates basic technical information to properly report failing conditions with the methods.
func (*T) Eq ¶
func (t *T) Eq[V comparable](f Failer, got, shouldBe V, args ...any)
Eq is a generic method of checking on equality between two provided values. It reports on failure of the condition through the provided instance of Failer, using (in exactly this order) line of the caller, first value (which is supposed to be obtained through the tested calculation) and second value (which the first value should be equal to). The method also supports additional arguments, which are supplied to the instance of Failer after the values above, keeping the order. It takes its format string from the field T.EqFormat, but if the information on the caller is impossible to obtain, the method reports on that through a distinct message, ignoring the parameters.
func (*T) NotEq ¶
func (t *T) NotEq[V comparable](f Failer, got, shouldNotBe V, args ...any)
NotEq is a generic method of checking on inequality between two provided values. It reports on failure of the condition through the provided instance of Failer, using (in exactly this order) line of the caller and first value. The method also supports additional arguments, which are supplied to the instance of Failer after the values above, keeping the order. It takes its format string from the field T.NotEqFormat field, but if the information on the caller is impossible to obtain, the method reports on that through a distinct message, ignoring the parameters.