def test_insert_middle(self): piece_table = PieceTable(self.long_string) piece_table.insert(self.short_string, 10) ret = piece_table.get_text() expected = self.long_string[:10] + self.short_string + self.long_string[ 10:] self.assertEqual(ret, expected)
class Buffer: def __init__(self, s): self._buf = PieceTable(s) self._update_line_starts(s) def _update_line_starts(self, s): self._linestarts = [0] + [i + 1 for i, c in enumerate(s) if c == '\n'] def as_str(self): return self._buf.as_str() def nlines(self): return len(self._linestarts) # TODO: add selection for substring # TODO: add iterlines def line(self, pos): return bisect.bisect_right(self._linestarts, pos) - 1 def pos_for_line(self, n): assert 0 <= n < len(self._linestarts), n return self._linestarts[n] def col(self, pos): return pos - self._linestarts[ bisect.bisect_right(self._linestarts, pos) - 1] def line_length(self, n): if n + 1 < len(self._linestarts): return self._linestarts[n + 1] - self._linestarts[n] else: # FIXME: once substr is added, get the length of the last line directly return len(self.as_str()) - self._linestarts[n] + 1 def insert(self, val, pos): self._buf.insert(val, pos) self._update_line_starts(self.as_str()) def delete(self, pos, length): self._buf.delete(pos, length) self._update_line_starts(self.as_str())
def test_series_of_inserts_and_deletes(self, data): nsteps = data.draw(st.integers(min_value=0, max_value=100), label='number of operations to perform') t = data.draw(st.text(), label='initial buffer') p = PieceTable(t) for step in range(nsteps): command = data.draw(st.sampled_from(['insert', 'delete'])) if command == 'insert': _, sub, i = data.draw(substr_insert_input(t)) t = insert_str(t, i, sub) p.insert(sub, i) self.assertEqual( t, p.as_str(), f'{step=} {p._table=} {p.original=} {p._add=}') elif command == 'delete': _, start, length = data.draw(substr_delete_input(t)) t = delete_str(t, start, length) p.delete(start, length) self.assertEqual( t, p.as_str(), f'{step=} {p._table=} {p.original=} {p._add=}') else: assert False
def test_insert(self, inputs): s, sub, i = inputs new_s = insert_str(s, i, sub) p = PieceTable(s) p.insert(sub, i) self.assertEqual(new_s, p.as_str())
def test_insert_end(self): piece_table = PieceTable(self.long_string) piece_table.insert(self.short_string, len(self.long_string)) ret = piece_table.get_text() expected = self.long_string + self.short_string self.assertEqual(ret, expected)
def test_string_at(self): piece_table = PieceTable(self.long_string) piece_table.insert(self.short_string, 10) ret = piece_table.string_at(10, len(self.short_string)) self.assertEqual(ret, self.short_string)