@@ -21,89 +21,19 @@ func main() {
2121 testutil .AssertString (t , "testdata/golden/hello_world.go.golden" , code )
2222}
2323
24- // Example: Using the fluent API
25- func TestFluentAPI (t * testing.T ) {
26- gf := testutil .NewGoldenFile (t , "testdata/golden" )
27-
28- // Generate code
29- code := generateServiceCode ()
30-
31- // Use fluent API for comparison
32- gf .StringContent (code ).
33- Path ("service.go.golden" ).
34- CompareContent ()
35- }
36-
37- // Example: Testing multiple files with batch operations
38- func TestBatchOperations (t * testing.T ) {
39- batch := testutil .NewBatch (t )
40-
41- // Generate multiple files
42- serverCode := generateServerCode ()
43- clientCode := generateClientCode ()
44- typesCode := generateTypesCode ()
45-
46- // Add all files to batch and compare
47- batch .
48- AddString ("server.go.golden" , serverCode ).
49- AddString ("client.go.golden" , clientCode ).
50- AddString ("types.go.golden" , typesCode ).
51- Compare ()
52- }
53-
54- // Example: Custom options for specific needs
55- func TestCustomOptions (t * testing.T ) {
56- opts := testutil.Options {
57- BasePath : "testdata/custom" ,
58- ContentType : testutil .ContentTypeGo ,
59- FormatCode : true ,
60- NormalizeWhitespace : true ,
61- CreateMissing : true , // Create golden files if they don't exist
62- DiffContextLines : 5 , // Show 5 lines of context in diffs
63- }
64-
65- gf := testutil .WithOptions (t , opts )
66-
67- code := generateComplexCode ()
68-
69- gf .StringContent (code ).
70- Path ("complex.go.golden" ).
71- CompareContent ()
72- }
73-
7424// Example: Format-aware comparisons
7525func TestFormatAwareComparisons (t * testing.T ) {
76- // Test Go code - automatically formatted
26+ // Test Go code - automatically formatted (auto-detected by .go.golden)
7727 goCode := `package main
7828import "fmt"
7929func main(){fmt.Println("unformatted")}`
80-
30+
8131 testutil .AssertGo (t , "testdata/golden/formatted.go.golden" , goCode )
8232
83- // Test JSON - automatically pretty-printed
33+ // Test JSON - automatically pretty-printed (auto-detected by .json.golden)
8434 jsonData := []byte (`{"name":"test","value":42,"items":["a","b","c"]}` )
85-
86- testutil .AssertJSON (t , "testdata/golden/config.json.golden" , jsonData )
87- }
8835
89-
90- // Example: Legacy migration
91- func TestLegacyMigration (t * testing.T ) {
92- code := generateLegacyCode ()
93-
94- // This is a drop-in replacement for the old compareOrUpdateGolden function
95- testutil .CompareOrUpdateGolden (t , code , "testdata/golden/legacy.golden" )
96- }
97-
98- // Example: Conditional golden file creation
99- func TestConditionalCreation (t * testing.T ) {
100- code := generateOptionalFeature ()
101-
102- // Only create/compare if feature is enabled
103- if code != "" {
104- gf := testutil .NewGoldenFile (t , "testdata/golden" )
105- gf .CompareOrCreate (code , "optional_feature.golden" )
106- }
36+ testutil .AssertJSON (t , "testdata/golden/config.json.golden" , jsonData )
10737}
10838
10939// Example: Testing with subtests
@@ -143,15 +73,15 @@ func TestWithSubtests(t *testing.T) {
14373// Example: Parallel golden file updates
14474func TestParallelUpdates (t * testing.T ) {
14575 // When running with -update, files are updated in parallel by default
146- files := map [string ]string {
147- "parallel1.golden" : generateParallel1 (),
148- "parallel2.golden" : generateParallel2 (),
149- "parallel3.golden" : generateParallel3 (),
150- "parallel4.golden" : generateParallel4 (),
76+ files := map [string ]string {}
77+ for i := 1 ; i <= 4 ; i ++ {
78+ files [fmt .Sprintf ("parallel%d.golden" , i )] = generateParallel (i )
15179 }
15280
153- gf := testutil .NewGoldenFile (t , "testdata/golden" )
154- gf .CompareMultiple (files )
81+ for golden , actual := range files {
82+ gf := testutil .NewGoldenFile (t , "testdata/golden" )
83+ gf .StringContent (actual ).Path (golden ).CompareContent ()
84+ }
15585}
15686
15787// Helper functions for examples
@@ -178,45 +108,7 @@ func (s *serviceImpl) DoSomething(ctx context.Context) error {
178108`
179109}
180110
181- func generateServerCode () string {
182- return `package main
183-
184- import (
185- "log"
186- "net/http"
187- )
188-
189- func main() {
190- http.HandleFunc("/", handler)
191- log.Fatal(http.ListenAndServe(":8080", nil))
192- }
193-
194- func handler(w http.ResponseWriter, r *http.Request) {
195- w.Write([]byte("Hello, Server!"))
196- }
197- `
198- }
199-
200- func generateClientCode () string {
201- return `package client
202-
203- import (
204- "net/http"
205- )
206-
207- type Client struct {
208- baseURL string
209- http *http.Client
210- }
211-
212- func New(baseURL string) *Client {
213- return &Client{
214- baseURL: baseURL,
215- http: &http.Client{},
216- }
217- }
218- `
219- }
111+ // removed verbose server/client helpers to keep file concise
220112
221113func generateTypesCode () string {
222114 return `package types
@@ -242,18 +134,7 @@ func generateComplexCode() string {
242134 return generateServiceCode () + "\n " + generateTypesCode ()
243135}
244136
245-
246- func generateLegacyCode () string {
247- return "// Legacy code example\n " + generateSimpleCode ()
248- }
249-
250- func generateOptionalFeature () string {
251- // Simulate optional feature generation
252- if testing .Short () {
253- return ""
254- }
255- return "// Optional feature code\n "
256- }
137+ // removed legacy code generator
257138
258139func generateSimpleCode () string {
259140 return `package simple
@@ -277,7 +158,6 @@ func Find(id string) error {
277158`
278159}
279160
280- func generateParallel1 () string { return fmt .Sprintf ("// Parallel 1\n %s" , generateSimpleCode ()) }
281- func generateParallel2 () string { return fmt .Sprintf ("// Parallel 2\n %s" , generateSimpleCode ()) }
282- func generateParallel3 () string { return fmt .Sprintf ("// Parallel 3\n %s" , generateSimpleCode ()) }
283- func generateParallel4 () string { return fmt .Sprintf ("// Parallel 4\n %s" , generateSimpleCode ()) }
161+ func generateParallel (i int ) string {
162+ return fmt .Sprintf ("// Parallel %d\n %s" , i , generateSimpleCode ())
163+ }
0 commit comments