| 1 |
from unittest import TestCase |
| 2 |
|
| 3 |
from _pyrepl.utils import str_width, wlen, prev_next_window, gen_colors |
| 4 |
|
| 5 |
|
| 6 |
class TestUtils(TestCase): |
| 7 |
def test_str_width(self): |
| 8 |
characters = [ |
| 9 |
'a', |
| 10 |
'1', |
| 11 |
'_', |
| 12 |
'!', |
| 13 |
'\x1a', |
| 14 |
'\u263A', |
| 15 |
'\uffb9', |
| 16 |
'\N{LATIN SMALL LETTER E WITH ACUTE}', # é |
| 17 |
'\N{LATIN SMALL LETTER E WITH CEDILLA}', # ȩ |
| 18 |
'\u00ad', |
| 19 |
] |
| 20 |
for c in characters: |
| 21 |
self.assertEqual(str_width(c), 1) |
| 22 |
|
| 23 |
zero_width_characters = [ |
| 24 |
'\N{COMBINING ACUTE ACCENT}', |
| 25 |
'\N{ZERO WIDTH JOINER}', |
| 26 |
] |
| 27 |
for c in zero_width_characters: |
| 28 |
with self.subTest(character=c): |
| 29 |
self.assertEqual(str_width(c), 0) |
| 30 |
|
| 31 |
characters = [chr(99989), chr(99999)] |
| 32 |
for c in characters: |
| 33 |
self.assertEqual(str_width(c), 2) |
| 34 |
|
| 35 |
def test_wlen(self): |
| 36 |
for c in ['a', 'b', '1', '!', '_']: |
| 37 |
self.assertEqual(wlen(c), 1) |
| 38 |
self.assertEqual(wlen('\x1a'), 2) |
| 39 |
|
| 40 |
char_east_asian_width_N = chr(3800) |
| 41 |
self.assertEqual(wlen(char_east_asian_width_N), 1) |
| 42 |
char_east_asian_width_W = chr(4352) |
| 43 |
self.assertEqual(wlen(char_east_asian_width_W), 2) |
| 44 |
|
| 45 |
self.assertEqual(wlen('hello'), 5) |
| 46 |
self.assertEqual(wlen('hello' + '\x1a'), 7) |
| 47 |
self.assertEqual(wlen('e\N{COMBINING ACUTE ACCENT}'), 1) |
| 48 |
self.assertEqual(wlen('a\N{ZERO WIDTH JOINER}b'), 2) |
| 49 |
|
| 50 |
def test_prev_next_window(self): |
| 51 |
def gen_normal(): |
| 52 |
yield 1 |
| 53 |
yield 2 |
| 54 |
yield 3 |
| 55 |
yield 4 |
| 56 |
|
| 57 |
pnw = prev_next_window(gen_normal()) |
| 58 |
self.assertEqual(next(pnw), (None, 1, 2)) |
| 59 |
self.assertEqual(next(pnw), (1, 2, 3)) |
| 60 |
self.assertEqual(next(pnw), (2, 3, 4)) |
| 61 |
self.assertEqual(next(pnw), (3, 4, None)) |
| 62 |
with self.assertRaises(StopIteration): |
| 63 |
next(pnw) |
| 64 |
|
| 65 |
def gen_short(): |
| 66 |
yield 1 |
| 67 |
|
| 68 |
pnw = prev_next_window(gen_short()) |
| 69 |
self.assertEqual(next(pnw), (None, 1, None)) |
| 70 |
with self.assertRaises(StopIteration): |
| 71 |
next(pnw) |
| 72 |
|
| 73 |
def gen_raise(): |
| 74 |
yield from gen_normal() |
| 75 |
1/0 |
| 76 |
|
| 77 |
pnw = prev_next_window(gen_raise()) |
| 78 |
self.assertEqual(next(pnw), (None, 1, 2)) |
| 79 |
self.assertEqual(next(pnw), (1, 2, 3)) |
| 80 |
self.assertEqual(next(pnw), (2, 3, 4)) |
| 81 |
self.assertEqual(next(pnw), (3, 4, None)) |
| 82 |
with self.assertRaises(ZeroDivisionError): |
| 83 |
next(pnw) |
| 84 |
|
| 85 |
def test_gen_colors_keyword_highlighting(self): |
| 86 |
cases = [ |
| 87 |
# no highlights |
| 88 |
("a.set", [(".", "op")]), |
| 89 |
("obj.list", [(".", "op")]), |
| 90 |
("obj.match", [(".", "op")]), |
| 91 |
("b. \\\n format", [(".", "op")]), |
| 92 |
# highlights |
| 93 |
("set", [("set", "builtin")]), |
| 94 |
("list", [("list", "builtin")]), |
| 95 |
(" \n dict", [("dict", "builtin")]), |
| 96 |
] |
| 97 |
for code, expected_highlights in cases: |
| 98 |
with self.subTest(code=code): |
| 99 |
colors = list(gen_colors(code)) |
| 100 |
# Extract (text, tag) pairs for comparison |
| 101 |
actual_highlights = [] |
| 102 |
for color in colors: |
| 103 |
span_text = code[color.span.start:color.span.end + 1] |
| 104 |
actual_highlights.append((span_text, color.tag)) |
| 105 |
self.assertEqual(actual_highlights, expected_highlights) |