-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_test.go
185 lines (165 loc) · 4.01 KB
/
html_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
package hcj_test
import (
"bytes"
"encoding/json"
"fmt"
"image"
"image/png"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/oakmound/hcj"
"github.com/oakmound/oak/v4/render"
)
var testDirs = []string{"htmlin"}
func Test_RenderHTML_Golden(t *testing.T) {
t.Parallel()
type input struct {
f io.Reader
cfg testConfig
}
inputs := make(map[string]input)
for _, inDirName := range testDirs {
inDir, err := os.ReadDir(filepath.Join("testdata", inDirName))
if err != nil {
t.Fatal(err)
}
for _, fi := range inDir {
ext := filepath.Ext(fi.Name())
if ext == ".json" {
continue
}
path := filepath.Join("testdata", inDirName, fi.Name())
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
htmlData, err := io.ReadAll(f)
if err != nil {
f.Close()
t.Fatal(err)
}
in := input{
f: bytes.NewReader(htmlData),
}
testName := strings.TrimSuffix(path, ext)
cfgFileName := testName + ".json"
if cfgFile, err := os.ReadFile(cfgFileName); err == nil {
var cfg testConfig
err = json.Unmarshal(cfgFile, &cfg)
if err != nil {
t.Fatal(err)
}
in.cfg = cfg
}
inputs[fi.Name()] = in
f.Close()
}
}
outDir, err := os.ReadDir(filepath.Join("testdata", "htmlout"))
if err != nil {
t.Fatal(err)
}
outputs := make(map[string]image.RGBA, len(outDir))
for _, fi := range outDir {
f, err := os.Open(filepath.Join("testdata", "htmlout", fi.Name()))
if err != nil {
t.Fatal(err)
}
pngData, err := png.Decode(f)
if err != nil {
f.Close()
t.Fatal(err)
}
f.Close()
bds := pngData.Bounds()
rgba := image.NewRGBA(bds)
for x := 0; x < bds.Max.X; x++ {
for y := 0; y < bds.Max.Y; y++ {
rgba.Set(x, y, pngData.At(x, y))
}
}
outputs[fi.Name()] = *rgba
}
for i, in := range inputs {
i := i
in := in
t.Run(i, func(t *testing.T) {
t.Parallel()
ext := filepath.Ext(i)
testName := strings.TrimSuffix(i, ext)
if len(in.cfg.Steps) == 0 {
in.cfg.Steps = append(in.cfg.Steps, testStep{})
}
for i, step := range in.cfg.Steps {
in.f.(io.Seeker).Seek(0, io.SeekStart)
stepTestName := testName
if len(in.cfg.Steps) != 1 {
stepTestName += "-step" + strconv.Itoa(i)
}
node, err := hcj.ParseHTML(in.f, hcj.WithInteractiveState(translateConfigState(step)))
if err != nil {
t.Error(err)
return
}
canvasWidth := 500
canvasHeight := 300
if in.cfg.CanvasWidth != 0 {
canvasWidth = in.cfg.CanvasWidth
}
if in.cfg.CanvasHeight != 0 {
canvasHeight = in.cfg.CanvasHeight
}
sp := render.NewEmptySprite(0, 0, canvasWidth, canvasHeight)
node.Draw(sp.GetRGBA(), 0, 0)
rgba1 := *sp.GetRGBA()
pngFileName := stepTestName + ".png"
rgba2, ok := outputs[pngFileName]
if !ok || os.Getenv("OVERRIDE_TESTDATA") != "" {
// create the baseline
fmt.Println("creating baseline for", pngFileName)
outfile, err := os.Create(filepath.Join("testdata", "htmlout", pngFileName))
if err != nil {
t.Fatal(err)
}
png.Encode(outfile, &rgba1)
outfile.Close()
continue
}
if !cmp.Equal(rgba1, rgba2) {
t.Error("mismatch for key " + stepTestName)
os.MkdirAll(filepath.Join("testdata", "htmlfail"), 0700)
outfile, err := os.Create(filepath.Join("testdata", "htmlfail", pngFileName))
if err != nil {
t.Fatal(err)
}
png.Encode(outfile, &rgba1)
// print out internal rep
fmt.Println(node)
outfile.Close()
}
}
})
}
}
type testConfig struct {
CanvasWidth int `json:"canvasWidth"`
CanvasHeight int `json:"canvasHeight"`
Steps []testStep `json:"steps"`
}
type testStep struct {
VisitedAddresses []string `json:"visitedAddresses"`
}
func translateConfigState(s testStep) hcj.InteractiveState {
addresses := make(map[string]struct{})
for _, address := range s.VisitedAddresses {
addresses[address] = struct{}{}
}
return hcj.InteractiveState{
VisitedAddresses: addresses,
}
}