From 8ed85363e2162140c8790b419be6e4fe5b02ece1 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 25 Nov 2023 17:50:59 +0100 Subject: [PATCH] Add test coverage and update the highlighter --- novelwriter/gui/dochighlight.py | 16 ++++++-------- tests/test_core/test_core_index.py | 35 ++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/novelwriter/gui/dochighlight.py b/novelwriter/gui/dochighlight.py index 6a77f267..b6850097 100644 --- a/novelwriter/gui/dochighlight.py +++ b/novelwriter/gui/dochighlight.py @@ -36,6 +36,8 @@ from PyQt5.QtGui import ( from novelwriter import CONFIG, SHARED from novelwriter.common import checkInt from novelwriter.constants import nwRegEx, nwUnicode +from novelwriter.core.index import processComment +from novelwriter.enum import nwComment logger = logging.getLogger(__name__) @@ -352,16 +354,12 @@ class GuiDocHighlighter(QSyntaxHighlighter): elif text.startswith("%"): # Comments self.setCurrentBlockState(self.BLOCK_TEXT) - toCheck = text[1:].lstrip() - synTag = toCheck[:9].lower() - tLen = len(text) - cLen = len(toCheck) - cOff = tLen - cLen - if synTag == "synopsis:": - self.setFormat(0, cOff+9, self._hStyles["modifier"]) - self.setFormat(cOff+9, tLen, self._hStyles["hidden"]) + cStyle, _, cPos = processComment(text) + if cStyle == nwComment.PLAIN: + self.setFormat(0, len(text), self._hStyles["hidden"]) else: - self.setFormat(0, tLen, self._hStyles["hidden"]) + self.setFormat(0, cPos, self._hStyles["modifier"]) + self.setFormat(cPos, len(text), self._hStyles["hidden"]) else: # Text Paragraph diff --git a/tests/test_core/test_core_index.py b/tests/test_core/test_core_index.py index 9e7856e8..d12ab318 100644 --- a/tests/test_core/test_core_index.py +++ b/tests/test_core/test_core_index.py @@ -29,9 +29,9 @@ from mocked import causeException from novelwriter.core.item import NWItem from tools import C, buildTestProject, cmpFiles, writeFile -from novelwriter.enum import nwItemClass, nwItemLayout +from novelwriter.enum import nwComment, nwItemClass, nwItemLayout from novelwriter.constants import nwFiles -from novelwriter.core.index import IndexItem, NWIndex, countWords, TagsIndex +from novelwriter.core.index import IndexItem, NWIndex, countWords, TagsIndex, processComment from novelwriter.core.project import NWProject @@ -1264,7 +1264,34 @@ def testCoreIndex_ItemIndex(mockGUI, fncPath, mockRnd): @pytest.mark.core -def testCoreIndex_CountWords(): +def testCoreIndex_processComment(): + """Test the comment processing function.""" + # Regular comment + assert processComment("%Hi") == (nwComment.PLAIN, "Hi", 0) + assert processComment("% Hi") == (nwComment.PLAIN, "Hi", 0) + assert processComment("% Hi:you") == (nwComment.PLAIN, "Hi:you", 0) + + # Synopsis + assert processComment("%synopsis:") == (nwComment.PLAIN, "synopsis:", 0) + assert processComment("%synopsis: Hi") == (nwComment.SYNOPSIS, "Hi", 10) + assert processComment("% synopsis: Hi") == (nwComment.SYNOPSIS, "Hi", 11) + assert processComment("% synopsis : Hi") == (nwComment.SYNOPSIS, "Hi", 13) + assert processComment("% Synopsis : Hi") == (nwComment.SYNOPSIS, "Hi", 15) + assert processComment("% \t SYNOPSIS : Hi") == (nwComment.SYNOPSIS, "Hi", 16) + + # Brief + assert processComment("%brief:") == (nwComment.PLAIN, "brief:", 0) + assert processComment("%brief: Hi") == (nwComment.BRIEF, "Hi", 7) + assert processComment("% brief: Hi") == (nwComment.BRIEF, "Hi", 8) + assert processComment("% brief : Hi") == (nwComment.BRIEF, "Hi", 10) + assert processComment("% Brief : Hi") == (nwComment.BRIEF, "Hi", 12) + assert processComment("% \t BRIEF : Hi") == (nwComment.BRIEF, "Hi", 13) + +# END Test testCoreIndex_processComment + + +@pytest.mark.core +def testCoreIndex_countWords(): """Test the word counter and the exclusion filers.""" # Non-Text assert countWords(None) == (0, 0, 0) # type: ignore @@ -1362,4 +1389,4 @@ def testCoreIndex_CountWords(): assert wC == 14 assert pC == 2 -# END Test testCoreIndex_CountWords +# END Test testCoreIndex_countWords