Skip to content

Commit

Permalink
Add tests to cover strsplit()
Browse files Browse the repository at this point in the history
Get ready for future optimization
  • Loading branch information
ymattw committed Nov 11, 2024
1 parent bf63594 commit 888ec7c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/test_ydiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,47 @@ def test_ok(self):
self.assertEqual(want, got)


class StrSplitTest(unittest.TestCase):

def test_not_colorized(self):
text = 'Hi, 你好\n'
tests = [
# (width, want)
(4, ('Hi, ', '你好\n', 4)),
(5, ('Hi, 你', '好\n', 6)),
(8, ('Hi, 你好', '\n', 8)),
(9, ('Hi, 你好\n', '', 9)),
(10, ('Hi, 你好\n', '', 9)),
]
for width, want in tests:
got = ydiff.strsplit(text, width, {})
self.assertEqual(want, got)

def test_colorized(self):
g = '\x1b[32m' # green
b = '\x1b[34m' # blue
r = '\x1b[0m' # reset
# Width: 1----2----------3-----[4]5--[6]7
parts = [g, 'H', 'i', r, b, '!', r, '你', '好']
codes = {g, b, r}
tests = [
# (width, want_left, want_right, want_width)
(1, (g, 'H', r), (g, 'i', r, b, '!', r, '你好'), 1),
(2, (g, 'Hi', r, b, r), (b, '!', r, '你好'), 2),
(3, (g, 'Hi', r, b, '!', r), ('你好'), 3),
(4, (g, 'Hi', r, b, '!', r, '你'), ('好',), 5),
(5, (g, 'Hi', r, b, '!', r, '你'), ('好',), 5),
(6, (g, 'Hi', r, b, '!', r, '你好'), (), 7),
(7, (g, 'Hi', r, b, '!', r, '你好'), (), 7),
(8, (g, 'Hi', r, b, '!', r, '你好'), (), 7),
]
for width, want_left, want_right, want_width in tests:
got = ydiff.strsplit(''.join(parts), width, codes)
self.assertEqual(''.join(want_left), got[0])
self.assertEqual(''.join(want_right), got[1])
self.assertEqual(want_width, got[2])


class DecodeTest(unittest.TestCase):

def test_normal(self):
Expand Down

0 comments on commit 888ec7c

Please sign in to comment.