Skip to content

Commit

Permalink
make pp.Diff behave like pp.PP for convenience sake
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed May 16, 2023
1 parent e0e0bb5 commit 272c6b1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
2 changes: 1 addition & 1 deletion assert/Diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ type diffFn func(value, othValue any) string

// DiffFunc is the function that will be used to print out two object if they are not equal.
// You can use your preferred diff implementation if you are not happy with the pretty print diff format.
var DiffFunc diffFn = pp.Diff
var DiffFunc diffFn = pp.DiffFormat
9 changes: 7 additions & 2 deletions pp/Diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import (
"text/tabwriter"
)

// Diff format the values in pp.Format and compare the results line by line in a side-by-side style.
func Diff(v1, v2 any) string {
// Diff will pretty print two value and show side-by-side the difference between them.
func Diff(v1, v2 any) {
_, _ = defaultWriter.Write([]byte(DiffString(Format(v1), Format(v2))))
}

// DiffFormat format the values in pp.Format and compare the results line by line in a side-by-side style.
func DiffFormat(v1, v2 any) string {
return DiffString(Format(v1), Format(v2))
}

Expand Down
48 changes: 34 additions & 14 deletions pp/Diff_test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package pp_test
package pp

import (
"bufio"
"bytes"
"strings"
"testing"

"github.com/adamluzsi/testcase/assert"
"github.com/adamluzsi/testcase/pp"
)

const DiffOutput = `
pp_test.X{ pp_test.X{
A: 1, | A: 2,
B: 2, B: 2,
} }
pp.X{ pp.X{
A: 1, | A: 2,
B: 2, B: 2,
} }
`

func TestDiff_smoke(t *testing.T) {
ogw := defaultWriter
defer func() { defaultWriter = ogw }()
buf := &bytes.Buffer{}
defaultWriter = buf

type X struct{ A, B int }
v1 := X{A: 1, B: 2}
v2 := X{A: 2, B: 2}
Diff(v1, v2)

exp := strings.TrimSpace(DiffOutput)
got := strings.TrimSpace(buf.String())
mustEqual(t, exp, got)
}

func TestDiffFormat_smoke(t *testing.T) {
type X struct{ A, B int }
v1 := X{A: 1, B: 2}
v2 := X{A: 2, B: 2}
tr := strings.TrimSpace
assert.Equal(t, tr(DiffOutput), tr(pp.Diff(v1, v2)))
mustEqual(t, tr(DiffOutput), tr(DiffFormat(v1, v2)))
}

const DiffStringA = `
Expand Down Expand Up @@ -56,14 +70,13 @@ ggg <

func TestPrettyPrinter_DiffString_smoke(t *testing.T) {
t.Run("E2E", func(t *testing.T) {

tr := strings.TrimSpace
got := pp.DiffString(tr(DiffStringA), tr(DiffStringB))
got := DiffString(tr(DiffStringA), tr(DiffStringB))
t.Logf("\n%s", got)
exp := tr(DiffStringOut)
act := tr(got)
t.Logf("\n\nexpected:\n%s\n\nactual:\n%s", exp, act)
assert.Equal(t, exp, act)
mustEqual(t, exp, act)
})
tr := func(str string) string {
var strs []string
Expand Down Expand Up @@ -126,8 +139,15 @@ func TestPrettyPrinter_DiffString_smoke(t *testing.T) {
} {
tc := tc
t.Run(tc.Desc, func(t *testing.T) {
diff := pp.DiffString(tr(tc.A), tr(tc.B))
assert.Equal(t, tr(tc.Diff), tr(diff))
diff := DiffString(tr(tc.A), tr(tc.B))
mustEqual(t, tr(tc.Diff), tr(diff))
})
}
}

func mustEqual(tb testing.TB, exp string, act string) {
tb.Helper()
if act != exp {
tb.Fatalf("exp and got not equal: \n\nexpected:\n%s\n\nactual:\n%s\n", exp, act)
}
}
20 changes: 11 additions & 9 deletions pp/PP_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package pp

import (
"bytes"
"fmt"
"path/filepath"
"runtime"
"testing"
"time"
)
Expand All @@ -21,10 +24,8 @@ func TestFPP(t *testing.T) {
}

act := buf.String()
if act != exp {
t.Logf("got: %#v", act)
t.Fatalf("exp:\n%s\n\nact:\n%s", exp, act)
}

mustEqual(t, exp, act)
}

func TestPP(t *testing.T) {
Expand All @@ -36,12 +37,13 @@ func TestPP(t *testing.T) {

v1 := time.Date(2022, time.July, 26, 17, 36, 19, 882377000, time.UTC)
v2 := "bar"

_, file, line, _ := runtime.Caller(0)
PP(v1, v2)

exp := "PP_test.go:39 time.Date(2022, time.July, 26, 17, 36, 19, 882377000, time.UTC)\t\"bar\"\n"
exp := fmt.Sprintf("%s:%d time.Date(2022, time.July, 26, 17, 36, 19, 882377000, time.UTC)\t\"bar\"\n",
filepath.Base(file), line+1)
act := buf.String()
if act != exp {
t.Logf("got: %#v", act)
t.Fatalf("exp:\n%s\n\nact:\n%s", exp, act)
}

mustEqual(t, exp, act)
}
15 changes: 13 additions & 2 deletions pp/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pp_test
import (
"bytes"
"encoding/json"
"fmt"
"github.com/adamluzsi/testcase/pp"
)

Expand All @@ -19,7 +20,7 @@ func ExampleFormat() {
}

func ExampleDiff() {
_ = pp.Diff(ExampleStruct{
pp.DiffFormat(ExampleStruct{
A: "The Answer",
B: 42,
}, ExampleStruct{
Expand All @@ -28,8 +29,18 @@ func ExampleDiff() {
})
}

func ExampleDiffFormat() {
fmt.Println(pp.DiffFormat(ExampleStruct{
A: "The Answer",
B: 42,
}, ExampleStruct{
A: "The Question",
B: 42,
}))
}

func ExampleDiffString() {
_ = pp.Diff("aaa\nbbb\nccc\n", "aaa\nccc\n")
_ = pp.DiffFormat("aaa\nbbb\nccc\n", "aaa\nccc\n")
}

func ExamplePP_unexportedFields() {
Expand Down

0 comments on commit 272c6b1

Please sign in to comment.