Update tests of spell check classes

This commit is contained in:
Veronica Berglyd Olsen
2022-08-17 21:49:59 +02:00
parent c917fc9c9c
commit 2178391bcd
+73 -12
View File
@@ -26,29 +26,57 @@ import pytest
from mock import causeOSError from mock import causeOSError
from tools import readFile, writeFile from tools import readFile, writeFile
from novelwriter.core.spellcheck import NWSpellEnchant from novelwriter.core.spellcheck import FakeEnchant, NWSpellEnchant
@pytest.mark.core @pytest.mark.core
def testCoreSpell_Enchant(monkeypatch, tmpDir): def testCoreSpell_FakeEnchant(monkeypatch):
"""Test the pyenchant spell checker """Test the FakeEnchant spell checker fallback.
""" """
wList = os.path.join(tmpDir, "wordlist.txt") # Make package import fail
writeFile(wList, "a_word\nb_word\nc_word\n")
# Block the enchant package (and trigger the default class)
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setitem(sys.modules, "enchant", None) mp.setitem(sys.modules, "enchant", None)
spChk = NWSpellEnchant() spChk = NWSpellEnchant()
spChk.setLanguage("en", "")
assert isinstance(spChk._theDict, FakeEnchant)
spChk.setLanguage("en", wList) # Request a non-existent dictionary
assert spChk.setLanguage("", "") is None spChk = NWSpellEnchant()
assert spChk.checkWord("") is True spChk.setLanguage("whatchamajig", "")
assert spChk.suggestWords("") == [] 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.listDictionaries() == []
assert spChk.describeDict() == ("", "") 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 = NWSpellEnchant()
spChk.theDict = None spChk.theDict = None
assert spChk.checkWord("word") is True assert spChk.checkWord("word") is True
@@ -59,6 +87,7 @@ def testCoreSpell_Enchant(monkeypatch, tmpDir):
spChk = NWSpellEnchant() spChk = NWSpellEnchant()
spChk.setLanguage("en", wList) spChk.setLanguage("en", wList)
spChk.setLanguage("en", wList) spChk.setLanguage("en", wList)
assert spChk.spellLanguage == "en"
# Add a word to the user's dictionary # Add a word to the user's dictionary
assert spChk._readProjectDictionary("stuff") is False assert spChk._readProjectDictionary("stuff") is False
@@ -102,3 +131,35 @@ def testCoreSpell_Enchant(monkeypatch, tmpDir):
assert aName != "" assert aName != ""
# END Test testCoreSpell_Enchant # 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