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
|
"""Tests for ambiguous_width parameter."""
# 3rd party
import pytest
# local
import wcwidth
AMBIGUOUS_CHARS = [
('\u00a1', 'INVERTED_EXCLAMATION'),
('\u00a7', 'SECTION_SIGN'),
('\u00b0', 'DEGREE_SIGN'),
('\u00b1', 'PLUS_MINUS'),
('\u00d7', 'MULTIPLICATION'),
('\u00f7', 'DIVISION'),
('\u2460', 'CIRCLED_ONE'),
('\u2500', 'BOX_HORIZONTAL'),
('\u25a0', 'BLACK_SQUARE'),
('\u2605', 'BLACK_STAR'),
]
@pytest.mark.parametrize('char,name', AMBIGUOUS_CHARS)
def test_wcwidth_ambiguous_default(char, name):
"""Ambiguous characters return width 1 by default."""
assert wcwidth.wcwidth(char) == 1
@pytest.mark.parametrize('char,name', AMBIGUOUS_CHARS)
def test_wcwidth_ambiguous_wide(char, name):
"""Ambiguous characters return width 2 when ambiguous_width=2."""
assert wcwidth.wcwidth(char, ambiguous_width=2) == 2
def test_wcswidth_mixed_ambiguous_and_wide():
"""Mixed CJK and ambiguous characters."""
text = '\u4e2d\u00b1' # CJK (wide=2) + PLUS-MINUS (ambiguous)
assert wcwidth.wcswidth(text) == 3
assert wcwidth.wcswidth(text, ambiguous_width=2) == 4
def test_width_ambiguous():
"""Width() respects ambiguous_width parameter."""
assert wcwidth.width('\u00b1') == 1
assert wcwidth.width('\u00b1', ambiguous_width=2) == 2
def test_ljust_ambiguous():
"""Ljust respects ambiguous_width parameter."""
text = '\u00b1'
assert wcwidth.ljust(text, 4) == '\u00b1 '
assert wcwidth.ljust(text, 4, ambiguous_width=2) == '\u00b1 '
def test_rjust_ambiguous():
"""Rjust respects ambiguous_width parameter."""
text = '\u00b1'
assert wcwidth.rjust(text, 4) == ' \u00b1'
assert wcwidth.rjust(text, 4, ambiguous_width=2) == ' \u00b1'
def test_center_ambiguous():
"""Center respects ambiguous_width parameter."""
text = '\u00b1'
assert wcwidth.center(text, 5) == ' \u00b1 '
assert wcwidth.center(text, 6, ambiguous_width=2) == ' \u00b1 '
def test_wrap_ambiguous():
"""Wrap respects ambiguous_width parameter."""
text = '\u00b1' * 5 # 5 ambiguous characters
assert wcwidth.wrap(text, 4) == ['\u00b1\u00b1\u00b1\u00b1', '\u00b1']
assert wcwidth.wrap(text, 4, ambiguous_width=2) == ['\u00b1\u00b1', '\u00b1\u00b1', '\u00b1']
def test_wide_not_affected_by_ambiguous():
"""Wide characters remain wide regardless of ambiguous_width."""
cjk = '\u4e2d' # CJK character (always wide)
assert wcwidth.wcwidth(cjk) == 2
assert wcwidth.wcwidth(cjk, ambiguous_width=2) == 2
assert wcwidth.wcwidth(cjk, ambiguous_width=1) == 2
|