Skip to content

Commit e196d65

Browse files
committed
feat(do): add new Do function to simplify golden usage
Add new Do function whihc ecapsulates the common pattern of writing to golden file if the update mechnism evaluates to true, followed by immediately reading the file. Compresses four lines into one.
1 parent 62e8344 commit e196d65

File tree

5 files changed

+343
-101
lines changed

5 files changed

+343
-101
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ SHELL := env \
3333
#
3434
# Tools
3535
#
36+
3637
# external tool
3738
define tool # 1: binary-name, 2: go-import-path
3839
TOOLS += $(TOOLDIR)/$(1)

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,28 @@ import "github.com/jimeh/go-golden"
2828

2929
```go
3030
func TestExampleMyStruct(t *testing.T) {
31-
got, err := json.Marshal(&MyStruct{Foo: "Bar"})
32-
require.NoError(t, err)
31+
got, err := json.Marshal(&MyStruct{Foo: "Bar"})
32+
require.NoError(t, err)
3333

34-
if golden.Update() {
35-
golden.Set(t, got)
36-
}
37-
want := golden.Get(t)
34+
want := golden.Do(t, got)
3835

39-
assert.Equal(t, want, got)
36+
assert.Equal(t, want, got)
4037
}
4138
```
4239

4340
The above example will read/write to:
4441

4542
- `testdata/TestExampleMyStruct.golden`
4643

44+
The call to `golden.Do()` is equivalent to:
45+
46+
```go
47+
if golden.Update() {
48+
golden.Set(t, got)
49+
}
50+
want := golden.Get(t)
51+
```
52+
4753
To update the golden file (have `golden.Update()` return `true`), simply set the
4854
`GOLDEN_UPDATE` environment variable to one of `1`, `y`, `t`, `yes`, `on`, or
4955
`true` when running tests.

example_test.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"github.com/stretchr/testify/require"
1111
)
1212

13+
// The tests in this file are examples from the README and the package-level Go
14+
// documentation.
15+
1316
type MyStruct struct {
1417
Foo string `json:"foo,omitempty"`
1518
}
@@ -21,10 +24,7 @@ func TestExampleMyStruct(t *testing.T) {
2124
got, err := json.Marshal(&MyStruct{Foo: "Bar"})
2225
require.NoError(t, err)
2326

24-
if golden.Update() {
25-
golden.Set(t, got)
26-
}
27-
want := golden.Get(t)
27+
want := golden.Do(t, got)
2828

2929
assert.Equal(t, want, got)
3030
}
@@ -46,10 +46,7 @@ func TestExampleMyStructTabular(t *testing.T) {
4646
got, err := json.Marshal(tt.obj)
4747
require.NoError(t, err)
4848

49-
if golden.Update() {
50-
golden.Set(t, got)
51-
}
52-
want := golden.Get(t)
49+
want := golden.Do(t, got)
5350

5451
assert.Equal(t, want, got)
5552
})
@@ -64,13 +61,11 @@ func TestExampleMyStructP(t *testing.T) {
6461
gotJSON, _ := json.Marshal(&MyStruct{Foo: "Bar"})
6562
gotXML, _ := xml.Marshal(&MyStruct{Foo: "Bar"})
6663

67-
if golden.Update() {
68-
golden.SetP(t, "json", gotJSON)
69-
golden.SetP(t, "xml", gotXML)
70-
}
64+
wantJSON := golden.DoP(t, "json", gotJSON)
65+
wantXML := golden.DoP(t, "xml", gotXML)
7166

72-
assert.Equal(t, golden.GetP(t, "json"), gotJSON)
73-
assert.Equal(t, golden.GetP(t, "xml"), gotXML)
67+
assert.Equal(t, wantJSON, gotJSON)
68+
assert.Equal(t, wantXML, gotXML)
7469
}
7570

7671
// TestExampleMyStructTabularP reads/writes the following golden file:
@@ -92,13 +87,11 @@ func TestExampleMyStructTabularP(t *testing.T) {
9287
gotJSON, _ := json.Marshal(tt.obj)
9388
gotXML, _ := xml.Marshal(tt.obj)
9489

95-
if golden.Update() {
96-
golden.SetP(t, "json", gotJSON)
97-
golden.SetP(t, "xml", gotXML)
98-
}
90+
wantJSON := golden.DoP(t, "json", gotJSON)
91+
wantXML := golden.DoP(t, "xml", gotXML)
9992

100-
assert.Equal(t, golden.GetP(t, "json"), gotJSON)
101-
assert.Equal(t, golden.GetP(t, "xml"), gotXML)
93+
assert.Equal(t, wantJSON, gotJSON)
94+
assert.Equal(t, wantXML, gotXML)
10295
})
10396
}
10497
}

0 commit comments

Comments
 (0)