From 3e711ebe7f3fc5efb3562ea617e6bf1303b1e3ce Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 13 Apr 2024 00:37:21 +0200 Subject: [PATCH 1/3] Fix word counter bug (#1816) --- novelwriter/text/counting.py | 1 + tests/test_text/test_core_counting.py | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/novelwriter/text/counting.py b/novelwriter/text/counting.py index 59974ee5..f63ee2a6 100644 --- a/novelwriter/text/counting.py +++ b/novelwriter/text/counting.py @@ -57,6 +57,7 @@ def preProcessText(text: str, keepHeaders: bool = True) -> list[str]: continue if line[0] == ">": line = line.lstrip(">").lstrip(" ") + if line: # Above check can return empty line (Issue #1816) if line[-1] == "<": line = line.rstrip("<").rstrip(" ") if "[" in line: diff --git a/tests/test_text/test_core_counting.py b/tests/test_text/test_core_counting.py index bab0596e..0c6bd448 100644 --- a/tests/test_text/test_core_counting.py +++ b/tests/test_text/test_core_counting.py @@ -29,7 +29,7 @@ from novelwriter.text.counting import bodyTextCounter, preProcessText, standardC def testTextCounting_preProcessText(): """Test the text preprocessor for counters.""" # Not Text - assert preProcessText(None) == [] + assert preProcessText(None) == [] # type: ignore # No Text assert preProcessText("") == [] @@ -81,6 +81,10 @@ def testTextCounting_standardCounter(): assert standardCounter(None) == (0, 0, 0) # type: ignore assert standardCounter(1234) == (0, 0, 0) # type: ignore + # Test Corner Cases, Bug #1816 + assert standardCounter("> ") == (0, 0, 0) + assert standardCounter(" <") == (0, 0, 0) + # General Text cC, wC, pC = standardCounter(( "#! Title\n\n" @@ -182,7 +186,7 @@ def testTextCounting_standardCounter(): def testTextCounting_bodyTextCounter(): """Test the body text counter.""" # Not Text - assert bodyTextCounter(None) == (0, 0, 0) + assert bodyTextCounter(None) == (0, 0, 0) # type: ignore # General Text wC, cC, sC = bodyTextCounter(( From 2f55055c75f8b1a7924287fe75c2a6ad958df86f Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 13 Apr 2024 00:38:29 +0200 Subject: [PATCH 2/3] Fix comment --- novelwriter/text/counting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/novelwriter/text/counting.py b/novelwriter/text/counting.py index f63ee2a6..61804edb 100644 --- a/novelwriter/text/counting.py +++ b/novelwriter/text/counting.py @@ -57,7 +57,7 @@ def preProcessText(text: str, keepHeaders: bool = True) -> list[str]: continue if line[0] == ">": line = line.lstrip(">").lstrip(" ") - if line: # Above check can return empty line (Issue #1816) + if line: # Above block can return empty line (Issue #1816) if line[-1] == "<": line = line.rstrip("<").rstrip(" ") if "[" in line: From 4c86c1a75acec1d56327d4ab6dc5300971212343 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 13 Apr 2024 00:42:26 +0200 Subject: [PATCH 3/3] Improve test coverage --- tests/test_text/test_core_counting.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/test_text/test_core_counting.py b/tests/test_text/test_core_counting.py index 0c6bd448..05780c49 100644 --- a/tests/test_text/test_core_counting.py +++ b/tests/test_text/test_core_counting.py @@ -92,7 +92,8 @@ def testTextCounting_standardCounter(): "# Heading One\n" "## Heading Two\n" "### Heading Three\n" - "#### Heading Four\n\n" + "###! Heading Four\n" + "#### Heading Five\n\n" "@tag: value\n\n" "% A comment that should not be counted.\n\n" "The first paragraph.\n\n" @@ -100,8 +101,8 @@ def testTextCounting_standardCounter(): "The third paragraph.\n\n" "Dashes\u2013and even longer\u2014dashes." )) - assert cC == 151 - assert wC == 24 + assert cC == 163 + assert wC == 26 assert pC == 4 # Text Alignment