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 AssertProjectionParity(t testing.TB, expected, actual *expr.AttributeExpr)
- func AssertProjectionViewParity(t testing.TB, result *expr.ResultTypeExpr, view string, ...)
- func AssertString(t testing.TB, goldenPath string, got string)
- func CompareOrUpdateGolden(t *testing.T, actual, golden string)
- func FormatProjectionParityDiffs(diffs []ProjectionParityDiff) 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
- type ProjectionParityDiff
- type TestAPI
- type TestField
- type TestMethod
- type TestService
- type TestType
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 AssertProjectionParity ¶ added in v1.0.13
func AssertProjectionParity(t testing.TB, expected, actual *expr.AttributeExpr)
AssertProjectionParity fails the test if the projection shape drifts.
func AssertProjectionViewParity ¶ added in v1.0.13
func AssertProjectionViewParity(t testing.TB, result *expr.ResultTypeExpr, view string, actual *expr.AttributeExpr)
AssertProjectionViewParity fails the test if actual does not contain every field required by the result type view.
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.
func FormatProjectionParityDiffs ¶ added in v1.0.13
func FormatProjectionParityDiffs(diffs []ProjectionParityDiff) string
FormatProjectionParityDiffs formats parity diffs with stable ordering.
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)
type ProjectionParityDiff ¶ added in v1.0.13
ProjectionParityDiff describes a projection drift found while comparing canonical and generated projection attributes.
func ProjectionParityDiffs ¶ added in v1.0.13
func ProjectionParityDiffs(expected, actual *expr.AttributeExpr) []ProjectionParityDiff
ProjectionParityDiffs compares expected and actual projection shapes.
func ProjectionViewParityDiffs ¶ added in v1.0.13
func ProjectionViewParityDiffs(result *expr.ResultTypeExpr, view string, actual *expr.AttributeExpr) []ProjectionParityDiff
ProjectionViewParityDiffs compares a result type view with a generated projected attribute. Extra fields in actual are ignored because generated projected types intentionally carry the union of all view fields while view-specific constructors and validators select the active subset.
type TestAPI ¶ added in v1.0.10
type TestAPI struct {
// Name is the API name. Defaults to "test".
Name string
// Services enumerated in the API.
Services []TestService
}
TestAPI describes a minimal design used to synthesize a DSL function for tests. It covers the common "one service, a handful of endpoints" shape used by most codegen test inputs. For cases that exercise transport- specific concerns (HTTP routes, gRPC rpc:tag, JSON-RPC method names) callers should still hand-write the DSL — this helper keeps the default cases terse and skippable.
Typical use:
fn := testutil.TestAPI{
Name: "svc",
Services: []testutil.TestService{{
Name: "Foo",
Methods: []testutil.TestMethod{{
Name: "GetUser",
Payload: testutil.TestType{Name: "Request", Fields: []testutil.TestField{{Name: "id", Type: expr.String, Required: true}}},
Result: testutil.TestType{Name: "User"},
}},
}},
}.DSL()
root := codegen.RunDSL(t, fn)
type TestField ¶ added in v1.0.10
type TestField struct {
// Name is the attribute name.
Name string
// Type is the DataType of the attribute (e.g., expr.String, expr.Int).
Type expr.DataType
// Required reports whether the attribute should be marked Required.
Required bool
// Description is attached as the attribute description.
Description string
}
TestField describes one attribute within a TestType.
type TestMethod ¶ added in v1.0.10
type TestMethod struct {
// Name is the method name.
Name string
// Payload is the request shape. Leave zero for a method with no payload.
Payload TestType
// Result is the response shape. Leave zero for a method with no result.
Result TestType
// Errors are method-level error names.
Errors []string
}
TestMethod describes one method within a TestService.
type TestService ¶ added in v1.0.10
type TestService struct {
// Name is the service name.
Name string
// Methods enumerated on this service.
Methods []TestMethod
// Errors are service-level error names.
Errors []string
}
TestService describes one service within a TestAPI.
type TestType ¶ added in v1.0.10
type TestType struct {
// Name is the type name. Empty means no type.
Name string
// Fields enumerated on the type. May be empty for a scalar-like or empty
// type (the DSL will emit an empty object).
Fields []TestField
}
TestType describes a user-type used as a payload or result. The zero value means "no type".