diff --git a/nw/core/spellcheck.py b/nw/core/spellcheck.py index 79c2accc..9d22afcd 100644 --- a/nw/core/spellcheck.py +++ b/nw/core/spellcheck.py @@ -153,7 +153,7 @@ class NWSpellEnchant(NWSpellCheck): try: import enchant if self.theBroker is not None: - logger.verbose("Deleting old pyenchant Broker") + logger.verbose("Deleting old pyenchant broker") del self.theBroker self.theBroker = enchant.Broker() diff --git a/nw/gui/wordlist.py b/nw/gui/wordlist.py index 0d3e3072..f5b6fe80 100644 --- a/nw/gui/wordlist.py +++ b/nw/gui/wordlist.py @@ -160,13 +160,14 @@ class GuiWordList(QDialog): logger.error("Could not save new word list") nw.logException() self.reject() + return False if os.path.isfile(dctFile): os.unlink(dctFile) os.rename(tmpFile, dctFile) self.accept() - return + return True def _doClose(self): """Close without saving the word list. diff --git a/tests/test_gui/test_gui_dialogs.py b/tests/test_gui/test_gui_dialogs.py index 243434d3..f4f6f27d 100644 --- a/tests/test_gui/test_gui_dialogs.py +++ b/tests/test_gui/test_gui_dialogs.py @@ -61,7 +61,7 @@ def testGuiDialogs_Quotes(qtbot, monkeypatch, nwGUI, nwMinimal): # END Test testDialogs_Quotes @pytest.mark.gui -def testGuiDialogs_Other(qtbot, monkeypatch, nwGUI, nwMinimal, tmpDir): +def testGuiDialogs_Other(qtbot, monkeypatch, nwGUI, tmpDir): """Various other dialog tests. """ monkeypatch.setattr(QFileDialog, "getExistingDirectory", lambda *args, **kwargs: tmpDir) diff --git a/tests/test_gui/test_gui_wordlist.py b/tests/test_gui/test_gui_wordlist.py new file mode 100644 index 00000000..fdbc1725 --- /dev/null +++ b/tests/test_gui/test_gui_wordlist.py @@ -0,0 +1,133 @@ +# -*- coding: utf-8 -*- +""" +novelWriter – Other Dialog Classes Tester +========================================= + +This file is a part of novelWriter +Copyright 2018–2021, Veronica Berglyd Olsen + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" + +import os + +from PyQt5.QtCore import Qt +from PyQt5.QtWidgets import QDialog, QMessageBox, QAction + +from tools import writeFile, readFile, getGuiItem +from dummy import causeOSError + +from nw.gui.wordlist import GuiWordList +from nw.constants import nwFiles + +keyDelay = 2 +typeDelay = 1 +stepDelay = 20 + +def testGuiWordList_Dialog(qtbot, monkeypatch, nwGUI, nwMinimal, tmpDir): + """test the word list editor. + """ + monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes) + monkeypatch.setattr(QMessageBox, "critical", lambda *args: QMessageBox.Yes) + monkeypatch.setattr(GuiWordList, "exec_", lambda *args: None) + monkeypatch.setattr(GuiWordList, "result", lambda *args: QDialog.Accepted) + monkeypatch.setattr(GuiWordList, "accept", lambda *args: None) + + # Open project + nwGUI.openProject(nwMinimal) + qtbot.wait(stepDelay) + dictFile = os.path.join(nwMinimal, "meta", nwFiles.PROJ_DICT) + + # Load the dialog + nwGUI.mainMenu.aEditWordList.activate(QAction.Trigger) + qtbot.waitUntil(lambda: getGuiItem("GuiWordList") is not None, timeout=1000) + + wList = getGuiItem("GuiWordList") + assert isinstance(wList, GuiWordList) + wList.show() + qtbot.wait(stepDelay) + + # List should be blank + 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" + )) + qtbot.wait(stepDelay) + assert wList._loadWordList() + + # Check that the content was loaded + assert wList.listBox.item(0).text() == "word_a" + assert wList.listBox.item(1).text() == "word_b" + assert wList.listBox.item(2).text() == "word_c" + assert wList.listBox.item(3).text() == "word_f" + assert wList.listBox.item(4).text() == "word_g" + + # Add a blank word + wList.newEntry.setText(" ") + assert not wList._doAdd() + + # Add an existing word + wList.newEntry.setText("word_c") + assert not wList._doAdd() + + # Add a new word + wList.newEntry.setText("word_d") + assert wList._doAdd() + + # Check that the content now + assert wList.listBox.item(0).text() == "word_a" + assert wList.listBox.item(1).text() == "word_b" + assert wList.listBox.item(2).text() == "word_c" + assert wList.listBox.item(3).text() == "word_d" + assert wList.listBox.item(4).text() == "word_f" + assert wList.listBox.item(5).text() == "word_g" + + # Delete a word + wList.newEntry.setText("delete_me") + assert wList._doAdd() + assert wList.listBox.item(0).text() == "delete_me" + + delItem = wList.listBox.findItems("delete_me", Qt.MatchExactly)[0] + assert delItem.text() == "delete_me" + delItem.setSelected(True) + wList._doDelete() + assert wList.listBox.findItems("delete_me", Qt.MatchExactly) == [] + assert wList.listBox.item(0).text() == "word_a" + + # 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() + + # qtbot.stopForInteraction() + wList._doClose() + +# END Test testGuiWordList_Dialog