summaryrefslogtreecommitdiffstats
path: root/contrib/python/wcwidth/py3/tests/test_clip_cjk_emoji.py
blob: e41bd6276639da623394aed220a798735a7a01e2 (plain) (blame)
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
"""
Tests for clip() with CJK and Emoji characters.

These ensure wide graphemes (CJK / emoji / ZWJ sequences) are clipped correctly:
- Partial columns of a wide grapheme are replaced by fillchar.
- Full grapheme included when fully inside slice.
"""

# 3rd party
import pytest

# local
from wcwidth import clip, width


@pytest.mark.parametrize("ch", [
    "中",
    "🙂",
    "👨\u200d👩\u200d👧",  # family ZWJ
    "👩\u200d👩\u200d👧"   # another ZWJ variant
])
def test_partial_and_full_wide_grapheme(ch):
    w = width(ch)
    assert w >= 1
    if w > 1:
        # partial clip of first column -> fillchar
        assert clip(ch, 0, 1) == ' '
        # full clip covering entire grapheme -> original grapheme
        assert clip(ch, 0, w) == ch
        # width of clipped full grapheme should match
        assert width(clip(ch, 0, w)) == w
    else:
        # narrow grapheme: trivial
        assert clip(ch, 0, 1) == ch


def test_mixed_cjk_emoji_sequence():
    text = 'A中🙂B'
    total_w = width(text)
    # sanity
    assert total_w >= 4
    # pick a slice that includes the middle two columns (center of string)
    # ensure clip doesn't raise and width matches requested slice
    start = 1
    end = 4
    out = clip(text, start, end)
    assert width(out) == (end - start)