-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidth_test.go
More file actions
109 lines (103 loc) · 2.91 KB
/
Copy pathwidth_test.go
File metadata and controls
109 lines (103 loc) · 2.91 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
// ABOUTME: Readable spot checks for charWidth/stringWidth (unicode-width 0.2.0
// ABOUTME: semantics) and the identifier-character predicates.
// Derived from crates/codegen/xai-grok-markdown/src/mermaid.rs in
// https://github.com/xai-org/grok-build at commit
// b189869b7755d2b482969acf6c92da3ecfeffd36 (Apache-2.0). See THIRD_PARTY_NOTICES.md.
// Exhaustive parity with unicode-width 0.2.0 is asserted in width_parity_test.go;
// these cases document the classes the renderer depends on.
package mermaidtext
import "testing"
func TestCharWidthSpotChecks(t *testing.T) {
cases := []struct {
r rune
want int
}{
{'a', 1},
{'Z', 1},
{'0', 1},
{' ', 1},
{'日', 2},
{'本', 2},
{'語', 2},
{'́', 0}, // combining acute accent
{'֑', 0}, // Hebrew accent etnahta
{'ְ', 0}, // Hebrew point sheva (niqqud)
{'ً', 0}, // Arabic fathatan (haraka)
{'्', 0}, // Devanagari sign virama
{'', 0}, // zero-width joiner
{'\x00', 0}, // NUL (the CONT sentinel)
{'\x1b', 0}, // ESC
{'\x7f', 0}, // DEL
{'\t', 0}, // control
{'៘', 3}, // Khmer sign beyyal
{'…', 1},
{'—', 1}, // em dash (ambiguous width -> narrow)
{'─', 1},
{'│', 1},
{'╭', 1},
{'×', 1},
{'●', 1},
{'▼', 1},
{'«', 1},
{'»', 1},
}
for _, c := range cases {
if got := charWidth(c.r); got != c.want {
t.Errorf("charWidth(%q) = %d, want %d", c.r, got, c.want)
}
}
}
func TestStringWidthSpotChecks(t *testing.T) {
cases := []struct {
s string
want int
}{
{"", 0},
{"abc", 3},
{"日本語ab", 8},
{"café", 4}, // combining mark contributes 0
{"a\x00b", 3}, // control chars count 1 at string level (charWidth 0)
{"\t", 1},
{"\r\n", 1}, // CRLF clusters to 1
{"╭─╮", 3},
{" mermaid: gantt ", 16},
{"בְרֵא", 3}, // Hebrew word with niqqud
{"لا", 1}, // Arabic lam-alef ligature
{"❤️", 2}, // VS16 makes the base wide
{"0️⃣", 2}, // keycap sequence
{"\U0001F468\U0001F469\U0001F467", 2}, // ZWJ family clusters
}
for _, c := range cases {
if got := stringWidth(c.s); got != c.want {
t.Errorf("stringWidth(%q) = %d, want %d", c.s, got, c.want)
}
}
}
func TestIsAlphanumericMatchesRustSemantics(t *testing.T) {
trues := []rune{'a', 'Z', '0', '9', 'é', '日', 'Ⓐ', '¼'}
for _, r := range trues {
if !isAlphanumeric(r) {
t.Errorf("isAlphanumeric(%q) = false, want true", r)
}
}
falses := []rune{'_', '-', '.', '/', ' ', '*', '<', '\x00'}
for _, r := range falses {
if isAlphanumeric(r) {
t.Errorf("isAlphanumeric(%q) = true, want false", r)
}
}
}
func TestIsIDChar(t *testing.T) {
trues := []rune{'a', 'Z', '0', '_', 'é', '日', 'Ⓐ'}
for _, r := range trues {
if !isIDChar(r) {
t.Errorf("isIDChar(%q) = false, want true", r)
}
}
falses := []rune{'-', '.', '/', ' ', '[', '>', '&'}
for _, r := range falses {
if isIDChar(r) {
t.Errorf("isIDChar(%q) = true, want false", r)
}
}
}