| 1 |
package tests |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"regexp" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/olekukonko/tablewriter" |
| 10 |
"github.com/olekukonko/tablewriter/renderer" |
| 11 |
) |
| 12 |
|
| 13 |
var svgRootWidthRe = regexp.MustCompile(`<svg[^>]*\bwidth="([0-9.]+)"`) |
| 14 |
|
| 15 |
// svgRootWidth renders a single-column SVG table whose only cell is content and |
| 16 |
// returns the width of the root <svg> element. |
| 17 |
func svgRootWidth(t *testing.T, content string) float64 { |
| 18 |
t.Helper() |
| 19 |
var buf bytes.Buffer |
| 20 |
svgCfg := defaultSVGConfigForTests(false) |
| 21 |
table := tablewriter.NewTable(&buf, tablewriter.WithRenderer(renderer.NewSVG(svgCfg))) |
| 22 |
table.Append([]string{content}) |
| 23 |
table.Render() |
| 24 |
|
| 25 |
m := svgRootWidthRe.FindStringSubmatch(buf.String()) |
| 26 |
if m == nil { |
| 27 |
t.Fatalf("no width attribute found in SVG output:\n%s", buf.String()) |
| 28 |
} |
| 29 |
w, err := strconv.ParseFloat(m[1], 64) |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("failed to parse width %q: %v", m[1], err) |
| 32 |
} |
| 33 |
return w |
| 34 |
} |
| 35 |
|
| 36 |
// TestSVGWideRuneColumnWidth verifies that the SVG renderer sizes columns by |
| 37 |
// the display width of the content, so that wide (CJK) runes which occupy two |
| 38 |
// terminal cells are not packed into half the space they need. A cell of four |
| 39 |
// fullwidth runes has the same display width as eight ASCII characters and so |
| 40 |
// must produce an equally wide column. Sizing by rune count would make the CJK |
| 41 |
// column too narrow and the text would overflow its cell. |
| 42 |
func TestSVGWideRuneColumnWidth(t *testing.T) { |
| 43 |
wide := svgRootWidth(t, "ああああ") // 4 fullwidth runes, display width 8 |
| 44 |
ascii := svgRootWidth(t, "abcdefgh") // 8 ASCII runes, display width 8 |
| 45 |
|
| 46 |
if wide != ascii { |
| 47 |
t.Errorf("CJK column sized by rune count, not display width: width(ああああ)=%v, width(abcdefgh)=%v; want equal", wide, ascii) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// TestSVGMixedWidthColumnWidth verifies that a cell mixing ASCII and fullwidth |
| 52 |
// runes is sized by the sum of their display widths rather than the rune count. |
| 53 |
// "aあ" is one ASCII cell plus one fullwidth (two-cell) rune, a display width of |
| 54 |
// three, the same as three ASCII characters. Sizing by rune count would treat |
| 55 |
// "aあ" as two runes and make the column too narrow. |
| 56 |
func TestSVGMixedWidthColumnWidth(t *testing.T) { |
| 57 |
mixed := svgRootWidth(t, "aあ") // display width 1 + 2 = 3 |
| 58 |
ascii := svgRootWidth(t, "abc") // display width 3 |
| 59 |
|
| 60 |
if mixed != ascii { |
| 61 |
t.Errorf("mixed cell sized by rune count, not display width: width(aあ)=%v, width(abc)=%v; want equal", mixed, ascii) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// TestSVGWideRuneColumnWidthScales verifies the display-width sizing holds at |
| 66 |
// several lengths, so a run of n fullwidth runes matches 2n ASCII characters |
| 67 |
// and not only at one particular length. |
| 68 |
func TestSVGWideRuneColumnWidthScales(t *testing.T) { |
| 69 |
cases := []struct { |
| 70 |
wide string |
| 71 |
ascii string |
| 72 |
}{ |
| 73 |
{"中", "ab"}, |
| 74 |
{"中文", "abcd"}, |
| 75 |
{"中文字", "abcdef"}, |
| 76 |
} |
| 77 |
for _, c := range cases { |
| 78 |
wide := svgRootWidth(t, c.wide) |
| 79 |
ascii := svgRootWidth(t, c.ascii) |
| 80 |
if wide != ascii { |
| 81 |
t.Errorf("width(%q)=%v, width(%q)=%v; want equal", c.wide, wide, c.ascii, ascii) |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// TestSVGCombiningMarkColumnWidth verifies that combining marks, which occupy no |
| 87 |
// display cells of their own, do not widen a column. "é" (e followed by a |
| 88 |
// combining acute accent) has the display width of a single "e" even though it |
| 89 |
// is two runes; sizing by rune count would make the column too wide. |
| 90 |
func TestSVGCombiningMarkColumnWidth(t *testing.T) { |
| 91 |
combining := svgRootWidth(t, "é") // e + combining acute, display width 1 |
| 92 |
plain := svgRootWidth(t, "e") // display width 1 |
| 93 |
|
| 94 |
if combining != plain { |
| 95 |
t.Errorf("combining mark widened the column: width(e+U+0301)=%v, width(e)=%v; want equal", combining, plain) |
| 96 |
} |
| 97 |
} |