def __init__(self, parent): super(TextEditor, self).__init__(parent) self.controller = TextEditorController() self.setAcceptDrops(True) self.setFrameShape(QtGui.QFrame.NoFrame) self.setFrameShadow(QtGui.QFrame.Plain)
class TextEditor(QtGui.QTextEdit): """ Text editor view. """ def __init__(self, parent): super(TextEditor, self).__init__(parent) self.controller = TextEditorController() self.setAcceptDrops(True) self.setFrameShape(QtGui.QFrame.NoFrame) self.setFrameShadow(QtGui.QFrame.Plain) self.highlighter = Highlighter(self.document()) def dropEvent(self, event): filepath = str(event.mimeData().urls()[0].toLocalFile()) success, document = self.controller.open(filepath) if success: self.setPlainText(document.text) else: QtGui.QMessageBox.critical(self, "Error", "The file <b>%s</b> doesn't exists." % filepath, QtGui.QMessageBox.Ok)
class DocumentModelTest(unittest.TestCase): def setUp(self): self.texteditor_controller = TextEditorController() def tearDown(self): test_filepath = os.path.join(os.getcwd(), "test_file") if os.path.exists(test_filepath): os.remove(test_filepath) def test_open_existent_file(self): text_file = open("test_file", "w") text_file.close() success, document = self.texteditor_controller.open("test_file") self.assertTrue(success) self.assertEquals("test_file", document.path) def test_open_nonexistent_file(self): success, document = self.texteditor_controller.open("afilethatnoexists") self.assertFalse(success) def test_content_save_existent_file(self): text_file = open("test_file", "w") text_file.close() success = self.texteditor_controller.save("this is a test file", "test_file") text_file = open("test_file", "r") text_file_content = text_file.read() text_file.close() self.assertTrue(success) self.assertEquals("this is a test file", text_file_content) def test_save_inexistent_file(self): #path isn't defined success = self.texteditor_controller.save("this is a test file") self.assertFalse(success) def test_create_save_as_file(self): save_status = self.texteditor_controller.save("this is a test file", "test_file") self.assertTrue(save_status) self.assertTrue(os.path.exists("test_file")) self.assertEquals("test_file", self.texteditor_controller.document.path) def test_content_save_as_file(self): success = self.texteditor_controller.save("this is a test file", "test_file") text_file = open("test_file", "r") text_file_content = text_file.read() text_file.close() self.assertTrue(success) self.assertEquals("this is a test file", text_file_content) self.assertEquals("this is a test file", self.texteditor_controller.document.text)
class TextEditor(QtGui.QTextEdit): """ Text editor view. """ def __init__(self, parent): super(TextEditor, self).__init__(parent) self.controller = TextEditorController() self.setAcceptDrops(True) self.setFrameShape(QtGui.QFrame.NoFrame) self.setFrameShadow(QtGui.QFrame.Plain) def dropEvent(self, event): filepath = str(event.mimeData().urls()[0].toLocalFile()) success, document = self.controller.open(filepath) if success: self.setPlainText(document.text) else: QtGui.QMessageBox.critical( self, "Error", "The file <b>%s</b> doesn't exists." % filepath, QtGui.QMessageBox.Ok)
def setUp(self): self.texteditor_controller = TextEditorController()
class DocumentModelTest(unittest.TestCase): def setUp(self): self.texteditor_controller = TextEditorController() def tearDown(self): test_filepath = os.path.join(os.getcwd(), "test_file") if os.path.exists(test_filepath): os.remove(test_filepath) def test_open_existent_file(self): text_file = open("test_file", "w") text_file.close() success, document = self.texteditor_controller.open("test_file") self.assertTrue(success) self.assertEquals("test_file", document.path) def test_open_nonexistent_file(self): success, document = self.texteditor_controller.open( "afilethatnoexists") self.assertFalse(success) def test_content_save_existent_file(self): text_file = open("test_file", "w") text_file.close() success = self.texteditor_controller.save("this is a test file", "test_file") text_file = open("test_file", "r") text_file_content = text_file.read() text_file.close() self.assertTrue(success) self.assertEquals("this is a test file", text_file_content) def test_save_inexistent_file(self): #path isn't defined success = self.texteditor_controller.save("this is a test file") self.assertFalse(success) def test_create_save_as_file(self): save_status = self.texteditor_controller.save("this is a test file", "test_file") self.assertTrue(save_status) self.assertTrue(os.path.exists("test_file")) self.assertEquals("test_file", self.texteditor_controller.document.path) def test_content_save_as_file(self): success = self.texteditor_controller.save("this is a test file", "test_file") text_file = open("test_file", "r") text_file_content = text_file.read() text_file.close() self.assertTrue(success) self.assertEquals("this is a test file", text_file_content) self.assertEquals("this is a test file", self.texteditor_controller.document.text)