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
|
# Copyright 2021-2025 Sebastian Ramacher <sebastian@ramacher.at>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import locale
import unittest
from cwcwidth import wcwidth, wcswidth
supports_utf8 = "UTF-8" in locale.getlocale()
class Tests(unittest.TestCase):
def _exec_test(
self, phrase, individual_expected_lengths, expected_length=None, n=None
):
if expected_length is None:
expected_length = sum(individual_expected_lengths)
self.assertEqual(
individual_expected_lengths,
tuple(map(wcwidth, phrase if n is None else phrase[:n])),
)
self.assertEqual(expected_length, wcswidth(phrase, n))
def test_exceptions(self):
"""wcwidth raises ValueError for strings of length != 1."""
with self.assertRaises(ValueError):
wcwidth("")
with self.assertRaises(ValueError):
wcwidth("abc")
def test_empty_string(self):
"""Width of an empty string is 0."""
self._exec_test("", tuple())
def test_hello_world(self):
"""Width of English phrase: Hello World!"""
self._exec_test("Hello World!", (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
@unittest.skipUnless(supports_utf8, "locale does not support UTF-8")
def test_hello_jp(self):
"""Width of Japanese phrase: コンニチハ, セカイ!"""
self._exec_test("コンニチハ, セカイ!", (2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 1))
@unittest.skipUnless(supports_utf8, "locale does not support UTF-8")
def test_wcswidth_substr(self):
"""Test wcswidth() optional 2nd parameter."""
self._exec_test("コンニチハ, セカイ!", (2, 2, 2, 2, 2, 1, 1), n=7)
def test_null_width_0(self):
"""NULL (0) reports width 0."""
self._exec_test("abc\x00def", (1, 1, 1, 0, 1, 1, 1))
self._exec_test("abc\x00def", (1, 1, 1, 0, 1, 1, 1), n=7)
def test_control_c0_width_negative_1(self):
"""CSI (Control sequence initiate) reports width -1."""
self._exec_test("\x1b[0m", (-1, 1, 1, 1), expected_length=-1)
@unittest.skipUnless(supports_utf8, "locale does not support UTF-8")
def test_combining_width_negative_1(self):
"""Simple test combining reports total width of 4."""
self._exec_test("--\u05bf--", (1, 1, 0, 1, 1))
@unittest.skipUnless(supports_utf8, "locale does not support UTF-8")
def test_combining_cafe(self):
"""Phrase cafe + COMBINING ACUTE ACCENT is café of length 4."""
self._exec_test("cafe\u0301", (1, 1, 1, 1, 0))
@unittest.skipUnless(supports_utf8, "locale does not support UTF-8")
def test_combining_enclosing(self):
"""CYRILLIC CAPITAL LETTER A + COMBINING CYRILLIC HUNDRED THOUSANDS SIGN is А҈ of length 1."""
self._exec_test("\u0410\u0488", (1, 0))
@unittest.skipUnless(supports_utf8, "locale does not support UTF-8")
def test_combining_spacing(self):
"""Balinese kapal (ship) is ᬓᬨᬮ᭄ of length 4."""
self._exec_test("\u1b13\u1b28\u1b2e\u1b44", (1, 1, 1, 1))
def test_carriage_return(self):
"""Control character reports width -1."""
self._exec_test("\r", (-1,))
|