testsupport

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 12 Imported by: 0

README

testsupport helpers

Utilities in this package centralise the fixture workflow described in go-form-gen.md:268-301.

  • LoadDocumentFromPath and LoadDocument construct pkg/openapi.Document wrappers for OpenAPI fixtures.
  • LoadFormModel/MustLoadFormModel and MustLoadOperations read golden JSON snapshots into their typed forms.
  • CompareGolden wraps cmp.Diff so tests surface human-friendly diffs.
  • WriteMaybeGolden, WriteGolden, and WriteFormModel honour the UPDATE_GOLDENS toggle used by contract tests.
  • Validation snapshots include canonical rule identifiers (min, max, minLength, maxLength, pattern) with string parameters so diff output remains stable even as numeric and textual constraints expand.

To refresh every golden in the project, run:

./scripts/update_goldens.sh

Pass package patterns (for example ./internal/model) to limit the update scope.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureTemplateOutput

func CaptureTemplateOutput(t *testing.T, render func(io.Writer) (string, error)) (string, string)

CaptureTemplateOutput executes a render function that writes to an io.Writer, returning both the string result and the writer contents. Tests can assert the renderer returns and writes the same payload without duplicating buffer setup.

func CompareGolden

func CompareGolden(want, got any) string

CompareGolden returns a diff string if the values differ.

func Context

func Context() context.Context

Context returns a background context for tests.

func LoadDocument

func LoadDocument(t *testing.T, path string) pkgopenapi.Document

LoadDocument reads a fixture and builds an openapi.Document using a file source. Testing helpers panic on failure to keep contract tests concise.

func LoadDocumentFromPath

func LoadDocumentFromPath(path string) (pkgopenapi.Document, error)

LoadDocumentFromPath returns a Document without requiring testing.T, allowing callers to wire fixtures in setup functions while honouring go-form-gen.md:268-301.

func LoadFormModel

func LoadFormModel(path string) (pkgmodel.FormModel, error)

LoadFormModel reads a JSON fixture into a FormModel, returning an error for callers managing setup outside of *testing.T.

func MustLoadFormModel

func MustLoadFormModel(t *testing.T, path string) pkgmodel.FormModel

MustLoadFormModel loads a JSON golden file into a FormModel structure.

func MustLoadOperations

func MustLoadOperations(t *testing.T, path string) map[string]pkgopenapi.Operation

MustLoadOperations loads a JSON golden file into the provided map pointer.

func MustReadGolden

func MustReadGolden(t *testing.T, path string) []byte

MustReadGolden reads a golden file and returns its raw bytes.

func MustReadGoldenString

func MustReadGoldenString(t *testing.T, path string) string

MustReadGoldenString reads a golden file and returns its string content.

func WriteFormModel

func WriteFormModel(t *testing.T, path string, value pkgmodel.FormModel)

WriteFormModel writes a form model golden when UPDATE_GOLDENS is enabled. The JSON mirrors the builder output (including validation metadata) to keep snapshot diffs focused on behavioural changes.

func WriteGolden

func WriteGolden(t *testing.T, path string, value any)

WriteGolden writes arbitrary data to a golden file when UPDATE_GOLDENS is set.

func WriteMaybeGolden

func WriteMaybeGolden(t *testing.T, path string, data []byte) bool

WriteMaybeGolden updates a golden file when UPDATE_GOLDENS is set. Returns true if the golden was written (test should exit early).

Types

This section is empty.

Jump to

Keyboard shortcuts

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