core

package
v0.44.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 6 Imported by: 0

README

The core package is the foundation of a dependency-free Go testing module, providing low-level utilities to support readable and robust test cases. It’s designed to simplify testing by offering tools for nil checking, panic detection, pointer comparison, and mocking *testing.T, all with a focus on developer experience (DX).

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

func IsNil(have any) bool

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

func Pointer(val reflect.Value) unsafe.Pointer

Pointer checks if the argument represents a pointer type and returns its memory address as an unsafe.Pointer, otherwise returns nil.

func Same

func Same(want, have any) bool

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

func Value(val reflect.Value) (any, bool)

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

func ValueSimple(val reflect.Value) (any, bool)

ValueSimple returns the underlying value and true when the provided reflect.Value is a simple type. Otherwise, returns untyped nil and false.

nolint: cyclop

func WillPanic added in v0.6.0

func WillPanic(fn func()) (val any, stack string)

WillPanic returns not empty stack trace if the passed function panicked when executed and the value that was passed to panic. When a function does not panic, it returns nil and empty stack.

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 NewSpy added in v0.6.0

func NewSpy() *Spy

NewSpy returns new instance of Spy.

func (*Spy) Capture added in v0.6.0

func (spy *Spy) Capture() *Spy

Capture turns on the collection of messages when Error, Errorf, Fatal, and Fatalf are called.

func (*Spy) Error added in v0.6.0

func (spy *Spy) Error(args ...any)

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

func (spy *Spy) Errorf(format string, args ...any)

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

func (spy *Spy) Failed() bool

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

func (spy *Spy) Fatal(args ...any)

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

func (spy *Spy) Fatalf(format string, args ...any)

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

func (spy *Spy) Log() string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL