def _get_or_create_file_buffer(self, path): ''' If the file is already opened in some tab, share file buffer. ''' if not path: return FileBuffer() for tab in self.tabs: if tab.file_buffer.path \ and path \ and os.path.samefile(tab.file_buffer.path, path): return tab.file_buffer return FileBuffer(path)
def test_invalid_insertions(self): buffer = FileBuffer() buffer.insert(0, b'x') with self.assertRaises(AssertionError): buffer.insert(2, b'whatever') with self.assertRaises(AssertionError): buffer.insert(-1, b'w')
def do_test(self, spec): window, input_texts, _, texts_w_hash = parse_spec(spec) buffer = FileBuffer() input_offset = 0 for text in input_texts: buffer.insert(input_offset, text) input_offset += len(text) self.assertEqual( buffer.get(window.start_offset, window.size), b''.join(texts_w_hash))
def test_inserting_with_split(self): buffer = FileBuffer() buffer.insert(0, b'abcd') buffer.insert(4, b'efgh') buffer.insert(3, b'!') self.assertEqual(buffer.windows, [ BufferContentWindow(0, b'abc'), BufferContentWindow(3, b'!'), BufferContentWindow(4, b'd'), BufferContentWindow(5, b'efgh'), ])
def test_inserting_at_edges(self): buffer = FileBuffer() buffer.insert(0, b'4') buffer.insert(0, b'2') buffer.insert(1, b'3') buffer.insert(0, b'1') self.assertEqual(buffer.windows, [ BufferContentWindow(0, b'1'), BufferContentWindow(1, b'2'), BufferContentWindow(2, b'3'), BufferContentWindow(3, b'4'), ])
def do_test(self, spec): window, input_texts, texts_wo_hash, _ = parse_spec(spec) buffer = FileBuffer() input_offset = 0 for text in input_texts: buffer.insert(input_offset, text) input_offset += len(text) buffer.delete(window.start_offset, window.size) output_offset = 0 output_windows = [BufferContentWindow(0, text) for text in texts_wo_hash] for output_range in output_windows: output_range.start_offset = output_offset output_offset += output_range.size self.assertEqual(buffer.windows, output_windows)
def test_empty_insertions(self): buffer = FileBuffer() buffer.insert(0, b'') self.assertEqual(buffer.windows, [])
def test_simple_inserting(self): buffer = FileBuffer() buffer.insert(0, b'test') self.assertEqual(buffer.windows, [BufferContentWindow(0, b'test')])