fixturetest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package fixturetest allows generating values using a possibly-random source on the first run of a test, and stores it in a file for subsequent runs.

Usage

In your test, add a flag or other means of requesting the update mode:

var _update = flag.Bool("update", false, "update golden files")

Configure the fixtures with the update flag:

var _fixtures = fixturetest.Config{Update: _update}

Set up one or more fixtures:

branchNameFixture := fixturetest.New(_fixtures, "branchName", func() string {
	return randomBranchName()
})

Generate or fetch values using the Get method:

branchNameFixture.Get(t)

If the test is in update mode, the provided function will be called, and the value persisted to disk. Otherwise, the value will be read from disk and passed to the function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Update is a pointer to a value speciftying
	// whether we're in update mode or read mode.
	//
	// This must not be nil.
	Update *bool // required
}

Config configures the behavior of the fixture system.

type Fixture

type Fixture[T any] struct {
	// contains filtered or unexported fields
}

Fixture is a value that is sourced from a function in update mode, and from a file in read mode.

func New

func New[T any](cfg Config, name string, gen func() T) Fixture[T]

New creates a new fixture with the given configuration.

func (Fixture[T]) Get

func (f Fixture[T]) Get(t TestingT) T

Get returns the value of the fixture. If in update mode, the value is generated using the provided function. Otherwise, the value is read from disk.

type TestingT

type TestingT interface {
	Helper()
	Name() string
	Errorf(format string, args ...interface{})
	FailNow()
}

TestingT is a subset of the testing.TB interface.

Jump to

Keyboard shortcuts

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