-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
133 lines (115 loc) · 3.86 KB
/
Copy pathdiff.go
File metadata and controls
133 lines (115 loc) · 3.86 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package testingz // import "ezpkg.io/testingz"
import (
"fmt"
"strings"
"github.com/smartystreets/goconvey/convey"
"ezpkg.io/colorz"
"ezpkg.io/diffz"
"ezpkg.io/fmtz"
"ezpkg.io/typez"
)
func DiffByChar(actual, expect string) (formatted string, isDiff bool) {
diffs := diffz.ByChar(actual, expect)
return diffz.Format(diffs), diffs.IsDiff()
}
func DiffByCharX(actual, expect string, opt diffz.Option) (formatted string, isDiff bool) {
diffs := diffz.ByCharX(actual, expect, opt)
return diffz.Format(diffs), diffs.IsDiff()
}
func DiffByCharZ(actual, expect string) (formatted string, isDiff bool) {
diffs := diffz.ByCharZ(actual, expect)
return diffz.Format(diffs), diffs.IsDiff()
}
func DiffByLine(actual, expect string) (formatted string, isDiff bool) {
diffs := diffz.ByLine(actual, expect)
return diffz.Format(diffs), diffs.IsDiff()
}
func DiffByLineX(actual, expect string, opt diffz.Option) (formatted string, isDiff bool) {
diffs := diffz.ByLineX(actual, expect, opt)
return diffz.Format(diffs), diffs.IsDiff()
}
func DiffByLineZ(actual, expect string) (formatted string, isDiff bool) {
diffs := diffz.ByLineZ(actual, expect)
return diffz.Format(diffs), diffs.IsDiff()
}
// Usage with conveyz:
//
// ΩxNoDiff := ConveyDiffByChar(diffz.IgnoreSpace().AndPlaceholder())
// ΩxNoDiff(expect, actual, "my message")
func ConveyDiffByChar(opt diffz.Option) func(actual, expect string, msgArgs ...any) {
pr := func(text string) {
if opt.IgnoreSpace {
fmt.Println(strings.TrimSpace(text))
} else {
fmt.Print(text)
if !strings.HasSuffix(text, "\n") {
fmt.Print(colorz.Yellow.Wrap("⛔\n(missing newline)\n"))
}
}
}
return func(actual, expect string, msgArgs ...any) {
diffs := diffz.ByCharX(actual, expect, opt)
if !diffs.IsDiff() {
return
}
fmt.Print(colorz.Green.Wrap("\n👉 EXPECTED:\n"))
pr(expect)
fmt.Print(colorz.Red.Wrap("\n👉 ACTUAL:\n"))
pr(actual)
fmt.Print("\n👉 DIFF (", colorz.Red.Wrap("actual"), colorz.Green.Wrap("expected"), "):\n")
fmt.Println(diffz.Format(diffs))
fmt.Println()
msg := typez.Coalesce(fmtz.FormatMsgArgs(msgArgs), "unexpected diff")
convey.So(0, func(any, ...any) string {
return msg // failure with message
})
}
}
// Usage with conveyz:
//
// ΩxNoDiff := ConveyDiffByLine(diffz.IgnoreSpace().AndPlaceholder())
// ΩxNoDiff(expect, actual, "my message")
func ConveyDiffByLine(opt diffz.Option) func(actual, expect string, msgArgs ...any) {
pr := func(text string) {
if opt.IgnoreSpace {
fmt.Println(strings.TrimSpace(text))
} else {
fmt.Print(text)
if !strings.HasSuffix(text, "\n") {
fmt.Print(colorz.Yellow.Wrap("⛔\n(missing newline)\n"))
}
}
}
return func(actual, expect string, msgArgs ...any) {
diffs := diffz.ByLineX(actual, expect, opt)
if !diffs.IsDiff() {
return
}
fmt.Print(colorz.Green.Wrap("\n👉 EXPECTED:\n"))
pr(expect)
fmt.Print(colorz.Red.Wrap("\n👉 ACTUAL:\n"))
pr(actual)
fmt.Print("\n👉 DIFF (", colorz.Red.Wrap("actual"), colorz.Green.Wrap("expected"), "):\n")
fmt.Println(diffz.Format(diffs))
msg := typez.Coalesce(fmtz.FormatMsgArgs(msgArgs), "unexpected diff")
convey.So(0, func(any, ...any) string {
return msg // failure with message
})
}
}
var _NoDiffByChar = ConveyDiffByChar(diffz.Option{})
var _NoDiffByCharZ = ConveyDiffByChar(diffz.IgnoreSpace().AndPlaceholder())
var _NoDiffByLine = ConveyDiffByLine(diffz.Option{})
var _NoDiffByLineZ = ConveyDiffByLine(diffz.IgnoreSpace().AndPlaceholder())
func ΩxNoDiffByChar(actual, expect string, msgArgs ...any) {
_NoDiffByChar(actual, expect, msgArgs...)
}
func ΩxNoDiffByCharZ(actual, expect string, msgArgs ...any) {
_NoDiffByCharZ(actual, expect, msgArgs...)
}
func ΩxNoDiffByLine(actual, expect string, msgArgs ...any) {
_NoDiffByLine(actual, expect, msgArgs...)
}
func ΩxNoDiffByLineZ(actual, expect string, msgArgs ...any) {
_NoDiffByLineZ(actual, expect, msgArgs...)
}