-
Notifications
You must be signed in to change notification settings - Fork 35
/
equality_diff.go
37 lines (32 loc) · 1.04 KB
/
equality_diff.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
package assertions
import (
"fmt"
"github.com/smarty/assertions/internal/go-diff/diffmatchpatch"
)
func composePrettyDiff(expected, actual string) string {
diff := diffmatchpatch.New()
diffs := diff.DiffMain(expected, actual, false)
if prettyDiffIsLikelyToBeHelpful(diffs) {
return fmt.Sprintf("\nDiff: '%s'", diff.DiffPrettyText(diffs))
}
return ""
}
// prettyDiffIsLikelyToBeHelpful returns true if the diff listing contains
// more 'equal' segments than 'deleted'/'inserted' segments.
func prettyDiffIsLikelyToBeHelpful(diffs []diffmatchpatch.Diff) bool {
equal, deleted, inserted := measureDiffTypeLengths(diffs)
return equal > deleted && equal > inserted
}
func measureDiffTypeLengths(diffs []diffmatchpatch.Diff) (equal, deleted, inserted int) {
for _, segment := range diffs {
switch segment.Type {
case diffmatchpatch.DiffEqual:
equal += len(segment.Text)
case diffmatchpatch.DiffDelete:
deleted += len(segment.Text)
case diffmatchpatch.DiffInsert:
inserted += len(segment.Text)
}
}
return equal, deleted, inserted
}