Repository navigation
Expand file tree
/
Copy pathexecutable_source_map_test.go
More file actions
161 lines (157 loc) · 4.63 KB
/
Copy pathexecutable_source_map_test.go
File metadata and controls
161 lines (157 loc) · 4.63 KB
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
// SPDX-License-Identifier: Apache-2.0
package mathjax_test
import (
"encoding/json"
"encoding/xml"
"io"
"os"
"strings"
"testing"
mathjax "github.com/d2lang/mathjax-go"
)
// Public TeX errors are rendered as merror SVG. These references check the
// complete public output and message; error IDs are retained observation metadata.
func TestExecutableSourceMapPublicReferences(t *testing.T) {
data, err := os.ReadFile("testdata/executable_source_map_mathjax_3_2_2.json")
if err != nil {
t.Fatal(err)
}
var fixture struct {
MathjaxGitCommit string
Cases []struct {
Name, TeX, SVG string
Display, Public bool
}
}
if err = json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
if fixture.MathjaxGitCommit != "ad8f5c21cb810236551da8c6512ba733e67357ee" || len(fixture.Cases) != 132 {
t.Fatal("unbound executable source-map references")
}
count := 0
for _, c := range fixture.Cases {
if !c.Public {
continue
}
count++
t.Run(c.Name, func(t *testing.T) {
options := mathjax.DefaultOptions()
options.Display = c.Display
got, err := mathjax.RenderWithOptions(c.TeX, options)
if err != nil {
t.Fatal(err)
}
if got != c.SVG {
t.Fatalf("complete primary SVG differs\ngot: %s\nwant: %s", got, c.SVG)
}
})
}
if count != 128 {
t.Fatalf("public references %d; want 128", count)
}
}
// The primary's error attribute contains a literal <. Retain that reference,
// but require XML-safe public output and its exact decoded message, not raw SVG
// equality or a normalized primary string.
func TestEscapedDelimiterLessThanXML(t *testing.T) {
data, err := os.ReadFile("testdata/escaped_delimiter_xml_mathjax_3_2_2.json")
if err != nil {
t.Fatal(err)
}
var fixture struct {
MathjaxGitCommit string
Cases []struct {
Name, TeX, Qualification, PrimarySVG, PrimaryErrorID string
ExpectedMessage, ExpectedText string
Display bool
}
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
if fixture.MathjaxGitCommit != "ad8f5c21cb810236551da8c6512ba733e67357ee" || len(fixture.Cases) != 2 {
t.Fatal("unbound escaped less-than XML references")
}
for _, c := range fixture.Cases {
t.Run(c.Name, func(t *testing.T) {
if c.TeX != `\<` || c.PrimaryErrorID != "UndefinedControlSequence" ||
c.ExpectedMessage != "Undefined control sequence "+c.TeX || c.ExpectedText != c.ExpectedMessage ||
c.Qualification != "XML-safe Go error; not a whole-primary-SVG reference" ||
!strings.Contains(c.PrimarySVG, `data-mjx-error="`+c.ExpectedMessage+`"`) {
t.Fatal("unbound primary error qualification")
}
options := mathjax.DefaultOptions()
options.Display = c.Display
got, err := mathjax.RenderWithOptions(c.TeX, options)
if err != nil {
t.Fatal(err)
}
decoder := xml.NewDecoder(strings.NewReader(got))
depth, roots, errors, texts := 0, 0, 0, 0
errorDepth, textDepth := 0, 0
var message string
var text strings.Builder
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("public SVG is not valid XML: %v", err)
}
switch token := token.(type) {
case xml.StartElement:
depth++
if depth == 1 {
roots++
if token.Name.Local != "svg" || token.Name.Space != "http://www.w3.org/2000/svg" {
t.Fatalf("unexpected XML root: %v", token.Name)
}
}
var kind, errorMessage string
for _, attr := range token.Attr {
if attr.Name.Local == "data-mml-node" {
kind = attr.Value
}
if attr.Name.Local == "data-mjx-error" {
errorMessage = attr.Value
}
}
if token.Name.Local == "g" && kind == "merror" {
errors++
errorDepth, message = depth, errorMessage
}
if errorDepth > 0 && token.Name.Local == "text" {
texts++
textDepth = depth
}
case xml.CharData:
if depth == 0 && strings.TrimSpace(string(token)) != "" {
t.Fatal("unexpected text outside SVG root")
}
if textDepth > 0 {
text.Write([]byte(token))
}
case xml.EndElement:
if depth == textDepth {
textDepth = 0
}
if depth == errorDepth {
errorDepth = 0
}
depth--
}
}
if roots != 1 || depth != 0 {
t.Fatalf("XML roots/depth = %d/%d; want 1/0", roots, depth)
}
if errors != 1 {
t.Fatalf("XML merror count = %d; want 1", errors)
}
if message != c.ExpectedMessage || texts != 1 || text.String() != c.ExpectedText {
t.Fatalf("decoded error = %q, text count = %d, text = %q; want %q", message, texts, text.String(), c.ExpectedMessage)
}
})
}
}