More tests for rewrap

This commit is contained in:
Kovid Goyal 2016-11-09 11:19:17 +05:30
parent 0904939e4a
commit 8623e7b6e7
2 changed files with 37 additions and 19 deletions

View File

@ -76,7 +76,10 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
static void
dealloc(LineBuf* self) {
PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); PyMem_Free(self->scratch);
PyMem_Free(self->buf);
PyMem_Free(self->line_map);
PyMem_Free(self->continued_map);
PyMem_Free(self->scratch);
Py_CLEAR(self->line);
Py_TYPE(self)->tp_free((PyObject*)self);
}
@ -387,10 +390,11 @@ static Py_ssize_t rewrap_inner(nextlinefunc src_next, void *src_data, nextlinefu
Line *src, *dest;
Py_ssize_t src_x = src_col, dest_x = dest_xnum, num;
src = src_next(src_data, oom); dest = dest_next(dest_data, oom);
bool prev_line_was_continued = false;
while (src && dest) {
if (src_x == src->xnum) {
if (!prev_line_was_continued && src_x == src->xnum) {
// Trim trailing whitespace
while(src_x && (src->chars[src_x - 1] & CHAR_MASK) != 32) src_x--;
while(src_x && (src->chars[src_x - 1] & CHAR_MASK) == 32) src_x--;
}
num = MIN(dest_x, src_x);
if (num > 0) {
@ -398,19 +402,22 @@ static Py_ssize_t rewrap_inner(nextlinefunc src_next, void *src_data, nextlinefu
copy_range(src, src_x, dest, dest_x, num);
}
if (src_x <= 0) {
if (!src->continued) {
// Hard break, start new line on dest
if (set_continued != NULL) set_continued(dest_data, false);
else dest->continued = false;
if (dest_x > 0) {
left_shift_line(src, 0, dest_x);
CLEAR_LINE(src, dest_xnum - dest_x, dest_x);
}
dest = dest_next(dest_data, oom);
dest_x = dest_xnum;
}
prev_line_was_continued = src->continued;
src = src_next(src_data, oom);
src_x = src_xnum;
if (!prev_line_was_continued) {
// Hard break, finalize this line
if (dest_x > 0) { // Left align dest line
left_shift_line(dest, 0, dest_x);
CLEAR_LINE(dest, dest_xnum - dest_x, dest_x);
}
if (src) { // Only start a new line if there is more in src
if (set_continued != NULL) set_continued(dest_data, false);
else dest->continued = false;
dest = dest_next(dest_data, oom);
}
dest_x = dest_xnum;
}
}
if (dest_x <= 0) {
if (set_continued != NULL) set_continued(dest_data, true);
@ -483,8 +490,8 @@ rewrap(LineBuf *self, PyObject *val) {
if (dest_y > 0) {
// Shift lines up so that untouched lines are at the bottom
do_delete(other, dest_y, 0, other->ynum);
cursor_y = MAX(0, other->ynum - dest_y - 1);
do_delete(other, dest_y, 0, other->ynum - 1);
cursor_y = MAX(0, other->ynum - (dest_y + 1));
} else cursor_y = other->ynum - 1;
if (src_y > 0 || (src_y == 0 && src_x > 0)) {

View File

@ -192,8 +192,8 @@ class TestDataTypes(BaseTest):
l.set_char(0, 'x', 1, q)
self.assertEqualAttributes(l.cursor_from(0), q)
def test_rewrap(self):
# Simple tests when xnum is unchanged
def test_rewrap_simple(self):
' Same width buffers '
lb = filled_line_buf(5, 5)
lb2 = LineBuf(lb.ynum, lb.xnum)
lb.rewrap(lb2)
@ -204,13 +204,24 @@ class TestDataTypes(BaseTest):
for i in range(lb2.ynum):
self.ae(lb2.line(i), lb.line(i + 2))
lb2 = LineBuf(8, 5)
lb.rewrap(lb2)
cy = lb.rewrap(lb2)[1]
self.ae(cy, 4)
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)))
def test_rewrap_wider(self):
' New buffer wider '
lb = LineBuf(5, 5)
lb.line(0).set_text('01234', 0, 5, C())
lb.line(1).set_text('56789', 0, 5, C())
lb.set_continued(1, True)
lb2 = LineBuf(2, 6)
lb.rewrap(lb2)
self.ae(str(lb2.line(1)), '456789')
def test_utils(self):
d = codecs.getincrementaldecoder('utf-8')('strict').decode
self.ae(tuple(map(wcwidth, 'a1\0')), (1, 1, 0, 2))