Ejemplo n.º 1
0
 def test_delete(self, inputs):
     s, start, length = inputs
     new_s = delete_str(s, start, length)
     p = PieceTable(s)
     p.delete(start, length)
     self.assertEqual(new_s, p.as_str(),
                      f'{p._table=} {p.original=} {p._add=}')
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 def test_delete_2(self):
     s = 'aa'
     p = PieceTable(s)
     p.delete(0, 1)
     p.delete(0, 1)
     self.assertEqual('', p.as_str(),
                      f'{p._table=} {p.original=} {p._add=}')
Ejemplo n.º 4
0
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())
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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())
Ejemplo n.º 9
0
 def test_length(self):
     piece_table = PieceTable(self.long_string)
     self.assertEqual(len(self.long_string), len(piece_table))
Ejemplo n.º 10
0
 def test_delete_end_negative_length(self):
     piece_table = PieceTable(self.long_string)
     piece_table.delete(len(self.long_string), -10)
     ret = piece_table.get_text()
     expected = self.long_string[:-10]
     self.assertEqual(ret, expected)
Ejemplo n.º 11
0
 def test_delete_upper_index_error(self):
     piece_table = PieceTable(self.long_string)
     self.assertRaises(IndexError, piece_table.delete,
                       len(self.long_string) + 1, 10)
Ejemplo n.º 12
0
 def test_delete_start(self):
     piece_table = PieceTable(self.long_string)
     piece_table.delete(0, 10)
     ret = piece_table.get_text()
     expected = self.long_string[10:]
     self.assertEqual(ret, expected)
Ejemplo n.º 13
0
 def test_delete_middle(self):
     piece_table = PieceTable(self.long_string)
     piece_table.delete(10, 10)
     ret = piece_table.get_text()
     expected = self.long_string[:10] + self.long_string[20:]
     self.assertEqual(ret, expected)
Ejemplo n.º 14
0
 def __init__(self, s):
     self._buf = PieceTable(s)
     self._update_line_starts(s)
Ejemplo n.º 15
0
 def test_integer_index(self):
     piece_table = PieceTable(self.long_string)
     self.assertEqual(self.long_string[1], piece_table[1])
Ejemplo n.º 16
0
 def test_get_piece_and_offset_upper_index_error(self):
     piece_table = PieceTable(self.long_string)
     self.assertRaises(IndexError, piece_table.get_piece_and_offset,
                       len(self.long_string) + 1)
Ejemplo n.º 17
0
 def test_get_piece_and_offset(self):
     piece_table = PieceTable(self.long_string)
     piece_index, piece_offset = piece_table.get_piece_and_offset(1)
     self.assertEqual(piece_index, 0)
     self.assertEqual(piece_offset, 1)
Ejemplo n.º 18
0
 def test_splice_index(self):
     piece_table = PieceTable(self.long_string)
     self.assertEqual(self.long_string[10:12], piece_table[10:12])
     self.assertEqual(self.long_string[-5:], piece_table[-5:])
     self.assertEqual(self.long_string[:-5], piece_table[:-5])