From bf884bf189940a45175557ebb38cab60c0341369 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sun, 15 Dec 2019 17:01:28 -0600 Subject: [PATCH] Use github.com/kylelemons/godebug for assert diffs --- go.mod | 1 + go.sum | 2 ++ internal/assert/assert.go | 5 ++++- internal/entryhuman/entry.go | 13 +++++++++++-- internal/entryhuman/entry_test.go | 4 ++-- sloggers/sloghuman/sloghuman_test.go | 2 +- sloggers/slogtest/assert/assert.go | 5 +++-- 7 files changed, 24 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 74d66ae..9247148 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 3ecd103..38f1ff5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/assert/assert.go b/internal/assert/assert.go index 69a92b3..2f8d2eb 100644 --- a/internal/assert/assert.go +++ b/internal/assert/assert.go @@ -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)) } } diff --git a/internal/entryhuman/entry.go b/internal/entryhuman/entry.go index fcda4e9..d17f80a 100644 --- a/internal/entryhuman/entry.go +++ b/internal/entryhuman/entry.go @@ -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) } diff --git a/internal/entryhuman/entry_test.go b/internal/entryhuman/entry_test.go index c668500..8530bd7 100644 --- a/internal/entryhuman/entry_test.go +++ b/internal/entryhuman/entry_test.go @@ -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) { @@ -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) { diff --git a/sloggers/sloghuman/sloghuman_test.go b/sloggers/sloghuman/sloghuman_test.go index 5c62637..5c28afb 100644 --- a/sloggers/sloghuman/sloghuman_test.go +++ b/sloggers/sloghuman/sloghuman_test.go @@ -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\t...\t{\"wowow\": \"me\\nyou\"}\n \"msg\": line1\n\n line2\n", rest) + assert.Equal(t, "entry", " [INFO]\t\t...\t{\"wowow\": \"me\\nyou\"}\n \"msg\": line1\n\n line2\n", rest) } diff --git a/sloggers/slogtest/assert/assert.go b/sloggers/slogtest/assert/assert.go index 1584f54..0989db5 100644 --- a/sloggers/slogtest/assert/assert.go +++ b/sloggers/slogtest/assert/assert.go @@ -11,6 +11,8 @@ import ( "strings" "testing" + "github.com/kylelemons/godebug/pretty" + "cdr.dev/slog" "cdr.dev/slog/sloggers/slogtest" ) @@ -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)), ) } }