-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_core.py
executable file
·460 lines (360 loc) · 16.1 KB
/
test_core.py
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
# coding: utf-8
"""Core tests for wcwidth module. isort:skip_file"""
try:
# std import
import importlib.metadata as importmeta
except ImportError:
# 3rd party for python3.7 and earlier
import importlib_metadata as importmeta
# local
import wcwidth
# 3rd party
import pytest
# some tests cannot be done on some builds of python, where the internal
# unicode structure is limited to 0x10000 for memory conservation,
# "ValueError: unichr() arg not in range(0x10000) (narrow Python build)"
try:
# python 2
_ = unichr
except NameError:
# python 3
unichr = chr
try:
unichr(0x2fffe)
NARROW_ONLY = False
except ValueError:
NARROW_ONLY = True
def test_package_version():
"""wcwidth.__version__ is expected value."""
# given,
expected = importmeta.version('wcwidth')
# exercise,
result = wcwidth.__version__
# verify.
assert result == expected
def test_empty_string():
"""
Test empty string is OK.
https://github.com/jquast/wcwidth/issues/24
"""
phrase = ""
expect_length_each = 0
expect_length_phrase = 0
# exercise,
length_each = wcwidth.wcwidth(phrase)
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def basic_string_type():
"""
This is a python 2-specific test of the basic "string type"
Such strings cannot contain anything but ascii in python2.
"""
# given,
phrase = 'hello\x00world'
expect_length_each = (1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1)
expect_length_phrase = sum(expect_length_each)
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_hello_jp():
u"""
Width of Japanese phrase: コンニチハ, セカイ!
Given a phrase of 5 and 3 Katakana ideographs, joined with
3 English-ASCII punctuation characters, totaling 11, this
phrase consumes 19 cells of a terminal emulator.
"""
# given,
phrase = u'コンニチハ, セカイ!'
expect_length_each = (2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1)
expect_length_phrase = sum(expect_length_each)
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_wcswidth_substr():
"""
Test wcswidth() optional 2nd parameter, ``n``.
``n`` determines at which position of the string
to stop counting length.
"""
# given,
phrase = u'コンニチハ, セカイ!'
end = 7
expect_length_each = (2, 2, 2, 2, 2, 1, 1,)
expect_length_phrase = sum(expect_length_each)
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))[:end]
length_phrase = wcwidth.wcswidth(phrase, end)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_null_width_0():
"""NULL (0) reports width 0."""
# given,
phrase = u'abc\x00def'
expect_length_each = (1, 1, 1, 0, 1, 1, 1)
expect_length_phrase = sum(expect_length_each)
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase, len(phrase))
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_control_c0_width_negative_1():
"""How the API reacts to CSI (Control sequence initiate).
An example of bad fortune, this terminal sequence is a width of 0
on all terminals, but wcwidth doesn't parse Control-Sequence-Inducer
(CSI) sequences.
Also the "legacy" posix functions wcwidth and wcswidth return -1 for
any string containing the C1 control character \x1b (ESC).
"""
# given,
phrase = u'\x1b[0m'
expect_length_each = (-1, 1, 1, 1)
expect_length_phrase = -1
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify, though this is actually *0* width for a terminal emulator
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_combining_width():
"""Simple test combining reports total width of 4."""
# given,
phrase = u'--\u05bf--'
expect_length_each = (1, 1, 0, 1, 1)
expect_length_phrase = 4
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_combining_cafe():
u"""Phrase cafe + COMBINING ACUTE ACCENT is café of length 4."""
phrase = u"cafe\u0301"
expect_length_each = (1, 1, 1, 1, 0)
expect_length_phrase = 4
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_combining_enclosing():
u"""CYRILLIC CAPITAL LETTER A + COMBINING CYRILLIC HUNDRED THOUSANDS SIGN is of length 1."""
phrase = u"\u0410\u0488"
expect_length_each = (1, 0)
expect_length_phrase = 1
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_balinese_script():
u"""
Balinese kapal (ship) is length 3.
This may be an example that is not yet correctly rendered by any terminal so
far, like devanagari.
"""
phrase = (u"\u1B13" # Category 'Lo', EAW 'N' -- BALINESE LETTER KA
u"\u1B28" # Category 'Lo', EAW 'N' -- BALINESE LETTER PA KAPAL
u"\u1B2E" # Category 'Lo', EAW 'N' -- BALINESE LETTER LA
u"\u1B44") # Category 'Mc', EAW 'N' -- BALINESE ADEG ADEG
expect_length_each = (1, 1, 1, 0)
expect_length_phrase = 3
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_kr_jamo_filler():
u"""
Jamo filler is 0 width.
According to https://www.unicode.org/L2/L2006/06310-hangul-decompose9.pdf this character and others
like it, ``\uffa0``, ``\u1160``, ``\u115f``, ``\u1160``, are not commonly viewed with a terminal,
seems it doesn't matter whether it is implemented or not, they are not typically used !
"""
phrase = u"\u1100\u1160"
expect_length_each = (2, 1)
expect_length_phrase = 3
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
@pytest.mark.skipif(NARROW_ONLY, reason="Test cannot verify on python 'narrow' builds")
def emoji_zwj_sequence():
u"""
Emoji zwj sequence of four codepoints is just 2 cells.
"""
phrase = (u"\U0001f469" # Base, Category So, East Asian Width property 'W' -- WOMAN
u"\U0001f3fb" # Modifier, Category Sk, East Asian Width property 'W' -- EMOJI MODIFIER FITZPATRICK TYPE-1-2
u"\u200d" # Joiner, Category Cf, East Asian Width property 'N' -- ZERO WIDTH JOINER
u"\U0001f4bb") # Fused, Category So, East Asian Width peroperty 'W' -- PERSONAL COMPUTER
# This test adapted from https://www.unicode.org/L2/L2023/23107-terminal-suppt.pdf
expect_length_each = (2, 0, 0, 2)
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
@pytest.mark.skipif(NARROW_ONLY, reason="Test cannot verify on python 'narrow' builds")
def test_unfinished_zwj_sequence():
u"""
Ensure index-out-of-bounds does not occur for zero-width joiner without any following character
"""
phrase = (u"\U0001f469" # Base, Category So, East Asian Width property 'W' -- WOMAN
u"\U0001f3fb" # Modifier, Category Sk, East Asian Width property 'W' -- EMOJI MODIFIER FITZPATRICK TYPE-1-2
u"\u200d") # Joiner, Category Cf, East Asian Width property 'N' -- ZERO WIDTH JOINER
expect_length_each = (2, 0, 0)
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
@pytest.mark.skipif(NARROW_ONLY, reason="Test cannot verify on python 'narrow' builds")
def test_non_recommended_zwj_sequence():
"""
Verify ZWJ is measured as though successful with characters that cannot be joined, wcwidth does not verify
"""
phrase = (u"\U0001f469" # Base, Category So, East Asian Width property 'W' -- WOMAN
u"\U0001f3fb" # Modifier, Category Sk, East Asian Width property 'W' -- EMOJI MODIFIER FITZPATRICK TYPE-1-2
u"\u200d") # Joiner, Category Cf, East Asian Width property 'N' -- ZERO WIDTH JOINER
expect_length_each = (2, 0, 0)
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
@pytest.mark.skipif(NARROW_ONLY, reason="Test cannot verify on python 'narrow' builds")
def test_longer_emoji_zwj_sequence():
"""
A much longer emoji ZWJ sequence of 10 total codepoints is just 2 cells!
"""
# 'Category Code', 'East Asian Width property' -- 'description'
phrase = (u"\U0001F9D1" # 'So', 'W' -- ADULT
u"\U0001F3FB" # 'Sk', 'W' -- EMOJI MODIFIER FITZPATRICK TYPE-1-2
u"\u200d" # 'Cf', 'N' -- ZERO WIDTH JOINER
u"\u2764" # 'So', 'N' -- HEAVY BLACK HEART
u"\uFE0F" # 'Mn', 'A' -- VARIATION SELECTOR-16
u"\u200d" # 'Cf', 'N' -- ZERO WIDTH JOINER
u"\U0001F48B" # 'So', 'W' -- KISS MARK
u"\u200d" # 'Cf', 'N' -- ZERO WIDTH JOINER
u"\U0001F9D1" # 'So', 'W' -- ADULT
u"\U0001F3FD") # 'Sk', 'W' -- EMOJI MODIFIER FITZPATRICK TYPE-4
# This test adapted from https://www.unicode.org/L2/L2023/23107-terminal-suppt.pdf
expect_length_each = (2, 0, 0, 1, 0, 0, 2, 0, 2, 0)
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_devanagari_script():
"""
Attempt to test the measurement width of Devanagari script.
I believe this 'phrase' should be length 3.
This is a difficult problem, and this library does not yet get it right,
because we interpret the unicode data files programmatically, but they do
not correctly describe how their terminal width is measured.
There are very few Terminals that do!
As of 2023,
- iTerm2: correct length but individual characters are out of order and
horizaontally misplaced as to be unreadable in its language when
using 'Noto Sans' font.
- mlterm: mixed results, it offers several options in the configuration
dialog, "Xft", "Cario", and "Variable Column Width" have some
effect, but with neither 'Noto Sans' or 'unifont', it is not
recognizable as the Devanagari script it is meant to display.
Previous testing with Devanagari documented at address https://benizi.com/vim/devanagari/
See also, https://askubuntu.com/questions/8437/is-there-a-good-mono-spaced-font-for-devanagari-script-in-the-terminal
"""
# This test adapted from https://www.unicode.org/L2/L2023/23107-terminal-suppt.pdf
# please note that document correctly points out that the final width cannot be determined
# as a sum of each individual width, as this library currently performs with exception of
# ZWJ, but I think it incorrectly gestures what a stateless call to wcwidth.wcwidth of
# each codepoint *should* return.
phrase = (u"\u0915" # Akhand, Category 'Lo', East Asian Width property 'N' -- DEVANAGARI LETTER KA
u"\u094D" # Joiner, Category 'Mn', East Asian Width property 'N' -- DEVANAGARI SIGN VIRAMA
u"\u0937" # Fused, Category 'Lo', East Asian Width property 'N' -- DEVANAGARI LETTER SSA
u"\u093F") # MatraL, Category 'Mc', East Asian Width property 'N' -- DEVANAGARI VOWEL SIGN I
# 23107-terminal-suppt.pdf suggests wcwidth.wcwidth should return (2, 0, 0, 1)
expect_length_each = (1, 0, 1, 0)
# I believe the final width *should* be 3.
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_tamil_script():
# This test adapted from https://www.unicode.org/L2/L2023/23107-terminal-suppt.pdf
phrase = (u"\u0b95" # Akhand, Category 'Lo', East Asian Width property 'N' -- TAMIL LETTER KA
u"\u0bcd" # Joiner, Category 'Mn', East Asian Width property 'N' -- TAMIL SIGN VIRAMA
u"\u0bb7" # Fused, Category 'Lo', East Asian Width property 'N' -- TAMIL LETTER SSA
u"\u0bcc") # MatraLR, Category 'Mc', East Asian Width property 'N' -- TAMIL VOWEL SIGN AU
# 23107-terminal-suppt.pdf suggests wcwidth.wcwidth should return (3, 0, 0, 4)
expect_length_each = (1, 0, 1, 0)
# I believe the final width should be about 5 or 6.
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_kannada_script():
# This test adapted from https://www.unicode.org/L2/L2023/23107-terminal-suppt.pdf
# |ರ್ಝೈ|
# |123|
phrase = (u"\u0cb0" # Repha, Category 'Lo', East Asian Width property 'N' -- KANNADA LETTER RA
u"\u0ccd" # Joiner, Category 'Mn', East Asian Width property 'N' -- KANNADA SIGN VIRAMA
u"\u0c9d" # Base, Category 'Lo', East Asian Width property 'N' -- KANNADA LETTER JHA
u"\u0cc8") # MatraUR, Category 'Mc', East Asian Width property 'N' -- KANNADA VOWEL SIGN AI
# 23107-terminal-suppt.pdf suggests should be (2, 0, 3, 1)
expect_length_each = (1, 0, 1, 0)
# I believe the correct final width *should* be 3 or 4.
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase
def test_kannada_script_2():
# This test adapted from https://www.unicode.org/L2/L2023/23107-terminal-suppt.pdf
# |ರ಼್ಚ|
# |12|
phrase = (u"\u0cb0" # Base, Category 'Lo', East Asian Width property 'N' -- KANNADA LETTER RA
u"\u0cbc" # Nukta, Category 'Mn', East Asian Width property 'N' -- KANNADA SIGN NUKTA
u"\u0ccd" # Joiner, Category 'Lo', East Asian Width property 'N' -- KANNADA SIGN VIRAMA
u"\u0c9a") # Subjoin, Category 'Mc', East Asian Width property 'N' -- KANNADA LETTER CA
# 23107-terminal-suppt.pdf suggests wcwidth.wcwidth should return (2, 0, 0, 1)
expect_length_each = (1, 0, 0, 1)
# I believe the final width is correct, but maybe for the wrong reasons!
expect_length_phrase = 2
# exercise,
length_each = tuple(map(wcwidth.wcwidth, phrase))
length_phrase = wcwidth.wcswidth(phrase)
# verify.
assert length_each == expect_length_each
assert length_phrase == expect_length_phrase