From 3c8a0ace6096d537b8ac2cb30f1c8b2a8ac45684 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 5 Jul 2025 17:18:04 +0200 Subject: [PATCH] Add test for utf16CharMap --- novelwriter/common.py | 8 ++++---- tests/test_base/test_base_common.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/novelwriter/common.py b/novelwriter/common.py index 006d651d..e9346a24 100644 --- a/novelwriter/common.py +++ b/novelwriter/common.py @@ -500,13 +500,13 @@ def utf16CharMap(text: str) -> list[int]: ASCII, UCS-2 or UCS-4. QStrings are in UTF-16, so wide characters use 2 indices, and thus creates an offset. """ - posMap = list(range(0, len(text) + 1)) + utf16Map = list(range(0, len(text) + 1)) offset = 0 - for i, c in enumerate(text): + for i, c in enumerate(text, 1): if ord(c) > 0xffff: offset += 1 - posMap[i + 1] = i + 1 + offset - return posMap + utf16Map[i] = i + offset + return utf16Map ## diff --git a/tests/test_base/test_base_common.py b/tests/test_base/test_base_common.py index ef16baaa..76a11bd7 100644 --- a/tests/test_base/test_base_common.py +++ b/tests/test_base/test_base_common.py @@ -38,8 +38,8 @@ from novelwriter.common import ( fuzzyTime, getFileSize, hexToInt, isHandle, isItemClass, isItemLayout, isItemType, isListInstance, isTitleTag, jsonEncode, makeFileNameSafe, minmax, numberToRoman, openExternalPath, processDialogSymbols, - readTextFile, simplified, transferCase, uniqueCompact, xmlElement, - xmlIndent, xmlSubElem, yesNo + readTextFile, simplified, transferCase, uniqueCompact, utf16CharMap, + xmlElement, xmlIndent, xmlSubElem, yesNo ) from novelwriter.enum import nwItemClass @@ -557,6 +557,14 @@ def testBaseCommon_encodeDecodeMimeHandles(monkeypatch): assert decodeMimeHandles(mimeData) == handles +@pytest.mark.base +def testBaseCommon_utf16CharMap(monkeypatch): + """Test the utf16CharMap function.""" + assert utf16CharMap("abc") == [0, 1, 2, 3] + assert utf16CharMap("a\u2014b\u2014c") == [0, 1, 2, 3, 4, 5] + assert utf16CharMap("a\U0001F605b\U0001F605c") == [0, 1, 3, 4, 6, 7] + + @pytest.mark.base def testBaseCommon_jsonEncode(): """Test the jsonEncode function."""