| 1 |
//go:build !js && !appengine |
| 2 |
// +build !js,!appengine |
| 3 |
|
| 4 |
package runewidth |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto/sha256" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"sort" |
| 11 |
"testing" |
| 12 |
"unicode/utf8" |
| 13 |
) |
| 14 |
|
| 15 |
var _ sort.Interface = (*table)(nil) // ensure that type "table" does implement sort.Interface |
| 16 |
|
| 17 |
func init() { |
| 18 |
os.Setenv("RUNEWIDTH_EASTASIAN", "") |
| 19 |
handleEnv() |
| 20 |
} |
| 21 |
|
| 22 |
func (t table) Len() int { |
| 23 |
return len(t) |
| 24 |
} |
| 25 |
|
| 26 |
func (t table) Less(i, j int) bool { |
| 27 |
return t[i].first < t[j].first |
| 28 |
} |
| 29 |
|
| 30 |
func (t *table) Swap(i, j int) { |
| 31 |
(*t)[i], (*t)[j] = (*t)[j], (*t)[i] |
| 32 |
} |
| 33 |
|
| 34 |
type tableInfo struct { |
| 35 |
tbl table |
| 36 |
name string |
| 37 |
wantN int |
| 38 |
wantSHA string |
| 39 |
} |
| 40 |
|
| 41 |
var tables = []tableInfo{ |
| 42 |
{private, "private", 137468, "a4a641206dc8c5de80bd9f03515a54a706a5a4904c7684dc6a33d65c967a51b2"}, |
| 43 |
{nonprint, "nonprint", 2143, "288904683eb225e7c4c0bd3ee481b53e8dace404ec31d443afdbc4d13729fe95"}, |
| 44 |
{combining, "combining", 2081, "bbb88427a84cf23bd601d560b32ffd88b1d0c1aeb365b54af37f1ad7d7e6944e"}, |
| 45 |
{doublewidth, "doublewidth", 182876, "55dcb1b999d6356d1a083085bb053bdeafc6dda05dec002617d85fda2a82d496"}, |
| 46 |
{ambiguous, "ambiguous", 138483, "f4ed2dd733c0821cf6297fc24be0baea527ec7cad10d23b0ac7f57dcdf344cdb"}, |
| 47 |
{emoji, "emoji", 2846, "09914b87febaa5493f2420a58f03dd6b026fa665b7c811abc7423a26a9b442c3"}, |
| 48 |
{narrow, "narrow", 111, "fa897699c5e3cd9141c638d539331b0bdd508b874e22996c5e929767d455fc5a"}, |
| 49 |
{neutral, "neutral", 33695, "f6af4edbdfe84d0c4c4419da8e2f86e49248010bad19c4ed7bf1897696db9084"}, |
| 50 |
} |
| 51 |
|
| 52 |
func TestTableChecksums(t *testing.T) { |
| 53 |
for _, ti := range tables { |
| 54 |
gotN := 0 |
| 55 |
buf := make([]byte, utf8.MaxRune+1) |
| 56 |
for r := rune(0); r <= utf8.MaxRune; r++ { |
| 57 |
if inTable(r, ti.tbl) { |
| 58 |
gotN++ |
| 59 |
buf[r] = 1 |
| 60 |
} |
| 61 |
} |
| 62 |
gotSHA := fmt.Sprintf("%x", sha256.Sum256(buf)) |
| 63 |
if gotN != ti.wantN || gotSHA != ti.wantSHA { |
| 64 |
t.Errorf("table = %s,\n\tn = %d want %d,\n\tsha256 = %s want %s", ti.name, gotN, ti.wantN, gotSHA, ti.wantSHA) |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestRuneWidthChecksums(t *testing.T) { |
| 70 |
var testcases = []struct { |
| 71 |
name string |
| 72 |
eastAsianWidth bool |
| 73 |
wantSHA string |
| 74 |
}{ |
| 75 |
{"ea-no", false, "b166b7c41c9231ce5a3f05d94b0da79c245714d5f708b3002ccef55bda38152d"}, |
| 76 |
{"ea-yes", true, "d46d9c64c35351d6daa041d69092fb1181d63096ca089149ecc4b922bc0e2cf8"}, |
| 77 |
} |
| 78 |
|
| 79 |
for _, testcase := range testcases { |
| 80 |
c := NewCondition() |
| 81 |
c.EastAsianWidth = testcase.eastAsianWidth |
| 82 |
buf := make([]byte, utf8.MaxRune+1) |
| 83 |
for r := rune(0); r <= utf8.MaxRune; r++ { |
| 84 |
buf[r] = byte(c.RuneWidth(r)) |
| 85 |
} |
| 86 |
gotSHA := fmt.Sprintf("%x", sha256.Sum256(buf)) |
| 87 |
if gotSHA != testcase.wantSHA { |
| 88 |
t.Errorf("TestRuneWidthChecksums = %s,\n\tsha256 = %s want %s", |
| 89 |
testcase.name, gotSHA, testcase.wantSHA) |
| 90 |
} |
| 91 |
|
| 92 |
// Test with LUT |
| 93 |
c.CreateLUT() |
| 94 |
for r := rune(0); r <= utf8.MaxRune; r++ { |
| 95 |
buf[r] = byte(c.RuneWidth(r)) |
| 96 |
} |
| 97 |
gotSHA = fmt.Sprintf("%x", sha256.Sum256(buf)) |
| 98 |
if gotSHA != testcase.wantSHA { |
| 99 |
t.Errorf("TestRuneWidthChecksums = %s,\n\tsha256 = %s want %s", |
| 100 |
testcase.name, gotSHA, testcase.wantSHA) |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
func TestDefaultLUT(t *testing.T) { |
| 106 |
var testcases = []struct { |
| 107 |
name string |
| 108 |
eastAsianWidth bool |
| 109 |
wantSHA string |
| 110 |
}{ |
| 111 |
{"ea-no", false, "b166b7c41c9231ce5a3f05d94b0da79c245714d5f708b3002ccef55bda38152d"}, |
| 112 |
{"ea-yes", true, "d46d9c64c35351d6daa041d69092fb1181d63096ca089149ecc4b922bc0e2cf8"}, |
| 113 |
} |
| 114 |
|
| 115 |
old := os.Getenv("RUNEWIDTH_EASTASIAN") |
| 116 |
defer os.Setenv("RUNEWIDTH_EASTASIAN", old) |
| 117 |
|
| 118 |
CreateLUT() |
| 119 |
for _, testcase := range testcases { |
| 120 |
c := DefaultCondition |
| 121 |
|
| 122 |
if testcase.eastAsianWidth { |
| 123 |
os.Setenv("RUNEWIDTH_EASTASIAN", "1") |
| 124 |
} else { |
| 125 |
os.Setenv("RUNEWIDTH_EASTASIAN", "0") |
| 126 |
} |
| 127 |
handleEnv() |
| 128 |
|
| 129 |
buf := make([]byte, utf8.MaxRune+1) |
| 130 |
for r := rune(0); r <= utf8.MaxRune; r++ { |
| 131 |
buf[r] = byte(c.RuneWidth(r)) |
| 132 |
} |
| 133 |
gotSHA := fmt.Sprintf("%x", sha256.Sum256(buf)) |
| 134 |
if gotSHA != testcase.wantSHA { |
| 135 |
t.Errorf("TestRuneWidthChecksums = %s,\n\tsha256 = %s want %s", |
| 136 |
testcase.name, gotSHA, testcase.wantSHA) |
| 137 |
} |
| 138 |
} |
| 139 |
// Remove for other tests. |
| 140 |
DefaultCondition.combinedLut = nil |
| 141 |
} |
| 142 |
|
| 143 |
func checkInterval(first, last rune) bool { |
| 144 |
return first >= 0 && first <= utf8.MaxRune && |
| 145 |
last >= 0 && last <= utf8.MaxRune && |
| 146 |
first <= last |
| 147 |
} |
| 148 |
|
| 149 |
func isCompact(t *testing.T, ti *tableInfo) bool { |
| 150 |
tbl := ti.tbl |
| 151 |
for i := range tbl { |
| 152 |
e := tbl[i] |
| 153 |
if !checkInterval(e.first, e.last) { // sanity check |
| 154 |
t.Errorf("table invalid: table = %s index = %d %v", ti.name, i, e) |
| 155 |
return false |
| 156 |
} |
| 157 |
if i+1 < len(tbl) && e.last+1 >= tbl[i+1].first { // can be combined into one entry |
| 158 |
t.Errorf("table not compact: table = %s index = %d %v %v", ti.name, i, e, tbl[i+1]) |
| 159 |
return false |
| 160 |
} |
| 161 |
} |
| 162 |
return true |
| 163 |
} |
| 164 |
|
| 165 |
func TestSorted(t *testing.T) { |
| 166 |
for _, ti := range tables { |
| 167 |
if !sort.IsSorted(&ti.tbl) { |
| 168 |
t.Errorf("table not sorted: %s", ti.name) |
| 169 |
} |
| 170 |
if !isCompact(t, &ti) { |
| 171 |
t.Errorf("table not compact: %s", ti.name) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
var runewidthtests = []struct { |
| 177 |
in rune |
| 178 |
out int |
| 179 |
eaout int |
| 180 |
nseout int |
| 181 |
}{ |
| 182 |
{'世', 2, 2, 2}, |
| 183 |
{'界', 2, 2, 2}, |
| 184 |
{'セ', 1, 1, 1}, |
| 185 |
{'カ', 1, 1, 1}, |
| 186 |
{'イ', 1, 1, 1}, |
| 187 |
{'☆', 1, 2, 2}, // double width in ambiguous |
| 188 |
{'☺', 1, 1, 2}, |
| 189 |
{'☻', 1, 1, 1}, |
| 190 |
{'♥', 1, 2, 2}, |
| 191 |
{'♦', 1, 1, 2}, |
| 192 |
{'♣', 1, 2, 2}, |
| 193 |
{'♠', 1, 2, 2}, |
| 194 |
{'♂', 1, 2, 2}, |
| 195 |
{'♀', 1, 2, 2}, |
| 196 |
{'♪', 1, 2, 2}, |
| 197 |
{'♫', 1, 1, 1}, |
| 198 |
{'☼', 1, 1, 1}, |
| 199 |
{'↕', 1, 2, 2}, |
| 200 |
{'‼', 1, 1, 2}, |
| 201 |
{'↔', 1, 2, 2}, |
| 202 |
{'\x00', 0, 0, 0}, |
| 203 |
{'\x01', 0, 0, 0}, |
| 204 |
{'\u0300', 0, 0, 0}, |
| 205 |
{'\u2028', 0, 0, 0}, |
| 206 |
{'\u2029', 0, 0, 0}, |
| 207 |
{'a', 1, 1, 1}, // ASCII classified as "na" (narrow) |
| 208 |
{'⟦', 1, 1, 1}, // non-ASCII classified as "na" (narrow) |
| 209 |
{'👁', 1, 1, 2}, |
| 210 |
{'\u093E', 1, 1, 1}, // DEVANAGARI VOWEL SIGN AA (Mc) - spacing mark occupies its own cell |
| 211 |
{'\u0941', 0, 0, 0}, // DEVANAGARI VOWEL SIGN U (Mn) - nonspacing mark |
| 212 |
{'\u094D', 0, 0, 0}, // DEVANAGARI SIGN VIRAMA (Mn) |
| 213 |
{'\u0915', 1, 1, 1}, // DEVANAGARI LETTER KA (Lo) - base consonant, width 1 |
| 214 |
} |
| 215 |
|
| 216 |
func TestRuneWidth(t *testing.T) { |
| 217 |
c := NewCondition() |
| 218 |
c.EastAsianWidth = false |
| 219 |
for _, tt := range runewidthtests { |
| 220 |
if out := c.RuneWidth(tt.in); out != tt.out { |
| 221 |
t.Errorf("RuneWidth(%q) = %d, want %d (EastAsianWidth=false)", tt.in, out, tt.out) |
| 222 |
} |
| 223 |
} |
| 224 |
c.EastAsianWidth = true |
| 225 |
for _, tt := range runewidthtests { |
| 226 |
if out := c.RuneWidth(tt.in); out != tt.eaout { |
| 227 |
t.Errorf("RuneWidth(%q) = %d, want %d (EastAsianWidth=true)", tt.in, out, tt.eaout) |
| 228 |
} |
| 229 |
} |
| 230 |
c.StrictEmojiNeutral = false |
| 231 |
for _, tt := range runewidthtests { |
| 232 |
if out := c.RuneWidth(tt.in); out != tt.nseout { |
| 233 |
t.Errorf("RuneWidth(%q) = %d, want %d (StrictEmojiNeutral=false)", tt.in, out, tt.eaout) |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
var isambiguouswidthtests = []struct { |
| 239 |
in rune |
| 240 |
out bool |
| 241 |
}{ |
| 242 |
{'世', false}, |
| 243 |
{'■', true}, |
| 244 |
{'界', false}, |
| 245 |
{'○', true}, |
| 246 |
{'㈱', false}, |
| 247 |
{'①', true}, |
| 248 |
{'②', true}, |
| 249 |
{'③', true}, |
| 250 |
{'④', true}, |
| 251 |
{'⑤', true}, |
| 252 |
{'⑥', true}, |
| 253 |
{'⑦', true}, |
| 254 |
{'⑧', true}, |
| 255 |
{'⑨', true}, |
| 256 |
{'⑩', true}, |
| 257 |
{'⑪', true}, |
| 258 |
{'⑫', true}, |
| 259 |
{'⑬', true}, |
| 260 |
{'⑭', true}, |
| 261 |
{'⑮', true}, |
| 262 |
{'⑯', true}, |
| 263 |
{'⑰', true}, |
| 264 |
{'⑱', true}, |
| 265 |
{'⑲', true}, |
| 266 |
{'⑳', true}, |
| 267 |
{'☆', true}, |
| 268 |
} |
| 269 |
|
| 270 |
func TestIsAmbiguousWidth(t *testing.T) { |
| 271 |
for _, tt := range isambiguouswidthtests { |
| 272 |
if out := IsAmbiguousWidth(tt.in); out != tt.out { |
| 273 |
t.Errorf("IsAmbiguousWidth(%q) = %v, want %v", tt.in, out, tt.out) |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
var stringwidthtests = []struct { |
| 279 |
in string |
| 280 |
out int |
| 281 |
eaout int |
| 282 |
}{ |
| 283 |
{"■㈱の世界①", 10, 12}, |
| 284 |
{"スター☆", 7, 8}, |
| 285 |
{"つのだ☆HIRO", 11, 12}, |
| 286 |
{"खा", 2, 2}, // base consonant + spacing mark (Mc), one cluster, two cells |
| 287 |
{"हिन्द", 4, 4}, // spacing marks count, nonspacing marks (Mn) do not |
| 288 |
{"फ़ल", 2, 2}, // nukta (Mn) is zero width |
| 289 |
{"🇩🇰", 2, 2}, // regional indicator pair renders as one two-cell flag |
| 290 |
} |
| 291 |
|
| 292 |
func TestStringWidth(t *testing.T) { |
| 293 |
c := NewCondition() |
| 294 |
c.EastAsianWidth = false |
| 295 |
for _, tt := range stringwidthtests { |
| 296 |
if out := c.StringWidth(tt.in); out != tt.out { |
| 297 |
t.Errorf("StringWidth(%q) = %d, want %d", tt.in, out, tt.out) |
| 298 |
} |
| 299 |
} |
| 300 |
c.EastAsianWidth = true |
| 301 |
for _, tt := range stringwidthtests { |
| 302 |
if out := c.StringWidth(tt.in); out != tt.eaout { |
| 303 |
t.Errorf("StringWidth(%q) = %d, want %d (EA)", tt.in, out, tt.eaout) |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
func TestStringWidthInvalid(t *testing.T) { |
| 309 |
s := "こんにちわ\x00世界" |
| 310 |
if out := StringWidth(s); out != 14 { |
| 311 |
t.Errorf("StringWidth(%q) = %d, want %d", s, out, 14) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
func TestTruncateSmaller(t *testing.T) { |
| 316 |
s := "あいうえお" |
| 317 |
expected := "あいうえお" |
| 318 |
|
| 319 |
if out := Truncate(s, 10, "..."); out != expected { |
| 320 |
t.Errorf("Truncate(%q) = %q, want %q", s, out, expected) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func TestTruncate(t *testing.T) { |
| 325 |
s := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおおお" |
| 326 |
expected := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおお..." |
| 327 |
out := Truncate(s, 80, "...") |
| 328 |
if out != expected { |
| 329 |
t.Errorf("Truncate(%q) = %q, want %q", s, out, expected) |
| 330 |
} |
| 331 |
width := StringWidth(out) |
| 332 |
if width != 79 { |
| 333 |
t.Errorf("width of Truncate(%q) should be %d, but %d", s, 79, width) |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
func TestTruncateFit(t *testing.T) { |
| 338 |
s := "aあいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおおお" |
| 339 |
expected := "aあいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおお..." |
| 340 |
|
| 341 |
out := Truncate(s, 80, "...") |
| 342 |
if out != expected { |
| 343 |
t.Errorf("Truncate(%q) = %q, want %q", s, out, expected) |
| 344 |
} |
| 345 |
width := StringWidth(out) |
| 346 |
if width != 80 { |
| 347 |
t.Errorf("width of Truncate(%q) should be %d, but %d", s, 80, width) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
func TestTruncateJustFit(t *testing.T) { |
| 352 |
s := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおお" |
| 353 |
expected := "あいうえおあいうえおえおおおおおおおおおおおおおおおおおおおおおおおおおおおおお" |
| 354 |
|
| 355 |
out := Truncate(s, 80, "...") |
| 356 |
if out != expected { |
| 357 |
t.Errorf("Truncate(%q) = %q, want %q", s, out, expected) |
| 358 |
} |
| 359 |
width := StringWidth(out) |
| 360 |
if width != 80 { |
| 361 |
t.Errorf("width of Truncate(%q) should be %d, but %d", s, 80, width) |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
func TestWrap(t *testing.T) { |
| 366 |
s := `東京特許許可局局長はよく柿喰う客だ/東京特許許可局局長はよく柿喰う客だ |
| 367 |
123456789012345678901234567890 |
| 368 |
|
| 369 |
END` |
| 370 |
expected := `東京特許許可局局長はよく柿喰う |
| 371 |
客だ/東京特許許可局局長はよく |
| 372 |
柿喰う客だ |
| 373 |
123456789012345678901234567890 |
| 374 |
|
| 375 |
END` |
| 376 |
|
| 377 |
if out := Wrap(s, 30); out != expected { |
| 378 |
t.Errorf("Wrap(%q) = %q, want %q", s, out, expected) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
func TestTruncateNoNeeded(t *testing.T) { |
| 383 |
s := "あいうえおあい" |
| 384 |
expected := "あいうえおあい" |
| 385 |
|
| 386 |
if out := Truncate(s, 80, "..."); out != expected { |
| 387 |
t.Errorf("Truncate(%q) = %q, want %q", s, out, expected) |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
var truncatelefttests = []struct { |
| 392 |
s string |
| 393 |
w int |
| 394 |
prefix string |
| 395 |
out string |
| 396 |
}{ |
| 397 |
{"source", 4, "", "ce"}, |
| 398 |
{"source", 4, "...", "...ce"}, |
| 399 |
{"あいうえお", 6, "", "えお"}, |
| 400 |
{"あいうえお", 6, "...", "...えお"}, |
| 401 |
{"あいうえお", 10, "", ""}, |
| 402 |
{"あいうえお", 10, "...", "..."}, |
| 403 |
{"あいうえお", 5, "", " えお"}, |
| 404 |
{"Aあいうえお", 5, "", "うえお"}, |
| 405 |
} |
| 406 |
|
| 407 |
func TestTruncateLeft(t *testing.T) { |
| 408 |
t.Parallel() |
| 409 |
|
| 410 |
for _, tt := range truncatelefttests { |
| 411 |
if out := TruncateLeft(tt.s, tt.w, tt.prefix); out != tt.out { |
| 412 |
t.Errorf("TruncateLeft(%q) = %q, want %q", tt.s, out, tt.out) |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
var truncateprefixtests = []struct { |
| 418 |
s string |
| 419 |
w int |
| 420 |
prefix string |
| 421 |
out string |
| 422 |
}{ |
| 423 |
{"source", 4, "*", "*rce"}, |
| 424 |
{"source", 6, "*", "source"}, |
| 425 |
{"あいうえお", 4, "*", "*お"}, |
| 426 |
{"あいうえお", 10, "*", "あいうえお"}, |
| 427 |
{"Aあいうえお", 5, "*", "*えお"}, |
| 428 |
|
| 429 |
{"source", 4, "", "urce"}, |
| 430 |
{"source", 4, "...", "...e"}, |
| 431 |
{"あいうえお", 6, "", "うえお"}, |
| 432 |
{"あいうえお", 6, "...", "...お"}, |
| 433 |
{"あいうえお", 10, "", "あいうえお"}, |
| 434 |
{"あいうえお", 10, "...", "あいうえお"}, |
| 435 |
{"あいうえお", 5, "", "えお"}, |
| 436 |
|
| 437 |
{"source", 1, "*", "*"}, |
| 438 |
{"source", 0, "*", "*"}, // same as Truncate |
| 439 |
|
| 440 |
// multi-rune grapheme clusters must be dropped or kept as a whole |
| 441 |
{"👨👩👧cde", 4, "*", "*cde"}, |
| 442 |
{"खाabc", 3, "*", "*bc"}, |
| 443 |
{"🇩🇰abc", 3, "", "abc"}, |
| 444 |
{"あ🏳️🌈い", 4, "*", "*い"}, |
| 445 |
} |
| 446 |
|
| 447 |
func TestTruncatePrefix(t *testing.T) { |
| 448 |
t.Parallel() |
| 449 |
|
| 450 |
for _, tt := range truncateprefixtests { |
| 451 |
if out := TruncatePrefix(tt.s, tt.w, tt.prefix); out != tt.out { |
| 452 |
t.Errorf("TruncatePrefix(%q) = %q, want %q", tt.s, out, tt.out) |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
var isneutralwidthtests = []struct { |
| 458 |
in rune |
| 459 |
out bool |
| 460 |
}{ |
| 461 |
{'→', false}, |
| 462 |
{'┊', false}, |
| 463 |
{'┈', false}, |
| 464 |
{'~', false}, |
| 465 |
{'└', false}, |
| 466 |
{'⣀', true}, |
| 467 |
{'⣀', true}, |
| 468 |
} |
| 469 |
|
| 470 |
func TestIsNeutralWidth(t *testing.T) { |
| 471 |
for _, tt := range isneutralwidthtests { |
| 472 |
if out := IsNeutralWidth(tt.in); out != tt.out { |
| 473 |
t.Errorf("IsNeutralWidth(%q) = %v, want %v", tt.in, out, tt.out) |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
var iscombiningwidthtests = []struct { |
| 479 |
in rune |
| 480 |
out bool |
| 481 |
}{ |
| 482 |
{'\u0300', true}, // COMBINING GRAVE ACCENT |
| 483 |
{'\uFE0F', true}, // VARIATION SELECTOR-16 |
| 484 |
{'\uFE0E', true}, // VARIATION SELECTOR-15 |
| 485 |
{'\u180B', true}, // MONGOLIAN FREE VARIATION SELECTOR ONE |
| 486 |
{'\U000E0100', true}, // VARIATION SELECTOR-17 |
| 487 |
{'A', false}, |
| 488 |
{'☆', false}, |
| 489 |
} |
| 490 |
|
| 491 |
func TestIsCombiningWidth(t *testing.T) { |
| 492 |
for _, tt := range iscombiningwidthtests { |
| 493 |
if out := IsCombiningWidth(tt.in); out != tt.out { |
| 494 |
t.Errorf("IsCombiningWidth(%q) = %v, want %v", tt.in, out, tt.out) |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
func TestFillLeft(t *testing.T) { |
| 500 |
s := "あxいうえお" |
| 501 |
expected := " あxいうえお" |
| 502 |
|
| 503 |
if out := FillLeft(s, 15); out != expected { |
| 504 |
t.Errorf("FillLeft(%q) = %q, want %q", s, out, expected) |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
func TestFillLeftFit(t *testing.T) { |
| 509 |
s := "あいうえお" |
| 510 |
expected := "あいうえお" |
| 511 |
|
| 512 |
if out := FillLeft(s, 10); out != expected { |
| 513 |
t.Errorf("FillLeft(%q) = %q, want %q", s, out, expected) |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
func TestFillRight(t *testing.T) { |
| 518 |
s := "あxいうえお" |
| 519 |
expected := "あxいうえお " |
| 520 |
|
| 521 |
if out := FillRight(s, 15); out != expected { |
| 522 |
t.Errorf("FillRight(%q) = %q, want %q", s, out, expected) |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
func TestFillRightFit(t *testing.T) { |
| 527 |
s := "あいうえお" |
| 528 |
expected := "あいうえお" |
| 529 |
|
| 530 |
if out := FillRight(s, 10); out != expected { |
| 531 |
t.Errorf("FillRight(%q) = %q, want %q", s, out, expected) |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
func TestEnv(t *testing.T) { |
| 536 |
old := os.Getenv("RUNEWIDTH_EASTASIAN") |
| 537 |
defer os.Setenv("RUNEWIDTH_EASTASIAN", old) |
| 538 |
|
| 539 |
os.Setenv("RUNEWIDTH_EASTASIAN", "0") |
| 540 |
handleEnv() |
| 541 |
|
| 542 |
if w := RuneWidth('│'); w != 1 { |
| 543 |
t.Errorf("RuneWidth('│') = %d, want %d", w, 1) |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
func TestZeroWidthJoiner(t *testing.T) { |
| 548 |
c := NewCondition() |
| 549 |
|
| 550 |
var tests = []struct { |
| 551 |
in string |
| 552 |
want int |
| 553 |
}{ |
| 554 |
{"👩", 2}, |
| 555 |
{"👩\u200d", 2}, |
| 556 |
{"👩\u200d🍳", 2}, |
| 557 |
{"\u200d🍳", 2}, |
| 558 |
{"👨\u200d👨", 2}, |
| 559 |
{"👨\u200d👨\u200d👧", 2}, |
| 560 |
{"🏳️\u200d🌈", 2}, |
| 561 |
{"あ👩\u200d🍳い", 6}, |
| 562 |
{"あ\u200d🍳い", 6}, |
| 563 |
{"あ\u200dい", 4}, |
| 564 |
} |
| 565 |
|
| 566 |
for _, tt := range tests { |
| 567 |
if got := c.StringWidth(tt.in); got != tt.want { |
| 568 |
t.Errorf("StringWidth(%q) = %d, want %d", tt.in, got, tt.want) |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
func TestZeroWidthJoinerFlag(t *testing.T) { |
| 574 |
// The deprecated flag must compile and must not change any result. |
| 575 |
for _, zwj := range []bool{true, false} { |
| 576 |
c := NewCondition() |
| 577 |
c.ZeroWidthJoiner = zwj |
| 578 |
if got := c.StringWidth("👨👨👧"); got != 2 { |
| 579 |
t.Errorf("StringWidth with ZeroWidthJoiner=%v = %d, want 2", zwj, got) |
| 580 |
} |
| 581 |
} |
| 582 |
} |