stub

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package stub provides helper functions to replace global variables for testing, and restore them afterwards.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Func

func Func(fnptr any, rets ...any) (restore func())

Func replaces the value of a function pointer with a function that returns the provided values. It returns a function that restores the original function. If the function has multiple return values, pass them all.

Idiomatic usage will look like this:

func TestSomething(t *testing.T) {
	defer stub.StubFunc(&globalFunc, 42)()

	globalFunc() // returns 42
	// ...
}

func Value

func Value[T any](ptr *T, value T) (restore func())

Value replaces the value of a pointer with a new value, and returns a function that restores the original value.

Idiomatic usage will look like this:

func TestSomething(t *testing.T) {
	defer stub.Value(&globalVar, newValue)()

	// ...
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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