forked from k8sgateway/k8sgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equal.go
45 lines (36 loc) · 1.36 KB
/
equal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package matchers
import (
"encoding/json"
"fmt"
"github.com/onsi/gomega/types"
"github.com/onsi/gomega/matchers"
"github.com/sergi/go-diff/diffmatchpatch"
)
var (
_ types.GomegaMatcher = new(BeEquivalentToDiffMatcher)
)
// BeEquivalentToDiff is the same as BeEquivalentTo
// but prints a nice diff on failure best effect use ginkgo with -noColor
func BeEquivalentToDiff(expected interface{}) *BeEquivalentToDiffMatcher {
return &BeEquivalentToDiffMatcher{
BeEquivalentToMatcher: matchers.BeEquivalentToMatcher{
Expected: expected,
},
}
}
type BeEquivalentToDiffMatcher struct {
matchers.BeEquivalentToMatcher
}
func (matcher *BeEquivalentToDiffMatcher) FailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("%s\ndiff: %s", matcher.BeEquivalentToMatcher.FailureMessage(actual), diff(matcher.Expected, actual))
}
func (matcher *BeEquivalentToDiffMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return fmt.Sprintf("%s\ndiff: %s", matcher.BeEquivalentToMatcher.NegatedFailureMessage(actual), diff(matcher.Expected, actual))
}
func diff(expected, actual interface{}) string {
jsonexpected, _ := json.MarshalIndent(expected, "", " ")
jsonactual, _ := json.MarshalIndent(actual, "", " ")
dmp := diffmatchpatch.New()
rawDiff := dmp.DiffMain(string(jsonactual), string(jsonexpected), true)
return dmp.DiffPrettyText(rawDiff)
}