Skip to content
This repository was archived by the owner on Sep 27, 2020. It is now read-only.

Commit cad3283

Browse files
authored
Use jsondiff (#59)
* feat: use nsf/jsondiff * chore: update go.mod
1 parent b72ccc0 commit cad3283

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module github.com/podhmo/go-webtest
22

33
require (
4+
github.com/nsf/jsondiff v0.0.0-20190712045011-8443391ee9b6
45
github.com/pkg/errors v0.8.1
56
github.com/podhmo/go-rfc3339 v0.1.0
67
github.com/podhmo/noerror v0.2.0

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/nsf/jsondiff v0.0.0-20190712045011-8443391ee9b6 h1:qsqscDgSJy+HqgMTR+3NwjYJBbp1+honwDsszLoS+pA=
2+
github.com/nsf/jsondiff v0.0.0-20190712045011-8443391ee9b6/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
13
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
24
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
35
github.com/podhmo/go-rfc3339 v0.1.0 h1:WzY1Kk+j9LSKKxvoZBm/FHx+DVrNqNZx2rPflpZzomw=

jsonequal/fail.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package jsonequal
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/nsf/jsondiff"
7+
)
8+
9+
// FailPlain :
10+
func FailPlain(
11+
left interface{},
12+
right interface{},
13+
lb []byte,
14+
rb []byte,
15+
) error {
16+
ls, rs := string(lb), string(rb)
17+
if ls == rs {
18+
msg := "not equal json\nleft (%[1]T):\n %[3]s\nright (%[2]T):\n %[4]s"
19+
return fmt.Errorf(msg, left, right, ls, rs)
20+
}
21+
// todo : more redable expression
22+
msg := "not equal json\nleft:\n %[3]s\nright:\n %[4]s"
23+
return fmt.Errorf(msg, left, right, ls, rs)
24+
}
25+
26+
// FailJSONDiff :
27+
func FailJSONDiff(
28+
left interface{},
29+
right interface{},
30+
lb []byte,
31+
rb []byte,
32+
) error {
33+
options := jsondiff.DefaultConsoleOptions()
34+
diff, s := jsondiff.Compare(lb, rb, &options)
35+
return fmt.Errorf("%s\n%s\n%s", diff.String(), s, FailPlain(left, right, lb, rb).Error())
36+
}

jsonequal/jsonequal.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package jsonequal
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"io"
76
"reflect"
87

@@ -106,7 +105,7 @@ func ShouldBeSame(
106105
caller.WrapfFunc = errors.WithMessagef
107106
}
108107
if caller.FailFunc == nil {
109-
caller.FailFunc = defaultFail
108+
caller.FailFunc = FailJSONDiff
110109
}
111110

112111
lv, lb, err := lsrc()
@@ -132,19 +131,3 @@ func Equal(
132131
) bool {
133132
return ShouldBeSame(lsrc, rsrc, options...) == nil
134133
}
135-
136-
func defaultFail(
137-
left interface{},
138-
right interface{},
139-
lb []byte,
140-
rb []byte,
141-
) error {
142-
ls, rs := string(lb), string(rb)
143-
if ls == rs {
144-
msg := "not equal json\nleft (%[1]T):\n %[3]s\nright (%[2]T):\n %[4]s"
145-
return fmt.Errorf(msg, left, right, ls, rs)
146-
}
147-
// todo : more redable expression
148-
msg := "not equal json\nleft:\n %[3]s\nright:\n %[4]s"
149-
return fmt.Errorf(msg, left, right, ls, rs)
150-
}

0 commit comments

Comments
 (0)