testutil

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package testutil provides utilities for golden file testing.

It mirrors Goa's code generation golden testing approach with: - Global -update/-u flags to refresh goldens - Go/JSON aware formatting and normalization - Generated header version normalization (so headers don't cause diffs) - Simple assertion helpers and a fluent API

Index

Constants

View Source
const DefaultBasePath = "testdata/golden"

DefaultBasePath is the default directory for golden files when using NewGoldenFile with an empty base path.

Variables

This section is empty.

Functions

func Assert

func Assert(t testing.TB, goldenPath string, got []byte)

Assert compares byte content against a golden file.

func AssertGo

func AssertGo(t testing.TB, goldenPath string, got string)

AssertGo formats and compares Go source code.

func AssertJSON

func AssertJSON(t testing.TB, goldenPath string, got []byte)

AssertJSON pretty-prints and compares JSON content.

func AssertString

func AssertString(t testing.TB, goldenPath string, got string)

AssertString compares string content against a golden file.

func CompareOrUpdateGolden

func CompareOrUpdateGolden(t *testing.T, actual, golden string)

CompareOrUpdateGolden is a convenience helper for direct string content. It accepts either absolute or relative golden file paths.

Types

type GoldenFile

type GoldenFile struct {
	// contains filtered or unexported fields
}

GoldenFile manages golden file comparisons using a fluent API.

func NewGoldenFile

func NewGoldenFile(t testing.TB, basePath string) *GoldenFile

NewGoldenFile creates a new GoldenFile. When basePath is empty, it defaults to DefaultBasePath.

func (*GoldenFile) Compare

func (g *GoldenFile) Compare(actual string, golden string)

Compare provides a legacy API: compare string content with a golden file path.

func (*GoldenFile) CompareBytes

func (g *GoldenFile) CompareBytes(actual []byte, golden string)

CompareBytes is like Compare but for byte slices.

func (*GoldenFile) CompareContent

func (g *GoldenFile) CompareContent()

CompareContent compares the prepared content against the golden file.

func (*GoldenFile) Content

func (g *GoldenFile) Content(content []byte) *GoldenFile

Content sets the comparison content.

func (*GoldenFile) Exists

func (g *GoldenFile) Exists(golden string) bool

Exists reports whether the golden file exists at the given path.

func (*GoldenFile) Path

func (g *GoldenFile) Path(path string) *GoldenFile

Path sets the golden file path (absolute or relative to basePath).

func (*GoldenFile) SetUpdateMode

func (g *GoldenFile) SetUpdateMode(update bool)

SetUpdateMode forces update behavior for this instance (independent of flags).

func (*GoldenFile) StringContent

func (g *GoldenFile) StringContent(content string) *GoldenFile

StringContent sets the comparison content from a string.

Jump to

Keyboard shortcuts

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