-
Notifications
You must be signed in to change notification settings - Fork 444
/
matchers.go
39 lines (32 loc) · 1.24 KB
/
matchers.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
package matchers
import (
"encoding/json"
"fmt"
"github.com/onsi/gomega/matchers"
"github.com/sergi/go-diff/diffmatchpatch"
)
// 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)
}