-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdiff_test.go
More file actions
77 lines (65 loc) · 1.5 KB
/
diff_test.go
File metadata and controls
77 lines (65 loc) · 1.5 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
package spruce
import (
"bufio"
"os"
"regexp"
"strings"
"unicode"
"github.com/geofffranks/simpleyaml"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Diff", func() {
YAML := func(s string) map[interface{}]interface{} {
y, err := simpleyaml.NewYaml([]byte(s))
Expect(err).NotTo(HaveOccurred())
data, err := y.Map()
Expect(err).NotTo(HaveOccurred())
return data
}
trim := func(s string) string {
shortest := regexp.MustCompile(`^ +`).FindString(s)
s = strings.TrimRightFunc(s, unicode.IsSpace)
return regexp.MustCompile(`(?m)^`+shortest).ReplaceAllString(s, "")
}
It("passes all diff test cases from tests/diff", func() {
var test, a, b, diff string
var current *string
testPat := regexp.MustCompile(`^###+\s+(.*)\s*$`)
runtest := func() {
if test == "" {
return
}
By(test)
d, err := Diff(YAML(a), YAML(b))
Expect(err).NotTo(HaveOccurred())
Expect(trim(d.String("$"))).To(Equal(trim(diff)))
}
in, err := os.Open("tests/diff")
Expect(err).NotTo(HaveOccurred())
defer in.Close()
s := bufio.NewScanner(in)
for s.Scan() {
if testPat.MatchString(s.Text()) {
m := testPat.FindStringSubmatch(s.Text())
runtest()
test, a, b, diff = m[1], "", "", ""
continue
}
if s.Text() == "---" {
if a == "" {
current = &a
} else if b == "" {
current = &b
} else {
current = &diff
}
continue
}
if current != nil {
*current = *current + s.Text() + "\n"
}
}
runtest()
})
})