testutil

package
v2.1.0-alpha.26 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 14 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 CollectStreamChunks

func CollectStreamChunks(t *testing.T, streamer model.Streamer) []model.Chunk

CollectStreamChunks drains a provider-neutral model stream through EOF and fails the test on any other receive error.

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.

func RunProviderConformance

func RunProviderConformance(t *testing.T, suite ProviderConformanceSuite)

RunProviderConformance validates and runs the provider-neutral behavioral matrix. Streaming providers must prove setup and receive errors, event ordering, premature termination, partial cancellation, close errors, and successful terminal behavior. Adapters without streaming must prove their explicit unsupported result.

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.

type ProviderCapabilityConformance

type ProviderCapabilityConformance struct {
	Supported   ProviderConformanceCase
	Unsupported ProviderConformanceCase
}

ProviderCapabilityConformance requires each adapter to prove either the supported behavior or an explicit unsupported contract for one capability. Exactly one case must be set.

type ProviderConformanceCase

type ProviderConformanceCase func(t *testing.T)

ProviderConformanceCase proves one observable model-provider contract.

type ProviderConformanceSuite

type ProviderConformanceSuite struct {
	Provider                      string
	OrdinaryProviderError         ProviderConformanceCase
	RateLimit                     ProviderConformanceCase
	MalformedToolCall             ProviderConformanceCase
	Cancellation                  ProviderConformanceCase
	StructuredOutputAndToolChoice ProviderConformanceCase
	UsageAccounting               ProviderConformanceCase
	OutputLimited                 ProviderConformanceCase
	MultimodalInput               ProviderCapabilityConformance
	TypedThinking                 ProviderCapabilityConformance
	ExactTokenCounting            ProviderCapabilityConformance
	ToolNameRoundTrip             ProviderCapabilityConformance
	Streaming                     ProviderStreamingConformance
}

ProviderConformanceSuite is the minimum behavioral matrix every model adapter must execute. Provider-specific SDK fixtures stay in the owning package; this suite standardizes only externally observable raw model.Provider behavior.

type ProviderStreamingConformance

type ProviderStreamingConformance struct {
	Unsupported      ProviderConformanceCase
	SetupError       ProviderConformanceCase
	ReceiveError     ProviderConformanceCase
	ReceiveRateLimit ProviderCapabilityConformance
	StateMachine     ProviderConformanceCase
	EarlyEOF         ProviderConformanceCase
	PartialCancel    ProviderConformanceCase
	CloseError       ProviderConformanceCase
	Terminal         ProviderConformanceCase
	OutputLimited    ProviderConformanceCase
}

ProviderStreamingConformance describes either an explicit unsupported streaming contract or the required lifecycle and event-grammar cases for a streaming provider.

Jump to

Keyboard shortcuts

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