-
Notifications
You must be signed in to change notification settings - Fork 78
/
diff_test.go
79 lines (68 loc) · 1.56 KB
/
diff_test.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
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
package spruce
import (
"bufio"
"os"
"regexp"
"strings"
"testing"
"unicode"
// Use geofffranks forks to persist the fix in https://github.com/go-yaml/yaml/pull/133/commits
// Also https://github.com/go-yaml/yaml/pull/195
"github.com/geofffranks/simpleyaml"
. "github.com/smartystreets/goconvey/convey"
)
func TestDiff(t *testing.T) {
YAML := func(s string) map[interface{}]interface{} {
y, err := simpleyaml.NewYaml([]byte(s))
So(err, ShouldBeNil)
data, err := y.Map()
So(err, ShouldBeNil)
return data
}
trim := func(s string) string {
/* find the shortest prefix of space characters */
shortest := regexp.MustCompile(`^ +`).FindString(s)
s = strings.TrimRightFunc(s, unicode.IsSpace)
return regexp.MustCompile(`(?m)^`+shortest).ReplaceAllString(s, "")
}
var test, a, b, diff string
var current *string
runtest := func() {
if test == "" {
return
}
Convey(test, t, func() {
d, err := Diff(YAML(a), YAML(b))
So(err, ShouldBeNil)
So(trim(d.String("$")), ShouldEqual, trim(diff))
})
}
testPat := regexp.MustCompile(`^###+\s+(.*)\s*$`)
in, err := os.Open("tests/diff")
if err != nil {
panic(err)
}
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()
}