Debian Codegraph @

tests/svg_test.go

raw
1 package tests
2
3 import (
4 "bytes"
5 "encoding/xml"
6 "math"
7 "regexp"
8 "strconv"
9 "strings"
10 "testing"
11
12 "github.com/olekukonko/tablewriter"
13 "github.com/olekukonko/tablewriter/renderer"
14 "github.com/olekukonko/tablewriter/tw"
15 )
16
17 // defaultSVGConfigForTests provides a consistent SVG config for tests.
18 // Parameter debug enables debug logging if true.
19 // Returns an SVGConfig struct with default settings.
20 func defaultSVGConfigForTests(debug bool) renderer.SVGConfig {
21 return renderer.SVGConfig{
22 FontFamily: "Arial",
23 FontSize: 10,
24 LineHeightFactor: 1.2,
25 Padding: 3,
26 StrokeWidth: 1,
27 StrokeColor: "black",
28 HeaderBG: "#DDD",
29 RowBG: "#FFF",
30 RowAltBG: "#EEE",
31 FooterBG: "#DDD",
32 HeaderColor: "#000",
33 RowColor: "#000",
34 FooterColor: "#000",
35 ApproxCharWidthFactor: 0.6,
36 MinColWidth: 20,
37 RenderTWConfigOverrides: true,
38 Debug: debug,
39 }
40 }
41
42 // xmlNode represents a node in the XML tree for structural comparison
43 type xmlNode struct {
44 XMLName xml.Name
45 Attrs []xml.Attr `xml:",any,attr"`
46 Content string `xml:",chardata"`
47 Nodes []xmlNode `xml:",any"`
48 }
49
50 // normalizeSVG performs lightweight normalization for fast comparison
51 func normalizeSVG(s string) string {
52 s = strings.TrimSpace(s)
53
54 // Remove comments
55 s = regexp.MustCompile(`<!--.*?-->`).ReplaceAllString(s, "")
56
57 // Normalize whitespace
58 s = regexp.MustCompile(`\s+`).ReplaceAllString(s, " ")
59 s = regexp.MustCompile(`>\s+<`).ReplaceAllString(s, "><")
60
61 // Normalize self-closing tags
62 s = regexp.MustCompile(`\s*/>`).ReplaceAllString(s, "/>")
63
64 // Normalize numeric values (1.0 -> 1, 0.500 -> 0.5)
65 s = regexp.MustCompile(`(\d+)\.0([^0-9])`).ReplaceAllString(s, "$1$2")
66 s = regexp.MustCompile(`(\d+\.\d+)0([^0-9])`).ReplaceAllString(s, "$1$2")
67 s = regexp.MustCompile(`\.0([^0-9])`).ReplaceAllString(s, "$1")
68
69 return s
70 }
71
72 // visualCheckSVG is the main comparison function
73 func visualCheckSVG(t *testing.T, testName, actual, expected string) bool {
74 t.Helper()
75
76 // First try normalized string comparison
77 normActual := normalizeSVG(actual)
78 normExpected := normalizeSVG(expected)
79
80 if normActual == normExpected {
81 return true
82 }
83
84 // If strings differ, try structural comparison
85 ok, err := compareSVGStructure(normActual, normExpected)
86 if err != nil {
87 t.Logf("Structural comparison failed: %v", err)
88 } else if ok {
89 return true
90 }
91
92 // Both comparisons failed - show formatted diff
93 t.Errorf("%s: SVG output mismatch", testName)
94 showFormattedDiff(t, normActual, normExpected)
95 return false
96 }
97
98 // compareSVGStructure does structural XML comparison
99 func compareSVGStructure(actual, expected string) (bool, error) {
100 var actualNode, expectedNode xmlNode
101
102 if err := xml.Unmarshal([]byte(actual), &actualNode); err != nil {
103 return false, err
104 }
105 if err := xml.Unmarshal([]byte(expected), &expectedNode); err != nil {
106 return false, err
107 }
108
109 return compareXMLNodes(actualNode, expectedNode), nil
110 }
111
112 // compareXMLNodes recursively compares XML nodes
113 func compareXMLNodes(a, b xmlNode) bool {
114 if a.XMLName != b.XMLName {
115 return false
116 }
117
118 // Compare attributes
119 if len(a.Attrs) != len(b.Attrs) {
120 return false
121 }
122
123 attrMap := make(map[xml.Name]string)
124 for _, attr := range a.Attrs {
125 attrMap[attr.Name] = attr.Value
126 }
127
128 for _, attr := range b.Attrs {
129 aVal, ok := attrMap[attr.Name]
130 if !ok {
131 return false
132 }
133
134 if isNumericAttribute(attr.Name.Local) {
135 if !compareFloatStrings(aVal, attr.Value) {
136 return false
137 }
138 } else if aVal != attr.Value {
139 return false
140 }
141 }
142
143 // Compare content
144 if strings.TrimSpace(a.Content) != strings.TrimSpace(b.Content) {
145 return false
146 }
147
148 // Compare children
149 if len(a.Nodes) != len(b.Nodes) {
150 return false
151 }
152
153 for i := range a.Nodes {
154 if !compareXMLNodes(a.Nodes[i], b.Nodes[i]) {
155 return false
156 }
157 }
158
159 return true
160 }
161
162 // Helper functions
163 func isNumericAttribute(name string) bool {
164 numericAttrs := map[string]bool{
165 "x": true, "y": true, "width": true, "height": true,
166 "x1": true, "y1": true, "x2": true, "y2": true,
167 "font-size": true, "stroke-width": true,
168 }
169 return numericAttrs[name]
170 }
171
172 func compareFloatStrings(a, b string) bool {
173 f1, err1 := strconv.ParseFloat(a, 64)
174 f2, err2 := strconv.ParseFloat(b, 64)
175
176 if err1 != nil || err2 != nil {
177 return a == b
178 }
179
180 const epsilon = 0.0001
181 return math.Abs(f1-f2) < epsilon
182 }
183
184 func showFormattedDiff(t *testing.T, actual, expected string) {
185 t.Helper()
186 format := func(s string) string {
187 var buf bytes.Buffer
188 enc := xml.NewEncoder(&buf)
189 enc.Indent("", " ")
190 if err := enc.Encode(xml.NewDecoder(strings.NewReader(s))); err != nil {
191 return s
192 }
193 return buf.String()
194 }
195
196 t.Logf("--- EXPECTED (Formatted) ---\n%s", format(expected))
197 t.Logf("--- ACTUAL (Formatted) ---\n%s", format(actual))
198 }
199
200 // TestSVGBasicTable tests rendering a basic SVG table.
201 // Parameter t is the testing context.
202 // No return value; logs debug info on failure.
203 func TestSVGBasicTable(t *testing.T) {
204 var buf bytes.Buffer
205 svgCfg := defaultSVGConfigForTests(true)
206 table := tablewriter.NewTable(&buf, tablewriter.WithRenderer(renderer.NewSVG(svgCfg)))
207 table.Header([]string{"Name", "Age", "City"})
208 table.Append([]string{"Alice", "25", "New York"})
209 table.Append([]string{"Bob", "30", "Boston"})
210 table.Render()
211 expected := `<svg xmlns="http://www.w3.org/2000/svg"width="118"height="58"font-family="Arial"font-size="10"><style>text { stroke: none; }</style><rect x="1"y="1"width="36"height="18"fill="#DDD"/><text x="19"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">NAME</text><rect x="38"y="1"width="24"height="18"fill="#DDD"/><text x="50"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">AGE</text><rect x="63"y="1"width="54"height="18"fill="#DDD"/><text x="90"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">CITY</text><rect x="1"y="20"width="36"height="18"fill="#FFF"/><text x="4"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">Alice</text><rect x="38"y="20"width="24"height="18"fill="#FFF"/><text x="41"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">25</text><rect x="63"y="20"width="54"height="18"fill="#FFF"/><text x="66"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">New York</text><rect x="1"y="39"width="36"height="18"fill="#EEE"/><text x="4"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">Bob</text><rect x="38"y="39"width="24"height="18"fill="#EEE"/><text x="41"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">30</text><rect x="63"y="39"width="54"height="18"fill="#EEE"/><text x="66"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">Boston</text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="117.5"y2="0.5"/><line x1="0.5"y1="19.5"x2="117.5"y2="19.5"/><line x1="0.5"y1="38.5"x2="117.5"y2="38.5"/><line x1="0.5"y1="57.5"x2="117.5"y2="57.5"/><line x1="0.5"y1="0.5"x2="0.5"y2="57.5"/><line x1="37.5"y1="0.5"x2="37.5"y2="57.5"/><line x1="62.5"y1="0.5"x2="62.5"y2="57.5"/><line x1="117.5"y1="0.5"x2="117.5"y2="57.5"/></g></svg>`
212 if !visualCheckSVG(t, "SVGBasicTable", buf.String(), expected) {
213 t.Log("--- Debug Log for SVGBasicTable ---")
214 t.Log(table.Debug())
215 }
216 }
217
218 // TestSVGEmptyTable tests rendering empty and header-only SVG tables.
219 // Parameter t is the testing context.
220 // No return value; logs debug info on failure.
221 func TestSVGEmptyTable(t *testing.T) {
222 var buf bytes.Buffer
223 svgCfg := defaultSVGConfigForTests(false)
224 table := tablewriter.NewTable(&buf, tablewriter.WithRenderer(renderer.NewSVG(svgCfg)))
225 table.Render()
226 expectedEmpty := `<svg xmlns="http://www.w3.org/2000/svg"width="2"height="2"></svg>`
227 if !visualCheckSVG(t, "SVGEmptyTable_CompletelyEmpty", buf.String(), expectedEmpty) {
228 t.Logf("Empty table output: '%s'", buf.String())
229 t.Log(table.Debug())
230 }
231 buf.Reset()
232 tableHeaderOnly := tablewriter.NewTable(&buf, tablewriter.WithRenderer(renderer.NewSVG(svgCfg)))
233 tableHeaderOnly.Header([]string{"Test"})
234 tableHeaderOnly.Render()
235 expectedHeaderOnly := `<svg xmlns="http://www.w3.org/2000/svg"width="32"height="20"font-family="Arial"font-size="10"><style>text { stroke: none; }</style><rect x="1"y="1"width="30"height="18"fill="#DDD"/><text x="16"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">TEST</text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="31.5"y2="0.5"/><line x1="0.5"y1="19.5"x2="31.5"y2="19.5"/><line x1="0.5"y1="0.5"x2="0.5"y2="19.5"/><line x1="31.5"y1="0.5"x2="31.5"y2="19.5"/></g></svg>`
236 if !visualCheckSVG(t, "SVGEmptyTable_HeaderOnly", buf.String(), expectedHeaderOnly) {
237 t.Log(table.Debug())
238 }
239 }
240
241 // TestSVGHierarchicalMerge tests SVG rendering with hierarchical merging.
242 // Parameter t is the testing context.
243 // No return value; logs debug info on failure.
244 func TestSVGHierarchicalMerge(t *testing.T) {
245 var buf bytes.Buffer
246 svgCfg := defaultSVGConfigForTests(false)
247 table := tablewriter.NewTable(&buf,
248 tablewriter.WithRenderer(renderer.NewSVG(svgCfg)),
249 tablewriter.WithConfig(tablewriter.Config{
250 Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}},
251 Row: tw.CellConfig{Merging: tw.CellMerging{Mode: tw.MergeHierarchical}},
252 }),
253 )
254 table.Header([]string{"L1", "L2", "L3"})
255 table.Append([]string{"A", "a", "1"})
256 table.Append([]string{"A", "b", "2"})
257 table.Append([]string{"A", "b", "3"})
258 table.Render()
259 expected := `<svg xmlns="http://www.w3.org/2000/svg"width="76"height="77"font-family="Arial"font-size="10"><style>text { stroke: none; }</style><rect x="1"y="1"width="24"height="18"fill="#DDD"/><text x="13"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">L 1</text><rect x="26"y="1"width="24"height="18"fill="#DDD"/><text x="38"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">L 2</text><rect x="51"y="1"width="24"height="18"fill="#DDD"/><text x="63"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">L 3</text><rect x="1"y="20"width="24"height="56"fill="#FFF"/><text x="4"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">A</text><rect x="26"y="20"width="24"height="18"fill="#FFF"/><text x="29"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">a</text><rect x="51"y="20"width="24"height="18"fill="#FFF"/><text x="54"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">1</text><rect x="26"y="39"width="24"height="37"fill="#EEE"/><text x="29"y="57.5"fill="#000"text-anchor="start"dominant-baseline="middle">b</text><rect x="51"y="39"width="24"height="18"fill="#EEE"/><text x="54"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">2</text><rect x="51"y="58"width="24"height="18"fill="#FFF"/><text x="54"y="67"fill="#000"text-anchor="start"dominant-baseline="middle">3</text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="75.5"y2="0.5"/><line x1="0.5"y1="19.5"x2="75.5"y2="19.5"/><line x1="0.5"y1="38.5"x2="75.5"y2="38.5"/><line x1="0.5"y1="57.5"x2="75.5"y2="57.5"/><line x1="0.5"y1="76.5"x2="75.5"y2="76.5"/><line x1="0.5"y1="0.5"x2="0.5"y2="76.5"/><line x1="25.5"y1="0.5"x2="25.5"y2="76.5"/><line x1="50.5"y1="0.5"x2="50.5"y2="76.5"/><line x1="75.5"y1="0.5"x2="75.5"y2="76.5"/></g></svg>`
260 if !visualCheckSVG(t, "SVGHierarchicalMerge", buf.String(), expected) {
261 t.Log(table.Debug())
262 }
263 }
264
265 // TestSVGMultiLineContent tests SVG rendering with multi-line content.
266 // Parameter t is the testing context.
267 // No return value; logs debug info on failure.
268 func TestSVGMultiLineContent(t *testing.T) {
269 var buf bytes.Buffer
270 svgCfg := defaultSVGConfigForTests(false)
271 table := tablewriter.NewTable(&buf, tablewriter.WithRenderer(renderer.NewSVG(svgCfg)))
272 table.Header([]string{"Description", "Status"})
273 table.Append([]string{"Line 1\nLine 2", "OK"})
274 table.Render()
275 expected := `<svg xmlns="http://www.w3.org/2000/svg"width="117"height="58"font-family="Arial"font-size="10"><style>text { stroke: none; }</style><rect x="1"y="1"width="72"height="18"fill="#DDD"/><text x="37"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">DESCRIPTION</text><rect x="74"y="1"width="42"height="18"fill="#DDD"/><text x="95"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">STATUS</text><rect x="1"y="20"width="72"height="18"fill="#FFF"/><text x="4"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">Line 1</text><rect x="74"y="20"width="42"height="18"fill="#FFF"/><text x="77"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">OK</text><rect x="1"y="39"width="72"height="18"fill="#FFF"/><text x="4"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">Line 2</text><rect x="74"y="39"width="42"height="18"fill="#FFF"/><text x="77"y="48"fill="#000"text-anchor="start"dominant-baseline="middle"></text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="116.5"y2="0.5"/><line x1="0.5"y1="19.5"x2="116.5"y2="19.5"/><line x1="0.5"y1="38.5"x2="116.5"y2="38.5"/><line x1="0.5"y1="57.5"x2="116.5"y2="57.5"/><line x1="0.5"y1="0.5"x2="0.5"y2="57.5"/><line x1="73.5"y1="0.5"x2="73.5"y2="57.5"/><line x1="116.5"y1="0.5"x2="116.5"y2="57.5"/></g></svg>`
276 if !visualCheckSVG(t, "SVGMultiLineContent", buf.String(), expected) {
277 t.Log(table.Debug())
278 }
279 }
280
281 // TestSVGPaddingAndFont tests SVG rendering with custom padding and font.
282 // Parameter t is the testing context.
283 // No return value; logs debug info on failure.
284 func TestSVGPaddingAndFont(t *testing.T) {
285 var buf bytes.Buffer
286 svgCfg := defaultSVGConfigForTests(false)
287 svgCfg.Padding = 10
288 svgCfg.FontSize = 16
289 svgCfg.FontFamily = "Verdana"
290 table := tablewriter.NewTable(&buf, tablewriter.WithRenderer(renderer.NewSVG(svgCfg)))
291 table.Header([]string{"Test"})
292 table.Render()
293 expected := `<svg xmlns="http://www.w3.org/2000/svg"width="60.4"height="41.2"font-family="Verdana"font-size="16"><style>text { stroke: none; }</style><rect x="1"y="1"width="58.4"height="39.2"fill="#DDD"/><text x="30.2"y="20.6"fill="#000"text-anchor="middle"dominant-baseline="middle">TEST</text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="59.9"y2="0.5"/><line x1="0.5"y1="40.7"x2="59.9"y2="40.7"/><line x1="0.5"y1="0.5"x2="0.5"y2="40.7"/><line x1="59.9"y1="0.5"x2="59.9"y2="40.7"/></g></svg>`
294 if !visualCheckSVG(t, "SVGPaddingAndFont", buf.String(), expected) {
295 t.Log(table.Debug())
296 }
297 }
298
299 // TestSVGVerticalMerge tests SVG rendering with vertical merging.
300 // Parameter t is the testing context.
301 // No return value; logs debug info on failure.
302 func TestSVGVerticalMerge(t *testing.T) {
303 var buf bytes.Buffer
304 svgCfg := defaultSVGConfigForTests(false)
305 table := tablewriter.NewTable(&buf,
306 tablewriter.WithRenderer(renderer.NewSVG(svgCfg)),
307 tablewriter.WithConfig(tablewriter.Config{
308 Row: tw.CellConfig{Merging: tw.CellMerging{Mode: tw.MergeVertical}},
309 }),
310 )
311 table.Header([]string{"Cat", "Item"})
312 table.Append([]string{"Fruit", "Apple"})
313 table.Append([]string{"Fruit", "Banana"})
314 table.Render()
315 expected := `<svg xmlns="http://www.w3.org/2000/svg"width="81"height="58"font-family="Arial"font-size="10"><style>text { stroke: none; }</style><rect x="1"y="1"width="36"height="18"fill="#DDD"/><text x="19"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">CAT</text><rect x="38"y="1"width="42"height="18"fill="#DDD"/><text x="59"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">ITEM</text><rect x="1"y="20"width="36"height="37"fill="#FFF"/><text x="4"y="38.5"fill="#000"text-anchor="start"dominant-baseline="middle">Fruit</text><rect x="38"y="20"width="42"height="18"fill="#FFF"/><text x="41"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">Apple</text><rect x="38"y="39"width="42"height="18"fill="#EEE"/><text x="41"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">Banana</text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="80.5"y2="0.5"/><line x1="0.5"y1="19.5"x2="80.5"y2="19.5"/><line x1="0.5"y1="38.5"x2="80.5"y2="38.5"/><line x1="0.5"y1="57.5"x2="80.5"y2="57.5"/><line x1="0.5"y1="0.5"x2="0.5"y2="57.5"/><line x1="37.5"y1="0.5"x2="37.5"y2="57.5"/><line x1="80.5"y1="0.5"x2="80.5"y2="57.5"/></g></svg>`
316 if !visualCheckSVG(t, "SVGVerticalMerge", buf.String(), expected) {
317 t.Log(table.Debug())
318 }
319 }
320
321 // TestSVGWithFooterAndAlignment tests SVG with footer and alignments.
322 // Parameter t is the testing context.
323 // No return value; skips test as experimental.
324 func TestSVGWithFooterAndAlignment(t *testing.T) {
325 var buf bytes.Buffer
326 svgCfg := defaultSVGConfigForTests(false)
327 table := tablewriter.NewTable(&buf,
328 tablewriter.WithRenderer(renderer.NewSVG(svgCfg)),
329 tablewriter.WithHeaderConfig(tw.CellConfig{
330 Formatting: tw.CellFormatting{AutoFormat: tw.On},
331 Alignment: tw.CellAlignment{Global: tw.AlignCenter},
332 }),
333 tablewriter.WithRowConfig(tw.CellConfig{
334 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignLeft, tw.AlignRight, tw.AlignCenter}},
335 }),
336 tablewriter.WithFooterConfig(tw.CellConfig{
337 Alignment: tw.CellAlignment{Global: tw.AlignRight},
338 }),
339 )
340 table.Header([]string{"Item", "Qty", "Price"})
341 table.Append([]string{"Apple", "5", "1.20"})
342 table.Append([]string{"Banana", "12", "0.35"})
343 table.Footer([]string{"", "Total", "7.20"})
344 table.Render()
345 expected := `<svg xmlns="http://www.w3.org/2000/svg"width="118"height="77"font-family="Arial"font-size="10"><style>text { stroke: none; }</style><rect x="1"y="1"width="42"height="18"fill="#DDD"/><text x="22"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">ITEM</text><rect x="44"y="1"width="36"height="18"fill="#DDD"/><text x="62"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">QTY</text><rect x="81"y="1"width="36"height="18"fill="#DDD"/><text x="99"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">PRICE</text><rect x="1"y="20"width="42"height="18"fill="#FFF"/><text x="4"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">Apple</text><rect x="44"y="20"width="36"height="18"fill="#FFF"/><text x="77"y="29"fill="#000"text-anchor="end"dominant-baseline="middle">5</text><rect x="81"y="20"width="36"height="18"fill="#FFF"/><text x="99"y="29"fill="#000"text-anchor="middle"dominant-baseline="middle">1.20</text><rect x="1"y="39"width="42"height="18"fill="#EEE"/><text x="4"y="48"fill="#000"text-anchor="start"dominant-baseline="middle">Banana</text><rect x="44"y="39"width="36"height="18"fill="#EEE"/><text x="77"y="48"fill="#000"text-anchor="end"dominant-baseline="middle">12</text><rect x="81"y="39"width="36"height="18"fill="#EEE"/><text x="99"y="48"fill="#000"text-anchor="middle"dominant-baseline="middle">0.35</text><rect x="1"y="58"width="42"height="18"fill="#DDD"/><text x="40" y="67"fill="#000"text-anchor="end"dominant-baseline="middle"></text><rect x="44"y="58"width="36"height="18"fill="#DDD"/><text x="77"y="67"fill="#000"text-anchor="end"dominant-baseline="middle">Total</text><rect x="81"y="58"width="36"height="18"fill="#DDD"/><text x="114"y="67"fill="#000"text-anchor="end"dominant-baseline="middle">7.20</text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="117.5"y2="0.5"/><line x1="0.5"y1="19.5"x2="117.5"y2="19.5"/><line x1="0.5"y1="38.5"x2="117.5"y2="38.5"/><line x1="0.5"y1="57.5"x2="117.5"y2="57.5"/><line x1="0.5"y1="76.5"x2="117.5"y2="76.5"/><line x1="0.5"y1="0.5"x2="0.5"y2="76.5"/><line x1="43.5"y1="0.5"x2="43.5"y2="76.5"/><line x1="80.5"y1="0.5"x2="80.5"y2="76.5"/><line x1="117.5"y1="0.5"x2="117.5"y2="76.5"/></g></svg>`
346 if !visualCheckSVG(t, "SVGWithFooterAndAlignment", buf.String(), expected) {
347 t.Log("--- Debug Log for SVGWithFooterAndAlignment ---")
348 t.Log(table.Debug())
349 }
350 }
351
352 // TestSVGColumnAlignmentOverride tests SVG with column alignment overrides.
353 // Parameter t is the testing context.
354 // No return value;
355 func TestSVGColumnAlignmentOverride(t *testing.T) {
356 var buf bytes.Buffer
357 svgCfg := defaultSVGConfigForTests(false)
358 table := tablewriter.NewTable(&buf,
359 tablewriter.WithRenderer(renderer.NewSVG(svgCfg)),
360 tablewriter.WithHeaderConfig(tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}, Formatting: tw.CellFormatting{AutoFormat: tw.Off}}),
361 tablewriter.WithRowConfig(tw.CellConfig{
362 Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.AlignRight, tw.AlignCenter, tw.AlignLeft}},
363 }),
364 )
365 table.Header([]string{"H1", "H2", "H3"})
366 table.Append([]string{"R1C1", "R1C2", "R1C3"})
367 table.Render()
368 expected := `<svg xmlns="http://www.w3.org/2000/svg"width="94"height="39"font-family="Arial"font-size="10"><style>text { stroke: none; }</style><rect x="1"y="1"width="30"height="18"fill="#DDD"/><text x="16"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">H1</text><rect x="32"y="1"width="30"height="18"fill="#DDD"/><text x="47"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">H2</text><rect x="63"y="1"width="30"height="18"fill="#DDD"/><text x="78"y="10"fill="#000"text-anchor="middle"dominant-baseline="middle">H3</text><rect x="1"y="20"width="30"height="18"fill="#FFF"/><text x="28"y="29"fill="#000"text-anchor="end"dominant-baseline="middle">R1C1</text><rect x="32"y="20"width="30"height="18"fill="#FFF"/><text x="47"y="29"fill="#000"text-anchor="middle"dominant-baseline="middle">R1C2</text><rect x="63"y="20"width="30"height="18"fill="#FFF"/><text x="66"y="29"fill="#000"text-anchor="start"dominant-baseline="middle">R1C3</text><g class="table-borders"stroke="black"stroke-width="1"stroke-linecap="square"><line x1="0.5"y1="0.5"x2="93.5"y2="0.5"/><line x1="0.5"y1="19.5"x2="93.5"y2="19.5"/><line x1="0.5"y1="38.5"x2="93.5"y2="38.5"/><line x1="0.5"y1="0.5"x2="0.5"y2="38.5"/><line x1="31.5"y1="0.5"x2="31.5"y2="38.5"/><line x1="62.5"y1="0.5"x2="62.5"y2="38.5"/><line x1="93.5"y1="0.5"x2="93.5"y2="38.5"/></g></svg>`
369 if !visualCheckSVG(t, "SVGColumnAlignmentOverride", buf.String(), expected) {
370 t.Log(table.Debug())
371 }
372 }
373
374 // TestSVGHorizontalMerge tests SVG with horizontal merging.
375 // Parameter t is the testing context.
376 // No return value;
377 func TestSVGHorizontalMerge(t *testing.T) {
378 var buf bytes.Buffer
379 svgCfg := defaultSVGConfigForTests(false)
380 table := tablewriter.NewTable(&buf,
381 tablewriter.WithRenderer(renderer.NewSVG(svgCfg)),
382 tablewriter.WithConfig(tablewriter.Config{
383 Header: tw.CellConfig{
384 Merging: tw.CellMerging{Mode: tw.MergeHorizontal},
385 Alignment: tw.CellAlignment{Global: tw.AlignCenter},
386 },
387 Row: tw.CellConfig{Merging: tw.CellMerging{Mode: tw.MergeHorizontal}, Alignment: tw.CellAlignment{Global: tw.AlignLeft}},
388 }),
389 )
390 table.Header([]string{"A", "Merged Header", "Merged Header"})
391 table.Append([]string{"Data 1", "Data 2", "Data 2"})
392 table.Render()
393 expected := `<svg xmlns="http://www.w3.org/2000/svg" width="135" height="39" font-family="Arial" font-size="10"><style>text { stroke: none; }</style><rect x="1" y="1" width="42" height="18" fill="#DDD"/><text x="22" y="10" fill="#000" text-anchor="middle" dominant-baseline="middle">A</text><rect x="44" y="1" width="91" height="18" fill="#DDD"/><text x="89.5" y="10" fill="#000" text-anchor="middle" dominant-baseline="middle">MERGED HEADER</text><rect x="1" y="20" width="42" height="18" fill="#FFF"/><text x="4" y="29" fill="#000" text-anchor="start" dominant-baseline="middle">Data 1</text><rect x="44" y="20" width="91" height="18" fill="#FFF"/><text x="47" y="29" fill="#000" text-anchor="start" dominant-baseline="middle">Data 2</text><g class="table-borders" stroke="black" stroke-width="1" stroke-linecap="square"><line x1="0.5" y1="0.5" x2="134.5" y2="0.5"/><line x1="0.5" y1="19.5" x2="134.5" y2="19.5"/><line x1="0.5" y1="38.5" x2="134.5" y2="38.5"/><line x1="0.5" y1="0.5" x2="0.5" y2="38.5"/><line x1="43.5" y1="0.5" x2="43.5" y2="38.5"/><line x1="134.5" y1="0.5" x2="134.5" y2="38.5"/></g></svg>`
394 if !visualCheckSVG(t, "SVGHorizontalMerge", buf.String(), expected) {
395 t.Log("--- Debug Log for SVGHorizontalMerge ---")
396 t.Log(table.Debug())
397 }
398 }