Skip to content

Commit

Permalink
Use github.com/kylelemons/godebug for assert diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Dec 15, 2019
1 parent 0632308 commit bf884bf
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
cloud.google.com/go v0.43.0
github.com/alecthomas/chroma v0.6.6
github.com/fatih/color v1.7.0
github.com/kylelemons/godebug v1.1.0
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.9 // indirect
go.opencensus.io v0.22.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
Expand Down
5 changes: 4 additions & 1 deletion internal/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ package assert
import (
"reflect"
"testing"

"github.com/kylelemons/godebug/pretty"
)

// Equal asserts exp == act.
func Equal(t testing.TB, name string, exp, act interface{}) {
t.Helper()
if !reflect.DeepEqual(exp, act) {
t.Fatalf("unexpected %v: exp: %q but got %q", name, exp, act)
t.Fatalf(`unexpected %v: diff:
%v`, name, pretty.Compare(exp, act))
}
}

Expand Down
13 changes: 11 additions & 2 deletions internal/entryhuman/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,20 @@ func Fmt(w io.Writer, ent slog.SinkEntry) string {
}

if multilineVal != "" {
multilineVal = strings.TrimSpace(multilineVal)
multilineKey = c(w, color.FgBlue).Sprintf(`"%v"`, multilineKey)
if msg != "..." {
ents += " ..."
}

// Proper indentation.
lines := strings.Split(multilineVal, "\n")
for i, line := range lines[1:] {
if line != "" {
lines[i+1] = strings.Repeat(" ", len(multilineKey)+4) + line
}
}
multilineVal = strings.Join(lines, "\n")

multilineKey = c(w, color.FgBlue).Sprintf(`"%v"`, multilineKey)
ents += fmt.Sprintf("\n%v: %v", multilineKey, multilineVal)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/entryhuman/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestEntry(t *testing.T) {
Level: slog.LevelInfo,
}, `0001-01-01 00:00:00.000 [INFO] <.:0> ...
"msg": line1
line2`)
line2`)
})

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

t.Run("named", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion sloggers/sloghuman/sloghuman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func TestMake(t *testing.T) {
et, rest, err := entryhuman.StripTimestamp(b.String())
assert.Success(t, "strip timestamp", err)
assert.False(t, "timestamp", et.IsZero())
assert.Equal(t, "entry", " [INFO]\t<sloghuman_test.go:21>\t...\t{\"wowow\": \"me\\nyou\"}\n \"msg\": line1\n\n line2\n", rest)
assert.Equal(t, "entry", " [INFO]\t<sloghuman_test.go:21>\t...\t{\"wowow\": \"me\\nyou\"}\n \"msg\": line1\n\n line2\n", rest)
}
5 changes: 3 additions & 2 deletions sloggers/slogtest/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"testing"

"github.com/kylelemons/godebug/pretty"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
)
Expand All @@ -30,8 +32,7 @@ func Equal(t testing.TB, name string, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
slogtest.Fatal(t, "unexpected value",
slog.F("name", name),
slog.F("exp", exp),
slog.F("act", act),
slog.F("diff", pretty.Compare(exp, act)),
)
}
}
Expand Down

0 comments on commit bf884bf

Please sign in to comment.