Fix test coverage of wordlist dialog

This commit is contained in:
Veronica Berglyd Olsen
2023-06-13 21:10:11 +02:00
parent 8d38f27eee
commit f33ba03a2f
17 changed files with 52 additions and 60 deletions
+1 -1
View File
@@ -114,7 +114,7 @@ class GuiAbout(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiAbout")
return
+1 -1
View File
@@ -113,7 +113,7 @@ class GuiDocMerge(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiDocMerge")
return
+1 -1
View File
@@ -142,7 +142,7 @@ class GuiDocSplit(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiDocSplit")
return
+1 -1
View File
@@ -87,7 +87,7 @@ class GuiPreferences(NPagedDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiPreferences")
return
+1 -1
View File
@@ -82,7 +82,7 @@ class GuiProjectDetails(NPagedDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiProjectDetails")
return
+1 -1
View File
@@ -151,7 +151,7 @@ class GuiProjectLoad(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiProjectLoad")
return
+1 -1
View File
@@ -97,7 +97,7 @@ class GuiProjectSettings(NPagedDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiProjectSettings")
return
+1 -1
View File
@@ -117,7 +117,7 @@ class GuiUpdates(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiUpdates")
return
+5 -5
View File
@@ -114,7 +114,7 @@ class GuiWordList(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiWordList")
return
@@ -122,25 +122,25 @@ class GuiWordList(QDialog):
# Slots
##
def _doAdd(self) -> bool:
def _doAdd(self):
"""Add a new word to the word list."""
word = self.newEntry.text().strip()
if word == "":
self.mainGui.makeAlert(self.tr(
"Cannot add a blank word."
), nwAlert.ERROR)
return False
return
if self.listBox.findItems(word, Qt.MatchExactly):
self.mainGui.makeAlert(self.tr(
"The word '{0}' is already in the word list."
).format(word), nwAlert.ERROR)
return False
return
self.listBox.addItem(word)
self.newEntry.setText("")
return True
return
def _doDelete(self):
"""Delete the selected item."""
+1 -1
View File
@@ -111,7 +111,7 @@ class GuiLipsum(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiLipsum")
return
+1 -2
View File
@@ -235,8 +235,7 @@ class GuiManuscriptBuild(QDialog):
return
def __del__(self):
"""For debug use only."""
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiManuscriptBuild")
return
+1 -2
View File
@@ -196,8 +196,7 @@ class GuiManuscript(QDialog):
return
def __del__(self):
"""For debug use only."""
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiManuscript")
return
+1 -2
View File
@@ -167,8 +167,7 @@ class GuiBuildSettings(QDialog):
return
def __del__(self):
"""For debug use only."""
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiBuildSettings")
def loadContent(self):
+1 -1
View File
@@ -80,7 +80,7 @@ class GuiProjectWizard(QWizard):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiProjectWizard")
return
+1 -1
View File
@@ -297,7 +297,7 @@ class GuiWritingStats(QDialog):
return
def __del__(self):
def __del__(self): # pragma: no cover
logger.debug("Delete: GuiWritingStats")
return
+4 -8
View File
@@ -61,8 +61,7 @@ def resetConfigVars():
@pytest.fixture(scope="session", autouse=True)
def sessionFixture():
"""A session wide fixture to set up the test environment.
"""
"""A session wide fixture to set up the test environment."""
if _TMP_ROOT.exists():
shutil.rmtree(_TMP_ROOT)
_TMP_ROOT.mkdir()
@@ -111,8 +110,7 @@ def tstPaths():
@pytest.fixture(scope="function")
def fncPath():
"""A temporary folder for a single test function.
"""
"""A temporary folder for a single test function."""
fncPath = _TMP_ROOT / "function"
if fncPath.is_dir():
shutil.rmtree(fncPath)
@@ -139,16 +137,14 @@ def projPath(fncPath):
@pytest.fixture(scope="function")
def mockGUI():
"""Create a mock instance of novelWriter's main GUI class.
"""
"""Create a mock instance of novelWriter's main GUI class."""
theGui = MockGuiMain()
return theGui
@pytest.fixture(scope="function")
def nwGUI(qtbot, monkeypatch, functionFixture):
"""Create an instance of the novelWriter GUI.
"""
"""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)
+29 -30
View File
@@ -24,9 +24,9 @@ import pytest
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog, QAction
from tools import buildTestProject, writeFile, readFile, getGuiItem
from mocked import causeOSError
from tools import buildTestProject, getGuiItem
from novelwriter.core.spellcheck import UserDictionary
from novelwriter.dialogs.wordlist import GuiWordList
@@ -42,7 +42,6 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
# Open project
nwGUI.openProject(projPath)
dictFile = projPath / "meta" / "wordlist.txt"
# Load the dialog
nwGUI.mainMenu.aEditWordList.activate(QAction.Trigger)
@@ -56,15 +55,15 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
assert wList.listBox.count() == 0
# Add words
writeFile(dictFile, (
"word_a\n"
"word_c\n"
"word_g\n"
" \n" # Should be ignored
"word_f\n"
"word_b\n"
))
assert wList._loadWordList()
userDict = UserDictionary(nwGUI.theProject)
userDict.add("word_a")
userDict.add("word_c")
userDict.add("word_g")
userDict.add("word_f")
userDict.add("word_b")
userDict.save()
wList._loadWordList()
# Check that the content was loaded
assert wList.listBox.item(0).text() == "word_a"
@@ -72,18 +71,22 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
assert wList.listBox.item(2).text() == "word_c"
assert wList.listBox.item(3).text() == "word_f"
assert wList.listBox.item(4).text() == "word_g"
assert wList.listBox.count() == 5
# Add a blank word
# Add a blank word, which is ignored
wList.newEntry.setText(" ")
assert not wList._doAdd()
wList._doAdd()
assert wList.listBox.count() == 5
# Add an existing word
# Add an existing word, which is ignored
wList.newEntry.setText("word_c")
assert not wList._doAdd()
wList._doAdd()
assert wList.listBox.count() == 5
# Add a new word
wList.newEntry.setText("word_d")
assert wList._doAdd()
wList._doAdd()
assert wList.listBox.count() == 6
# Check that the content now
assert wList.listBox.item(0).text() == "word_a"
@@ -95,7 +98,7 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
# Delete a word
wList.newEntry.setText("delete_me")
assert wList._doAdd()
wList._doAdd()
assert wList.listBox.item(0).text() == "delete_me"
delItem = wList.listBox.findItems("delete_me", Qt.MatchExactly)[0]
@@ -107,18 +110,14 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
# Save files
assert wList._doSave()
assert readFile(dictFile) == (
"word_a\n"
"word_b\n"
"word_c\n"
"word_d\n"
"word_f\n"
"word_g\n"
)
# Save again and make it fail
monkeypatch.setattr("builtins.open", causeOSError)
assert not wList._doSave()
userDict.load()
assert len(list(userDict)) == 6
assert "word_a" in userDict
assert "word_b" in userDict
assert "word_c" in userDict
assert "word_d" in userDict
assert "word_f" in userDict
assert "word_g" in userDict
# qtbot.stop()
wList._doClose()