2016-10-16 18:06:27 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# vim:fileencoding=utf-8
|
|
|
|
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
|
|
|
2016-10-17 06:41:10 +03:00
|
|
|
import codecs
|
|
|
|
|
2016-11-04 12:26:31 +03:00
|
|
|
from . import BaseTest, filled_line_buf, filled_cursor
|
2016-10-16 18:06:27 +03:00
|
|
|
|
2016-10-21 01:23:31 +03:00
|
|
|
from kitty.utils import is_simple_string, wcwidth, sanitize_title
|
2016-11-04 20:02:50 +03:00
|
|
|
from kitty.fast_data_types import LineBuf, Cursor as C, REVERSE
|
2016-10-16 18:06:27 +03:00
|
|
|
|
|
|
|
|
2016-11-09 16:51:08 +03:00
|
|
|
def create_lbuf(*lines):
|
|
|
|
maxw = max(map(len, lines))
|
|
|
|
ans = LineBuf(len(lines), maxw)
|
|
|
|
prev_full_length = False
|
|
|
|
for i, l in enumerate(lines):
|
|
|
|
ans.line(i).set_text(l, 0, len(l), C())
|
|
|
|
ans.set_continued(i, prev_full_length)
|
|
|
|
prev_full_length = len(l) == maxw
|
|
|
|
return ans
|
|
|
|
|
|
|
|
|
2016-10-16 18:06:27 +03:00
|
|
|
class TestDataTypes(BaseTest):
|
|
|
|
|
2016-11-04 12:14:46 +03:00
|
|
|
def test_linebuf(self):
|
2016-11-04 12:26:31 +03:00
|
|
|
old = filled_line_buf(2, 3, filled_cursor())
|
2016-11-04 12:14:46 +03:00
|
|
|
new = LineBuf(1, 3)
|
|
|
|
new.copy_old(old)
|
2016-11-04 12:26:31 +03:00
|
|
|
self.ae(new.line(0), old.line(1))
|
2016-11-04 12:41:08 +03:00
|
|
|
new.clear()
|
|
|
|
self.ae(str(new.line(0)), ' ' * new.xnum)
|
2016-11-04 20:02:50 +03:00
|
|
|
old.set_attribute(REVERSE, False)
|
|
|
|
for y in range(old.ynum):
|
|
|
|
for x in range(old.xnum):
|
2016-11-07 10:33:28 +03:00
|
|
|
l = old.line(y)
|
|
|
|
c = l.cursor_from(x)
|
2016-11-04 20:02:50 +03:00
|
|
|
self.assertFalse(c.reverse)
|
|
|
|
self.assertTrue(c.bold)
|
2016-11-04 20:11:35 +03:00
|
|
|
self.assertFalse(old.is_continued(0))
|
|
|
|
old.set_continued(0, True)
|
|
|
|
self.assertTrue(old.is_continued(0))
|
|
|
|
self.assertFalse(old.is_continued(1))
|
2016-11-04 12:14:46 +03:00
|
|
|
|
2016-11-07 10:33:28 +03:00
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb2 = LineBuf(5, 5)
|
|
|
|
lb2.copy_old(lb)
|
|
|
|
lb.index(0, 4)
|
|
|
|
for i in range(0, 4):
|
2016-11-07 18:25:40 +03:00
|
|
|
self.ae(lb.line(i), lb2.line(i + 1))
|
2016-11-07 10:33:28 +03:00
|
|
|
self.ae(lb.line(4), lb2.line(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.index(1, 3)
|
|
|
|
self.ae(lb.line(0), lb2.line(0))
|
|
|
|
self.ae(lb.line(1), lb2.line(2))
|
|
|
|
self.ae(lb.line(2), lb2.line(3))
|
|
|
|
self.ae(lb.line(3), lb2.line(1))
|
|
|
|
self.ae(lb.line(4), lb2.line(4))
|
|
|
|
self.ae(lb.create_line_copy(1), lb2.line(2))
|
|
|
|
l = lb.create_line_copy(2)
|
|
|
|
lb.copy_line_to(1, l)
|
|
|
|
self.ae(l, lb2.line(2))
|
|
|
|
lb.clear_line(0)
|
|
|
|
self.ae(lb.line(0), LineBuf(1, lb.xnum).create_line_copy(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.reverse_index(0, 4)
|
|
|
|
self.ae(lb.line(0), lb2.line(4))
|
|
|
|
for i in range(1, 5):
|
2016-11-07 18:25:40 +03:00
|
|
|
self.ae(lb.line(i), lb2.line(i - 1))
|
2016-11-07 10:33:28 +03:00
|
|
|
|
2016-11-07 13:45:46 +03:00
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
clb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb2 = LineBuf(1, 5)
|
|
|
|
lb.insert_lines(2, 1, lb.ynum - 1)
|
|
|
|
self.ae(lb.line(0), clb.line(0))
|
|
|
|
self.ae(lb.line(3), clb.line(1))
|
|
|
|
self.ae(lb.line(4), clb.line(2))
|
|
|
|
self.ae(lb.line(1), lb2.line(0))
|
|
|
|
self.ae(lb.line(2), lb2.line(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.insert_lines(10, 0, lb.ynum - 1)
|
|
|
|
for i in range(lb.ynum):
|
|
|
|
self.ae(lb.line(i), lb2.line(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.insert_lines(10, 1, lb.ynum - 1)
|
|
|
|
self.ae(lb.line(0), clb.line(0))
|
|
|
|
for i in range(1, lb.ynum):
|
|
|
|
self.ae(lb.line(i), lb2.line(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.insert_lines(1, 1, 3)
|
|
|
|
self.ae(lb.line(0), clb.line(0))
|
|
|
|
self.ae(lb.line(1), lb2.line(0))
|
|
|
|
self.ae(lb.line(2), clb.line(1))
|
|
|
|
self.ae(lb.line(3), clb.line(2))
|
|
|
|
self.ae(lb.line(4), clb.line(4))
|
|
|
|
|
2016-11-07 18:25:40 +03:00
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.delete_lines(2, 1, lb.ynum - 1)
|
|
|
|
self.ae(lb.line(0), clb.line(0))
|
|
|
|
self.ae(lb.line(1), clb.line(3))
|
|
|
|
self.ae(lb.line(2), clb.line(4))
|
|
|
|
self.ae(lb.line(3), lb2.line(0))
|
|
|
|
self.ae(lb.line(4), lb2.line(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.delete_lines(10, 0, lb.ynum - 1)
|
|
|
|
for i in range(lb.ynum):
|
|
|
|
self.ae(lb.line(i), lb2.line(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.delete_lines(10, 1, lb.ynum - 1)
|
|
|
|
self.ae(lb.line(0), clb.line(0))
|
|
|
|
for i in range(1, lb.ynum):
|
|
|
|
self.ae(lb.line(i), lb2.line(0))
|
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
lb.delete_lines(1, 1, 3)
|
|
|
|
self.ae(lb.line(0), clb.line(0))
|
|
|
|
self.ae(lb.line(1), clb.line(2))
|
|
|
|
self.ae(lb.line(2), clb.line(3))
|
|
|
|
self.ae(lb.line(3), lb2.line(0))
|
|
|
|
self.ae(lb.line(4), clb.line(4))
|
|
|
|
|
2016-11-08 06:30:18 +03:00
|
|
|
lb = filled_line_buf(5, 5, filled_cursor())
|
|
|
|
l = lb.line(0)
|
|
|
|
l.add_combining_char(1, 'a')
|
|
|
|
l.clear_text(1, 2)
|
|
|
|
self.ae(str(l), '0 00')
|
|
|
|
self.assertEqualAttributes(l.cursor_from(1), l.cursor_from(0))
|
|
|
|
|
2016-11-01 16:34:38 +03:00
|
|
|
def test_line(self):
|
2016-10-31 19:39:34 +03:00
|
|
|
lb = LineBuf(2, 3)
|
2016-11-07 10:33:28 +03:00
|
|
|
for y in range(lb.ynum):
|
2016-11-01 15:48:12 +03:00
|
|
|
line = lb.line(y)
|
2016-11-07 10:33:28 +03:00
|
|
|
self.ae(str(line), ' ' * lb.xnum)
|
|
|
|
for x in range(lb.xnum):
|
2016-11-02 08:14:35 +03:00
|
|
|
self.ae(line[x], ' ')
|
2016-10-31 20:21:22 +03:00
|
|
|
with self.assertRaises(ValueError):
|
2016-11-07 10:33:28 +03:00
|
|
|
lb.line(lb.ynum)
|
2016-10-31 20:21:22 +03:00
|
|
|
with self.assertRaises(ValueError):
|
2016-11-07 10:33:28 +03:00
|
|
|
lb.line(0)[lb.xnum]
|
2016-11-01 16:34:38 +03:00
|
|
|
l = lb.line(0)
|
|
|
|
l.add_combining_char(0, '1')
|
2016-11-02 08:14:35 +03:00
|
|
|
self.ae(l[0], ' 1')
|
2016-11-01 16:34:38 +03:00
|
|
|
l.add_combining_char(0, '2')
|
2016-11-02 08:14:35 +03:00
|
|
|
self.ae(l[0], ' 12')
|
2016-11-01 16:34:38 +03:00
|
|
|
l.add_combining_char(0, '3')
|
2016-11-02 08:14:35 +03:00
|
|
|
self.ae(l[0], ' 13')
|
|
|
|
self.ae(l[1], ' ')
|
2016-11-01 16:34:38 +03:00
|
|
|
self.ae(str(l), ' 13 ')
|
2016-11-02 08:14:35 +03:00
|
|
|
t = 'Testing with simple text'
|
|
|
|
lb = LineBuf(2, len(t))
|
|
|
|
l = lb.line(0)
|
|
|
|
l.set_text(t, 0, len(t), C())
|
|
|
|
self.ae(str(l), t)
|
|
|
|
l.set_text('a', 0, 1, C())
|
|
|
|
self.assertEqual(str(l), 'a' + t[1:])
|
|
|
|
|
|
|
|
c = C(3, 5)
|
|
|
|
c.bold = c.italic = c.reverse = c.strikethrough = True
|
|
|
|
c.fg = c.bg = c.decoration_fg = 0x0101
|
|
|
|
self.ae(c, c)
|
|
|
|
c2, c3 = c.copy(), c.copy()
|
|
|
|
self.ae(repr(c), repr(c2))
|
|
|
|
self.ae(c, c2)
|
|
|
|
c2.bold = c2.hidden = False
|
|
|
|
self.assertNotEqual(c, c2)
|
|
|
|
l.set_text(t, 0, len(t), C())
|
|
|
|
l.apply_cursor(c2, 3)
|
|
|
|
self.assertEqualAttributes(c2, l.cursor_from(3))
|
|
|
|
l.apply_cursor(c2, 0, len(l))
|
|
|
|
for i in range(len(l)):
|
|
|
|
self.assertEqualAttributes(c2, l.cursor_from(i))
|
|
|
|
l.apply_cursor(c3, 0)
|
|
|
|
self.assertEqualAttributes(c3, l.cursor_from(0))
|
|
|
|
l.copy_char(0, l, 1)
|
|
|
|
self.assertEqualAttributes(c3, l.cursor_from(1))
|
2016-10-31 19:39:34 +03:00
|
|
|
|
2016-10-17 04:35:54 +03:00
|
|
|
t = '0123456789'
|
2016-11-02 17:06:28 +03:00
|
|
|
lb = LineBuf(1, len(t))
|
|
|
|
l = lb.line(0)
|
|
|
|
l.set_text(t, 0, len(t), C())
|
|
|
|
self.ae(t, str(l))
|
2016-10-17 04:35:54 +03:00
|
|
|
l.right_shift(4, 2)
|
2016-11-02 17:06:28 +03:00
|
|
|
self.ae('0123454567', str(l))
|
|
|
|
l.set_text(t, 0, len(t), C())
|
2016-10-17 04:35:54 +03:00
|
|
|
l.right_shift(0, 0)
|
2016-11-02 17:06:28 +03:00
|
|
|
self.ae(t, str(l))
|
2016-10-17 04:35:54 +03:00
|
|
|
l.right_shift(0, 1)
|
|
|
|
self.ae(str(l), '0' + t[:-1])
|
2016-11-02 17:06:28 +03:00
|
|
|
l.set_text(t, 0, len(t), C())
|
2016-10-17 04:35:54 +03:00
|
|
|
l.left_shift(0, 2)
|
|
|
|
self.ae(str(l), t[2:] + '89')
|
2016-11-02 17:06:28 +03:00
|
|
|
l.set_text(t, 0, len(t), C())
|
2016-10-17 04:35:54 +03:00
|
|
|
l.left_shift(7, 3)
|
|
|
|
self.ae(str(l), t)
|
2016-10-17 05:54:16 +03:00
|
|
|
|
2016-11-02 17:06:28 +03:00
|
|
|
l.set_text(t, 0, len(t), C())
|
|
|
|
q = C()
|
|
|
|
q.bold = q.italic = q.reverse = q.strikethrough = True
|
2016-10-17 05:54:16 +03:00
|
|
|
q.decoration = 2
|
2016-11-02 17:06:28 +03:00
|
|
|
c = C()
|
|
|
|
c.x = 3
|
|
|
|
l.set_text('axyb', 1, 2, c)
|
2016-10-17 11:20:53 +03:00
|
|
|
self.ae(str(l), '012xy56789')
|
2016-11-02 17:06:28 +03:00
|
|
|
l.set_char(0, 'x', 1, q)
|
|
|
|
self.assertEqualAttributes(l.cursor_from(0), q)
|
2016-10-17 06:41:10 +03:00
|
|
|
|
2016-11-09 08:49:17 +03:00
|
|
|
def test_rewrap_simple(self):
|
|
|
|
' Same width buffers '
|
2016-11-08 18:55:26 +03:00
|
|
|
lb = filled_line_buf(5, 5)
|
|
|
|
lb2 = LineBuf(lb.ynum, lb.xnum)
|
|
|
|
lb.rewrap(lb2)
|
|
|
|
for i in range(lb.ynum):
|
|
|
|
self.ae(lb2.line(i), lb.line(i))
|
|
|
|
lb2 = LineBuf(8, 5)
|
2016-11-09 08:49:17 +03:00
|
|
|
cy = lb.rewrap(lb2)[1]
|
|
|
|
self.ae(cy, 4)
|
2016-11-08 18:55:26 +03:00
|
|
|
for i in range(lb.ynum):
|
|
|
|
self.ae(lb2.line(i), lb.line(i))
|
|
|
|
empty = LineBuf(1, lb2.xnum)
|
|
|
|
for i in range(lb.ynum, lb2.ynum):
|
|
|
|
self.ae(str(lb2.line(i)), str(empty.line(0)))
|
2016-11-09 15:17:03 +03:00
|
|
|
lb2 = LineBuf(3, 5)
|
|
|
|
extra, cy = lb.rewrap(lb2)
|
|
|
|
self.ae(cy, 2)
|
|
|
|
for i in range(lb2.ynum):
|
|
|
|
self.ae(lb2.line(i), lb.line(i + 2))
|
2016-11-08 18:55:26 +03:00
|
|
|
|
2016-11-09 16:51:08 +03:00
|
|
|
def line_comparison(self, lb, *lines):
|
|
|
|
lb2 = LineBuf(len(lines), max(map(len, lines)))
|
|
|
|
lb.rewrap(lb2)
|
|
|
|
for i, l in enumerate(lines):
|
|
|
|
l2 = lb2.line(i)
|
|
|
|
self.ae(l, str(l2))
|
|
|
|
return lb2
|
|
|
|
|
|
|
|
def assertContinued(self, lb, *vals):
|
|
|
|
self.ae(list(vals), [lb.is_continued(i) for i in range(len(vals))])
|
|
|
|
|
2016-11-09 08:49:17 +03:00
|
|
|
def test_rewrap_wider(self):
|
|
|
|
' New buffer wider '
|
2016-11-09 16:51:08 +03:00
|
|
|
lb = create_lbuf('0123 ', '56789')
|
|
|
|
lb2 = self.line_comparison(lb, '0123 5', '6789 ', ' ' * 6)
|
|
|
|
self.assertContinued(lb2, False, True)
|
|
|
|
|
|
|
|
lb = create_lbuf('12', 'abc')
|
|
|
|
lb2 = self.line_comparison(lb, '12 ', 'abc ')
|
|
|
|
self.assertContinued(lb2, False, False)
|
|
|
|
|
|
|
|
def test_rewrap_narrower(self):
|
|
|
|
' New buffer narrower '
|
|
|
|
lb = create_lbuf('123 ', 'abcde')
|
|
|
|
lb2 = self.line_comparison(lb, '123', 'abc', 'de ')
|
|
|
|
self.assertContinued(lb2, False, False, True)
|
2016-11-09 08:49:17 +03:00
|
|
|
|
2016-10-17 06:41:10 +03:00
|
|
|
def test_utils(self):
|
|
|
|
d = codecs.getincrementaldecoder('utf-8')('strict').decode
|
|
|
|
self.ae(tuple(map(wcwidth, 'a1\0コ')), (1, 1, 0, 2))
|
|
|
|
for s in ('abd38453*(+\n\t\f\r !\0~[]{}()"\':;<>/?ASD`',):
|
|
|
|
self.assertTrue(is_simple_string(s))
|
|
|
|
self.assertTrue(is_simple_string(d(s.encode('utf-8'))))
|
|
|
|
self.assertFalse(is_simple_string('a1コ'))
|
2016-10-21 01:23:31 +03:00
|
|
|
self.assertEqual(sanitize_title('a\0\01 \t\n\f\rb'), 'a b')
|