Documentation
¶
Overview ¶
Package testutil provides testing utilities for the Loom code generation framework.
Golden File Testing ¶
The package provides utilities for golden file testing, a technique where expected outputs are stored in files and compared against actual outputs during tests. This is particularly useful for testing code generation where outputs can be large and complex.
Basic Usage:
func TestCodeGeneration(t *testing.T) {
// Create a golden file manager
gf := testutil.NewGoldenFile(t, "testdata/golden")
// Generate code
code := generateCode()
// Compare with golden file
gf.Compare(code, "mytest.golden")
}
Updating Golden Files:
To update golden files when the expected output changes, run tests with the -update flag:
go test ./... -update
Legacy Compatibility:
For backward compatibility with existing tests, use CompareOrUpdateGolden:
testutil.CompareOrUpdateGolden(t, actual, "path/to/file.golden")
This function uses the same -update flag but requires the full path to the golden file.
Advanced Usage:
The GoldenFile type provides additional methods for more complex scenarios:
// Compare multiple files at once
gf.CompareMultiple(map[string]string{
"file1.golden": code1,
"file2.golden": code2,
})
// Create golden file if it doesn't exist
gf.CompareOrCreate(code, "new.golden")
// Check if a golden file exists
if gf.Exists("optional.golden") {
gf.Compare(code, "optional.golden")
}
Organization:
Golden files are typically organized under a testdata/golden directory within each package. This keeps test data close to the tests while maintaining a clean structure.
Package testutil provides world-class utilities for testing code generation with golden files. It offers a fluent API, intelligent diffing, batch operations, and format-aware comparisons.
Index ¶
- Constants
- func Assert(t testing.TB, goldenPath string, got []byte)
- func AssertGo(t testing.TB, goldenPath string, got string)
- func AssertJSON(t testing.TB, goldenPath string, got []byte)
- func AssertString(t testing.TB, goldenPath string, got string)
- func CompareOrUpdateGolden(t *testing.T, actual, golden string)
- type GoldenFile
- func (g *GoldenFile) Compare(actual string, golden string)deprecated
- func (g *GoldenFile) CompareBytes(actual []byte, golden string)
- func (g *GoldenFile) CompareContent()
- func (g *GoldenFile) Content(content []byte) *GoldenFile
- func (g *GoldenFile) Exists(golden string) bool
- func (g *GoldenFile) Path(path string) *GoldenFile
- func (g *GoldenFile) SetUpdateMode(update bool)
- func (g *GoldenFile) StringContent(content string) *GoldenFile
Constants ¶
const DefaultBasePath = "testdata/golden"
DefaultBasePath is the default directory for golden files
Variables ¶
This section is empty.
Functions ¶
func AssertJSON ¶
AssertJSON compares JSON content with proper formatting
func AssertString ¶
AssertString provides a simple assertion API for strings
func CompareOrUpdateGolden ¶
CompareOrUpdateGolden provides a drop-in replacement for the legacy function used throughout the codebase. New code should use GoldenFile instead. The golden parameter should be a full path to the golden file.
Types ¶
type GoldenFile ¶
type GoldenFile struct {
// contains filtered or unexported fields
}
GoldenFile manages golden file testing operations with a fluent API
func NewGoldenFile ¶
func NewGoldenFile(t testing.TB, basePath string) *GoldenFile
NewGoldenFile creates a new GoldenFile instance with default options
func (*GoldenFile) Compare
deprecated
func (g *GoldenFile) Compare(actual string, golden string)
Compare compares the actual content with the golden file content (legacy API)
Deprecated: Use StringContent().Path().CompareContent() for the fluent API
func (*GoldenFile) CompareBytes ¶
func (g *GoldenFile) CompareBytes(actual []byte, golden string)
CompareBytes is like Compare but works with byte slices (legacy API)
func (*GoldenFile) CompareContent ¶
func (g *GoldenFile) CompareContent()
CompareContent performs the golden file comparison
func (*GoldenFile) Content ¶
func (g *GoldenFile) Content(content []byte) *GoldenFile
Content sets the content to compare (fluent API)
func (*GoldenFile) Exists ¶
func (g *GoldenFile) Exists(golden string) bool
Exists checks if a golden file exists
func (*GoldenFile) Path ¶
func (g *GoldenFile) Path(path string) *GoldenFile
Path sets the golden file path (fluent API)
func (*GoldenFile) SetUpdateMode ¶
func (g *GoldenFile) SetUpdateMode(update bool)
SetUpdateMode sets whether this GoldenFile should update golden files on compare.
func (*GoldenFile) StringContent ¶
func (g *GoldenFile) StringContent(content string) *GoldenFile
StringContent sets string content to compare (fluent API)