class TestDocument(unittest.TestCase): def setUp(self): self.d = Document() self.d.insert("a") def test_cursor(self): self.assertEqual(self.d.cursor.position, 1) self.d.save("tst") try: remove("tst") except OSError: pass self.d.cursor.back() self.d.delete() self.assertEqual(self.d.cursor.position, 0) def test_multiple_chars_and_escape(self): self.d.cursor.home() self.d.delete() string = ["h", "e", "l", "l", "o", "\n", "w", "o", "r", "l", "d", "!"] for i in string: self.d.insert(i) self.assertEqual(self.d.string, "hello\nworld!") def test_string_property(self): self.assertEqual(self.d.string, "a")
class CodeTools(object): def __init__(self, window): self.document = Document(window.get_active_document()) self.view = View(window.get_active_view()) self.start = None self.end = None self.lines = 0 self.text = "" self.selection = 0 self._initialize_text() def _initialize_text(self): self._get_code_selection() self.current_start_offset = self.start.get_offset() self.current_start_line_offset = self.start.get_line_offset() self.current_end_offset = self.end.get_offset() self.current_insert_offset = self.document.get_insert_offset() self._get_selection_character_count() self._define_selection_block() self.view.move_to_end_of_selection_block(self.document, self.end.get_line()) self.text = self.document.read(self.start, self.end) def _get_code_selection(self): selection = self.document.get_selection() insert = self.document.get_insert() (self.start, self.end) = selection or (insert, insert.copy()) return (self.start, self.end) def _get_selection_character_count(self): self.selection = math.fabs(self.current_end_offset - self.current_start_offset) return self.selection def _define_selection_block(self): self.lines = self.end.get_line()-self.start.get_line() self.start.set_line_offset(0) if not self.end.ends_line(): self.end.forward_to_line_end() def select_and_delete(self): self.view.select_by_offsets( self.start.get_offset(), self.document.get_insert_offset(), self.end.get_offset() ) self.document.delete() def comment_text(self): self.select_and_delete() code_comment = CodeComment(self.document, self.text) code_comment.analyze_comment_tags() code_comment.verify_commenting() code_comment.do_comment_or_uncomment() (self.current_start_offset, self.selection) = code_comment.calculate_new_position_values(self.current_start_offset, self.selection) self.text = code_comment.change_text() def copy_text_to_clipboard(self): clipboard = gtk.clipboard_get() clipboard.set_text(self.text) clipboard.store() def erase_line(self, down): self.select_and_delete() end_line = self.document.get_insert().copy() end_line.set_line_offset(0) end_line.forward_line() self.document.delete(self.document.get_insert(), end_line) self.view.move_line_cursor(1 if down else -1, select=False) def __enter__(self): self.document.begin_user_action() return self def __exit__(self, type, value, traceback): self.document.end_user_action()
# a1 - переміщення курсору перед початком файлу - починає відлік з кінця файлу # (від'ємні індекси) - якщо індекс "-х" де х >= length(file) # - вставляє символ на першу позицію d.cursor.home() d.cursor.back() d.insert('l') print(d.string) # b - видалення символу, якого не існує # вкінці - list assignment index out of range (Index Error) # спочатку по аналогії до пункту а - починає видаляти з від'ємних індексів d.cursor.end() d.cursor.forward() try: d.delete() except IndexError as e: print(e) d.cursor.back() d.cursor.home() d.cursor.back() d.delete() print(d.string) # c - збереження файлу без імені # неможливо передати в цьому модулі, не виправляючи код класів # виникає FileNotFoundError - при відкритті файлу в методі save # d - введення декількох символів # Assertion Error
assert isinstance(doc, Document) assert isinstance(cursor, Cursor) assert cursor.document == doc m = Character('m', bold=True) a = Character('a') x = Character('x', underline=True) y = Character('y') assert isinstance(m, Character) assert m.character == 'm' doc.insert(m) cursor.forward() doc.insert(a) doc.insert(a) cursor.forward() cursor.forward() doc.insert(x) cursor.end() doc.insert(x) cursor.end() cursor.forward() cursor.back() doc.delete() doc.insert(y) assert doc.string == "|B:maa|U:xy" print("Testing is done successfull")