| 1 |
package displaywidth |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
var defaultOptions = Options{} |
| 9 |
|
| 10 |
var eawOptions = Options{EastAsianWidth: true} |
| 11 |
|
| 12 |
func TestStringWidth(t *testing.T) { |
| 13 |
tests := []struct { |
| 14 |
name string |
| 15 |
input string |
| 16 |
options Options |
| 17 |
expected int |
| 18 |
}{ |
| 19 |
|
| 20 |
{"empty string", "", defaultOptions, 0}, |
| 21 |
{"single ASCII", "a", defaultOptions, 1}, |
| 22 |
{"multiple ASCII", "hello", defaultOptions, 5}, |
| 23 |
{"ASCII with spaces", "hello world", defaultOptions, 11}, |
| 24 |
|
| 25 |
|
| 26 |
{"newline", "\n", defaultOptions, 0}, |
| 27 |
{"tab", "\t", defaultOptions, 0}, |
| 28 |
{"carriage return", "\r", defaultOptions, 0}, |
| 29 |
{"backspace", "\b", defaultOptions, 0}, |
| 30 |
|
| 31 |
|
| 32 |
{"ASCII with newline", "hello\nworld", defaultOptions, 10}, |
| 33 |
{"ASCII with tab", "hello\tworld", defaultOptions, 10}, |
| 34 |
|
| 35 |
|
| 36 |
{"CJK ideograph", "中", defaultOptions, 2}, |
| 37 |
{"CJK with ASCII", "hello中", defaultOptions, 7}, |
| 38 |
|
| 39 |
|
| 40 |
{"ambiguous character", "★", defaultOptions, 1}ow |
| 41 |
{"ambiguous character EAW", "★", eawOptions, 2}de |
| 42 |
|
| 43 |
|
| 44 |
{"emoji", "😀", defaultOptions, 2}, th |
| 45 |
{"checkered flag", "🏁", defaultOptions, 2} 2 |
| 46 |
|
| 47 |
|
| 48 |
{"invalid UTF-8", "\xff", defaultOptions, 1}, |
| 49 |
{"partial UTF-8", "\xc2", defaultOptions, 1}, |
| 50 |
|
| 51 |
|
| 52 |
{"☺ text default", "☺", defaultOptions, 1}, ault |
| 53 |
{"☺️ emoji with VS16", "☺️", defaultOptiowidth 2) |
| 54 |
{"⌛ emoji default", "⌛", defaultOptions, 2}, ault |
| 55 |
{"⌛︎ with VS15", "⌛︎", defaultOptions, 2}emains 2 |
| 56 |
{"❤ text default", "❤", defaultOptions, 1}, ault |
| 57 |
{"❤️ emoji with VS16", "❤️", defaultOptiowidth 2) |
| 58 |
{"✂ text default", "✂", defaultOptions, 1}, ault |
| 59 |
{"✂️ emoji with VS16", "✂️", defaultOptiowidth 2) |
| 60 |
{"keycap 1️⃣", "1️⃣", defaultOptions, 2},width 2) |
| 61 |
{"keycap #️⃣", "#️⃣", defaultOptions, 2},width 2) |
| 62 |
|
| 63 |
|
| 64 |
{"flag US", "🇺🇸", defaultOptions, 2}, |
| 65 |
{"flag JP", "🇯🇵", defaultOptions, 2}, |
| 66 |
{"text with flags", "Go 🇺🇸🚀", defaultOptions, 3 + 2 + 2}, |
| 67 |
|
| 68 |
|
| 69 |
{"ASCII 8 bytes then emoji", "12345678😀", defaultOptions, 8 + 2}, |
| 70 |
{"ASCII 16 bytes then CJK", "1234567890abcdef中", defaultOptions, 16 + 2}, |
| 71 |
{"emoji then ASCII 8 bytes", "😀12345678", defaultOptions, 2 + 8}, |
| 72 |
{"CJK then ASCII 16 bytes", "中1234567890abcdef", defaultOptions, 2 + 16}, |
| 73 |
{"ASCII-emoji-ASCII sandwich", "12345678😀abcdefgh", defaultOptions, 8 + 2 + 8}, |
| 74 |
{"short ASCII then emoji", "hello😀", defaultOptions, 5 + 2}, |
| 75 |
{"emoji-short ASCII-emoji", "😀abc😀", defaultOptions, 2 + 3 + 2}, |
| 76 |
{"long mixed", "Hello World! 你好世界 12345678 emoji: 🎉🎊", defaultOp + 9 + 7 + 4 |
| 77 |
|
| 78 |
|
| 79 |
{"ASCII with null in middle", "hello\x00world", defaultOptions, 10}, |
| 80 |
{"ASCII with DEL in middle", "hello\x7Fworld", defaultOptions, 10}, |
| 81 |
{"ASCII with multiple controls", "a\x00b\tc\nd", defaultOptions, 4}, |
| 82 |
|
| 83 |
|
| 84 |
{"alternating ASCII-CJK", "a中b文c", defaultOptions, 7} + 1 |
| 85 |
{"alternating CJK-ASCII", "中a文b字c", defaultOptions, 2 + 1 |
| 86 |
{"single char alternating", "a😀b🎉c", defaultOptions, + 1 |
| 87 |
{"rapid alternation", "aあbいcうd", defaultOptions, 10 2 + 1 |
| 88 |
} |
| 89 |
|
| 90 |
for _, tt := range tests { |
| 91 |
t.Run(tt.name, func(t *testing.T) { |
| 92 |
result := tt.options.String(tt.input) |
| 93 |
if result != tt.expected { |
| 94 |
t.Errorf("StringWidth(%q, %v) = %d, want %d", |
| 95 |
tt.input, tt.options, result, tt.expected) |
| 96 |
} |
| 97 |
|
| 98 |
b := []byte(tt.input) |
| 99 |
result = tt.options.Bytes(b) |
| 100 |
if result != tt.expected { |
| 101 |
t.Errorf("BytesWidth(%q, %v) = %d, want %d", |
| 102 |
b, tt.options, result, tt.expected) |
| 103 |
} |
| 104 |
}) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
var controlSequences = Options{ControlSequences: true} |
| 109 |
var controlSequences8Bit = Options{ControlSequences8Bit: true} |
| 110 |
var controlSequencesBoth = Options{ControlSequences: true, ControlSequences8Bit: true} |
| 111 |
|
| 112 |
func TestAnsiEscapeSequences(t *testing.T) { |
| 113 |
tests := []struct { |
| 114 |
name string |
| 115 |
input string |
| 116 |
options Options |
| 117 |
expected int |
| 118 |
}{ |
| 119 |
|
| 120 |
{"SGR red", "\x1b[31m", controlSequences, 0}, |
| 121 |
{"SGR reset", "\x1b[0m", controlSequences, 0}, |
| 122 |
{"SGR bold", "\x1b[1m", controlSequences, 0}, |
| 123 |
{"SGR 256-color", "\x1b[38;5;196m", controlSequences, 0}, |
| 124 |
{"SGR truecolor", "\x1b[38;2;255;0;0m", controlSequences, 0}, |
| 125 |
{"cursor up", "\x1b[A", controlSequences, 0}, |
| 126 |
{"cursor position", "\x1b[10;20H", controlSequences, 0}, |
| 127 |
{"erase in display", "\x1b[2J", controlSequences, 0}, |
| 128 |
|
| 129 |
|
| 130 |
{"red hello", "\x1b[31mhello\x1b[0m", controlSequences, 5}, |
| 131 |
{"bold world", "\x1b[1mworld\x1b[0m", controlSequences, 5}, |
| 132 |
{"colored CJK", "\x1b[31m中文\x1b[0m", controlSequences, 4}, |
| 133 |
{"colored emoji", "\x1b[31m😀\x1b[0m", controlSequences, 2}, |
| 134 |
{"nested SGR", "\x1b[1m\x1b[31mhi\x1b[0m", controlSequences, 2}, |
| 135 |
|
| 136 |
|
| 137 |
{"CRLF", "\r\n", controlSequences, 0}, |
| 138 |
{"text with CRLF", "hello\r\nworld", controlSequences, 10}, |
| 139 |
|
| 140 |
|
| 141 |
{"bare ESC default options", "\x1b", defaultOptions, 0}, |
| 142 |
{"SGR red default options", "\x1b[31m", defaultOptions, 4}, |
| 143 |
{"red hello default options", "\x1b[31mhello\x1b[0m", defaultOptions, 12}, |
| 144 |
|
| 145 |
|
| 146 |
{"plain ASCII with option", "hello", controlSequences, 5}, |
| 147 |
{"plain ASCII spaces with option", "hello world", controlSequences, 11}, |
| 148 |
{"CJK with option", "中文", controlSequences, 4}, |
| 149 |
{"emoji with option", "😀", controlSequences, 2}, |
| 150 |
{"flag with option", "🇺🇸", controlSequences, 2}, |
| 151 |
{"mixed with option", "hello中文😀", controlSequences, 5 + 4 + 2}, |
| 152 |
{"ambiguous with option", "★", controlSequences, 1}, |
| 153 |
{"combining mark with option", "é", controlSequences, 1}, |
| 154 |
{"control chars with option", "\t\n", controlSequences, 0}, |
| 155 |
{"empty with option", "", controlSequences, 0}, |
| 156 |
} |
| 157 |
|
| 158 |
for _, tt := range tests { |
| 159 |
t.Run(tt.name, func(t *testing.T) { |
| 160 |
result := tt.options.String(tt.input) |
| 161 |
if result != tt.expected { |
| 162 |
t.Errorf("String(%q) = %d, want %d", tt.input, result, tt.expected) |
| 163 |
} |
| 164 |
|
| 165 |
result = tt.options.Bytes([]byte(tt.input)) |
| 166 |
if result != tt.expected { |
| 167 |
t.Errorf("Bytes(%q) = %d, want %d", tt.input, result, tt.expected) |
| 168 |
} |
| 169 |
}) |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestAnsiEscapeSequences8Bit(t *testing.T) { |
| 174 |
tests := []struct { |
| 175 |
name string |
| 176 |
input string |
| 177 |
options Options |
| 178 |
expected int |
| 179 |
}{ |
| 180 |
|
| 181 |
{"C1 CSI red", "\x9B31m", controlSequences8Bit, 0}, |
| 182 |
{"C1 CSI reset", "\x9B0m", controlSequences8Bit, 0}, |
| 183 |
{"C1 CSI bold", "\x9B1m", controlSequences8Bit, 0}, |
| 184 |
{"C1 CSI multi-param", "\x9B1;2;3m", controlSequences8Bit, 0}, |
| 185 |
{"C1 CSI cursor up", "\x9BA", controlSequences8Bit, 0}, |
| 186 |
|
| 187 |
|
| 188 |
{"C1 OSC with ST", "\x9D0;Title\x9C", controlSequences8Bit, 0}, |
| 189 |
{"C1 OSC with BEL", "\x9D0;Title\x07", controlSequences8Bit, 0}, |
| 190 |
{"C1 DCS with ST", "\x90qpayload\x9C", controlSequences8Bit, 0}, |
| 191 |
{"C1 SOS with ST", "\x98hello\x9C", controlSequences8Bit, 0}, |
| 192 |
{"C1 APC with ST", "\x9Fdata\x9C", controlSequences8Bit, 0}, |
| 193 |
|
| 194 |
|
| 195 |
{"C1 IND", "\x84", controlSequences8Bit, 0}, |
| 196 |
{"C1 NEL", "\x85", controlSequences8Bit, 0}, |
| 197 |
|
| 198 |
|
| 199 |
{"C1 CSI red hello", "\x9B31mhello\x9B0m", controlSequences8Bit, 5}, |
| 200 |
{"C1 CSI colored CJK", "\x9B31m中文\x9B0m", controlSequences8Bit, 4}, |
| 201 |
{"C1 CSI colored emoji", "\x9B31m😀\x9B0m", controlSequences8Bit, 2}, |
| 202 |
{"C1 CSI nested", "\x9B1m\x9B31mhi\x9B0m", controlSequences8Bit, 2}, |
| 203 |
|
| 204 |
|
| 205 |
{"C1 CSI default options", "\x9B31m", defaultOptions, 4}, |
| 206 |
|
| 207 |
|
| 208 |
{"plain ASCII with 8-bit option", "hello", controlSequences8Bit, 5}, |
| 209 |
{"CJK with 8-bit option", "中文", controlSequences8Bit, 4}, |
| 210 |
{"emoji with 8-bit option", "😀", controlSequences8Bit, 2}, |
| 211 |
{"empty with 8-bit option", "", controlSequences8Bit, 0}, |
| 212 |
|
| 213 |
|
| 214 |
{"both: 7-bit SGR", "\x1b[31mhello\x1b[0m", controlSequencesBoth, 5}, |
| 215 |
{"both: 8-bit CSI", "\x9B31mhello\x9B0m", controlSequencesBoth, 5}, |
| 216 |
{"both: mixed 7 and 8-bit", "\x1b[31mhello\x9B0m", controlSequencesBoth, 5}, |
| 217 |
} |
| 218 |
|
| 219 |
for _, tt := range tests { |
| 220 |
t.Run(tt.name, func(t *testing.T) { |
| 221 |
result := tt.options.String(tt.input) |
| 222 |
if result != tt.expected { |
| 223 |
t.Errorf("String(%q) = %d, want %d", tt.input, result, tt.expected) |
| 224 |
} |
| 225 |
|
| 226 |
result = tt.options.Bytes([]byte(tt.input)) |
| 227 |
if result != tt.expected { |
| 228 |
t.Errorf("Bytes(%q) = %d, want %d", tt.input, result, tt.expected) |
| 229 |
} |
| 230 |
}) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
func TestAnsiEscapeSequencesIndependence(t *testing.T) { |
| 238 |
tests := []struct { |
| 239 |
name string |
| 240 |
input string |
| 241 |
options Options |
| 242 |
expected int |
| 243 |
desc string |
| 244 |
}{ |
| 245 |
|
| 246 |
|
| 247 |
{ |
| 248 |
name: "7-bit on, 8-bit input C1 CSI", |
| 249 |
input: "\x9B31m", |
| 250 |
options: controlSequences, |
| 251 |
expected: 4, |
| 252 |
desc: "C1 CSI should not be recognized when only 7-bit is enabled", |
| 253 |
}, |
| 254 |
{ |
| 255 |
name: "7-bit on, 8-bit input standalone C1", |
| 256 |
input: "\x84", |
| 257 |
options: controlSequences, |
| 258 |
expected: 1, |
| 259 |
desc: "Standalone C1 byte should have width 1 when only 7-bit is enabled", |
| 260 |
}, |
| 261 |
{ |
| 262 |
name: "7-bit on, 8-bit input C1 with text", |
| 263 |
input: "\x9B31mhello\x9B0m", |
| 264 |
options: controlSequences, |
| 265 |
expected: 4 + 5 + 3, |
| 266 |
desc: "C1 CSI sequences should contribute visible width when only 7-bit is enabled", |
| 267 |
}, |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
{ |
| 272 |
name: "8-bit on, 7-bit input SGR", |
| 273 |
input: "\x1b[31m", |
| 274 |
options: controlSequences8Bit, |
| 275 |
expected: 4, |
| 276 |
desc: "7-bit SGR should not be recognized when only 8-bit is enabled", |
| 277 |
}, |
| 278 |
{ |
| 279 |
name: "8-bit on, 7-bit input SGR with text", |
| 280 |
input: "\x1b[31mhello\x1b[0m", |
| 281 |
options: controlSequences8Bit, |
| 282 |
expected: 4 + 5 + 3, |
| 283 |
desc: "7-bit SGR should contribute visible width when only 8-bit is enabled", |
| 284 |
}, |
| 285 |
|
| 286 |
|
| 287 |
{ |
| 288 |
name: "both on, 7-bit SGR", |
| 289 |
input: "\x1b[31m", |
| 290 |
options: controlSequencesBoth, |
| 291 |
expected: 0, |
| 292 |
desc: "7-bit SGR should be zero-width when both are enabled", |
| 293 |
}, |
| 294 |
{ |
| 295 |
name: "both on, 8-bit CSI", |
| 296 |
input: "\x9B31m", |
| 297 |
options: controlSequencesBoth, |
| 298 |
expected: 0, |
| 299 |
desc: "C1 CSI should be zero-width when both are enabled", |
| 300 |
}, |
| 301 |
{ |
| 302 |
name: "both on, mixed sequences with text", |
| 303 |
input: "\x1b[31mhello\x9B0m", |
| 304 |
options: controlSequencesBoth, |
| 305 |
expected: 5, |
| 306 |
desc: "Mixed 7-bit and 8-bit sequences should both be zero-width", |
| 307 |
}, |
| 308 |
|
| 309 |
|
| 310 |
{ |
| 311 |
name: "neither, 7-bit SGR", |
| 312 |
input: "\x1b[31m", |
| 313 |
options: defaultOptions, |
| 314 |
expected: 4, |
| 315 |
desc: "7-bit SGR should contribute visible width when neither is enabled", |
| 316 |
}, |
| 317 |
{ |
| 318 |
name: "neither, 8-bit CSI", |
| 319 |
input: "\x9B31m", |
| 320 |
options: defaultOptions, |
| 321 |
expected: 4, |
| 322 |
desc: "C1 CSI should contribute visible width when neither is enabled", |
| 323 |
}, |
| 324 |
} |
| 325 |
|
| 326 |
for _, tt := range tests { |
| 327 |
t.Run(tt.name, func(t *testing.T) { |
| 328 |
result := tt.options.String(tt.input) |
| 329 |
if result != tt.expected { |
| 330 |
t.Errorf("String(%q) = %d, want %d (%s)", tt.input, result, tt.expected, tt.desc) |
| 331 |
} |
| 332 |
|
| 333 |
result = tt.options.Bytes([]byte(tt.input)) |
| 334 |
if result != tt.expected { |
| 335 |
t.Errorf("Bytes(%q) = %d, want %d (%s)", tt.input, result, tt.expected, tt.desc) |
| 336 |
} |
| 337 |
}) |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
func TestRuneWidth(t *testing.T) { |
| 342 |
tests := []struct { |
| 343 |
name string |
| 344 |
input rune |
| 345 |
options Options |
| 346 |
expected int |
| 347 |
}{ |
| 348 |
|
| 349 |
{"null char", '\x00', defaultOptions, 0}, |
| 350 |
{"bell", '\x07', defaultOptions, 0}, |
| 351 |
{"backspace", '\x08', defaultOptions, 0}, |
| 352 |
{"tab", '\t', defaultOptions, 0}, |
| 353 |
{"newline", '\n', defaultOptions, 0}, |
| 354 |
{"carriage return", '\r', defaultOptions, 0}, |
| 355 |
{"escape", '\x1B', defaultOptions, 0}, |
| 356 |
{"delete", '\x7F', defaultOptions, 0}, |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
{"combining grave accent", '\u0300', defaultOptions, 0}, |
| 361 |
{"combining acute accent", '\u0301', defaultOptions, 0}, |
| 362 |
{"combining diaeresis", '\u0308', defaultOptions, 0}, |
| 363 |
{"combining tilde", '\u0303', defaultOptions, 0}, |
| 364 |
|
| 365 |
|
| 366 |
{"zero width space", '\u200B', defaultOptions, 0}, |
| 367 |
{"zero width non-joiner", '\u200C', defaultOptions, 0}, |
| 368 |
{"zero width joiner", '\u200D', defaultOptions, 0}, |
| 369 |
|
| 370 |
|
| 371 |
{"space", ' ', defaultOptions, 1}, |
| 372 |
{"letter a", 'a', defaultOptions, 1}, |
| 373 |
{"letter Z", 'Z', defaultOptions, 1}, |
| 374 |
{"digit 0", '0', defaultOptions, 1}, |
| 375 |
{"digit 9", '9', defaultOptions, 1}, |
| 376 |
{"exclamation", '!', defaultOptions, 1}, |
| 377 |
{"at sign", '@', defaultOptions, 1}, |
| 378 |
{"tilde", '~', defaultOptions, 1}, |
| 379 |
|
| 380 |
|
| 381 |
{"latin e with acute", 'é', defaultOptions, 1}, |
| 382 |
{"latin n with tilde", 'ñ', defaultOptions, 1}, |
| 383 |
{"latin o with diaeresis", 'ö', defaultOptions, 1}, |
| 384 |
|
| 385 |
|
| 386 |
{"CJK ideograph", '中', defaultOptions, 2}, |
| 387 |
{"CJK ideograph", '文', defaultOptions, 2}, |
| 388 |
{"hiragana a", 'あ', defaultOptions, 2}, |
| 389 |
{"katakana a", 'ア', defaultOptions, 2}, |
| 390 |
{"hangul syllable", '가', defaultOptions, 2}, |
| 391 |
{"hangul syllable", '한', defaultOptions, 2}, |
| 392 |
|
| 393 |
|
| 394 |
{"fullwidth A", 'A', defaultOptions, 2}, |
| 395 |
{"fullwidth Z", 'Z', defaultOptions, 2}, |
| 396 |
{"fullwidth 0", '0', defaultOptions, 2}, |
| 397 |
{"fullwidth 9", '9', defaultOptions, 2}, |
| 398 |
{"fullwidth exclamation", '!', defaultOptions, 2}, |
| 399 |
{"fullwidth space", ' ', defaultOptions, 2}, |
| 400 |
|
| 401 |
|
| 402 |
{"black star default", '★', defaultOptions, 1}, |
| 403 |
{"degree sign default", '°', defaultOptions, 1}, |
| 404 |
{"plus-minus default", '±', defaultOptions, 1}, |
| 405 |
{"section sign default", '§', defaultOptions, 1}, |
| 406 |
{"copyright sign default", '©', defaultOptions, 1}, |
| 407 |
{"registered sign default", '®', defaultOptions, 1}, |
| 408 |
|
| 409 |
|
| 410 |
{"black star EAW", '★', eawOptions, 2}, |
| 411 |
{"degree sign EAW", '°', eawOptions, 2}, |
| 412 |
{"plus-minus EAW", '±', eawOptions, 2}, |
| 413 |
{"section sign EAW", '§', eawOptions, 2}, |
| 414 |
{"copyright sign EAW", '©', eawOptions, 1},y |
| 415 |
{"registered sign EAW", '®', eawOptions, 2}, |
| 416 |
|
| 417 |
|
| 418 |
{"grinning face", '😀', defaultOptions, 2}, |
| 419 |
{"grinning face with smiling eyes", '😁', defaultOptions, 2}, |
| 420 |
{"smiling face with heart-eyes", '😍', defaultOptions, 2}, |
| 421 |
{"thinking face", '🤔', defaultOptions, 2}, |
| 422 |
{"rocket", '🚀', defaultOptions, 2}, |
| 423 |
{"party popper", '🎉', defaultOptions, 2}, |
| 424 |
{"fire", '🔥', defaultOptions, 2}, |
| 425 |
{"thumbs up", '👍', defaultOptions, 2}, |
| 426 |
{"red heart", '❤', defaultOptions, 1}, lt |
| 427 |
{"checkered flag", '🏁', defaultOptions, 2}ag |
| 428 |
|
| 429 |
|
| 430 |
{"infinity", '∞', defaultOptions, 1}, |
| 431 |
{"summation", '∑', defaultOptions, 1}, |
| 432 |
{"integral", '∫', defaultOptions, 1}, |
| 433 |
{"square root", '√', defaultOptions, 1}, |
| 434 |
|
| 435 |
|
| 436 |
{"dollar", '$', defaultOptions, 1}, |
| 437 |
{"euro", '€', defaultOptions, 1}, |
| 438 |
{"pound", '£', defaultOptions, 1}, |
| 439 |
{"yen", '¥', defaultOptions, 1}, |
| 440 |
|
| 441 |
|
| 442 |
{"box light horizontal", '─', defaultOptions, 1}, |
| 443 |
{"box light vertical", '│', defaultOptions, 1}, |
| 444 |
{"box light down and right", '┌', defaultOptions, 1}, |
| 445 |
|
| 446 |
|
| 447 |
{"bullet", '•', defaultOptions, 1}, |
| 448 |
{"ellipsis", '…', defaultOptions, 1}, |
| 449 |
{"em dash", '—', defaultOptions, 1}, |
| 450 |
{"left single quote", '\u2018', defaultOptions, 1}, |
| 451 |
{"right single quote", '\u2019', defaultOptions, 1}, |
| 452 |
|
| 453 |
|
| 454 |
{"ambiguous EAW disabled", '★', defaultOptions, 1}, |
| 455 |
|
| 456 |
|
| 457 |
{"☺ U+263A text default", '☺', defaultOptions, 1},ault |
| 458 |
{"⌛ U+231B emoji default", '⌛', defaultOptions, 2}ault |
| 459 |
{"❤ U+2764 text default", '❤', defaultOptions, 1},ault |
| 460 |
{"✂ U+2702 text default", '✂', defaultOptions, 1},ault |
| 461 |
{"VS16 U+FE0F alone", '\ufe0f', defaultOptions, 0}, |
| 462 |
{"VS15 U+FE0E alone", '\ufe0e', defaultOptions, 0}, |
| 463 |
{"keycap U+20E3 alone", '\u20e3', defaultOptions, 0}, |
| 464 |
} |
| 465 |
|
| 466 |
for _, tt := range tests { |
| 467 |
t.Run(tt.name, func(t *testing.T) { |
| 468 |
result := tt.options.Rune(tt.input) |
| 469 |
if result != tt.expected { |
| 470 |
t.Errorf("RuneWidth(%q, %v) = %d, want %d", |
| 471 |
tt.input, tt.options, result, tt.expected) |
| 472 |
} |
| 473 |
}) |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
func TestEmojiPresentation(t *testing.T) { |
| 480 |
tests := []struct { |
| 481 |
name string |
| 482 |
input string |
| 483 |
wantDefault int |
| 484 |
wantWithVS16 int |
| 485 |
wantWithVS15 int |
| 486 |
description string |
| 487 |
}{ |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
{ |
| 492 |
name: "Watch (EP=Yes, EmojiPres=Yes)", |
| 493 |
input: "\u231A", |
| 494 |
wantDefault: 2, |
| 495 |
wantWithVS16: 2, |
| 496 |
wantWithVS15: 2, |
| 497 |
description: "⌚ U+231A has default emoji presentation", |
| 498 |
}, |
| 499 |
{ |
| 500 |
name: "Hourglass (EP=Yes, EmojiPres=Yes)", |
| 501 |
input: "\u231B", |
| 502 |
wantDefault: 2, |
| 503 |
wantWithVS16: 2, |
| 504 |
wantWithVS15: 2, |
| 505 |
description: "⌛ U+231B has default emoji presentation", |
| 506 |
}, |
| 507 |
{ |
| 508 |
name: "Fast-forward (EP=Yes, EmojiPres=Yes)", |
| 509 |
input: "\u23E9", |
| 510 |
wantDefault: 2, |
| 511 |
wantWithVS16: 2, |
| 512 |
wantWithVS15: 2, |
| 513 |
description: "⏩ U+23E9 has default emoji presentation", |
| 514 |
}, |
| 515 |
{ |
| 516 |
name: "Alarm Clock (EP=Yes, EmojiPres=Yes)", |
| 517 |
input: "\u23F0", |
| 518 |
wantDefault: 2, |
| 519 |
wantWithVS16: 2, |
| 520 |
wantWithVS15: 2, |
| 521 |
description: "⏰ U+23F0 has default emoji presentation", |
| 522 |
}, |
| 523 |
{ |
| 524 |
name: "Soccer Ball (EP=Yes, EmojiPres=Yes)", |
| 525 |
input: "\u26BD", |
| 526 |
wantDefault: 2, |
| 527 |
wantWithVS16: 2, |
| 528 |
wantWithVS15: 2, |
| 529 |
description: "⚽ U+26BD has default emoji presentation", |
| 530 |
}, |
| 531 |
{ |
| 532 |
name: "Anchor (EP=Yes, EmojiPres=Yes)", |
| 533 |
input: "\u2693", |
| 534 |
wantDefault: 2, |
| 535 |
wantWithVS16: 2, |
| 536 |
wantWithVS15: 2, |
| 537 |
description: "⚓ U+2693 has default emoji presentation", |
| 538 |
}, |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
{ |
| 543 |
name: "Star of David (EP=Yes, EmojiPres=No)", |
| 544 |
input: "\u2721", |
| 545 |
wantDefault: 1, |
| 546 |
wantWithVS16: 2, |
| 547 |
wantWithVS15: 1, |
| 548 |
description: "✡ U+2721 has default text presentation", |
| 549 |
}, |
| 550 |
{ |
| 551 |
name: "Hammer and Pick (EP=Yes, EmojiPres=No)", |
| 552 |
input: "\u2692", |
| 553 |
wantDefault: 1, |
| 554 |
wantWithVS16: 2, |
| 555 |
wantWithVS15: 1, |
| 556 |
description: "⚒ U+2692 has default text presentation", |
| 557 |
}, |
| 558 |
{ |
| 559 |
name: "Gear (EP=Yes, EmojiPres=No)", |
| 560 |
input: "\u2699", |
| 561 |
wantDefault: 1, |
| 562 |
wantWithVS16: 2, |
| 563 |
wantWithVS15: 1, |
| 564 |
description: "⚙ U+2699 has default text presentation", |
| 565 |
}, |
| 566 |
{ |
| 567 |
name: "Star and Crescent (EP=Yes, EmojiPres=No)", |
| 568 |
input: "\u262A", |
| 569 |
wantDefault: 1, |
| 570 |
wantWithVS16: 2, |
| 571 |
wantWithVS15: 1, |
| 572 |
description: "☪ U+262A has default text presentation", |
| 573 |
}, |
| 574 |
{ |
| 575 |
name: "Infinity (EP=Yes, EmojiPres=No)", |
| 576 |
input: "\u267E", |
| 577 |
wantDefault: 1, |
| 578 |
wantWithVS16: 2, |
| 579 |
wantWithVS15: 1, |
| 580 |
description: "♾ U+267E has default text presentation", |
| 581 |
}, |
| 582 |
{ |
| 583 |
name: "Recycling Symbol (EP=Yes, EmojiPres=No)", |
| 584 |
input: "\u267B", |
| 585 |
wantDefault: 1, |
| 586 |
wantWithVS16: 2, |
| 587 |
wantWithVS15: 1, |
| 588 |
description: "♻ U+267B has default text presentation", |
| 589 |
}, |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
{ |
| 594 |
name: "Hash Sign (Emoji=Yes, EP=No)", |
| 595 |
input: "\u0023", |
| 596 |
wantDefault: 1, |
| 597 |
wantWithVS16: 2, |
| 598 |
wantWithVS15: 1, |
| 599 |
description: "# U+0023 has default text presentation", |
| 600 |
}, |
| 601 |
{ |
| 602 |
name: "Asterisk (Emoji=Yes, EP=No)", |
| 603 |
input: "\u002A", |
| 604 |
wantDefault: 1, |
| 605 |
wantWithVS16: 2, |
| 606 |
wantWithVS15: 1, |
| 607 |
description: "* U+002A has default text presentation", |
| 608 |
}, |
| 609 |
{ |
| 610 |
name: "Digit Zero (Emoji=Yes, EP=No)", |
| 611 |
input: "\u0030", |
| 612 |
wantDefault: 1, |
| 613 |
wantWithVS16: 2, |
| 614 |
wantWithVS15: 1, |
| 615 |
description: "0 U+0030 has default text presentation", |
| 616 |
}, |
| 617 |
} |
| 618 |
|
| 619 |
for _, tt := range tests { |
| 620 |
t.Run(tt.name, func(t *testing.T) { |
| 621 |
|
| 622 |
gotDefault := String(tt.input) |
| 623 |
if gotDefault != tt.wantDefault { |
| 624 |
t.Errorf("String(%q) default = %d, want %d (%s)", |
| 625 |
tt.input, gotDefault, tt.wantDefault, tt.description) |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
inputWithVS16 := tt.input + "\uFE0F" |
| 630 |
gotWithVS16 := String(inputWithVS16) |
| 631 |
if gotWithVS16 != tt.wantWithVS16 { |
| 632 |
t.Errorf("String(%q) with VS16 = %d, want %d (%s)", |
| 633 |
tt.input, gotWithVS16, tt.wantWithVS16, tt.description) |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
inputWithVS15 := tt.input + "\uFE0E" |
| 639 |
gotWithVS15 := String(inputWithVS15) |
| 640 |
if gotWithVS15 != tt.wantWithVS15 { |
| 641 |
t.Errorf("String(%q) with VS15 = %d, want %d (%s)", |
| 642 |
tt.input, gotWithVS15, tt.wantWithVS15, tt.description) |
| 643 |
} |
| 644 |
}) |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
func TestEmojiPresentationRune(t *testing.T) { |
| 650 |
tests := []struct { |
| 651 |
name string |
| 652 |
r rune |
| 653 |
want int |
| 654 |
desc string |
| 655 |
}{ |
| 656 |
|
| 657 |
{name: "Watch", r: '\u231A', want: 2, desc: "⌚ has default emoji presentation"}, |
| 658 |
{name: "Alarm Clock", r: '\u23F0', want: 2, desc: "⏰ has default emoji presentation"}, |
| 659 |
{name: "Soccer Ball", r: '\u26BD', want: 2, desc: "⚽ has default emoji presentation"}, |
| 660 |
|
| 661 |
|
| 662 |
{name: "Star of David", r: '\u2721', want: 1, desc: "✡ has default text presentation"}, |
| 663 |
{name: "Hammer and Pick", r: '\u2692', want: 1, desc: "⚒ has default text presentation"}, |
| 664 |
{name: "Gear", r: '\u2699', want: 1, desc: "⚙ has default text presentation"}, |
| 665 |
{name: "Infinity", r: '\u267E', want: 1, desc: "♾ has default text presentation"}, |
| 666 |
|
| 667 |
|
| 668 |
{name: "Hash Sign", r: '#', want: 1, desc: "# is regular ASCII"}, |
| 669 |
{name: "Asterisk", r: '*', want: 1, desc: "* is regular ASCII"}, |
| 670 |
{name: "Digit Zero", r: '0', want: 1, desc: "0 is regular ASCII"}, |
| 671 |
} |
| 672 |
|
| 673 |
for _, tt := range tests { |
| 674 |
t.Run(tt.name, func(t *testing.T) { |
| 675 |
got := Rune(tt.r) |
| 676 |
if got != tt.want { |
| 677 |
t.Errorf("Rune(%U) = %d, want %d (%s)", tt.r, got, tt.want, tt.desc) |
| 678 |
} |
| 679 |
}) |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
func TestComplexEmojiSequences(t *testing.T) { |
| 685 |
tests := []struct { |
| 686 |
name string |
| 687 |
input string |
| 688 |
want int |
| 689 |
desc string |
| 690 |
}{ |
| 691 |
{ |
| 692 |
name: "Keycap sequence #️⃣", |
| 693 |
input: "#\uFE0F\u20E3", |
| 694 |
want: 2, |
| 695 |
desc: "# + VS16 + combining enclosing keycap", |
| 696 |
}, |
| 697 |
{ |
| 698 |
name: "Keycap sequence 0️⃣", |
| 699 |
input: "0\uFE0F\u20E3", |
| 700 |
want: 2, |
| 701 |
desc: "0 + VS16 + combining enclosing keycap", |
| 702 |
}, |
| 703 |
{ |
| 704 |
name: "Flag sequence 🇺🇸 (Regional Indicator pair)", |
| 705 |
input: "\U0001F1FA\U0001F1F8", |
| 706 |
want: 2, |
| 707 |
desc: "US flag (RI pair)", |
| 708 |
}, |
| 709 |
{ |
| 710 |
name: "Single Regional Indicator 🇺", |
| 711 |
input: "\U0001F1FA", |
| 712 |
want: 2, |
| 713 |
desc: "U (RI)", |
| 714 |
}, |
| 715 |
{ |
| 716 |
name: "ZWJ sequence 👨👩👧", |
| 717 |
input: "\U0001F468\u200D\U0001F469\u200D\U0001F467", |
| 718 |
want: 2, |
| 719 |
desc: "Family emoji (man + ZWJ + woman + ZWJ + girl)", |
| 720 |
}, |
| 721 |
{ |
| 722 |
name: "Skin tone modifier 👍🏽", |
| 723 |
input: "\U0001F44D\U0001F3FD", |
| 724 |
want: 2, |
| 725 |
desc: "Thumbs up with medium skin tone", |
| 726 |
}, |
| 727 |
} |
| 728 |
|
| 729 |
for _, tt := range tests { |
| 730 |
t.Run(tt.name, func(t *testing.T) { |
| 731 |
got := String(tt.input) |
| 732 |
if got != tt.want { |
| 733 |
t.Errorf("String(%q) = %d, want %d (%s)", |
| 734 |
tt.input, got, tt.want, tt.desc) |
| 735 |
} |
| 736 |
}) |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
func TestMixedContent(t *testing.T) { |
| 742 |
tests := []struct { |
| 743 |
name string |
| 744 |
input string |
| 745 |
want int |
| 746 |
desc string |
| 747 |
}{ |
| 748 |
{ |
| 749 |
name: "Text with emoji-presentation emoji", |
| 750 |
input: "Hi\u231AWorld", |
| 751 |
want: 9, 5) |
| 752 |
desc: "Text with watch emoji (emoji presentation)", |
| 753 |
}, |
| 754 |
{ |
| 755 |
name: "Text with text-presentation emoji", |
| 756 |
input: "Hi\u2721Go", |
| 757 |
want: 5, 2) |
| 758 |
desc: "Text with star of David (text presentation)", |
| 759 |
}, |
| 760 |
{ |
| 761 |
name: "Text with text-presentation emoji + VS16", |
| 762 |
input: "Hi\u2721\uFE0FGo", |
| 763 |
want: 6, (2) |
| 764 |
desc: "Text with star of David (forced emoji presentation)", |
| 765 |
}, |
| 766 |
{ |
| 767 |
name: "Multiple emojis", |
| 768 |
input: "⌚⚽⚓", |
| 769 |
want: 6, |
| 770 |
desc: "Watch, soccer ball, anchor", |
| 771 |
}, |
| 772 |
{ |
| 773 |
name: "Mixed presentation", |
| 774 |
input: "⌚⚙⚓", |
| 775 |
want: 5, ⚓(2) |
| 776 |
desc: "Watch (emoji), gear (text), anchor (emoji)", |
| 777 |
}, |
| 778 |
} |
| 779 |
|
| 780 |
for _, tt := range tests { |
| 781 |
t.Run(tt.name, func(t *testing.T) { |
| 782 |
got := String(tt.input) |
| 783 |
if got != tt.want { |
| 784 |
t.Errorf("String(%q) = %d, want %d (%s)", |
| 785 |
tt.input, got, tt.want, tt.desc) |
| 786 |
} |
| 787 |
}) |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
func TestTR51Conformance(t *testing.T) { |
| 793 |
t.Run("C1: Default Emoji Presentation", func(t *testing.T) { |
| 794 |
|
| 795 |
emojiPresentationChars := []rune{ |
| 796 |
'\u231A', ch |
| 797 |
'\u231B', ss |
| 798 |
'\u23F0', ck |
| 799 |
'\u26BD', ll |
| 800 |
'\u2693', or |
| 801 |
} |
| 802 |
|
| 803 |
for _, r := range emojiPresentationChars { |
| 804 |
got := Rune(r) |
| 805 |
if got != 2 { |
| 806 |
t.Errorf("Rune(%U) = %d, want 2 (should have default emoji presentation)", r, got) |
| 807 |
} |
| 808 |
} |
| 809 |
}) |
| 810 |
|
| 811 |
t.Run("C1: Default Text Presentation", func(t *testing.T) { |
| 812 |
|
| 813 |
textPresentationChars := []rune{ |
| 814 |
'\u2721', id |
| 815 |
'\u2692', ck |
| 816 |
'\u2699', ar |
| 817 |
'\u267E', ty |
| 818 |
'\u267B', ol |
| 819 |
} |
| 820 |
|
| 821 |
for _, r := range textPresentationChars { |
| 822 |
got := Rune(r) |
| 823 |
if got != 1 { |
| 824 |
t.Errorf("Rune(%U) = %d, want 1 (should have default text presentation)", r, got) |
| 825 |
} |
| 826 |
} |
| 827 |
}) |
| 828 |
|
| 829 |
t.Run("C2: VS15 is a no-op for width calculation", func(t *testing.T) { |
| 830 |
|
| 831 |
|
| 832 |
emojiWithVS15 := []struct { |
| 833 |
char string |
| 834 |
base string |
| 835 |
}{ |
| 836 |
{"\u231A\uFE0E", "\u231A"}, VS15 |
| 837 |
{"\u26BD\uFE0E", "\u26BD"}, VS15 |
| 838 |
{"\u2693\uFE0E", "\u2693"}, VS15 |
| 839 |
} |
| 840 |
|
| 841 |
for _, tc := range emojiWithVS15 { |
| 842 |
baseWidth := String(tc.base) |
| 843 |
vs15Width := String(tc.char) |
| 844 |
if vs15Width != baseWidth { |
| 845 |
t.Errorf("String(%q) with VS15 = %d, want %d (VS15 is a no-op, width should match base)", tc.char, vs15Width, baseWidth) |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
eastAsianWideWithVS15 := []struct { |
| 851 |
char string |
| 852 |
base string |
| 853 |
}{ |
| 854 |
{"中\uFE0E", "中VS15 |
| 855 |
{"日\uFE0E", "日VS15 |
| 856 |
} |
| 857 |
|
| 858 |
for _, tc := range eastAsianWideWithVS15 { |
| 859 |
baseWidth := String(tc.base) |
| 860 |
vs15Width := String(tc.char) |
| 861 |
if vs15Width != baseWidth { |
| 862 |
t.Errorf("String(%q) with VS15 = %d, want %d (VS15 is a no-op, width should match base)", tc.char, vs15Width, baseWidth) |
| 863 |
} |
| 864 |
} |
| 865 |
}) |
| 866 |
|
| 867 |
t.Run("C3: VS16 forces emoji presentation", func(t *testing.T) { |
| 868 |
|
| 869 |
textWithVS16 := []string{ |
| 870 |
"\u2721\uFE0F", VS16 |
| 871 |
"\u2692\uFE0F", VS16 |
| 872 |
"\u2699\uFE0F", VS16 |
| 873 |
"\u0023\uFE0F", 16 |
| 874 |
} |
| 875 |
|
| 876 |
for _, s := range textWithVS16 { |
| 877 |
got := String(s) |
| 878 |
if got != 2 { |
| 879 |
t.Errorf("String(%q) with VS16 = %d, want 2 (VS16 should force emoji presentation)", s, got) |
| 880 |
} |
| 881 |
} |
| 882 |
}) |
| 883 |
|
| 884 |
t.Run("ED-16: ZWJ Sequences treated as single grapheme", func(t *testing.T) { |
| 885 |
|
| 886 |
|
| 887 |
tests := []struct { |
| 888 |
name string |
| 889 |
sequence string |
| 890 |
want int |
| 891 |
desc string |
| 892 |
}{ |
| 893 |
{ |
| 894 |
name: "Family", |
| 895 |
sequence: "\U0001F468\u200D\U0001F469\u200D\U0001F467\u200D\U0001F466", 👧👦 |
| 896 |
want: 2, |
| 897 |
desc: "Family: man, woman, girl, boy (4 people + 3 ZWJ)", |
| 898 |
}, |
| 899 |
{ |
| 900 |
name: "Kiss", |
| 901 |
sequence: "\U0001F469\u200D\u2764\uFE0F\u200D\U0001F48B\u200D\U0001F468", ️💋👨 |
| 902 |
want: 2, |
| 903 |
desc: "Kiss: woman, heart, kiss mark, man", |
| 904 |
}, |
| 905 |
{ |
| 906 |
name: "Couple with heart", |
| 907 |
sequence: "\U0001F469\u200D\u2764\uFE0F\u200D\U0001F468", ❤️👨 |
| 908 |
want: 2, |
| 909 |
desc: "Couple with heart: woman, heart, man", |
| 910 |
}, |
| 911 |
{ |
| 912 |
name: "Woman technologist", |
| 913 |
sequence: "\U0001F469\u200D\U0001F4BB", 💻 |
| 914 |
want: 2, |
| 915 |
desc: "Woman technologist: woman, ZWJ, laptop", |
| 916 |
}, |
| 917 |
{ |
| 918 |
name: "Rainbow flag", |
| 919 |
sequence: "\U0001F3F4\u200D\U0001F308", 🌈 |
| 920 |
want: 2, |
| 921 |
desc: "Rainbow flag: black flag, ZWJ, rainbow", |
| 922 |
}, |
| 923 |
} |
| 924 |
|
| 925 |
for _, tt := range tests { |
| 926 |
t.Run(tt.name, func(t *testing.T) { |
| 927 |
got := String(tt.sequence) |
| 928 |
if got != tt.want { |
| 929 |
t.Errorf("String(%q) = %d, want %d (%s)", |
| 930 |
tt.sequence, got, tt.want, tt.desc) |
| 931 |
|
| 932 |
t.Logf(" Sequence: %+q", tt.sequence) |
| 933 |
t.Logf(" Expected: single grapheme cluster of width %d", tt.want) |
| 934 |
t.Logf(" Got: %d (if > 2, grapheme tokenizer may not be recognizing ZWJ sequence)", got) |
| 935 |
} |
| 936 |
}) |
| 937 |
} |
| 938 |
}) |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
t.Run("ED-13: Emoji Modifier Sequences", func(t *testing.T) { |
| 944 |
tests := []struct { |
| 945 |
sequence string |
| 946 |
want int |
| 947 |
desc string |
| 948 |
}{ |
| 949 |
{"👍🏻", 2, "thumbs up + light skin tone"}, |
| 950 |
{"👍🏼", 2, "thumbs up + medium-light skin tone"}, |
| 951 |
{"👍🏽", 2, "thumbs up + medium skin tone"}, |
| 952 |
{"👍🏾", 2, "thumbs up + medium-dark skin tone"}, |
| 953 |
{"👍🏿", 2, "thumbs up + dark skin tone"}, |
| 954 |
{"👋🏻", 2, "waving hand + light skin tone"}, |
| 955 |
{"🧑🏽", 2, "person + medium skin tone"}, |
| 956 |
{"👶🏿", 2, "baby + dark skin tone"}, |
| 957 |
{"👩🏼", 2, "woman + medium-light skin tone"}, |
| 958 |
} |
| 959 |
|
| 960 |
for _, tt := range tests { |
| 961 |
t.Run(tt.desc, func(t *testing.T) { |
| 962 |
got := String(tt.sequence) |
| 963 |
if got != tt.want { |
| 964 |
t.Errorf("String(%q) = %d, want %d (%s)", |
| 965 |
tt.sequence, got, tt.want, tt.desc) |
| 966 |
t.Logf(" Sequence: %+q", tt.sequence) |
| 967 |
t.Logf(" Expected: single grapheme cluster of width %d", tt.want) |
| 968 |
t.Logf(" Got: %d (if > 2, grapheme tokenizer may not be recognizing modifier sequence)", got) |
| 969 |
} |
| 970 |
}) |
| 971 |
} |
| 972 |
}) |
| 973 |
} |
| 974 |
|
| 975 |
func TestStringGraphemes(t *testing.T) { |
| 976 |
tests := []struct { |
| 977 |
name string |
| 978 |
input string |
| 979 |
options Options |
| 980 |
}{ |
| 981 |
{"empty string", "", defaultOptions}, |
| 982 |
{"single ASCII", "a", defaultOptions}, |
| 983 |
{"multiple ASCII", "hello", defaultOptions}, |
| 984 |
{"ASCII with spaces", "hello world", defaultOptions}, |
| 985 |
{"ASCII with newline", "hello\nworld", defaultOptions}, |
| 986 |
{"CJK ideograph", "中", defaultOptions}, |
| 987 |
{"CJK with ASCII", "hello中", defaultOptions}, |
| 988 |
{"ambiguous character", "★", defaultOptions}, |
| 989 |
{"ambiguous character EAW", "★", eawOptions}, |
| 990 |
{"emoji", "😀", defaultOptions}, |
| 991 |
{"flag US", "🇺🇸", defaultOptions}, |
| 992 |
{"text with flags", "Go 🇺🇸🚀", defaultOptions}, |
| 993 |
{"keycap 1️⃣", "1️⃣", defaultOptions}, |
| 994 |
{"mixed content", "Hi⌚⚙⚓", defaultOptions}, |
| 995 |
} |
| 996 |
|
| 997 |
for _, tt := range tests { |
| 998 |
t.Run(tt.name, func(t *testing.T) { |
| 999 |
|
| 1000 |
expected := tt.options.String(tt.input) |
| 1001 |
|
| 1002 |
|
| 1003 |
iter := tt.options.StringGraphemes(tt.input) |
| 1004 |
got := 0 |
| 1005 |
for iter.Next() { |
| 1006 |
got += iter.Width() |
| 1007 |
} |
| 1008 |
|
| 1009 |
if got != expected { |
| 1010 |
t.Errorf("StringGraphemes(%q) sum = %d, want %d (from String)", |
| 1011 |
tt.input, got, expected) |
| 1012 |
} |
| 1013 |
}) |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
func TestBytesGraphemes(t *testing.T) { |
| 1018 |
tests := []struct { |
| 1019 |
name string |
| 1020 |
input []byte |
| 1021 |
options Options |
| 1022 |
}{ |
| 1023 |
{"empty bytes", []byte(""), defaultOptions}, |
| 1024 |
{"single ASCII", []byte("a"), defaultOptions}, |
| 1025 |
{"multiple ASCII", []byte("hello"), defaultOptions}, |
| 1026 |
{"ASCII with spaces", []byte("hello world"), defaultOptions}, |
| 1027 |
{"ASCII with newline", []byte("hello\nworld"), defaultOptions}, |
| 1028 |
{"CJK ideograph", []byte("中"), defaultOptions}, |
| 1029 |
{"CJK with ASCII", []byte("hello中"), defaultOptions}, |
| 1030 |
{"ambiguous character", []byte("★"), defaultOptions}, |
| 1031 |
{"ambiguous character EAW", []byte("★"), eawOptions}, |
| 1032 |
{"emoji", []byte("😀"), defaultOptions}, |
| 1033 |
{"flag US", []byte("🇺🇸"), defaultOptions}, |
| 1034 |
{"text with flags", []byte("Go 🇺🇸🚀"), defaultOptions}, |
| 1035 |
{"keycap 1️⃣", []byte("1️⃣"), defaultOptions}, |
| 1036 |
{"mixed content", []byte("Hi⌚⚙⚓"), defaultOptions}, |
| 1037 |
} |
| 1038 |
|
| 1039 |
for _, tt := range tests { |
| 1040 |
t.Run(tt.name, func(t *testing.T) { |
| 1041 |
|
| 1042 |
expected := tt.options.Bytes(tt.input) |
| 1043 |
|
| 1044 |
|
| 1045 |
iter := tt.options.BytesGraphemes(tt.input) |
| 1046 |
got := 0 |
| 1047 |
for iter.Next() { |
| 1048 |
got += iter.Width() |
| 1049 |
} |
| 1050 |
|
| 1051 |
if got != expected { |
| 1052 |
t.Errorf("BytesGraphemes(%q) sum = %d, want %d (from Bytes)", |
| 1053 |
tt.input, got, expected) |
| 1054 |
} |
| 1055 |
}) |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
func TestGraphemesControlSequences(t *testing.T) { |
| 1060 |
tests := []struct { |
| 1061 |
name string |
| 1062 |
input string |
| 1063 |
options Options |
| 1064 |
}{ |
| 1065 |
|
| 1066 |
{"ControlSequences ANSI wrapped", "\x1b[31mhello\x1b[0m", controlSequences}, |
| 1067 |
{"ControlSequences ANSI only", "\x1b[0m", controlSequences}, |
| 1068 |
{"ControlSequences plain text", "hi", controlSequences}, |
| 1069 |
{"ControlSequences ANSI mid", "a\x1b[31mb\x1b[0mc", controlSequences}, |
| 1070 |
|
| 1071 |
{"default ANSI wrapped", "\x1b[31mhello\x1b[0m", defaultOptions}, |
| 1072 |
{"default plain", "hello", defaultOptions}, |
| 1073 |
|
| 1074 |
{"8-bit C1 CSI wrapped", "\x9B31mhello\x9B0m", controlSequences8Bit}, |
| 1075 |
{"8-bit C1 CSI only", "\x9B0m", controlSequences8Bit}, |
| 1076 |
{"8-bit plain text", "hi", controlSequences8Bit}, |
| 1077 |
{"8-bit C1 CSI mid", "a\x9B31mb\x9B0mc", controlSequences8Bit}, |
| 1078 |
|
| 1079 |
{"both: mixed", "\x1b[31mhello\x9B0m", controlSequencesBoth}, |
| 1080 |
{"both: 7-bit only input", "\x1b[31mhi\x1b[0m", controlSequencesBoth}, |
| 1081 |
{"both: 8-bit only input", "\x9B31mhi\x9B0m", controlSequencesBoth}, |
| 1082 |
ly |
| 1083 |
{"7-bit on, 8-bit input", "\x9B31mhello\x9B0m", controlSequences}, |
| 1084 |
|
| 1085 |
{"8-bit on, 7-bit input", "\x1b[31mhello\x1b[0m", controlSequences8Bit}, |
| 1086 |
} |
| 1087 |
|
| 1088 |
for _, tt := range tests { |
| 1089 |
t.Run(tt.name, func(t *testing.T) { |
| 1090 |
|
| 1091 |
expected := tt.options.String(tt.input) |
| 1092 |
iter := tt.options.StringGraphemes(tt.input) |
| 1093 |
got := 0 |
| 1094 |
for iter.Next() { |
| 1095 |
got += iter.Width() |
| 1096 |
} |
| 1097 |
if got != expected { |
| 1098 |
t.Errorf("StringGraphemes(%q) sum Width() = %d, want %d (String)", |
| 1099 |
tt.input, got, expected) |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
b := []byte(tt.input) |
| 1104 |
expectedBytes := tt.options.Bytes(b) |
| 1105 |
iterBytes := tt.options.BytesGraphemes(b) |
| 1106 |
gotBytes := 0 |
| 1107 |
for iterBytes.Next() { |
| 1108 |
gotBytes += iterBytes.Width() |
| 1109 |
} |
| 1110 |
if gotBytes != expectedBytes { |
| 1111 |
t.Errorf("BytesGraphemes(%q) sum Width() = %d, want %d (Bytes)", |
| 1112 |
b, gotBytes, expectedBytes) |
| 1113 |
} |
| 1114 |
}) |
| 1115 |
} |
| 1116 |
} |
| 1117 |
|
| 1118 |
func TestAsciiWidth(t *testing.T) { |
| 1119 |
tests := []struct { |
| 1120 |
name string |
| 1121 |
b byte |
| 1122 |
expected int |
| 1123 |
desc string |
| 1124 |
}{ |
| 1125 |
|
| 1126 |
{"null", 0x00, 0, "NULL character"}, |
| 1127 |
{"bell", 0x07, 0, "BEL (bell)"}, |
| 1128 |
{"backspace", 0x08, 0, "BS (backspace)"}, |
| 1129 |
{"tab", 0x09, 0, "TAB"}, |
| 1130 |
{"newline", 0x0A, 0, "LF (newline)"}, |
| 1131 |
{"carriage return", 0x0D, 0, "CR (carriage return)"}, |
| 1132 |
{"escape", 0x1B, 0, "ESC (escape)"}, |
| 1133 |
{"last control", 0x1F, 0, "Last control character"}, |
| 1134 |
|
| 1135 |
|
| 1136 |
{"space", 0x20, 1, "Space (first printable)"}, |
| 1137 |
{"exclamation", 0x21, 1, "!"}, |
| 1138 |
{"zero", 0x30, 1, "0"}, |
| 1139 |
{"nine", 0x39, 1, "9"}, |
| 1140 |
{"A", 0x41, 1, "A"}, |
| 1141 |
{"Z", 0x5A, 1, "Z"}, |
| 1142 |
{"a", 0x61, 1, "a"}, |
| 1143 |
{"z", 0x7A, 1, "z"}, |
| 1144 |
{"tilde", 0x7E, 1, "~ (last printable)"}, |
| 1145 |
|
| 1146 |
|
| 1147 |
{"delete", 0x7F, 0, "DEL (delete)"}, |
| 1148 |
|
| 1149 |
|
| 1150 |
{"0x80", 0x80, 1, "First byte >= 128"}, |
| 1151 |
{"0xFF", 0xFF, 1, "Last byte value"}, |
| 1152 |
} |
| 1153 |
|
| 1154 |
for _, tt := range tests { |
| 1155 |
t.Run(tt.name, func(t *testing.T) { |
| 1156 |
got := asciiWidth(tt.b) |
| 1157 |
if got != tt.expected { |
| 1158 |
t.Errorf("asciiWidth(0x%02X '%s') = %d, want %d (%s)", |
| 1159 |
tt.b, string(tt.b), got, tt.expected, tt.desc) |
| 1160 |
} |
| 1161 |
}) |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
func TestTruncateString(t *testing.T) { |
| 1166 |
tests := []struct { |
| 1167 |
name string |
| 1168 |
input string |
| 1169 |
maxWidth int |
| 1170 |
tail string |
| 1171 |
options Options |
| 1172 |
expected string |
| 1173 |
}{ |
| 1174 |
|
| 1175 |
{"empty string", "", 0, "", defaultOptions, ""}, |
| 1176 |
{"empty string with tail", "", 5, "...", defaultOptions, ""}, |
| 1177 |
{"empty string large maxWidth", "", 100, "...", defaultOptions, ""}, |
| 1178 |
|
| 1179 |
|
| 1180 |
{"fits exactly", "hello", 5, "...", defaultOptions, "hello"}, |
| 1181 |
{"fits with room", "hi", 10, "...", defaultOptions, "hi"}, |
| 1182 |
{"single char fits", "a", 1, "...", defaultOptions, "a"}, |
| 1183 |
|
| 1184 |
|
| 1185 |
{"truncate ASCII", "hello world", 5, "...", defaultOptions, "he..."}, |
| 1186 |
{"truncate ASCII at start", "hello", 0, "...", defaultOptions, "..."}, |
| 1187 |
{"truncate ASCII single char", "hello", 1, "...", defaultOptions, "..."}, |
| 1188 |
{"truncate ASCII with empty tail", "hello world", 5, "", defaultOptions, "hello"}, |
| 1189 |
|
| 1190 |
|
| 1191 |
{"CJK fits", "中", 2, "...", defaultOptions, "中"}, |
| 1192 |
{"CJK truncate", "中", 1, "...", defaultOptions, "..."}, |
| 1193 |
{"CJK with ASCII", "hello中", 5, "...", defaultOptions, "he..."}, |
| 1194 |
{"CJK with ASCII fits", "hello中", 7, "...", defaultOptions, "hello中"}, |
| 1195 |
{"CJK with ASCII partial", "hello中", 6, "...", defaultOptions, "hel..."}, |
| 1196 |
{"multiple CJK", "中文", 2, "...", defaultOptions, "..."}, |
| 1197 |
{"multiple CJK fits", "中文", 4, "...", defaultOptions, "中文"}, |
| 1198 |
|
| 1199 |
|
| 1200 |
{"emoji fits", "😀", 2, "...", defaultOptions, "😀"}, |
| 1201 |
{"emoji truncate", "😀", 1, "...", defaultOptions, "..."}, |
| 1202 |
{"emoji with ASCII", "hello😀", 5, "...", defaultOptions, "he..."}, |
| 1203 |
{"emoji with ASCII fits", "hello😀", 7, "...", defaultOptions, "hello😀"}, |
| 1204 |
{"multiple emoji", "😀😁", 2, "...", defaultOptions, "..."}, |
| 1205 |
{"multiple emoji fits", "😀😁", 4, "...", defaultOptions, "😀😁"}, |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
{"with newline", "hello\nworld", 5, "...", defaultOptions, "he..."}, |
| 1210 |
{"with tab", "hello\tworld", 5, "...", defaultOptions, "he..."}, |
| 1211 |
{"newline at start", "\nhello", 5, "...", defaultOptions, "\nhello"}, |
| 1212 |
{"multiple newlines", "a\n\nb", 1, "...", defaultOptions, "..."}, |
| 1213 |
|
| 1214 |
|
| 1215 |
{"ASCII CJK emoji", "hi中😀", 2, "...", defaultOptions, "..."}, |
| 1216 |
{"ASCII CJK emoji fits", "hi中😀", 6, "...", defaultOptions, "hi中😀"}, |
| 1217 |
{"ASCII CJK emoji partial", "hi中😀", 4, "...", defaultOptions, "h..."}, |
| 1218 |
{"complex mixed", "Go 🇺🇸🚀", 3, "...", defaultOptions, "..."}, |
| 1219 |
{"complex mixed fits", "Go 🇺🇸🚀", 7, "...", defaultOptions, "Go 🇺🇸🚀"}, |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
{"ControlSequences plain no truncation", "hello", 5, "...", controlSequences, "hello"}, |
| 1226 |
{"ControlSequences ANSI wrapped no truncation", "\x1b[31mhello\x1b[0m", 8, "...", controlSequences, "\x1b[31mhello\x1b[0m"}, |
| 1227 |
{"ControlSequences ANSI wrapped truncate", "\x1b[31mhello\x1b[0m", 4, "...", controlSequences, "\x1b[31mh...\x1b[0m"}, |
| 1228 |
{"ControlSequences ANSI in middle truncate", "hello\x1b[31mworld", 5, "...", controlSequences, "he...\x1b[31m"}, |
| 1229 |
{"ControlSequences CJK truncate", "\x1b[31m中文\x1b[0m", 2, "...", controlSequences, "...\x1b[31m\x1b[0m"}, |
| 1230 |
{"ControlSequences CJK no truncation", "\x1b[31m中文\x1b[0m", 7, "...", controlSequences, "\x1b[31m中文\x1b[0m"}, |
| 1231 |
{"ControlSequences CJK one wide then tail", "\x1b[31m中文xx\x1b[0m", 5, "...", controlSequences, "\x1b[31m中...\x1b[0m"}, |
| 1232 |
|
| 1233 |
{"ControlSequences stacked SGR", "\x1b[31m\x1b[42mhello\x1b[0m", 4, "...", controlSequences, "\x1b[31m\x1b[42mh...\x1b[0m"}, |
| 1234 |
|
| 1235 |
{"ControlSequences mid-escape after cut", "\x1b[31mhello\x1b[42mworld\x1b[0m", 6, "...", controlSequences, "\x1b[31mhel...\x1b[42m\x1b[0m"}, |
| 1236 |
|
| 1237 |
{"ControlSequences no trailing escape", "\x1b[31mhello", 4, "...", controlSequences, "\x1b[31mh..."}, |
| 1238 |
|
| 1239 |
{"ControlSequences multi color", "a\x1b[31mb\x1b[32mc\x1b[33md\x1b[0m", 2, "...", controlSequences, "...\x1b[31m\x1b[32m\x1b[33m\x1b[0m"}, |
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
{"8-bit plain no truncation", "hello", 5, "...", controlSequences8Bit, "hello"}, |
| 1247 |
{"8-bit C1 CSI wrapped truncate", "\x9B31mhello\x9B0m", 8, "...", controlSequences8Bit, "\x9B31mh..."}, |
| 1248 |
{"8-bit C1 CSI wrapped truncate narrow", "\x9B31mhello\x9B0m", 4, "...", controlSequences8Bit, "\x9B..."}, |
| 1249 |
{"8-bit C1 CSI in middle truncate", "hello\x9B31mworld", 5, "...", controlSequences8Bit, "he..."}, |
| 1250 |
{"8-bit C1 CSI CJK truncate", "\x9B31m中文\x9B0m", 2, "...", controlSequences8Bit, "..."}, |
| 1251 |
{"8-bit C1 CSI no trailing escape", "\x9B31mhello", 4, "...", controlSequences8Bit, "\x9B..."}, |
| 1252 |
{"8-bit C1 stacked SGR", "\x9B31m\x9B42mhello\x9B0m", 4, "...", controlSequences8Bit, "\x9B..."}, |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
{"7-bit only ignores trailing C1", "hello\x9B0m", 5, "...", controlSequences, "he..."}, |
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
{"both: mixed trailing escapes", "\x1b[31mhello\x9B0m", 4, "...", controlSequencesBoth, "\x1b[31mh..."}, |
| 1262 |
{"both: 7-bit wrapped truncate", "\x1b[31mhello\x1b[0m", 4, "...", controlSequencesBoth, "\x1b[31mh...\x1b[0m"}, |
| 1263 |
{"both: 8-bit wrapped truncate", "\x9B31mhello\x9B0m", 4, "...", controlSequencesBoth, "\x9B..."}, |
| 1264 |
|
| 1265 |
|
| 1266 |
{"ambiguous EAW fits", "★", 2, "...", eawOptions, "★"}, |
| 1267 |
{"ambiguous EAW truncate", "★", 1, "...", eawOptions, "..."}, |
| 1268 |
{"ambiguous default fits", "★", 1, "...", defaultOptions, "★"}, |
| 1269 |
{"ambiguous mixed", "a★b", 2, "...", eawOptions, "..."}, |
| 1270 |
{"ambiguous mixed default", "a★b", 2, "...", defaultOptions, "..."}, |
| 1271 |
|
| 1272 |
|
| 1273 |
{"zero maxWidth", "hello", 0, "...", defaultOptions, "..."}, |
| 1274 |
{"very long string", "a very long string that will definitely be truncated", 10, "...", defaultOptions, "a very ..."}, |
| 1275 |
|
| 1276 |
h 4) |
| 1277 |
{"wide char boundary bug fix", "中cde", 4, "ab", defaultOptions, "中ab"}, |
| 1278 |
|
| 1279 |
|
| 1280 |
{"custom tail", "hello world", 5, "…", defaultOptions, "hell…"}, |
| 1281 |
{"long tail", "hello", 3, ">>>", defaultOptions, ">>>"}, |
| 1282 |
{"tail with wide char", "hello", 3, "中", defaultOptions, "h中"}, |
| 1283 |
{"tail with emoji", "hello", 3, "😀", defaultOptions, "h😀"}, |
| 1284 |
|
| 1285 |
|
| 1286 |
{"keycap sequence", "1️⃣2️⃣", 2, "...", defaultOptions, "..."}, |
| 1287 |
{"flag sequence", "🇺🇸🇯🇵", 2, "...", defaultOptions, "..."}, |
| 1288 |
{"ZWJ sequence", "👨👩👧", 2, "...", defaultOptions, "👨👩👧"}, |
| 1289 |
{"ZWJ sequence truncate", "👨👩👧👨👩👧", 2, "...", defaultOptions, "..."}, |
| 1290 |
} |
| 1291 |
|
| 1292 |
for _, tt := range tests { |
| 1293 |
t.Run(tt.name, func(t *testing.T) { |
| 1294 |
{ |
| 1295 |
got := tt.options.TruncateString(tt.input, tt.maxWidth, tt.tail) |
| 1296 |
if got != tt.expected { |
| 1297 |
t.Errorf("TruncateString(%q, %d, %q) with options %v = %q, want %q", |
| 1298 |
tt.input, tt.maxWidth, tt.tail, tt.options, got, tt.expected) |
| 1299 |
inputWidth := tt.options.String(tt.input) |
| 1300 |
gotWidth := tt.options.String(got) |
| 1301 |
t.Logf(" Input width: %d, Got width: %d, MaxWidth: %d", inputWidth, gotWidth, tt.maxWidth) |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
gotWidth := tt.options.String(got) |
| 1306 |
limit := tt.maxWidth |
| 1307 |
tailWidth := tt.options.String(tt.tail) |
| 1308 |
if tailWidth > limit { |
| 1309 |
limit = tailWidth |
| 1310 |
} |
| 1311 |
if gotWidth > limit { |
| 1312 |
t.Errorf("Result visible width (%d) exceeds max(maxWidth, tailWidth) (%d)", gotWidth, limit) |
| 1313 |
} |
| 1314 |
} |
| 1315 |
{ |
| 1316 |
input := []byte(tt.input) |
| 1317 |
tail := []byte(tt.tail) |
| 1318 |
expected := []byte(tt.expected) |
| 1319 |
got := tt.options.TruncateBytes(input, tt.maxWidth, tail) |
| 1320 |
if !bytes.Equal(got, expected) { |
| 1321 |
t.Errorf("TruncateBytes(%q, %d, %q) with options %v = %q, want %q", |
| 1322 |
input, tt.maxWidth, tail, tt.options, got, expected) |
| 1323 |
inputWidth := tt.options.Bytes(input) |
| 1324 |
gotWidth := tt.options.Bytes(got) |
| 1325 |
t.Logf(" Input width: %d, Got width: %d, MaxWidth: %d", inputWidth, gotWidth, tt.maxWidth) |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
gotWidth := tt.options.Bytes(got) |
| 1330 |
limit := tt.maxWidth |
| 1331 |
tailWidth := tt.options.Bytes(tail) |
| 1332 |
if tailWidth > limit { |
| 1333 |
limit = tailWidth |
| 1334 |
} |
| 1335 |
if gotWidth > limit { |
| 1336 |
t.Errorf("Result visible width (%d) exceeds max(maxWidth, tailWidth) (%d)", gotWidth, limit) |
| 1337 |
} |
| 1338 |
} |
| 1339 |
}) |
| 1340 |
} |
| 1341 |
} |
| 1342 |
|
| 1343 |
func TestTruncateBytesDoesNotMutateInput(t *testing.T) { |
| 1344 |
|
| 1345 |
original := []byte("hello world") |
| 1346 |
originalCopy := make([]byte, len(original)) |
| 1347 |
copy(originalCopy, original) |
| 1348 |
|
| 1349 |
tail := []byte("...") |
| 1350 |
_ = TruncateBytes(original, 5, tail) |
| 1351 |
|
| 1352 |
if !bytes.Equal(original, originalCopy) { |
| 1353 |
t.Errorf("TruncateBytes mutated the input slice: got %q, want %q", original, originalCopy) |
| 1354 |
} |
| 1355 |
} |
| 1356 |
|
| 1357 |
func TestPrintableASCIILength(t *testing.T) { |
| 1358 |
tests := []struct { |
| 1359 |
name string |
| 1360 |
input string |
| 1361 |
expected int |
| 1362 |
desc string |
| 1363 |
}{ |
| 1364 |
|
| 1365 |
|
| 1366 |
{"empty string", "", 0, "Empty string has 0 printable bytes"}, |
| 1367 |
{"single char", "a", 1, "Single printable byte"}, |
| 1368 |
{"single space", " ", 1, "Space is printable"}, |
| 1369 |
{"7 bytes", "1234567", 7, "7 printable bytes"}, |
| 1370 |
{"8 bytes", "12345678", 8, "8 printable bytes"}, |
| 1371 |
{"9 bytes", "123456789", 9, "9 printable bytes"}, |
| 1372 |
{"15 bytes", "123456789012345", 15, "15 printable bytes"}, |
| 1373 |
{"16 bytes", "1234567890123456", 16, "16 printable bytes"}, |
| 1374 |
{"17 bytes", "12345678901234567", 17, "17 printable bytes"}, |
| 1375 |
{"24 bytes", "123456789012345678901234", 24, "24 printable bytes"}, |
| 1376 |
{"long ASCII", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 62, "All 62 printable bytes"}, |
| 1377 |
{"all printable range", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", 95, "All 95 printable ASCII chars"}, |
| 1378 |
|
| 1379 |
|
| 1380 |
{"control at start", "\x00hello world", 0, "Control char at start"}, |
| 1381 |
{"DEL at start", "\x7Fhello world", 0, "DEL at start"}, |
| 1382 |
{"non-ASCII at start", "\x80hello world", 0, "Non-ASCII at start"}, |
| 1383 |
{"UTF-8 at start", "\xC2\xA0hello world", 0, "UTF-8 at start"}, |
| 1384 |
{"emoji at start", "\xF0\x9F\x98\x80hello123", 0, "Emoji at start"}, |
| 1385 |
|
| 1386 |
|
| 1387 |
{"control in middle", "hello\x00world123", 5, "Control at pos 5, returns 5"}, |
| 1388 |
{"DEL in middle", "hello\x7Fworld123", 5, "DEL at pos 5, returns 5"}, |
| 1389 |
{"control after 8", "12345678\x00world", 8, "Control at pos 8, returns 8"}, |
| 1390 |
{"DEL after 8", "12345678\x7Fworld", 8, "DEL at pos 8, returns 8"}, |
| 1391 |
{"control at pos 15", "123456789012345\x00", 15, "Control at pos 15, returns 15"}, |
| 1392 |
|
| 1393 |
|
| 1394 |
{"non-ASCII at end of 9", "12345678\x80", 7, "Backs off 1 before non-ASCII"}, |
| 1395 |
{"non-ASCII at end of 17", "1234567890123456\x80", 15, "Backs off 1 before non-ASCII"}, |
| 1396 |
{"combining after 16", "1234567890123456\u0301", 15, "Backs off 1 before combining mark"}, |
| 1397 |
{"non-ASCII after 1", "a\x80", 0, "Backs off 1 from 1, returns 0"}, |
| 1398 |
{"16 ASCII then emoji", "1234567890123456\xF0\x9F\x98\x80", 15, "Backs off 1 before emoji"}, |
| 1399 |
|
| 1400 |
|
| 1401 |
{"8 spaces", " ", 8, "Space (0x20) is first printable"}, |
| 1402 |
{"8 tildes", "~~~~~~~~", 8, "Tilde (0x7E) is last printable"}, |
| 1403 |
{"mixed printable", "Hello, World! 123", 17, "All 17 are printable"}, |
| 1404 |
|
| 1405 |
|
| 1406 |
{"control at pos 0", "\x00234567890", 0, "Control at position 0"}, |
| 1407 |
{"control at pos 3", "123\x00567890", 3, "Control at position 3"}, |
| 1408 |
{"control at pos 7", "1234567\x000", 7, "Control at position 7"}, |
| 1409 |
|
| 1410 |
|
| 1411 |
{"DEL at pos 0", "\x7F234567890", 0, "DEL at position 0"}, |
| 1412 |
{"DEL at pos 3", "123\x7F567890", 3, "DEL at position 3"}, |
| 1413 |
{"DEL at pos 7", "1234567\x7F0", 7, "DEL at position 7"}, |
| 1414 |
} |
| 1415 |
|
| 1416 |
for _, tt := range tests { |
| 1417 |
t.Run(tt.name, func(t *testing.T) { |
| 1418 |
got := printableASCIILength(tt.input) |
| 1419 |
if got != tt.expected { |
| 1420 |
t.Errorf("printableASCIILength(%q) = %d, want %d (%s)", |
| 1421 |
tt.input, got, tt.expected, tt.desc) |
| 1422 |
if len(tt.input) > 0 { |
| 1423 |
t.Logf(" String length: %d bytes", len(tt.input)) |
| 1424 |
for i, b := range []byte(tt.input) { |
| 1425 |
isPrintable := b >= 0x20 && b <= 0x7E |
| 1426 |
t.Logf(" [%d]: 0x%02X printable=%v", i, b, isPrintable) |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
}) |
| 1431 |
} |
| 1432 |
} |
| 1433 |
|
| 1434 |
func TestPrintableASCIILengthBytes(t *testing.T) { |
| 1435 |
tests := []struct { |
| 1436 |
name string |
| 1437 |
input []byte |
| 1438 |
expected int |
| 1439 |
desc string |
| 1440 |
}{ |
| 1441 |
|
| 1442 |
{"empty slice", []byte{}, 0, "Empty slice has 0 printable bytes"}, |
| 1443 |
{"single space", []byte{0x20}, 1, "Single space"}, |
| 1444 |
{"single char", []byte("a"), 1, "Single printable byte"}, |
| 1445 |
{"7 bytes", []byte("1234567"), 7, "7 printable bytes"}, |
| 1446 |
{"8 bytes all printable", []byte("12345678"), 8, "8 bytes all printable"}, |
| 1447 |
{"9 bytes all printable", []byte("123456789"), 9, "9 printable bytes"}, |
| 1448 |
{"15 bytes all printable", []byte("123456789012345"), 15, "15 printable bytes"}, |
| 1449 |
{"16 bytes all printable", []byte("1234567890123456"), 16, "16 printable bytes"}, |
| 1450 |
{"17 bytes all printable", []byte("12345678901234567"), 17, "17 printable bytes"}, |
| 1451 |
{"24 bytes all printable", []byte("123456789012345678901234"), 24, "24 printable bytes"}, |
| 1452 |
{"long all printable", []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"), 72, "72 printable bytes"}, |
| 1453 |
{"all printable range", []byte(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"), 95, "All 95 printable ASCII chars"}, |
| 1454 |
|
| 1455 |
|
| 1456 |
{"8 bytes all spaces", []byte(" "), 8, "8 spaces"}, |
| 1457 |
{"8 bytes all tildes", []byte("~~~~~~~~"), 8, "8 tildes"}, |
| 1458 |
{"8 bytes boundary low", []byte{0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20}, 8, "8 spaces (0x20)"}, |
| 1459 |
{"8 bytes boundary high", []byte{0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E}, 8, "8 tildes (0x7E)"}, |
| 1460 |
|
| 1461 |
|
| 1462 |
{"control at start", []byte("\x00hello world"), 0, "Control char at start"}, |
| 1463 |
{"DEL at start", []byte("\x7Fhello world"), 0, "DEL at start"}, |
| 1464 |
{"non-ASCII at start", []byte("\x80hello world"), 0, "Non-ASCII at start"}, |
| 1465 |
{"UTF-8 at start", []byte("\xC2\xA0hello world"), 0, "UTF-8 at start"}, |
| 1466 |
{"emoji at start", []byte("\xF0\x9F\x98\x80hello123"), 0, "Emoji at start"}, |
| 1467 |
|
| 1468 |
|
| 1469 |
{"control in middle", []byte("hel\x00o123"), 3, "Control at pos 3, returns 3"}, |
| 1470 |
{"DEL in middle", []byte("hel\x7Fo123"), 3, "DEL at pos 3, returns 3"}, |
| 1471 |
{"control at pos 8", []byte("12345678\x00world"), 8, "Control at pos 8, returns 8"}, |
| 1472 |
{"DEL at pos 8", []byte("12345678\x7Fworld"), 8, "DEL at pos 8, returns 8"}, |
| 1473 |
{"control at pos 15", []byte("123456789012345\x00"), 15, "Control at pos 15, returns 15"}, |
| 1474 |
|
| 1475 |
|
| 1476 |
{"non-ASCII at end of 9", []byte("12345678\x80"), 7, "Backs off 1 before non-ASCII"}, |
| 1477 |
{"non-ASCII at end of 17", []byte("1234567890123456\x80"), 15, "Backs off 1 before non-ASCII"}, |
| 1478 |
{"16 ASCII then emoji", []byte("1234567890123456\xF0\x9F\x98\x80"), 15, "Backs off 1 before emoji"}, |
| 1479 |
{"16 ASCII then combining acute", []byte("1234567890123456\u0301"), 15, "Backs off 1 before combining mark"}, |
| 1480 |
{"16 ASCII then combining grave", []byte("1234567890123456\u0300"), 15, "Backs off 1 before combining mark"}, |
| 1481 |
{"non-ASCII after 1", []byte("a\x80"), 0, "Backs off 1 from 1, returns 0"}, |
| 1482 |
|
| 1483 |
|
| 1484 |
{"control at pos 0", []byte("\x00234567890"), 0, "Control at position 0"}, |
| 1485 |
{"control at pos 3", []byte("123\x00567890"), 3, "Control at position 3"}, |
| 1486 |
{"control at pos 7", []byte("1234567\x000"), 7, "Control at position 7"}, |
| 1487 |
|
| 1488 |
|
| 1489 |
{"DEL at pos 0", []byte("\x7F234567890"), 0, "DEL at position 0"}, |
| 1490 |
{"DEL at pos 3", []byte("123\x7F567890"), 3, "DEL at position 3"}, |
| 1491 |
{"DEL at pos 7", []byte("1234567\x7F0"), 7, "DEL at position 7"}, |
| 1492 |
} |
| 1493 |
|
| 1494 |
for _, tt := range tests { |
| 1495 |
t.Run(tt.name, func(t *testing.T) { |
| 1496 |
got := printableASCIILength(tt.input) |
| 1497 |
if got != tt.expected { |
| 1498 |
t.Errorf("printableASCIILength(%v) = %d, want %d (%s)", |
| 1499 |
tt.input, got, tt.expected, tt.desc) |
| 1500 |
if len(tt.input) > 0 { |
| 1501 |
t.Logf(" Slice length: %d bytes", len(tt.input)) |
| 1502 |
for i, b := range tt.input { |
| 1503 |
isPrintable := b >= 0x20 && b <= 0x7E |
| 1504 |
t.Logf(" [%d]: 0x%02X printable=%v", i, b, isPrintable) |
| 1505 |
} |
| 1506 |
} |
| 1507 |
} |
| 1508 |
}) |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
func TestPrintableASCIIOptimization(t *testing.T) { |
| 1515 |
tests := []struct { |
| 1516 |
name string |
| 1517 |
input string |
| 1518 |
}{ |
| 1519 |
{"empty", ""}, |
| 1520 |
{"single char", "a"}, |
| 1521 |
{"short ASCII", "hello"}, |
| 1522 |
{"long ASCII", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}, |
| 1523 |
{"with spaces", "hello world"}, |
| 1524 |
{"with punctuation", "Hello, World!"}, |
| 1525 |
{"all printable range", " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"}, |
| 1526 |
{"exactly 8 bytes", "12345678"}, |
| 1527 |
{"exactly 16 bytes", "1234567890123456"}, |
| 1528 |
{"exactly 24 bytes", "123456789012345678901234"}, |
| 1529 |
{"7 bytes", "1234567"}, |
| 1530 |
{"9 bytes", "123456789"}, |
| 1531 |
{"15 bytes", "123456789012345"}, |
| 1532 |
{"17 bytes", "12345678901234567"}, |
| 1533 |
} |
| 1534 |
|
| 1535 |
for _, tt := range tests { |
| 1536 |
t.Run(tt.name, func(t *testing.T) { |
| 1537 |
|
| 1538 |
width := String(tt.input) |
| 1539 |
expected := len(tt.input) |
| 1540 |
if width != expected { |
| 1541 |
t.Errorf("String(%q) = %d, want %d", tt.input, width, expected) |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
widthBytes := Bytes([]byte(tt.input)) |
| 1546 |
if widthBytes != expected { |
| 1547 |
t.Errorf("Bytes(%q) = %d, want %d", tt.input, widthBytes, expected) |
| 1548 |
} |
| 1549 |
}) |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
func TestUnicode16IndicConjunctBreak(t *testing.T) { |
| 1562 |
tests := []struct { |
| 1563 |
name string |
| 1564 |
input string |
| 1565 |
expectedWidth int |
| 1566 |
expectedClusters int |
| 1567 |
description string |
| 1568 |
verifyClusterFormation bool |
| 1569 |
}{ |
| 1570 |
|
| 1571 |
{ |
| 1572 |
name: "Devanagari conjunct क्ष", |
| 1573 |
input: "क्a + ष (ṣa) |
| 1574 |
expectedWidth: 1, |
| 1575 |
expectedClusters: 1, |
| 1576 |
description: "Devanagari conjunct formed with virama (U+094D) - should be single cluster", |
| 1577 |
verifyClusterFormation: true, |
| 1578 |
}, |
| 1579 |
{ |
| 1580 |
name: "Devanagari conjunct त्र", |
| 1581 |
input: "त्+ र (ra) |
| 1582 |
expectedWidth: 1, |
| 1583 |
expectedClusters: 1, |
| 1584 |
description: "Devanagari conjunct with र (ra) as subscript - should be single cluster", |
| 1585 |
verifyClusterFormation: true, |
| 1586 |
}, |
| 1587 |
{ |
| 1588 |
name: "Devanagari conjunct ज्ञ", |
| 1589 |
input: "ज् + ञ (ña) |
| 1590 |
expectedWidth: 1, |
| 1591 |
expectedClusters: 1, |
| 1592 |
description: "Devanagari conjunct ज्ञ - should be single cluster", |
| 1593 |
verifyClusterFormation: true, |
| 1594 |
}, |
| 1595 |
{ |
| 1596 |
name: "Devanagari word with conjuncts", |
| 1597 |
input: "क्षtains conjunct क्ष |
| 1598 |
expectedWidth: 3, h |
| 1599 |
expectedClusters: 3, त्रि, य |
| 1600 |
description: "Devanagari word with multiple conjuncts", |
| 1601 |
}, |
| 1602 |
{ |
| 1603 |
name: "Devanagari with repha", |
| 1604 |
input: "रा contexts |
| 1605 |
expectedWidth: 2, h |
| 1606 |
expectedClusters: 2, ा, म |
| 1607 |
description: "Devanagari with potential repha formation", |
| 1608 |
}, |
| 1609 |
|
| 1610 |
|
| 1611 |
{ |
| 1612 |
name: "Bengali conjunct ক্ষ", |
| 1613 |
input: "ক্a + ষ (ṣa) |
| 1614 |
expectedWidth: 1, |
| 1615 |
expectedClusters: 1, |
| 1616 |
description: "Bengali conjunct ক্ষ - should be single cluster", |
| 1617 |
verifyClusterFormation: true, |
| 1618 |
}, |
| 1619 |
{ |
| 1620 |
name: "Bengali conjunct জ্ঞ", |
| 1621 |
input: "জ্ + ঞ (ña) |
| 1622 |
expectedWidth: 1, |
| 1623 |
expectedClusters: 1, |
| 1624 |
description: "Bengali conjunct জ্ঞ - should be single cluster", |
| 1625 |
verifyClusterFormation: true, |
| 1626 |
}, |
| 1627 |
{ |
| 1628 |
name: "Bengali word", |
| 1629 |
input: "বাṅlā - Bengali |
| 1630 |
expectedWidth: 2, h |
| 1631 |
expectedClusters: 2, ং, লা |
| 1632 |
description: "Bengali word with conjuncts", |
| 1633 |
}, |
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
{ |
| 1639 |
name: "Tamil with virama", |
| 1640 |
input: "க்a + ஷ (ṣa) |
| 1641 |
expectedWidth: 2, ், ஷ |
| 1642 |
expectedClusters: 2, |
| 1643 |
description: "Tamil conjunct with visible virama - may break into multiple clusters", |
| 1644 |
verifyClusterFormation: false, |
| 1645 |
}, |
| 1646 |
{ |
| 1647 |
name: "Tamil word", |
| 1648 |
input: "தமiḻ - Tamil |
| 1649 |
expectedWidth: 3, h |
| 1650 |
expectedClusters: 3, ி, ழ் |
| 1651 |
description: "Tamil word", |
| 1652 |
}, |
| 1653 |
|
| 1654 |
|
| 1655 |
{ |
| 1656 |
name: "Telugu conjunct క్ష", |
| 1657 |
input: "క్a + ష (ṣa) |
| 1658 |
expectedWidth: 1, |
| 1659 |
expectedClusters: 1, |
| 1660 |
description: "Telugu conjunct క్ష - should be single cluster", |
| 1661 |
verifyClusterFormation: true, |
| 1662 |
}, |
| 1663 |
{ |
| 1664 |
name: "Telugu word", |
| 1665 |
input: "తెల", // telugu |
| 1666 |
expectedWidth: 3, h |
| 1667 |
expectedClusters: 3, లు, గు |
| 1668 |
description: "Telugu word", |
| 1669 |
}, |
| 1670 |
|
| 1671 |
|
| 1672 |
{ |
| 1673 |
name: "Gujarati conjunct ક્ષ", |
| 1674 |
input: "ક્a + ષ (ṣa) |
| 1675 |
expectedWidth: 1, |
| 1676 |
expectedClusters: 1, |
| 1677 |
description: "Gujarati conjunct ક્ષ - should be single cluster", |
| 1678 |
verifyClusterFormation: true, |
| 1679 |
}, |
| 1680 |
{ |
| 1681 |
name: "Gujarati word", |
| 1682 |
input: "ગુજ", // gujarātī |
| 1683 |
expectedWidth: 4, h |
| 1684 |
expectedClusters: 4, રા, તી |
| 1685 |
description: "Gujarati word", |
| 1686 |
}, |
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
{ |
| 1691 |
name: "Kannada conjunct ಕ್ಷ", |
| 1692 |
input: "ಕ್a + ಷ (ṣa) |
| 1693 |
expectedWidth: 2, ್, ಷ |
| 1694 |
expectedClusters: 2, |
| 1695 |
description: "Kannada conjunct ಕ್ಷ - may break into multiple clusters", |
| 1696 |
verifyClusterFormation: false, |
| 1697 |
}, |
| 1698 |
{ |
| 1699 |
name: "Kannada word", |
| 1700 |
input: "ಕನ// kannada |
| 1701 |
expectedWidth: 4, h |
| 1702 |
expectedClusters: 4, , ನ, ಡ |
| 1703 |
description: "Kannada word", |
| 1704 |
}, |
| 1705 |
|
| 1706 |
|
| 1707 |
{ |
| 1708 |
name: "Malayalam conjunct ക്ഷ", |
| 1709 |
input: "ക്a + ഷ (ṣa) |
| 1710 |
expectedWidth: 1, |
| 1711 |
expectedClusters: 1, |
| 1712 |
description: "Malayalam conjunct ക്ഷ - should be single cluster", |
| 1713 |
verifyClusterFormation: true, |
| 1714 |
}, |
| 1715 |
{ |
| 1716 |
name: "Malayalam word", |
| 1717 |
input: "മലയ// malayāḷaṁ |
| 1718 |
expectedWidth: 4, h |
| 1719 |
expectedClusters: 4, യാ, ളം |
| 1720 |
description: "Malayalam word", |
| 1721 |
}, |
| 1722 |
|
| 1723 |
|
| 1724 |
{ |
| 1725 |
name: "Mixed Indic scripts", |
| 1726 |
input: "क्ष বাvanagari + Bengali + Tamil |
| 1727 |
expectedWidth: 8, |
| 1728 |
expectedClusters: 8, space, த, மி, ழ் |
| 1729 |
description: "Mixed Indic scripts with spaces", |
| 1730 |
}, |
| 1731 |
|
| 1732 |
|
| 1733 |
{ |
| 1734 |
name: "Devanagari with explicit virama", |
| 1735 |
input: "कter) |
| 1736 |
expectedWidth: 1, |
| 1737 |
expectedClusters: 1, |
| 1738 |
description: "Devanagari consonant with virama (no following consonant) - should be single cluster", |
| 1739 |
verifyClusterFormation: true, |
| 1740 |
}, |
| 1741 |
|
| 1742 |
|
| 1743 |
{ |
| 1744 |
name: "Devanagari with vowel sign", |
| 1745 |
input: "कowel sign) |
| 1746 |
expectedWidth: 1, |
| 1747 |
expectedClusters: 1, |
| 1748 |
description: "Devanagari with combining vowel sign - should be single cluster", |
| 1749 |
verifyClusterFormation: true, |
| 1750 |
}, |
| 1751 |
{ |
| 1752 |
name: "Bengali with vowel sign", |
| 1753 |
input: "কowel sign) |
| 1754 |
expectedWidth: 1, |
| 1755 |
expectedClusters: 1, |
| 1756 |
description: "Bengali with combining vowel sign - should be single cluster", |
| 1757 |
verifyClusterFormation: true, |
| 1758 |
}, |
| 1759 |
} |
| 1760 |
|
| 1761 |
for _, tt := range tests { |
| 1762 |
t.Run(tt.name, func(t *testing.T) { |
| 1763 |
|
| 1764 |
got := String(tt.input) |
| 1765 |
if got != tt.expectedWidth { |
| 1766 |
t.Errorf("String(%q) = %d, want %d (%s)", |
| 1767 |
tt.input, got, tt.expectedWidth, tt.description) |
| 1768 |
} |
| 1769 |
|
| 1770 |
|
| 1771 |
gotBytes := Bytes([]byte(tt.input)) |
| 1772 |
if gotBytes != tt.expectedWidth { |
| 1773 |
t.Errorf("Bytes(%q) = %d, want %d (%s)", |
| 1774 |
tt.input, gotBytes, tt.expectedWidth, tt.description) |
| 1775 |
} |
| 1776 |
|
| 1777 |
|
| 1778 |
iter := StringGraphemes(tt.input) |
| 1779 |
sumWidth := 0 |
| 1780 |
clusterCount := 0 |
| 1781 |
var clusters []string |
| 1782 |
for iter.Next() { |
| 1783 |
clusterCount++ |
| 1784 |
width := iter.Width() |
| 1785 |
sumWidth += width |
| 1786 |
clusters = append(clusters, iter.Value()) |
| 1787 |
} |
| 1788 |
|
| 1789 |
if clusterCount != tt.expectedClusters { |
| 1790 |
t.Errorf("Number of grapheme clusters = %d, want %d (%s)", |
| 1791 |
clusterCount, tt.expectedClusters, tt.description) |
| 1792 |
for i, cluster := range clusters { |
| 1793 |
t.Logf(" Cluster %d: %q (width %d)", i+1, cluster, String(cluster)) |
| 1794 |
} |
| 1795 |
} |
| 1796 |
|
| 1797 |
if sumWidth != tt.expectedWidth { |
| 1798 |
t.Errorf("Sum of grapheme cluster widths = %d, want %d", sumWidth, tt.expectedWidth) |
| 1799 |
} |
| 1800 |
|
| 1801 |
|
| 1802 |
if tt.verifyClusterFormation && clusterCount != 1 { |
| 1803 |
t.Errorf("Expected single grapheme cluster for conjunct, got %d clusters: %v", |
| 1804 |
clusterCount, clusters) |
| 1805 |
} |
| 1806 |
|
| 1807 |
|
| 1808 |
reconstructed := "" |
| 1809 |
iter2 := StringGraphemes(tt.input) |
| 1810 |
for iter2.Next() { |
| 1811 |
reconstructed += iter2.Value() |
| 1812 |
} |
| 1813 |
if reconstructed != tt.input { |
| 1814 |
t.Errorf("Reconstructed string from clusters = %q, want %q", reconstructed, tt.input) |
| 1815 |
} |
| 1816 |
}) |
| 1817 |
} |
| 1818 |
} |
| 1819 |
|
| 1820 |
func TestReproduceFuzzTruncate(t *testing.T) { |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
text := "00000000000\x1bX\x18" |
| 1825 |
options := []Options{ |
| 1826 |
{EastAsianWidth: false}, |
| 1827 |
{EastAsianWidth: true}, |
| 1828 |
{ControlSequences: true}, |
| 1829 |
{EastAsianWidth: true, ControlSequences: true}, |
| 1830 |
} |
| 1831 |
|
| 1832 |
for _, opt := range options { |
| 1833 |
ts := opt.TruncateString(text, 10, "...") |
| 1834 |
w := opt.String(ts) |
| 1835 |
if w > 10 { |
| 1836 |
t.Errorf("TruncateString() returned string longer than maxWidth for %q with opts %+v: %q (width %d)", text, opt, ts, w) |
| 1837 |
} |
| 1838 |
|
| 1839 |
tb := opt.TruncateBytes([]byte(text), 10, []byte("...")) |
| 1840 |
if !bytes.Equal(tb, []byte(ts)) { |
| 1841 |
t.Errorf("TruncateBytes() != TruncateString() for %q with opts %+v: %q != %q", text, opt, tb, ts) |
| 1842 |
} |
| 1843 |
} |
| 1844 |
} |
| 1845 |
|
| 1846 |
func TestTruncateIgnores8Bit(t *testing.T) { |
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
cases := []struct { |
| 1857 |
name string |
| 1858 |
text string |
| 1859 |
}{ |
| 1860 |
{ |
| 1861 |
|
| 1862 |
|
| 1863 |
. |
| 1864 |
name: "byte recombination", |
| 1865 |
text: "000000000000000000000\x9f\xcf\x1a\x90", |
| 1866 |
}, |
| 1867 |
{ |
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
name: "SOS terminator mismatch", |
| 1872 |
text: "00\x98\x1bX\x9c0000000000\x18", |
| 1873 |
}, |
| 1874 |
} |
| 1875 |
|
| 1876 |
options := []Options{ |
| 1877 |
{ControlSequences8Bit: true}, |
| 1878 |
{ControlSequences: true, ControlSequences8Bit: true}, |
| 1879 |
{EastAsianWidth: true, ControlSequences8Bit: true}, |
| 1880 |
} |
| 1881 |
|
| 1882 |
for _, tc := range cases { |
| 1883 |
for _, opt := range options { |
| 1884 |
|
| 1885 |
measureOpt := opt |
| 1886 |
measureOpt.ControlSequences8Bit = false |
| 1887 |
|
| 1888 |
ts := opt.TruncateString(tc.text, 10, "...") |
| 1889 |
w := measureOpt.String(ts) |
| 1890 |
if w > 10 { |
| 1891 |
t.Errorf("%s: TruncateString() width %d > 10 (measured without 8-bit) for %q with opts %+v: %q", |
| 1892 |
tc.name, w, tc.text, opt, ts) |
| 1893 |
} |
| 1894 |
|
| 1895 |
tb := opt.TruncateBytes([]byte(tc.text), 10, []byte("...")) |
| 1896 |
bw := measureOpt.Bytes(tb) |
| 1897 |
if bw > 10 { |
| 1898 |
t.Errorf("%s: TruncateBytes() width %d > 10 (measured without 8-bit) for %q with opts %+v: %q", |
| 1899 |
tc.name, bw, tc.text, opt, tb) |
| 1900 |
} |
| 1901 |
|
| 1902 |
if !bytes.Equal(tb, []byte(ts)) { |
| 1903 |
t.Errorf("%s: TruncateBytes() != TruncateString() for %q with opts %+v: %q != %q", |
| 1904 |
tc.name, tc.text, opt, tb, ts) |
| 1905 |
} |
| 1906 |
} |
| 1907 |
} |
| 1908 |
} |