def setUp(self): self.doc = UndoableDocument()
class UndoableDocumentTestCase(unittest.TestCase): def setUp(self): self.doc = UndoableDocument() def test_undo_insert(self): self.doc.insert(0, "here is some text") self.assertEqual(self.doc.text, "here is some text") self.assert_(self.doc.can_undo()) self.doc.undo() self.assert_(not self.doc.can_undo()) self.assertEqual(self.doc.text, "") self.doc.insert(0, "here is some text") self.doc.insert(2, "3") self.assertEqual(self.doc.text, "he3re is some text") self.assert_(self.doc.can_undo()) self.doc.undo() self.assert_(self.doc.can_undo()) self.assertEqual(self.doc.text, "here is some text") def test_undo_remove(self): self.doc.insert(0, "here is some text") self.assertEqual(self.doc.text, "here is some text") self.doc.remove(1, 2) self.assertEqual(self.doc.text, "he is some text") self.assert_(self.doc.can_undo()) self.doc.undo() self.assertEqual(self.doc.text, "here is some text") def test_undo_insert_search(self): self.doc.insert(0, "here is some text\nhello there") self.doc.search("hello") self.assertEqual(self.doc.visible_text, "hello there") self.doc.insert(0, "123 ") self.assertEqual(self.doc.visible_text, "123 hello there") self.assertEqual(self.doc.text, "here is some text\n123 hello there") self.assert_(self.doc.can_undo()) self.doc.undo() self.assertEqual(self.doc.visible_text, "hello there") self.assertEqual(self.doc.text, "here is some text\nhello there") self.assertEqual(self.doc.current_search, "hello") self.assert_(self.doc.can_undo()) self.doc.undo() # undo search self.assert_(self.doc.can_undo()) self.doc.undo() # undo first insert self.assert_(not self.doc.can_undo()) self.assertEqual(self.doc.current_search, "") self.assertEqual(self.doc.visible_text, "") self.assertEqual(self.doc.text, "") def test_undo_remove_search(self): self.doc.insert(0, "here is some text\nhello there") self.doc.search("hello") self.assertEqual(self.doc.visible_text, "hello there") self.doc.remove(1, 2) self.assertEqual(self.doc.visible_text, "hlo there") self.assert_(self.doc.can_undo()) self.doc.undo() self.assertEqual(self.doc.current_search, "hello") self.assertEqual(self.doc.visible_text, "hello there") self.assertEqual(self.doc.text, "here is some text\nhello there") def test_undo_remove_search_accross_visible(self): self.doc.insert(0, "hello today\nhere is some text\nhello there") self.doc.search("hello") self.assertEqual(self.doc.visible_text, "hello today\nhello there") self.doc.remove(10, 3) self.assertEqual(self.doc.visible_text, "hello todaello there") self.assertEqual(self.doc.text, "hello todaello there\nhere is some text\n") self.assert_(self.doc.can_undo()) self.doc.undo() self.assertEqual(self.doc.current_search, "hello") self.assertEqual(self.doc.visible_text, "hello today\nhello there") self.assertEqual(self.doc.text, "hello today\nhere is some text\nhello there") self.doc.undo() # undo search self.doc.undo() # undo first insert self.assertEqual(self.doc.visible_text, self.doc.text) self.assertEqual(self.doc.text, "") def test_undo_redo(self): self.doc.insert(0, "hello today\nhere is some text\nhello there") self.assertEqual(self.doc.visible_text, "hello today\nhere is some text\nhello there") self.doc.search("hello") self.assertEqual(self.doc.visible_text, "hello today\nhello there") self.assert_(not self.doc.can_redo()) self.assertEqual(self.doc.current_search, "hello") self.doc.undo() # undo the search self.assertEqual(self.doc.visible_text, "hello today\nhere is some text\nhello there") self.assertEqual(self.doc.current_search, "") self.assert_(self.doc.can_redo()) self.doc.redo() # redo the search self.assertEqual(self.doc.visible_text, "hello today\nhello there") self.assert_(not self.doc.can_redo()) self.assertEqual(self.doc.current_search, "hello") self.doc.undo() # undo the search again # now do something else and verify we have lost the redo self.assert_(self.doc.can_redo()) self.doc.insert(0, "12") self.assert_(not self.doc.can_redo()) self.assertEqual(self.doc.visible_text, "12hello today\nhere is some text\nhello there") self.doc.undo() self.assertEqual(self.doc.visible_text, "hello today\nhere is some text\nhello there") self.doc.redo() self.assertEqual(self.doc.visible_text, "12hello today\nhere is some text\nhello there")