-
Notifications
You must be signed in to change notification settings - Fork 44
/
examples-regression_test.go
205 lines (183 loc) · 5.47 KB
/
examples-regression_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package goat
import (
"bytes"
"errors"
"flag"
//"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"text/template"
)
const (
examplesDir = "examples"
)
var (
write = flag.Bool("write",
false, "write reference SVG output files")
svgColorLightScheme = flag.String("svg-color-light-scheme", "#000000",
`See help for cmd/goat`)
svgColorDarkScheme = flag.String("svg-color-dark-scheme", "#FFFFFF",
`See help for cmd/goat`)
// Begin the directory name with '_' to hide from git.
svgDeltaDir = flag.String("svg-delta-dir", "_examples_new",
`Directory to be filled with a delta-image file for each
newly-generated SVG that does not match those in ` + examplesDir)
)
func TestExamples(t *testing.T) {
// XX This sweeps up ~every~ *.txt file in examples/
txtPaths, err := filepath.Glob(filepath.Join(examplesDir, "*.txt"))
if err != nil {
t.Fatal(err)
}
baseNames := make([]string, len(txtPaths))
for i := range txtPaths {
baseName, found := strings.CutPrefix(txtPaths[i], examplesDir+"/")
if !found {
panic("Could not cut prefix from pathname.")
}
baseNames[i] = baseName
}
if *write {
writeExamples(examplesDir, examplesDir, baseNames, *svgColorLightScheme, *svgColorDarkScheme)
} else {
t.Logf("Verifying equality of current SVG with examples/ references.\n")
verifyExamples(t, examplesDir, baseNames)
}
}
func writeExamples(inDir, outDir string, baseNames []string, lightColor, darkColor string) {
for _, name := range baseNames {
in := getIn(inDir + "/" + name)
out := getOut(outDir + "/" + name)
BuildAndWriteSVG(in, out, lightColor, darkColor)
in.Close()
out.Close()
}
}
func verifyExamples(t *testing.T, examplesDir string, baseNames []string) {
var failures []string
for _, name := range baseNames {
in := getIn(examplesDir + "/" + name)
buff := &bytes.Buffer{}
BuildAndWriteSVG(in, buff, *svgColorLightScheme, *svgColorDarkScheme)
in.Close()
if nil != compareSVG(t, buff, examplesDir, name) {
failures = append(failures, name)
}
}
if len(failures) > 0 {
t.Logf(`Failed to verify contents of %d .svg files`,
len(failures))
err := os.Mkdir(*svgDeltaDir, 0770)
if err != nil {
t.Fatalf(`
Aborting: "%v"`, err)
}
writeExamples(examplesDir, *svgDeltaDir, failures, "#000088", "#88CCFF")
writeDeltaHTML(t, "../" + examplesDir, *svgDeltaDir, failures)
t.FailNow()
}
}
func compareSVG(t *testing.T, buff *bytes.Buffer, examplesDir string, baseName string) error {
fileName := examplesDir + "/" + baseName
golden, err := getOutString(fileName)
if err != nil {
t.Log(err)
}
if newStr := buff.String(); newStr != golden {
// Skip complaint if the modification timestamp of the .txt file
// source is fresher than that of the .svg?
// => NO, Any .txt difference might be an editing mistake.
t.Logf("Content mismatch for %s. Length was %d, expected %d",
toSVGFilename(fileName), buff.Len(), len(golden))
for i:=0; i<min(len(golden), len(newStr)); i++ {
if newStr[i] != golden[i] {
t.Logf("Differing runes at offset %d: new='%#v' reference='%#v'\n",
i, newStr[i], golden[i])
break
}
}
t.Logf("Generated contents do not match existing %s",
toSVGFilename(fileName))
return errors.New("Generated contents do not match existing")
} else {
if testing.Verbose() {
t.Logf("Existing and generated contents match %s\n",
toSVGFilename(fileName))
}
}
return nil
}
// See https://developer.mozilla.org/en-US/docs/Web/CSS/blend-mode#example_using_difference
func writeDeltaHTML(t *testing.T, examplesDir, deltaDir string, baseNames []string) {
t.Logf("Writing new SVG and HTML delta files into %s/", deltaDir)
tmpl := template.Must(template.New("_ignored_").Parse(`
<style type="text/css">
.blended-images {
height: 100%; /* XX How to make equal to pixel bounds of the SVGs? */
background-size: contain, contain;
background-repeat: no-repeat;
background-blend-mode: difference;
background-image: url('{{.ExamplesDir}}/{{.SvgBaseName}}'), url('{{.DeltaDir}}/{{.SvgBaseName}}');
}
</style>
<div style="background-color: grey;">
<div class="blended-images"></div>
</div>
`))
for _, name := range baseNames {
htmlOutName := stripSuffix(name) + ".html"
t.Logf("\t%s", htmlOutName)
htmlOutFile, err := os.Create(deltaDir + "/" + htmlOutName)
err = tmpl.Execute(htmlOutFile, map[string]string{
"ExamplesDir": examplesDir,
"DeltaDir": ".",
"SvgBaseName": toSVGFilename(name),
})
htmlOutFile.Close()
if err != nil {
panic(err)
}
}
}
func getIn(txtFilename string) io.ReadCloser {
in, err := os.Open(txtFilename)
if err != nil {
panic(err)
}
return in
}
func getOutExport(pathPrefix, txtBaseName string) io.WriteCloser {
svgBaseName := toSVGFilename(txtBaseName)
out, err := os.Create(pathPrefix + svgBaseName)
if err != nil {
panic(err)
}
return out
}
func getOut(txtFilename string) io.WriteCloser {
out, err := os.Create(toSVGFilename(txtFilename))
if err != nil {
panic(err)
}
return out
}
func getOutString(txtFilename string) (string, error) {
b, err := ioutil.ReadFile(toSVGFilename(txtFilename))
if err != nil {
// XX Simply panic rather than return an error?
return "", err
}
// XX Why are there RETURN characters in contents of the .SVG files?
b = bytes.ReplaceAll(b, []byte("\r\n"), []byte("\n"))
return string(b), nil
}
func toSVGFilename(txtFilename string) string {
return strings.TrimSuffix(txtFilename, filepath.Ext(txtFilename)) + ".svg"
}
func stripSuffix(basename string) string {
return strings.Split(basename,".")[0]
}