Documentation
¶
Overview ¶
Package core provides foundational utilities for the Go Testing Module, offering low-level functions and types to support readable and robust test cases.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNil ¶
IsNil checks whether the provided interface is actual nil or wrapped nil. Actual nil means the interface itself has no type or value (have == nil).
func Pointer ¶ added in v0.18.1
Pointer checks if the argument represents a pointer type and returns its memory address as an unsafe.Pointer, otherwise returns nil.
func Same ¶
Same returns true when two generic pointers point to the same memory.
It works with pointers to objects, slices, maps and functions. For arrays, it always returns false.
nolint: cyclop
func Value ¶ added in v0.13.0
Value returns the underlying value represented by the reflect.Value. Returns nil, false if the underlying value cannot be returned.
func ValueSimple ¶ added in v0.19.0
ValueSimple returns the underlying value and true when the provided reflect.Value is a simple type. Otherwise, returns untyped nil and false.
nolint: cyclop
Types ¶
type Spy ¶ added in v0.6.0
type Spy struct {
HelperCalled bool // Tracks if Helper was called.
ReportedError bool // Tracks if Error or Errorf was called.
TriggeredFailure bool // Tracks if Fatal or Fatalf was called.
Messages *bytes.Buffer // Log messages if set.
}
Spy is a testing utility that captures and tracks calls to error and failure reporting methods, used to mock `*testing.T` when testing helper functions. It logs calls to Error, Errorf, Fatal, and Fatalf, allowing verification of test behavior without causing actual test failures.
func (*Spy) Capture ¶ added in v0.6.0
Capture turns on the collection of messages when Error, Errorf, Fatal, and Fatalf are called.
func (*Spy) Error ¶ added in v0.6.0
Error records a non-fatal error with the provided arguments, appending them as a space-separated message to Messages, followed by a newline. It sets [Spy.ReportedError] to true.
func (*Spy) Errorf ¶ added in v0.6.0
Errorf records a non-fatal error using the provided format string and arguments, appending the formatted message to Messages with a newline. It sets [Spy.ReportedError] to true.
func (*Spy) Failed ¶ added in v0.6.0
Failed reports whether an error or failure has been recorded, returning true if either ReportedError or TriggeredFailure is set. This mimics the behavior of testing.T.Failed, allowing tests to check the Spy's state without modifying it.
func (*Spy) Fatal ¶ added in v0.6.0
Fatal records a fatal error with the provided arguments, appending them as a space-separated message to Messages, followed by a newline. It sets [Spy.TriggeredFailure] to true.
func (*Spy) Fatalf ¶ added in v0.6.0
Fatalf records a fatal error using the provided format string and arguments, appending the formatted message to Messages with a newline. It sets [Spy.TriggeredFailure] to true.
func (*Spy) Helper ¶ added in v0.6.0
func (spy *Spy) Helper()
Helper is a no-op method that satisfies the testing.TB.Helper interface, allowing Spy to be used in contexts expecting a testing helper.
func (*Spy) Log ¶ added in v0.6.0
Log returns the logged messages. If logging has not been enabled via Spy.Capture, it will panic to indicate misuse.
type T ¶ added in v0.6.0
type T interface {
Error(args ...any)
Errorf(format string, args ...any)
Failed() bool
Fatal(args ...any)
Fatalf(format string, args ...any)
Helper()
}
T defines an interface capturing a subset of testing.TB methods, used to test helper functions that accept `*testing.T` and provide reusable assertions for test cases. Implemented by Spy, it allows verification of interactions between test helpers and `*testing.T` behavior without causing actual test errors or failures.