diff --git a/tests/test_core/test_core_spellcheck.py b/tests/test_core/test_core_spellcheck.py index 4835e667..01615809 100644 --- a/tests/test_core/test_core_spellcheck.py +++ b/tests/test_core/test_core_spellcheck.py @@ -26,29 +26,57 @@ import pytest from mock import causeOSError from tools import readFile, writeFile -from novelwriter.core.spellcheck import NWSpellEnchant +from novelwriter.core.spellcheck import FakeEnchant, NWSpellEnchant @pytest.mark.core -def testCoreSpell_Enchant(monkeypatch, tmpDir): - """Test the pyenchant spell checker +def testCoreSpell_FakeEnchant(monkeypatch): + """Test the FakeEnchant spell checker fallback. """ - wList = os.path.join(tmpDir, "wordlist.txt") - writeFile(wList, "a_word\nb_word\nc_word\n") - - # Block the enchant package (and trigger the default class) + # Make package import fail with monkeypatch.context() as mp: mp.setitem(sys.modules, "enchant", None) spChk = NWSpellEnchant() + spChk.setLanguage("en", "") + assert isinstance(spChk._theDict, FakeEnchant) - spChk.setLanguage("en", wList) - assert spChk.setLanguage("", "") is None - assert spChk.checkWord("") is True - assert spChk.suggestWords("") == [] + # Request a non-existent dictionary + spChk = NWSpellEnchant() + spChk.setLanguage("whatchamajig", "") + assert isinstance(spChk._theDict, FakeEnchant) + + # Request an emety language string + # See issue https://github.com/vkbo/novelWriter/issues/1096 + spChk = NWSpellEnchant() + spChk.setLanguage("", "") + assert isinstance(spChk._theDict, FakeEnchant) + + # FakeEnchant should handle requests + fkChk = FakeEnchant() + assert fkChk.tag == "" + assert fkChk.provider.name == "" + assert fkChk.check("whatchamajig") is True + assert fkChk.suggest("whatchamajig") == [] + assert fkChk.add_to_session("whatchamajig") is None + +# END Test testCoreSpell_FakeEnchant + + +@pytest.mark.core +def testCoreSpell_Enchant(monkeypatch, fncDir): + """Test the pyenchant spell checker. + """ + wList = os.path.join(fncDir, "wordlist.txt") + writeFile(wList, "a_word\nb_word\nc_word\n") + + # Break the enchant package, and check error handling + with monkeypatch.context() as mp: + mp.setitem(sys.modules, "enchant", None) + spChk = NWSpellEnchant() assert spChk.listDictionaries() == [] assert spChk.describeDict() == ("", "") - # Break the enchant package, and check error handling + # Set the dict to None, and check dictionary call error handling spChk = NWSpellEnchant() spChk.theDict = None assert spChk.checkWord("word") is True @@ -59,6 +87,7 @@ def testCoreSpell_Enchant(monkeypatch, tmpDir): spChk = NWSpellEnchant() spChk.setLanguage("en", wList) spChk.setLanguage("en", wList) + assert spChk.spellLanguage == "en" # Add a word to the user's dictionary assert spChk._readProjectDictionary("stuff") is False @@ -102,3 +131,35 @@ def testCoreSpell_Enchant(monkeypatch, tmpDir): assert aName != "" # END Test testCoreSpell_Enchant + + +@pytest.mark.core +def testCoreSpell_SessionWords(fncDir): + """Test the handling of the custom word list in the spell checker. + New project sessions should not inherit the project word list from + other sessions, so this test checks that they don't bleed through. + """ + wList1 = os.path.join(fncDir, "wordlist1.txt") + wList2 = os.path.join(fncDir, "wordlist2.txt") + writeFile(wList1, "a_word\nb_word\nc_word\n") + writeFile(wList2, "d_word\ne_word\nf_word\n") + + spChk = NWSpellEnchant() + + spChk.setLanguage("en", wList1) + assert spChk.checkWord("a_word") is True + assert spChk.checkWord("b_word") is True + assert spChk.checkWord("c_word") is True + assert spChk.checkWord("d_word") is False + assert spChk.checkWord("e_word") is False + assert spChk.checkWord("f_word") is False + + spChk.setLanguage("en", wList2) + assert spChk.checkWord("a_word") is False + assert spChk.checkWord("b_word") is False + assert spChk.checkWord("c_word") is False + assert spChk.checkWord("d_word") is True + assert spChk.checkWord("e_word") is True + assert spChk.checkWord("f_word") is True + +# END Test testCoreSpell_SessionWords