-
Notifications
You must be signed in to change notification settings - Fork 18
/
wcwidth_test.go
136 lines (126 loc) · 2.62 KB
/
wcwidth_test.go
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
package wcwidth
import (
"testing"
)
// test cases copied from https://github.com/mattn/go-runewidth/raw/master/runewidth_test.go
var stringwidthtests = []struct {
in string
out int
eaout int
}{
{"■㈱の世界①", 10, 12},
{"スター☆", 7, 8},
{"つのだ☆HIRO", 11, 12},
}
func BenchmarkStringWidth(b *testing.B) {
for i := 0; i < b.N; i++ {
StringWidth(stringwidthtests[i%len(stringwidthtests)].in)
}
}
func TestStringWidth(t *testing.T) {
for _, tt := range stringwidthtests {
if out := StringWidth(tt.in); out != tt.out {
t.Errorf("StringWidth(%q) = %d, want %d", tt.in, out, tt.out)
}
}
}
var runewidthtests = []struct {
in rune
out int
}{
{'世', 2},
{'界', 2},
{'セ', 1},
{'カ', 1},
{'イ', 1},
{'☆', 1}, // double width in ambiguous
{'☺', 1},
{'☻', 1},
{'♥', 1},
{'♦', 1},
{'♣', 1},
{'♠', 1},
{'♂', 1},
{'♀', 1},
{'♪', 1},
{'♫', 1},
{'☼', 1},
{'↕', 1},
{'‼', 1},
{'↔', 1},
{'\x00', 0},
{'\x01', 0},
{'\u0300', 0},
{'\u2028', 0},
{'\u2029', 0},
{'a', 1}, // ASCII classified as "na" (narrow)
{'⟦', 1}, // non-ASCII classified as "na" (narrow)
{'👁', 1},
{'\u0301', 0}, // Combining acute accent
{'a', 1},
{'Ω', 1},
{'好', 2},
{'か', 2},
{'コ', 2},
{'ン', 2},
{'ニ', 2},
{'チ', 2},
{'ハ', 2},
{',', 1},
{' ', 1},
{'セ', 2},
{'カ', 2},
{'イ', 2},
{'!', 1},
{'a', 1},
{'A', 1},
{'z', 1},
{'Z', 1},
{'#', 1},
{'\u05bf', 0}, // Combining
{'\u0301', 0}, // Combining acute accent
{'\u0410', 1}, // Cyrillic Capital Letter A
{'\u0488', 0}, // Combining Cyrillic Hundred Thousands Sign
{'\u00ad', 0}, // Soft hyphen
{0, 0}, // Special case, width of null rune is zero
{'\u00a0', 1}, // non-breaking space
}
func BenchmarkRuneWidth(b *testing.B) {
for i := 0; i < b.N; i++ {
RuneWidth(runewidthtests[i%len(runewidthtests)].in)
}
}
func TestRuneWidth(t *testing.T) {
for i, tt := range runewidthtests {
if out := RuneWidth(tt.in); out != tt.out {
t.Errorf("case %d: RuneWidth(%q) = %d, want %d", i, tt.in, out, tt.out)
}
}
}
func TestZeroWidthJoiner(t *testing.T) {
tests := []struct {
in string
want int
}{
{"👩", 2},
{"👩\u200d", 2},
{"👩\u200d🍳", 4},
{"\u200d🍳", 2},
{"👨\u200d👨", 4},
{"👨\u200d👨\u200d👧", 6},
{"🏳️\u200d🌈", 3},
{"あ👩\u200d🍳い", 8},
{"あ\u200d🍳い", 6},
{"あ\u200dい", 4},
{"abc", 3},
{"你好", 4},
{"Hello!", 6},
{"Hello, 世界!", 12},
{"ᬓᬨᬮ᭄", 4},
}
for _, tt := range tests {
if got := StringWidth(tt.in); got != tt.want {
t.Errorf("StringWidth(%q) = %d, want %d", tt.in, got, tt.want)
}
}
}