Clean up code and tests
This commit is contained in:
+2
-4
@@ -145,10 +145,8 @@ def mockGUI():
|
||||
@pytest.fixture(scope="function")
|
||||
def nwGUI(qtbot, monkeypatch, functionFixture):
|
||||
"""Create an instance of the novelWriter GUI."""
|
||||
monkeypatch.setattr(QMessageBox, "warning", lambda *a: QMessageBox.Ok)
|
||||
monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Ok)
|
||||
monkeypatch.setattr(QMessageBox, "information", lambda *a: QMessageBox.Ok)
|
||||
monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes)
|
||||
monkeypatch.setattr(QMessageBox, "exec_", lambda *a: None)
|
||||
monkeypatch.setattr(QMessageBox, "result", lambda *a: QMessageBox.Yes)
|
||||
|
||||
nwGUI = main(["--testmode", f"--config={_TMP_CONF}", f"--data={_TMP_CONF}"])
|
||||
qtbot.addWidget(nwGUI)
|
||||
|
||||
+8
-8
@@ -39,22 +39,22 @@ class MockGuiMain(QObject):
|
||||
# Test Variables
|
||||
self.askResponse = True
|
||||
self.lastAlert = ""
|
||||
self.lastQuestion = ("", "")
|
||||
self.lastQuestion = ""
|
||||
|
||||
return
|
||||
|
||||
def postLaunchTasks(self, cmdOpen):
|
||||
return
|
||||
|
||||
def makeAlert(self, message, level=0, exception=None):
|
||||
assert isinstance(message, str) or isinstance(message, list)
|
||||
print("%s: %s" % (str(level), message))
|
||||
self.lastAlert = str(message)
|
||||
def makeAlert(self, text, info="", detals="", level=0, exception=None):
|
||||
assert isinstance(text, str)
|
||||
print("%s: %s" % (str(level), text))
|
||||
self.lastAlert = str(text)
|
||||
return
|
||||
|
||||
def askQuestion(self, title, qustion):
|
||||
print("Question: %s" % qustion)
|
||||
self.lastQuestion = (title, qustion)
|
||||
def askQuestion(self, text, info="", details="", level=3):
|
||||
print("Question: %s" % text)
|
||||
self.lastQuestion = text
|
||||
return self.askResponse
|
||||
|
||||
def setStatus(self, theMessage):
|
||||
|
||||
@@ -224,7 +224,7 @@ def testCoreProject_Open(monkeypatch, caplog, mockGUI, fncPath, mockRnd):
|
||||
mp.setattr(ProjectXMLReader, "state", property(lambda *a: XMLReadState.WAS_LEGACY))
|
||||
mockGUI.askResponse = False
|
||||
assert theProject.openProject(fncPath) is False
|
||||
assert "The file format of your project is about to be" in mockGUI.lastQuestion[1]
|
||||
assert "The file format of your project is about to be" in mockGUI.lastQuestion
|
||||
mockGUI.askResponse = True
|
||||
|
||||
# Won't open project from newer version
|
||||
@@ -232,7 +232,7 @@ def testCoreProject_Open(monkeypatch, caplog, mockGUI, fncPath, mockRnd):
|
||||
mp.setattr(ProjectXMLReader, "hexVersion", property(lambda *a: 0x99999999))
|
||||
mockGUI.askResponse = False
|
||||
assert theProject.openProject(fncPath) is False
|
||||
assert "This project was saved by a newer version" in mockGUI.lastQuestion[1]
|
||||
assert "This project was saved by a newer version" in mockGUI.lastQuestion
|
||||
mockGUI.askResponse = True
|
||||
|
||||
# Fail checking items should still pass
|
||||
@@ -249,7 +249,7 @@ def testCoreProject_Open(monkeypatch, caplog, mockGUI, fncPath, mockRnd):
|
||||
mockGUI.askResponse = True
|
||||
theProject.index._indexBroken = True
|
||||
assert theProject.openProject(fncPath) is True
|
||||
assert "The file format of your project is about to be" in mockGUI.lastQuestion[1]
|
||||
assert "The file format of your project is about to be" in mockGUI.lastQuestion
|
||||
assert theProject.index._indexBroken is False
|
||||
|
||||
theProject.closeProject()
|
||||
|
||||
@@ -114,7 +114,7 @@ def testGuiMain_NewProject(monkeypatch, nwGUI, projPath):
|
||||
# Close project
|
||||
with monkeypatch.context() as mp:
|
||||
nwGUI.hasProject = True
|
||||
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
|
||||
assert nwGUI.newProject(projData={"projPath": projPath}) is False
|
||||
|
||||
# No project path
|
||||
|
||||
@@ -34,12 +34,9 @@ LANG_DATA = CONFIG.listLanguages(CONFIG.LANG_NW)
|
||||
@pytest.mark.skipif(not LANG_DATA, reason="No i18n Data")
|
||||
@pytest.mark.parametrize("language", [a for a, b in LANG_DATA])
|
||||
def testI18n_Localisation(qtbot, monkeypatch, language, fncPath):
|
||||
"""test loading the gui with a specific language.
|
||||
"""
|
||||
monkeypatch.setattr(QMessageBox, "warning", lambda *a: QMessageBox.Ok)
|
||||
monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Ok)
|
||||
monkeypatch.setattr(QMessageBox, "information", lambda *a: QMessageBox.Ok)
|
||||
monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes)
|
||||
"""Test loading the gui with a specific language."""
|
||||
monkeypatch.setattr(QMessageBox, "exec_", lambda *a: None)
|
||||
monkeypatch.setattr(QMessageBox, "result", lambda *a: QMessageBox.Yes)
|
||||
|
||||
# Set the test langauge
|
||||
CONFIG.guiLocale = language
|
||||
|
||||
@@ -642,7 +642,7 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd):
|
||||
|
||||
# The document isn't empty, so the message box should pop
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(QMessageBox, "question", lambda *a, **k: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a, **k: QMessageBox.No)
|
||||
assert not nwGUI.importDocument()
|
||||
assert nwGUI.docEditor.getText() == "Bar"
|
||||
|
||||
@@ -655,16 +655,16 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd):
|
||||
|
||||
theMessage = ""
|
||||
|
||||
def recordMsg(*args):
|
||||
def recordMsg(*args, **kwargs):
|
||||
nonlocal theMessage
|
||||
theMessage = args[3]
|
||||
theMessage = "%s|%s" % (args[0], kwargs["info"])
|
||||
return None
|
||||
|
||||
assert not theMessage
|
||||
monkeypatch.setattr(QMessageBox, "information", recordMsg)
|
||||
monkeypatch.setattr(nwGUI, "makeAlert", recordMsg)
|
||||
nwGUI.mainMenu.aFileDetails.activate(QAction.Trigger)
|
||||
|
||||
theBits = theMessage.split("<br>")
|
||||
theBits = theMessage.split("|")
|
||||
assert len(theBits) == 2
|
||||
assert theBits[0] == "The currently open file is saved in:"
|
||||
assert theBits[1] == str(projPath / "content" / "000000000000f.nwd")
|
||||
|
||||
@@ -397,7 +397,7 @@ def testGuiProjTree_MoveItemToTrash(qtbot, caplog, monkeypatch, nwGUI, projPath,
|
||||
|
||||
# User cancels action
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
|
||||
assert projTree.moveItemToTrash(C.hTitlePage) is False
|
||||
assert theProject.tree.isTrash(C.hTitlePage) is False
|
||||
|
||||
@@ -444,7 +444,7 @@ def testGuiProjTree_PermanentlyDeleteItem(qtbot, caplog, monkeypatch, nwGUI, pro
|
||||
|
||||
# User cancels action
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
|
||||
assert projTree.permDeleteItem(C.hTitlePage) is False
|
||||
assert C.hTitlePage in theProject.tree
|
||||
|
||||
@@ -496,7 +496,7 @@ def testGuiProjTree_EmptyTrash(qtbot, caplog, monkeypatch, nwGUI, projPath, mock
|
||||
|
||||
# User cancels
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
|
||||
assert projTree.emptyTrash() is False
|
||||
assert C.hTitlePage in theProject.tree
|
||||
assert C.hChapterDir in theProject.tree
|
||||
@@ -617,7 +617,7 @@ def testGuiProjTree_ContextMenu(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
|
||||
|
||||
# Click no on the dialog
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
|
||||
projTree._covertFolderToFile(hNewFolderOne, nwItemLayout.DOCUMENT)
|
||||
assert nwGUI.theProject.tree[hNewFolderOne].isFolderType()
|
||||
|
||||
@@ -864,7 +864,7 @@ def testGuiProjTree_Duplicate(qtbot, monkeypatch, nwGUI: GuiMain, projPath, mock
|
||||
|
||||
# Duplicate title page, but select no
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
|
||||
assert projTree._duplicateFromHandle(C.hTitlePage) is False
|
||||
assert len(nwGUI.theProject.tree) == 8
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ def testManuscriptBuild_Main(
|
||||
manus.buildPath.setText(str(fncPath))
|
||||
manus.buildName.setText("TestBuild")
|
||||
with monkeypatch.context() as mp:
|
||||
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
|
||||
mp.setattr(QMessageBox, "result", lambda *a: QMessageBox.No)
|
||||
assert manus._runBuild() is False
|
||||
|
||||
# Finish
|
||||
|
||||
Reference in New Issue
Block a user