-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvg_test.go
More file actions
331 lines (258 loc) · 7.63 KB
/
svg_test.go
File metadata and controls
331 lines (258 loc) · 7.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"strings"
"testing"
)
func TestSvgHeader(t *testing.T) {
header := svgHeader(800, 600, nil)
if !strings.Contains(header, `width="800"`) {
t.Error("Header should contain width=\"800\"")
}
if !strings.Contains(header, `height="600"`) {
t.Error("Header should contain height=\"600\"")
}
if !strings.Contains(header, `<svg`) {
t.Error("Header should contain <svg tag")
}
if !strings.Contains(header, `arrowhead`) {
t.Error("Header should contain arrowhead marker definition")
}
}
func TestSvgHeader_ArrowheadStyle(t *testing.T) {
header := svgHeader(800, 600, nil)
// Should use polyline instead of polygon for two-line arrowhead
if !strings.Contains(header, `<polyline`) {
t.Error("Arrowhead should use <polyline> element")
}
// Should not have filled polygon
if strings.Contains(header, `<polygon`) {
t.Error("Arrowhead should not use <polygon> element (should be polyline)")
}
// Should have fill="none" for outline style
if !strings.Contains(header, `fill="none"`) {
t.Error("Arrowhead should have fill=\"none\"")
}
// Should have stroke color
if !strings.Contains(header, `stroke="#000"`) {
t.Error("Arrowhead should have stroke=\"#000\"")
}
// Should have stroke-width
if !strings.Contains(header, `stroke-width="1.5"`) {
t.Error("Arrowhead should have stroke-width=\"1.5\"")
}
}
func TestSvgFooter(t *testing.T) {
footer := svgFooter()
if footer != `</svg>` {
t.Errorf("Footer should be </svg>, got %s", footer)
}
}
func TestStraightArrow(t *testing.T) {
arrow := straightArrow(10, 20, 30, 40)
if !strings.Contains(arrow, `<line`) {
t.Error("Should be a line element")
}
if !strings.Contains(arrow, `x1="10"`) {
t.Error("Should have x1=\"10\"")
}
if !strings.Contains(arrow, `y1="20"`) {
t.Error("Should have y1=\"20\"")
}
if !strings.Contains(arrow, `x2="30"`) {
t.Error("Should have x2=\"30\"")
}
if !strings.Contains(arrow, `y2="40"`) {
t.Error("Should have y2=\"40\"")
}
if !strings.Contains(arrow, `marker-end="url(#arrowhead)"`) {
t.Error("Should have arrowhead marker")
}
}
func TestOneBentArrow(t *testing.T) {
// Test vertical-first (vertical then horizontal)
arrowVertFirst := oneBentArrow(10, 20, 30, 40, true)
if !strings.Contains(arrowVertFirst, `<polyline`) {
t.Error("Should be a polyline element")
}
// Check for 3 points (vertical then horizontal)
if !strings.Contains(arrowVertFirst, `points="10,20`) {
t.Error("Should start at 10,20")
}
if !strings.Contains(arrowVertFirst, `10,40`) {
t.Error("Should have vertical segment to 10,40")
}
if !strings.Contains(arrowVertFirst, `30,40"`) {
t.Error("Should end at 30,40")
}
if !strings.Contains(arrowVertFirst, `marker-end="url(#arrowhead)"`) {
t.Error("Should have arrowhead marker")
}
// Test horizontal-first (horizontal then vertical)
arrowHorizFirst := oneBentArrow(10, 20, 30, 40, false)
if !strings.Contains(arrowHorizFirst, `<polyline`) {
t.Error("Should be a polyline element")
}
// Check for 3 points (horizontal then vertical)
if !strings.Contains(arrowHorizFirst, `points="10,20`) {
t.Error("Should start at 10,20")
}
if !strings.Contains(arrowHorizFirst, `30,20`) {
t.Error("Should have horizontal segment to 30,20")
}
if !strings.Contains(arrowHorizFirst, `30,40"`) {
t.Error("Should end at 30,40")
}
}
func TestTwoBentArrow(t *testing.T) {
arrow := twoBentArrow(10, 20, 50, 60)
if !strings.Contains(arrow, `<polyline`) {
t.Error("Should be a polyline element")
}
// Check for 4 points
midX := (10 + 50) / 2 // 30
if !strings.Contains(arrow, `points="10,20`) {
t.Error("Should start at 10,20")
}
if !strings.Contains(arrow, `30,20`) {
t.Errorf("Should have horizontal segment to %d,20", midX)
}
if !strings.Contains(arrow, `30,60`) {
t.Errorf("Should have vertical segment to %d,60", midX)
}
if !strings.Contains(arrow, `50,60"`) {
t.Error("Should end at 50,60")
}
}
func TestTwoBentArrowVertical(t *testing.T) {
arrow := twoBentArrowVertical(10, 20, 50, 60)
if !strings.Contains(arrow, `<polyline`) {
t.Error("Should be a polyline element")
}
// Check for 4 points (V-H-V path)
midY := (20 + 60) / 2 // 40
if !strings.Contains(arrow, `points="10,20`) {
t.Error("Should start at 10,20")
}
if !strings.Contains(arrow, `10,40`) {
t.Errorf("Should have vertical segment to 10,%d", midY)
}
if !strings.Contains(arrow, `50,40`) {
t.Errorf("Should have horizontal segment to 50,%d", midY)
}
if !strings.Contains(arrow, `50,60"`) {
t.Error("Should end at 50,60")
}
if !strings.Contains(arrow, `marker-end="url(#arrowhead)"`) {
t.Error("Should have arrowhead marker")
}
}
func TestDrawBox(t *testing.T) {
box := drawBox(10, 20, 100, 50, "#FFCE33", "", 0)
if !strings.Contains(box, `<rect`) {
t.Error("Should be a rect element")
}
if !strings.Contains(box, `x="10"`) {
t.Error("Should have x=\"10\"")
}
if !strings.Contains(box, `y="20"`) {
t.Error("Should have y=\"20\"")
}
if !strings.Contains(box, `width="100"`) {
t.Error("Should have width=\"100\"")
}
if !strings.Contains(box, `height="50"`) {
t.Error("Should have height=\"50\"")
}
if !strings.Contains(box, `fill="#FFCE33"`) {
t.Error("Should have fill=\"#FFCE33\"")
}
}
func TestDrawLine(t *testing.T) {
tests := []struct {
name string
dashed bool
withArrow bool
shouldContain []string
}{
{
name: "Plain line",
dashed: false,
withArrow: false,
shouldContain: []string{`<line`, `x1="10"`, `y1="20"`, `x2="30"`, `y2="40"`},
},
{
name: "Dashed line",
dashed: true,
withArrow: false,
shouldContain: []string{`stroke-dasharray="5,5"`},
},
{
name: "Line with arrow",
dashed: false,
withArrow: true,
shouldContain: []string{`marker-end="url(#arrowhead)"`},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
line := drawLine(10, 20, 30, 40, tt.dashed, tt.withArrow)
for _, s := range tt.shouldContain {
if !strings.Contains(line, s) {
t.Errorf("Line should contain %s", s)
}
}
})
}
}
func TestDrawText(t *testing.T) {
attrs := map[string]string{
"font-weight": "normal",
"text-anchor": "middle",
}
text := drawText(100, 200, "Hello", 24, attrs, nil)
if !strings.Contains(text, `<text`) {
t.Error("Should be a text element")
}
if !strings.Contains(text, `x="100"`) {
t.Error("Should have x=\"100\"")
}
if !strings.Contains(text, `y="200"`) {
t.Error("Should have y=\"200\"")
}
if !strings.Contains(text, `font-size="24"`) {
t.Error("Should have font-size=\"24\"")
}
if !strings.Contains(text, `Hello`) {
t.Error("Should contain text content")
}
if !strings.Contains(text, `font-weight="normal"`) {
t.Error("Should have font-weight attribute")
}
if !strings.Contains(text, `text-anchor="middle"`) {
t.Error("Should have text-anchor attribute")
}
}
func TestDrawBoxText(t *testing.T) {
lines := []string{"Line 1", "Line 2"}
text := drawBoxText(100, 100, 200, 100, 0, "", lines, nil) // 0 means use default font size, "" means default color
// Should contain two text elements
count := strings.Count(text, "<text")
if count != 2 {
t.Errorf("Expected 2 text elements, got %d", count)
}
if !strings.Contains(text, "Line 1") {
t.Error("Should contain 'Line 1'")
}
if !strings.Contains(text, "Line 2") {
t.Error("Should contain 'Line 2'")
}
if !strings.Contains(text, `font-size="24"`) {
t.Error("Should have font-size=\"24\"")
}
if !strings.Contains(text, `font-weight="normal"`) {
t.Error("Should have bold font weight")
}
if !strings.Contains(text, `dominant-baseline="middle"`) {
t.Error("Should have dominant-baseline=\"middle\" for vertical centering")
}
}