-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidth_test.go
More file actions
1908 lines (1740 loc) · 71.4 KB
/
width_test.go
File metadata and controls
1908 lines (1740 loc) · 71.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package displaywidth
import (
"bytes"
"testing"
)
var defaultOptions = Options{}
var eawOptions = Options{EastAsianWidth: true}
func TestStringWidth(t *testing.T) {
tests := []struct {
name string
input string
options Options
expected int
}{
// Basic ASCII characters
{"empty string", "", defaultOptions, 0},
{"single ASCII", "a", defaultOptions, 1},
{"multiple ASCII", "hello", defaultOptions, 5},
{"ASCII with spaces", "hello world", defaultOptions, 11},
// Control characters (width 0)
{"newline", "\n", defaultOptions, 0},
{"tab", "\t", defaultOptions, 0},
{"carriage return", "\r", defaultOptions, 0},
{"backspace", "\b", defaultOptions, 0},
// Mixed content
{"ASCII with newline", "hello\nworld", defaultOptions, 10},
{"ASCII with tab", "hello\tworld", defaultOptions, 10},
// East Asian characters (should be in trie)
{"CJK ideograph", "中", defaultOptions, 2},
{"CJK with ASCII", "hello中", defaultOptions, 7},
// Ambiguous characters
{"ambiguous character", "★", defaultOptions, 1}, // Default narrow
{"ambiguous character EAW", "★", eawOptions, 2}, // East Asian wide
// Emoji
{"emoji", "😀", defaultOptions, 2}, // Default emoji width
{"checkered flag", "🏁", defaultOptions, 2}, // U+1F3C1 chequered flag is an emoji, width 2
// Invalid UTF-8 - the trie treats \xff as a valid character with default properties
{"invalid UTF-8", "\xff", defaultOptions, 1},
{"partial UTF-8", "\xc2", defaultOptions, 1},
// Variation selectors - VS16 (U+FE0F) requests emoji, VS15 (U+FE0E) is a no-op per Unicode TR51
{"☺ text default", "☺", defaultOptions, 1}, // U+263A has text presentation by default
{"☺️ emoji with VS16", "☺️", defaultOptions, 2}, // VS16 forces emoji presentation (width 2)
{"⌛ emoji default", "⌛", defaultOptions, 2}, // U+231B has emoji presentation by default
{"⌛︎ with VS15", "⌛︎", defaultOptions, 2}, // VS15 is a no-op, width remains 2
{"❤ text default", "❤", defaultOptions, 1}, // U+2764 has text presentation by default
{"❤️ emoji with VS16", "❤️", defaultOptions, 2}, // VS16 forces emoji presentation (width 2)
{"✂ text default", "✂", defaultOptions, 1}, // U+2702 has text presentation by default
{"✂️ emoji with VS16", "✂️", defaultOptions, 2}, // VS16 forces emoji presentation (width 2)
{"keycap 1️⃣", "1️⃣", defaultOptions, 2}, // Keycap sequence: 1 + VS16 + U+20E3 (always width 2)
{"keycap #️⃣", "#️⃣", defaultOptions, 2}, // Keycap sequence: # + VS16 + U+20E3 (always width 2)
// Flags (regional indicator pairs form a single grapheme, always width 2 per TR51)
{"flag US", "🇺🇸", defaultOptions, 2},
{"flag JP", "🇯🇵", defaultOptions, 2},
{"text with flags", "Go 🇺🇸🚀", defaultOptions, 3 + 2 + 2},
// Partial ASCII optimization tests (8+ byte ASCII runs)
{"ASCII 8 bytes then emoji", "12345678😀", defaultOptions, 8 + 2},
{"ASCII 16 bytes then CJK", "1234567890abcdef中", defaultOptions, 16 + 2},
{"emoji then ASCII 8 bytes", "😀12345678", defaultOptions, 2 + 8},
{"CJK then ASCII 16 bytes", "中1234567890abcdef", defaultOptions, 2 + 16},
{"ASCII-emoji-ASCII sandwich", "12345678😀abcdefgh", defaultOptions, 8 + 2 + 8},
{"short ASCII then emoji", "hello😀", defaultOptions, 5 + 2},
{"emoji-short ASCII-emoji", "😀abc😀", defaultOptions, 2 + 3 + 2},
{"long mixed", "Hello World! 你好世界 12345678 emoji: 🎉🎊", defaultOptions, 42}, // 13 + 9 + 9 + 7 + 4
// ASCII with embedded control characters
{"ASCII with null in middle", "hello\x00world", defaultOptions, 10}, // 5 + 0 + 5
{"ASCII with DEL in middle", "hello\x7Fworld", defaultOptions, 10}, // 5 + 0 + 5
{"ASCII with multiple controls", "a\x00b\tc\nd", defaultOptions, 4}, // 1 + 0 + 1 + 0 + 1 + 0 + 1
// Alternating short ASCII/non-ASCII sequences
{"alternating ASCII-CJK", "a中b文c", defaultOptions, 7}, // 1 + 2 + 1 + 2 + 1
{"alternating CJK-ASCII", "中a文b字c", defaultOptions, 9}, // 2 + 1 + 2 + 1 + 2 + 1
{"single char alternating", "a😀b🎉c", defaultOptions, 7}, // 1 + 2 + 1 + 2 + 1
{"rapid alternation", "aあbいcうd", defaultOptions, 10}, // 1 + 2 + 1 + 2 + 1 + 2 + 1
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.options.String(tt.input)
if result != tt.expected {
t.Errorf("StringWidth(%q, %v) = %d, want %d",
tt.input, tt.options, result, tt.expected)
}
b := []byte(tt.input)
result = tt.options.Bytes(b)
if result != tt.expected {
t.Errorf("BytesWidth(%q, %v) = %d, want %d",
b, tt.options, result, tt.expected)
}
})
}
}
var controlSequences = Options{ControlSequences: true}
var controlSequences8Bit = Options{ControlSequences8Bit: true}
var controlSequencesBoth = Options{ControlSequences: true, ControlSequences8Bit: true}
func TestAnsiEscapeSequences(t *testing.T) {
tests := []struct {
name string
input string
options Options
expected int
}{
// ANSI escape sequences (ECMA-48) should be zero width when parsed as single graphemes
{"SGR red", "\x1b[31m", controlSequences, 0},
{"SGR reset", "\x1b[0m", controlSequences, 0},
{"SGR bold", "\x1b[1m", controlSequences, 0},
{"SGR 256-color", "\x1b[38;5;196m", controlSequences, 0},
{"SGR truecolor", "\x1b[38;2;255;0;0m", controlSequences, 0},
{"cursor up", "\x1b[A", controlSequences, 0},
{"cursor position", "\x1b[10;20H", controlSequences, 0},
{"erase in display", "\x1b[2J", controlSequences, 0},
// ANSI escape sequences mixed with visible text
{"red hello", "\x1b[31mhello\x1b[0m", controlSequences, 5},
{"bold world", "\x1b[1mworld\x1b[0m", controlSequences, 5},
{"colored CJK", "\x1b[31m中文\x1b[0m", controlSequences, 4},
{"colored emoji", "\x1b[31m😀\x1b[0m", controlSequences, 2},
{"nested SGR", "\x1b[1m\x1b[31mhi\x1b[0m", controlSequences, 2},
// CR+LF as a multi-byte C0-led grapheme (zero width)
{"CRLF", "\r\n", controlSequences, 0},
{"text with CRLF", "hello\r\nworld", controlSequences, 10},
// Without ControlSequences, ESC is zero width but the rest of the sequence is visible
{"bare ESC default options", "\x1b", defaultOptions, 0},
{"SGR red default options", "\x1b[31m", defaultOptions, 4},
{"red hello default options", "\x1b[31mhello\x1b[0m", defaultOptions, 12},
// ControlSequences should not regress width for strings with no escape sequences
{"plain ASCII with option", "hello", controlSequences, 5},
{"plain ASCII spaces with option", "hello world", controlSequences, 11},
{"CJK with option", "中文", controlSequences, 4},
{"emoji with option", "😀", controlSequences, 2},
{"flag with option", "🇺🇸", controlSequences, 2},
{"mixed with option", "hello中文😀", controlSequences, 5 + 4 + 2},
{"ambiguous with option", "★", controlSequences, 1},
{"combining mark with option", "é", controlSequences, 1},
{"control chars with option", "\t\n", controlSequences, 0},
{"empty with option", "", controlSequences, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.options.String(tt.input)
if result != tt.expected {
t.Errorf("String(%q) = %d, want %d", tt.input, result, tt.expected)
}
result = tt.options.Bytes([]byte(tt.input))
if result != tt.expected {
t.Errorf("Bytes(%q) = %d, want %d", tt.input, result, tt.expected)
}
})
}
}
func TestAnsiEscapeSequences8Bit(t *testing.T) {
tests := []struct {
name string
input string
options Options
expected int
}{
// 8-bit C1 CSI sequences should be zero width
{"C1 CSI red", "\x9B31m", controlSequences8Bit, 0},
{"C1 CSI reset", "\x9B0m", controlSequences8Bit, 0},
{"C1 CSI bold", "\x9B1m", controlSequences8Bit, 0},
{"C1 CSI multi-param", "\x9B1;2;3m", controlSequences8Bit, 0},
{"C1 CSI cursor up", "\x9BA", controlSequences8Bit, 0},
// 8-bit C1 OSC/DCS/SOS/APC with C1 ST terminator
{"C1 OSC with ST", "\x9D0;Title\x9C", controlSequences8Bit, 0},
{"C1 OSC with BEL", "\x9D0;Title\x07", controlSequences8Bit, 0},
{"C1 DCS with ST", "\x90qpayload\x9C", controlSequences8Bit, 0},
{"C1 SOS with ST", "\x98hello\x9C", controlSequences8Bit, 0},
{"C1 APC with ST", "\x9Fdata\x9C", controlSequences8Bit, 0},
// Standalone C1 controls (single byte, no body)
{"C1 IND", "\x84", controlSequences8Bit, 0},
{"C1 NEL", "\x85", controlSequences8Bit, 0},
// 8-bit sequences mixed with visible text
{"C1 CSI red hello", "\x9B31mhello\x9B0m", controlSequences8Bit, 5},
{"C1 CSI colored CJK", "\x9B31m中文\x9B0m", controlSequences8Bit, 4},
{"C1 CSI colored emoji", "\x9B31m😀\x9B0m", controlSequences8Bit, 2},
{"C1 CSI nested", "\x9B1m\x9B31mhi\x9B0m", controlSequences8Bit, 2},
// Without ControlSequences8Bit, C1 bytes have width per asciiWidth (1 for >= 0x80)
{"C1 CSI default options", "\x9B31m", defaultOptions, 4},
// 8-bit option should not regress plain text
{"plain ASCII with 8-bit option", "hello", controlSequences8Bit, 5},
{"CJK with 8-bit option", "中文", controlSequences8Bit, 4},
{"emoji with 8-bit option", "😀", controlSequences8Bit, 2},
{"empty with 8-bit option", "", controlSequences8Bit, 0},
// Both options enabled
{"both: 7-bit SGR", "\x1b[31mhello\x1b[0m", controlSequencesBoth, 5},
{"both: 8-bit CSI", "\x9B31mhello\x9B0m", controlSequencesBoth, 5},
{"both: mixed 7 and 8-bit", "\x1b[31mhello\x9B0m", controlSequencesBoth, 5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.options.String(tt.input)
if result != tt.expected {
t.Errorf("String(%q) = %d, want %d", tt.input, result, tt.expected)
}
result = tt.options.Bytes([]byte(tt.input))
if result != tt.expected {
t.Errorf("Bytes(%q) = %d, want %d", tt.input, result, tt.expected)
}
})
}
}
// TestAnsiEscapeSequencesIndependence verifies that the 7-bit and 8-bit options
// are strictly independent: enabling one must NOT cause the other's sequences
// to be treated as escape sequences.
func TestAnsiEscapeSequencesIndependence(t *testing.T) {
tests := []struct {
name string
input string
options Options
expected int
desc string
}{
// 7-bit only: C1 bytes must NOT be treated as escape sequences.
// \x9B31m is 4 visible chars (0x9B has width 1, '3' '1' 'm' each width 1)
{
name: "7-bit on, 8-bit input C1 CSI",
input: "\x9B31m",
options: controlSequences,
expected: 4,
desc: "C1 CSI should not be recognized when only 7-bit is enabled",
},
{
name: "7-bit on, 8-bit input standalone C1",
input: "\x84",
options: controlSequences,
expected: 1,
desc: "Standalone C1 byte should have width 1 when only 7-bit is enabled",
},
{
name: "7-bit on, 8-bit input C1 with text",
input: "\x9B31mhello\x9B0m",
options: controlSequences,
expected: 4 + 5 + 3,
desc: "C1 CSI sequences should contribute visible width when only 7-bit is enabled",
},
// 8-bit only: 7-bit ESC sequences must NOT be treated as escape sequences.
// \x1b[31m is: ESC (width 0) + '[' (1) + '3' (1) + '1' (1) + 'm' (1) = 4
{
name: "8-bit on, 7-bit input SGR",
input: "\x1b[31m",
options: controlSequences8Bit,
expected: 4,
desc: "7-bit SGR should not be recognized when only 8-bit is enabled",
},
{
name: "8-bit on, 7-bit input SGR with text",
input: "\x1b[31mhello\x1b[0m",
options: controlSequences8Bit,
expected: 4 + 5 + 3,
desc: "7-bit SGR should contribute visible width when only 8-bit is enabled",
},
// Both enabled: both kinds should be zero-width
{
name: "both on, 7-bit SGR",
input: "\x1b[31m",
options: controlSequencesBoth,
expected: 0,
desc: "7-bit SGR should be zero-width when both are enabled",
},
{
name: "both on, 8-bit CSI",
input: "\x9B31m",
options: controlSequencesBoth,
expected: 0,
desc: "C1 CSI should be zero-width when both are enabled",
},
{
name: "both on, mixed sequences with text",
input: "\x1b[31mhello\x9B0m",
options: controlSequencesBoth,
expected: 5,
desc: "Mixed 7-bit and 8-bit sequences should both be zero-width",
},
// Neither enabled: both kinds contribute visible width
{
name: "neither, 7-bit SGR",
input: "\x1b[31m",
options: defaultOptions,
expected: 4,
desc: "7-bit SGR should contribute visible width when neither is enabled",
},
{
name: "neither, 8-bit CSI",
input: "\x9B31m",
options: defaultOptions,
expected: 4,
desc: "C1 CSI should contribute visible width when neither is enabled",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.options.String(tt.input)
if result != tt.expected {
t.Errorf("String(%q) = %d, want %d (%s)", tt.input, result, tt.expected, tt.desc)
}
result = tt.options.Bytes([]byte(tt.input))
if result != tt.expected {
t.Errorf("Bytes(%q) = %d, want %d (%s)", tt.input, result, tt.expected, tt.desc)
}
})
}
}
func TestRuneWidth(t *testing.T) {
tests := []struct {
name string
input rune
options Options
expected int
}{
// Control characters (width 0)
{"null char", '\x00', defaultOptions, 0},
{"bell", '\x07', defaultOptions, 0},
{"backspace", '\x08', defaultOptions, 0},
{"tab", '\t', defaultOptions, 0},
{"newline", '\n', defaultOptions, 0},
{"carriage return", '\r', defaultOptions, 0},
{"escape", '\x1B', defaultOptions, 0},
{"delete", '\x7F', defaultOptions, 0},
// Combining marks - when tested standalone as runes, they have width 0
// (In actual strings with grapheme clusters, they combine and have width 0)
{"combining grave accent", '\u0300', defaultOptions, 0},
{"combining acute accent", '\u0301', defaultOptions, 0},
{"combining diaeresis", '\u0308', defaultOptions, 0},
{"combining tilde", '\u0303', defaultOptions, 0},
// Zero width characters
{"zero width space", '\u200B', defaultOptions, 0},
{"zero width non-joiner", '\u200C', defaultOptions, 0},
{"zero width joiner", '\u200D', defaultOptions, 0},
// ASCII printable (width 1)
{"space", ' ', defaultOptions, 1},
{"letter a", 'a', defaultOptions, 1},
{"letter Z", 'Z', defaultOptions, 1},
{"digit 0", '0', defaultOptions, 1},
{"digit 9", '9', defaultOptions, 1},
{"exclamation", '!', defaultOptions, 1},
{"at sign", '@', defaultOptions, 1},
{"tilde", '~', defaultOptions, 1},
// Latin extended (width 1)
{"latin e with acute", 'é', defaultOptions, 1},
{"latin n with tilde", 'ñ', defaultOptions, 1},
{"latin o with diaeresis", 'ö', defaultOptions, 1},
// East Asian Wide characters
{"CJK ideograph", '中', defaultOptions, 2},
{"CJK ideograph", '文', defaultOptions, 2},
{"hiragana a", 'あ', defaultOptions, 2},
{"katakana a", 'ア', defaultOptions, 2},
{"hangul syllable", '가', defaultOptions, 2},
{"hangul syllable", '한', defaultOptions, 2},
// Fullwidth characters
{"fullwidth A", 'A', defaultOptions, 2},
{"fullwidth Z", 'Z', defaultOptions, 2},
{"fullwidth 0", '0', defaultOptions, 2},
{"fullwidth 9", '9', defaultOptions, 2},
{"fullwidth exclamation", '!', defaultOptions, 2},
{"fullwidth space", ' ', defaultOptions, 2},
// Ambiguous characters - default narrow
{"black star default", '★', defaultOptions, 1},
{"degree sign default", '°', defaultOptions, 1},
{"plus-minus default", '±', defaultOptions, 1},
{"section sign default", '§', defaultOptions, 1},
{"copyright sign default", '©', defaultOptions, 1},
{"registered sign default", '®', defaultOptions, 1},
// Ambiguous characters - EastAsianWidth wide
{"black star EAW", '★', eawOptions, 2},
{"degree sign EAW", '°', eawOptions, 2},
{"plus-minus EAW", '±', eawOptions, 2},
{"section sign EAW", '§', eawOptions, 2},
{"copyright sign EAW", '©', eawOptions, 1}, // Not in ambiguous category
{"registered sign EAW", '®', eawOptions, 2},
// Emoji (width 2)
{"grinning face", '😀', defaultOptions, 2},
{"grinning face with smiling eyes", '😁', defaultOptions, 2},
{"smiling face with heart-eyes", '😍', defaultOptions, 2},
{"thinking face", '🤔', defaultOptions, 2},
{"rocket", '🚀', defaultOptions, 2},
{"party popper", '🎉', defaultOptions, 2},
{"fire", '🔥', defaultOptions, 2},
{"thumbs up", '👍', defaultOptions, 2},
{"red heart", '❤', defaultOptions, 1}, // Text presentation by default
{"checkered flag", '🏁', defaultOptions, 2}, // U+1F3C1 chequered flag
// Mathematical symbols
{"infinity", '∞', defaultOptions, 1},
{"summation", '∑', defaultOptions, 1},
{"integral", '∫', defaultOptions, 1},
{"square root", '√', defaultOptions, 1},
// Currency symbols
{"dollar", '$', defaultOptions, 1},
{"euro", '€', defaultOptions, 1},
{"pound", '£', defaultOptions, 1},
{"yen", '¥', defaultOptions, 1},
// Box drawing characters
{"box light horizontal", '─', defaultOptions, 1},
{"box light vertical", '│', defaultOptions, 1},
{"box light down and right", '┌', defaultOptions, 1},
// Special Unicode characters
{"bullet", '•', defaultOptions, 1},
{"ellipsis", '…', defaultOptions, 1},
{"em dash", '—', defaultOptions, 1},
{"left single quote", '\u2018', defaultOptions, 1},
{"right single quote", '\u2019', defaultOptions, 1},
// Test edge cases with options disabled
{"ambiguous EAW disabled", '★', defaultOptions, 1},
// Variation selectors (note: Rune() tests single runes without VS, not sequences)
{"☺ U+263A text default", '☺', defaultOptions, 1}, // Has text presentation by default
{"⌛ U+231B emoji default", '⌛', defaultOptions, 2}, // Has emoji presentation by default
{"❤ U+2764 text default", '❤', defaultOptions, 1}, // Has text presentation by default
{"✂ U+2702 text default", '✂', defaultOptions, 1}, // Has text presentation by default
{"VS16 U+FE0F alone", '\ufe0f', defaultOptions, 0}, // Variation selectors are zero-width by themselves
{"VS15 U+FE0E alone", '\ufe0e', defaultOptions, 0}, // Variation selectors are zero-width by themselves
{"keycap U+20E3 alone", '\u20e3', defaultOptions, 0}, // Combining enclosing keycap is zero-width alone
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.options.Rune(tt.input)
if result != tt.expected {
t.Errorf("RuneWidth(%q, %v) = %d, want %d",
tt.input, tt.options, result, tt.expected)
}
})
}
}
// TestEmojiPresentation verifies correct width behavior for characters with different
// Emoji_Presentation property values according to TR51 conformance
func TestEmojiPresentation(t *testing.T) {
tests := []struct {
name string
input string
wantDefault int
wantWithVS16 int
wantWithVS15 int
description string
}{
// Characters with Extended_Pictographic=Yes AND Emoji_Presentation=Yes
// Should have width 2 by default (emoji presentation)
// VS15 is a no-op per Unicode TR51 - it requests text presentation but doesn't change width
{
name: "Watch (EP=Yes, EmojiPres=Yes)",
input: "\u231A",
wantDefault: 2,
wantWithVS16: 2,
wantWithVS15: 2, // VS15 is a no-op, width remains 2
description: "⌚ U+231A has default emoji presentation",
},
{
name: "Hourglass (EP=Yes, EmojiPres=Yes)",
input: "\u231B",
wantDefault: 2,
wantWithVS16: 2,
wantWithVS15: 2, // VS15 is a no-op, width remains 2
description: "⌛ U+231B has default emoji presentation",
},
{
name: "Fast-forward (EP=Yes, EmojiPres=Yes)",
input: "\u23E9",
wantDefault: 2,
wantWithVS16: 2,
wantWithVS15: 2, // VS15 is a no-op, width remains 2
description: "⏩ U+23E9 has default emoji presentation",
},
{
name: "Alarm Clock (EP=Yes, EmojiPres=Yes)",
input: "\u23F0",
wantDefault: 2,
wantWithVS16: 2,
wantWithVS15: 2, // VS15 is a no-op, width remains 2
description: "⏰ U+23F0 has default emoji presentation",
},
{
name: "Soccer Ball (EP=Yes, EmojiPres=Yes)",
input: "\u26BD",
wantDefault: 2,
wantWithVS16: 2,
wantWithVS15: 2, // VS15 is a no-op, width remains 2
description: "⚽ U+26BD has default emoji presentation",
},
{
name: "Anchor (EP=Yes, EmojiPres=Yes)",
input: "\u2693",
wantDefault: 2,
wantWithVS16: 2,
wantWithVS15: 2, // VS15 is a no-op, width remains 2
description: "⚓ U+2693 has default emoji presentation",
},
// Characters with Extended_Pictographic=Yes BUT Emoji_Presentation=No
// Should have width 1 by default (text presentation)
{
name: "Star of David (EP=Yes, EmojiPres=No)",
input: "\u2721",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "✡ U+2721 has default text presentation",
},
{
name: "Hammer and Pick (EP=Yes, EmojiPres=No)",
input: "\u2692",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "⚒ U+2692 has default text presentation",
},
{
name: "Gear (EP=Yes, EmojiPres=No)",
input: "\u2699",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "⚙ U+2699 has default text presentation",
},
{
name: "Star and Crescent (EP=Yes, EmojiPres=No)",
input: "\u262A",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "☪ U+262A has default text presentation",
},
{
name: "Infinity (EP=Yes, EmojiPres=No)",
input: "\u267E",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "♾ U+267E has default text presentation",
},
{
name: "Recycling Symbol (EP=Yes, EmojiPres=No)",
input: "\u267B",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "♻ U+267B has default text presentation",
},
// Characters with Emoji=Yes but NOT Extended_Pictographic
// These are typically ASCII characters like # that can become emoji with VS16
{
name: "Hash Sign (Emoji=Yes, EP=No)",
input: "\u0023",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "# U+0023 has default text presentation",
},
{
name: "Asterisk (Emoji=Yes, EP=No)",
input: "\u002A",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "* U+002A has default text presentation",
},
{
name: "Digit Zero (Emoji=Yes, EP=No)",
input: "\u0030",
wantDefault: 1,
wantWithVS16: 2,
wantWithVS15: 1,
description: "0 U+0030 has default text presentation",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Test default width (no variation selector)
gotDefault := String(tt.input)
if gotDefault != tt.wantDefault {
t.Errorf("String(%q) default = %d, want %d (%s)",
tt.input, gotDefault, tt.wantDefault, tt.description)
}
// Test with VS16 (U+FE0F) for emoji presentation
inputWithVS16 := tt.input + "\uFE0F"
gotWithVS16 := String(inputWithVS16)
if gotWithVS16 != tt.wantWithVS16 {
t.Errorf("String(%q) with VS16 = %d, want %d (%s)",
tt.input, gotWithVS16, tt.wantWithVS16, tt.description)
}
// Test with VS15 (U+FE0E) - VS15 is a no-op per Unicode TR51
// It requests text presentation but does not affect width calculation
inputWithVS15 := tt.input + "\uFE0E"
gotWithVS15 := String(inputWithVS15)
if gotWithVS15 != tt.wantWithVS15 {
t.Errorf("String(%q) with VS15 = %d, want %d (%s)",
tt.input, gotWithVS15, tt.wantWithVS15, tt.description)
}
})
}
}
// TestEmojiPresentationRune tests the Rune() function specifically
func TestEmojiPresentationRune(t *testing.T) {
tests := []struct {
name string
r rune
want int
desc string
}{
// Emoji_Presentation=Yes
{name: "Watch", r: '\u231A', want: 2, desc: "⌚ has default emoji presentation"},
{name: "Alarm Clock", r: '\u23F0', want: 2, desc: "⏰ has default emoji presentation"},
{name: "Soccer Ball", r: '\u26BD', want: 2, desc: "⚽ has default emoji presentation"},
// Emoji_Presentation=No (but Extended_Pictographic=Yes)
{name: "Star of David", r: '\u2721', want: 1, desc: "✡ has default text presentation"},
{name: "Hammer and Pick", r: '\u2692', want: 1, desc: "⚒ has default text presentation"},
{name: "Gear", r: '\u2699', want: 1, desc: "⚙ has default text presentation"},
{name: "Infinity", r: '\u267E', want: 1, desc: "♾ has default text presentation"},
// Not Extended_Pictographic
{name: "Hash Sign", r: '#', want: 1, desc: "# is regular ASCII"},
{name: "Asterisk", r: '*', want: 1, desc: "* is regular ASCII"},
{name: "Digit Zero", r: '0', want: 1, desc: "0 is regular ASCII"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Rune(tt.r)
if got != tt.want {
t.Errorf("Rune(%U) = %d, want %d (%s)", tt.r, got, tt.want, tt.desc)
}
})
}
}
// TestComplexEmojiSequences tests width of complex emoji sequences
func TestComplexEmojiSequences(t *testing.T) {
tests := []struct {
name string
input string
want int
desc string
}{
{
name: "Keycap sequence #️⃣",
input: "#\uFE0F\u20E3",
want: 2,
desc: "# + VS16 + combining enclosing keycap",
},
{
name: "Keycap sequence 0️⃣",
input: "0\uFE0F\u20E3",
want: 2,
desc: "0 + VS16 + combining enclosing keycap",
},
{
name: "Flag sequence 🇺🇸 (Regional Indicator pair)",
input: "\U0001F1FA\U0001F1F8",
want: 2,
desc: "US flag (RI pair)",
},
{
name: "Single Regional Indicator 🇺",
input: "\U0001F1FA",
want: 2,
desc: "U (RI)",
},
{
name: "ZWJ sequence 👨👩👧",
input: "\U0001F468\u200D\U0001F469\u200D\U0001F467",
want: 2,
desc: "Family emoji (man + ZWJ + woman + ZWJ + girl)",
},
{
name: "Skin tone modifier 👍🏽",
input: "\U0001F44D\U0001F3FD",
want: 2,
desc: "Thumbs up with medium skin tone",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := String(tt.input)
if got != tt.want {
t.Errorf("String(%q) = %d, want %d (%s)",
tt.input, got, tt.want, tt.desc)
}
})
}
}
// TestMixedContent tests width of strings with mixed emoji and text
func TestMixedContent(t *testing.T) {
tests := []struct {
name string
input string
want int
desc string
}{
{
name: "Text with emoji-presentation emoji",
input: "Hi\u231AWorld",
want: 9, // "Hi" (2) + ⌚ (2) + "World" (5)
desc: "Text with watch emoji (emoji presentation)",
},
{
name: "Text with text-presentation emoji",
input: "Hi\u2721Go",
want: 5, // "Hi" (2) + ✡ (1) + "Go" (2)
desc: "Text with star of David (text presentation)",
},
{
name: "Text with text-presentation emoji + VS16",
input: "Hi\u2721\uFE0FGo",
want: 6, // "Hi" (2) + ✡️ (2) + "Go" (2)
desc: "Text with star of David (forced emoji presentation)",
},
{
name: "Multiple emojis",
input: "⌚⚽⚓",
want: 6, // All three have Emoji_Presentation=Yes
desc: "Watch, soccer ball, anchor",
},
{
name: "Mixed presentation",
input: "⌚⚙⚓",
want: 5, // ⌚(2) + ⚙(1) + ⚓(2)
desc: "Watch (emoji), gear (text), anchor (emoji)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := String(tt.input)
if got != tt.want {
t.Errorf("String(%q) = %d, want %d (%s)",
tt.input, got, tt.want, tt.desc)
}
})
}
}
// TestTR51Conformance verifies key TR51 conformance requirements
func TestTR51Conformance(t *testing.T) {
t.Run("C1: Default Emoji Presentation", func(t *testing.T) {
// Characters with Emoji_Presentation=Yes should display as emoji by default (width 2)
emojiPresentationChars := []rune{
'\u231A', // ⌚ watch
'\u231B', // ⌛ hourglass
'\u23F0', // ⏰ alarm clock
'\u26BD', // ⚽ soccer ball
'\u2693', // ⚓ anchor
}
for _, r := range emojiPresentationChars {
got := Rune(r)
if got != 2 {
t.Errorf("Rune(%U) = %d, want 2 (should have default emoji presentation)", r, got)
}
}
})
t.Run("C1: Default Text Presentation", func(t *testing.T) {
// Characters with Emoji_Presentation=No should display as text by default (width 1)
textPresentationChars := []rune{
'\u2721', // ✡ star of David
'\u2692', // ⚒ hammer and pick
'\u2699', // ⚙ gear
'\u267E', // ♾ infinity
'\u267B', // ♻ recycling symbol
}
for _, r := range textPresentationChars {
got := Rune(r)
if got != 1 {
t.Errorf("Rune(%U) = %d, want 1 (should have default text presentation)", r, got)
}
}
})
t.Run("C2: VS15 is a no-op for width calculation", func(t *testing.T) {
// VS15 (U+FE0E) requests text presentation but does not affect width per Unicode TR51.
// The width should be the same as the base character.
emojiWithVS15 := []struct {
char string
base string
}{
{"\u231A\uFE0E", "\u231A"}, // ⌚︎ watch with VS15
{"\u26BD\uFE0E", "\u26BD"}, // ⚽︎ soccer ball with VS15
{"\u2693\uFE0E", "\u2693"}, // ⚓︎ anchor with VS15
}
for _, tc := range emojiWithVS15 {
baseWidth := String(tc.base)
vs15Width := String(tc.char)
if vs15Width != baseWidth {
t.Errorf("String(%q) with VS15 = %d, want %d (VS15 is a no-op, width should match base)", tc.char, vs15Width, baseWidth)
}
}
// VS15 with East Asian Wide characters should preserve width 2 (no-op)
eastAsianWideWithVS15 := []struct {
char string
base string
}{
{"中\uFE0E", "中"}, // CJK ideograph with VS15
{"日\uFE0E", "日"}, // CJK ideograph with VS15
}
for _, tc := range eastAsianWideWithVS15 {
baseWidth := String(tc.base)
vs15Width := String(tc.char)
if vs15Width != baseWidth {
t.Errorf("String(%q) with VS15 = %d, want %d (VS15 is a no-op, width should match base)", tc.char, vs15Width, baseWidth)
}
}
})
t.Run("C3: VS16 forces emoji presentation", func(t *testing.T) {
// VS16 (U+FE0F) should force emoji presentation (width 2) even for text-presentation characters
textWithVS16 := []string{
"\u2721\uFE0F", // ✡️ star of David with VS16
"\u2692\uFE0F", // ⚒️ hammer and pick with VS16
"\u2699\uFE0F", // ⚙️ gear with VS16
"\u0023\uFE0F", // #️ hash with VS16
}
for _, s := range textWithVS16 {
got := String(s)
if got != 2 {
t.Errorf("String(%q) with VS16 = %d, want 2 (VS16 should force emoji presentation)", s, got)
}
}
})
t.Run("ED-16: ZWJ Sequences treated as single grapheme", func(t *testing.T) {
// ZWJ sequences should be treated as a single grapheme cluster by the grapheme tokenizer
// and should have width 2 (since they display as a single emoji image)
tests := []struct {
name string
sequence string
want int
desc string
}{
{
name: "Family",
sequence: "\U0001F468\u200D\U0001F469\u200D\U0001F467\u200D\U0001F466", // 👨👩👧👦
want: 2,
desc: "Family: man, woman, girl, boy (4 people + 3 ZWJ)",
},
{
name: "Kiss",
sequence: "\U0001F469\u200D\u2764\uFE0F\u200D\U0001F48B\u200D\U0001F468", // 👩❤️💋👨
want: 2,
desc: "Kiss: woman, heart, kiss mark, man",
},
{
name: "Couple with heart",
sequence: "\U0001F469\u200D\u2764\uFE0F\u200D\U0001F468", // 👩❤️👨
want: 2,
desc: "Couple with heart: woman, heart, man",
},
{
name: "Woman technologist",
sequence: "\U0001F469\u200D\U0001F4BB", // 👩💻
want: 2,
desc: "Woman technologist: woman, ZWJ, laptop",
},
{
name: "Rainbow flag",
sequence: "\U0001F3F4\u200D\U0001F308", // 🏴🌈
want: 2,
desc: "Rainbow flag: black flag, ZWJ, rainbow",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := String(tt.sequence)
if got != tt.want {
t.Errorf("String(%q) = %d, want %d (%s)",
tt.sequence, got, tt.want, tt.desc)
// Show the individual components for debugging
t.Logf(" Sequence: %+q", tt.sequence)
t.Logf(" Expected: single grapheme cluster of width %d", tt.want)
t.Logf(" Got: %d (if > 2, grapheme tokenizer may not be recognizing ZWJ sequence)", got)
}
})
}
})
// ED-13: Emoji Modifier Sequences
// Per TR51: emoji_modifier_sequence := emoji_modifier_base emoji_modifier
// These should be treated as single grapheme clusters with width 2
t.Run("ED-13: Emoji Modifier Sequences", func(t *testing.T) {
tests := []struct {
sequence string
want int
desc string
}{
{"👍🏻", 2, "thumbs up + light skin tone"},
{"👍🏼", 2, "thumbs up + medium-light skin tone"},
{"👍🏽", 2, "thumbs up + medium skin tone"},
{"👍🏾", 2, "thumbs up + medium-dark skin tone"},
{"👍🏿", 2, "thumbs up + dark skin tone"},
{"👋🏻", 2, "waving hand + light skin tone"},
{"🧑🏽", 2, "person + medium skin tone"},
{"👶🏿", 2, "baby + dark skin tone"},
{"👩🏼", 2, "woman + medium-light skin tone"},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
got := String(tt.sequence)
if got != tt.want {
t.Errorf("String(%q) = %d, want %d (%s)",
tt.sequence, got, tt.want, tt.desc)
t.Logf(" Sequence: %+q", tt.sequence)
t.Logf(" Expected: single grapheme cluster of width %d", tt.want)
t.Logf(" Got: %d (if > 2, grapheme tokenizer may not be recognizing modifier sequence)", got)
}
})
}
})
}
func TestStringGraphemes(t *testing.T) {
tests := []struct {
name string
input string
options Options
}{
{"empty string", "", defaultOptions},
{"single ASCII", "a", defaultOptions},
{"multiple ASCII", "hello", defaultOptions},
{"ASCII with spaces", "hello world", defaultOptions},
{"ASCII with newline", "hello\nworld", defaultOptions},
{"CJK ideograph", "中", defaultOptions},
{"CJK with ASCII", "hello中", defaultOptions},
{"ambiguous character", "★", defaultOptions},
{"ambiguous character EAW", "★", eawOptions},
{"emoji", "😀", defaultOptions},
{"flag US", "🇺🇸", defaultOptions},
{"text with flags", "Go 🇺🇸🚀", defaultOptions},
{"keycap 1️⃣", "1️⃣", defaultOptions},
{"mixed content", "Hi⌚⚙⚓", defaultOptions},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Get expected width using String
expected := tt.options.String(tt.input)