forked from blampe/goat
-
Notifications
You must be signed in to change notification settings - Fork 8
/
examples_test.go
120 lines (101 loc) · 2.07 KB
/
examples_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
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
package goat
import (
"bytes"
"flag"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
qt "github.com/frankban/quicktest"
)
var write = flag.Bool("write", false, "write examples to disk")
func TestExamplesStableOutput(t *testing.T) {
c := qt.New(t)
var previous string
for i := 0; i < 3; i++ {
in, err := os.Open(filepath.Join(basePath, "circuits.txt"))
if err != nil {
t.Fatal(err)
}
var out bytes.Buffer
BuildAndWriteSVG(in, &out)
in.Close()
if i > 0 && previous != out.String() {
c.Fail()
}
previous = out.String()
}
}
func TestExamples(t *testing.T) {
c := qt.New(t)
filenames, err := filepath.Glob(filepath.Join(basePath, "*.txt"))
c.Assert(err, qt.IsNil)
var buff *bytes.Buffer
for _, name := range filenames {
in := getIn(name)
var out io.WriteCloser
if *write {
out = getOut(name)
} else {
if buff == nil {
buff = &bytes.Buffer{}
} else {
buff.Reset()
}
out = struct {
io.Writer
io.Closer
}{
buff,
io.NopCloser(nil),
}
}
BuildAndWriteSVG(in, out)
in.Close()
out.Close()
if buff != nil {
golden := getOutString(name)
if buff.String() != golden {
c.Log(buff.Len(), len(golden))
c.Fatalf("Content mismatch for %s", name)
}
in.Close()
out.Close()
}
}
}
func BenchmarkComplicated(b *testing.B) {
in := getIn(filepath.FromSlash("examples/complicated.txt"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
BuildAndWriteSVG(in, io.Discard)
}
}
const basePath string = "examples"
func getIn(filename string) io.ReadCloser {
in, err := os.Open(filename)
if err != nil {
panic(err)
}
return in
}
func getOut(filename string) io.WriteCloser {
out, err := os.Create(toSVGFilename(filename))
if err != nil {
panic(err)
}
return out
}
func getOutString(filename string) string {
b, err := ioutil.ReadFile(toSVGFilename(filename))
if err != nil {
panic(err)
}
b = bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n"))
return string(b)
}
func toSVGFilename(filename string) string {
return strings.TrimSuffix(filename, filepath.Ext(filename)) + ".svg"
}