-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot_test.go
79 lines (67 loc) · 1.71 KB
/
snapshot_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 ftml_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/roblillack/ftml"
)
func CompareDoc(t *testing.T, doc *ftml.Document, snapshotFile string) {
buf := &bytes.Buffer{}
if err := ftml.Write(buf, doc); err != nil {
t.Fatal(err)
}
actual := buf.Bytes()
if err := os.Remove(snapshotFile + ".new"); err != nil && !os.IsNotExist(err) {
t.Fatalf("Unable to remove old snapshot: %s", err)
}
haveError := func(layout string, args ...interface{}) {
out, err := os.Create(snapshotFile + ".new")
if err == nil {
defer out.Close()
_, err = out.Write(actual)
}
if err != nil {
t.Errorf("%s\nUnable to write new snapshot: %s", fmt.Sprintf(layout, args...), err)
}
t.Errorf("%s\nNew snapshot written to %s", fmt.Sprintf(layout, args...), snapshotFile+".new")
}
expected, err := os.ReadFile(snapshotFile)
if err != nil {
haveError("Unable to read snapshot: %s", err)
}
e := strings.Split(string(expected), "\n")
a := strings.Split(string(actual), "\n")
if len(e) != len(a) {
haveError("Number of lines differ: %d != %d", len(e), len(a))
return
}
for i := 0; i < len(e) && i < len(a); i++ {
if e[i] != a[i] {
haveError("Line %d differs:\nExpected: %s\nGot: %s", i, e[i], a[i])
return
}
}
}
func TestParsingAndFormattingExampleFiles(t *testing.T) {
_, filename, _, _ := runtime.Caller(0)
testdocs, err := filepath.Glob(filepath.Join(filepath.Dir(filename), "examples", "*.ftml"))
if err != nil {
t.Fatal(err)
}
for _, inputFile := range testdocs {
f, err := os.Open(inputFile)
if err != nil {
t.Fatal(err)
}
defer f.Close()
doc, err := ftml.Parse(f)
if err != nil {
t.Fatal(err)
}
CompareDoc(t, doc, inputFile)
}
}