Skip to content

Commit a13a242

Browse files
authored
Merge pull request #77 from cdr/diff
Use github.com/kylelemons/godebug for assert diffs
2 parents 0632308 + bf884bf commit a13a242

File tree

7 files changed

+24
-8
lines changed

7 files changed

+24
-8
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
cloud.google.com/go v0.43.0
77
github.com/alecthomas/chroma v0.6.6
88
github.com/fatih/color v1.7.0
9+
github.com/kylelemons/godebug v1.1.0
910
github.com/mattn/go-colorable v0.1.2 // indirect
1011
github.com/mattn/go-isatty v0.0.9 // indirect
1112
go.opencensus.io v0.22.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
6464
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
6565
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
6666
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
67+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
68+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
6769
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
6870
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
6971
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=

internal/assert/assert.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ package assert
44
import (
55
"reflect"
66
"testing"
7+
8+
"github.com/kylelemons/godebug/pretty"
79
)
810

911
// Equal asserts exp == act.
1012
func Equal(t testing.TB, name string, exp, act interface{}) {
1113
t.Helper()
1214
if !reflect.DeepEqual(exp, act) {
13-
t.Fatalf("unexpected %v: exp: %q but got %q", name, exp, act)
15+
t.Fatalf(`unexpected %v: diff:
16+
%v`, name, pretty.Compare(exp, act))
1417
}
1518
}
1619

internal/entryhuman/entry.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,20 @@ func Fmt(w io.Writer, ent slog.SinkEntry) string {
119119
}
120120

121121
if multilineVal != "" {
122-
multilineVal = strings.TrimSpace(multilineVal)
123-
multilineKey = c(w, color.FgBlue).Sprintf(`"%v"`, multilineKey)
124122
if msg != "..." {
125123
ents += " ..."
126124
}
125+
126+
// Proper indentation.
127+
lines := strings.Split(multilineVal, "\n")
128+
for i, line := range lines[1:] {
129+
if line != "" {
130+
lines[i+1] = strings.Repeat(" ", len(multilineKey)+4) + line
131+
}
132+
}
133+
multilineVal = strings.Join(lines, "\n")
134+
135+
multilineKey = c(w, color.FgBlue).Sprintf(`"%v"`, multilineKey)
127136
ents += fmt.Sprintf("\n%v: %v", multilineKey, multilineVal)
128137
}
129138

internal/entryhuman/entry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestEntry(t *testing.T) {
4444
Level: slog.LevelInfo,
4545
}, `0001-01-01 00:00:00.000 [INFO] <.:0> ...
4646
"msg": line1
47-
line2`)
47+
line2`)
4848
})
4949

5050
t.Run("multilineField", func(t *testing.T) {
@@ -56,7 +56,7 @@ line2`)
5656
Fields: slog.M(slog.F("field", "line1\nline2")),
5757
}, `0001-01-01 00:00:00.000 [INFO] <.:0> msg ...
5858
"field": line1
59-
line2`)
59+
line2`)
6060
})
6161

6262
t.Run("named", func(t *testing.T) {

sloggers/sloghuman/sloghuman_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ func TestMake(t *testing.T) {
2424
et, rest, err := entryhuman.StripTimestamp(b.String())
2525
assert.Success(t, "strip timestamp", err)
2626
assert.False(t, "timestamp", et.IsZero())
27-
assert.Equal(t, "entry", " [INFO]\t<sloghuman_test.go:21>\t...\t{\"wowow\": \"me\\nyou\"}\n \"msg\": line1\n\n line2\n", rest)
27+
assert.Equal(t, "entry", " [INFO]\t<sloghuman_test.go:21>\t...\t{\"wowow\": \"me\\nyou\"}\n \"msg\": line1\n\n line2\n", rest)
2828
}

sloggers/slogtest/assert/assert.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"github.com/kylelemons/godebug/pretty"
15+
1416
"cdr.dev/slog"
1517
"cdr.dev/slog/sloggers/slogtest"
1618
)
@@ -30,8 +32,7 @@ func Equal(t testing.TB, name string, exp, act interface{}) {
3032
if !reflect.DeepEqual(exp, act) {
3133
slogtest.Fatal(t, "unexpected value",
3234
slog.F("name", name),
33-
slog.F("exp", exp),
34-
slog.F("act", act),
35+
slog.F("diff", pretty.Compare(exp, act)),
3536
)
3637
}
3738
}

0 commit comments

Comments
 (0)