Skip to content

Commit c3241de

Browse files
authored
Merge pull request #4 from go-tstr/pretty-json
Add optional JSON formatting
2 parents acf0980 + f800f05 commit c3241de

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

file.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/stretchr/testify/assert"
15+
"github.com/tidwall/pretty"
1516
)
1617

1718
var DefaultHandler = &FileHandler{
@@ -152,6 +153,15 @@ func NoError(t T, err error, msg string) {
152153
}
153154
}
154155

156+
// EqualWithDiff compares two strings and returns true if they are equal.
157+
// If they are not equal, test will be marked as failed and the diff will be logged.
155158
func EqualWithDiff(t T, expected, actual string, msgAndArgs ...interface{}) (ok bool) {
156159
return assert.Equal(t, expected, actual, msgAndArgs...)
157160
}
161+
162+
// PrettyJSON formats the JSON string using the pretty package.
163+
// This is useful for making the JSON more readable in the golden file
164+
// also provides cleaner diffs when comparing JSON files.
165+
func PrettyJSON(t T, data string) string {
166+
return string(pretty.Pretty([]byte(data)))
167+
}

file_test.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func TestEqual_No_Match(t *testing.T) {
142142
got := golden.Assert(&mt, "other string")
143143
assert.False(t, got)
144144
assert.Contains(t, mt.msg, "Not equal:")
145-
assert.False(t, mt.failed) // In case of assert failure, FailNow is not called
145+
assert.True(t, mt.failed)
146146
}
147147

148148
func TestRequest(t *testing.T) {
@@ -156,6 +156,38 @@ func TestRequest(t *testing.T) {
156156
golden.Request(t, http.DefaultClient, req, http.StatusBadRequest)
157157
}
158158

159+
func TestProcessJSON(t *testing.T) {
160+
const (
161+
data1 = `{"name": "someone", "age": 41, "active": true}`
162+
data2 = `{"name": "someone", "age": 42, "active": true}`
163+
)
164+
165+
fh := &golden.FileHandler{
166+
FileName: golden.TestNameToFilePath,
167+
ShouldRecreate: func(t golden.T) bool { return true },
168+
Equal: golden.EqualWithDiff,
169+
ProcessContent: golden.PrettyJSON,
170+
}
171+
172+
t.Cleanup(func() { assert.NoError(t, os.RemoveAll("./testdata/TestSomeJSON"), "failed to remove testdata") })
173+
mt1 := mockT{name: "TestSomeJSON"}
174+
assert.True(t, fh.Assert(&mt1, data1))
175+
assert.Empty(t, mt1.msg)
176+
assert.False(t, mt1.failed)
177+
178+
fh.ShouldRecreate = func(t golden.T) bool { return false }
179+
mt2 := mockT{name: "TestSomeJSON"}
180+
assert.True(t, fh.Assert(&mt2, data1))
181+
assert.Empty(t, mt2.msg)
182+
assert.False(t, mt2.failed)
183+
184+
mt3 := mockT{name: "TestSomeJSON"}
185+
assert.False(t, fh.Assert(&mt3, data2))
186+
assert.Contains(t, mt3.msg, `- "age": 41,`)
187+
assert.Contains(t, mt3.msg, `+ "age": 42,`)
188+
assert.True(t, mt3.failed)
189+
}
190+
159191
func assertResult(t *testing.T, tt test, mt *mockT) {
160192
t.Helper()
161193
assert.Empty(t, mt.msg)
@@ -172,8 +204,11 @@ type mockT struct {
172204
msg string
173205
}
174206

175-
func (m *mockT) Name() string { return m.name }
176-
func (m *mockT) Logf(f string, args ...interface{}) { fmt.Printf(f, args...) }
177-
func (m *mockT) Errorf(f string, args ...interface{}) { m.msg += "\n" + fmt.Sprintf(f, args...) }
178-
func (m *mockT) FailNow() { m.failed = true }
179-
func (m *mockT) Helper() {}
207+
func (m *mockT) Name() string { return m.name }
208+
func (m *mockT) Logf(f string, args ...interface{}) { fmt.Printf(f, args...) }
209+
func (m *mockT) FailNow() { m.failed = true }
210+
func (m *mockT) Helper() {}
211+
func (m *mockT) Errorf(f string, args ...interface{}) {
212+
m.failed = true
213+
m.msg += "\n" + fmt.Sprintf(f, args...)
214+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/go-tstr/golden
22

33
go 1.24.2
44

5-
require github.com/stretchr/testify v1.10.0
5+
require (
6+
github.com/stretchr/testify v1.10.0
7+
github.com/tidwall/pretty v1.2.1
8+
)
69

710
require (
811
4d63.com/gocheckcompilerdirectives v1.3.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
575575
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
576576
github.com/tetafro/godot v1.5.1 h1:PZnjCol4+FqaEzvZg5+O8IY2P3hfY9JzRBNPv1pEDS4=
577577
github.com/tetafro/godot v1.5.1/go.mod h1:cCdPtEndkmqqrhiCfkmxDodMQJ/f3L1BCNskCUZdTwk=
578+
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
579+
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
578580
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67 h1:9LPGD+jzxMlnk5r6+hJnar67cgpDIz/iyD+rfl5r2Vk=
579581
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67/go.mod h1:mkjARE7Yr8qU23YcGMSALbIxTQ9r9QBVahQOBRfU460=
580582
github.com/timonwong/loggercheck v0.11.0 h1:jdaMpYBl+Uq9mWPXv1r8jc5fC3gyXx4/WGwTnnNKn4M=

0 commit comments

Comments
 (0)