From dc41f495be5d129d9e089908bfcf107a96d04a14 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Wed, 24 Apr 2024 18:39:06 +0200 Subject: [PATCH] Fix ODT tests --- .../coreToOdt_SaveFlat_document.fodt | 14 +-- tests/reference/coreToOdt_SaveFull_styles.xml | 8 +- tests/test_core/test_core_toodt.py | 88 ++++++++++--------- 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/tests/reference/coreToOdt_SaveFlat_document.fodt b/tests/reference/coreToOdt_SaveFlat_document.fodt index b97f7cea..78c9ab59 100644 --- a/tests/reference/coreToOdt_SaveFlat_document.fodt +++ b/tests/reference/coreToOdt_SaveFlat_document.fodt @@ -1,13 +1,13 @@ - 2024-03-14T23:26:28 - novelWriter/2.4a2 + 2024-04-24T18:30:38 + novelWriter/2.5a2 Jane Smith 1234 P42DT12H34M56S Test Project - 2024-03-14T23:26:28 + 2024-04-24T18:30:38 Jane Smith @@ -31,7 +31,7 @@ - + @@ -64,10 +64,14 @@ + + + + - + diff --git a/tests/reference/coreToOdt_SaveFull_styles.xml b/tests/reference/coreToOdt_SaveFull_styles.xml index 6ecb370a..03395db1 100644 --- a/tests/reference/coreToOdt_SaveFull_styles.xml +++ b/tests/reference/coreToOdt_SaveFull_styles.xml @@ -21,7 +21,7 @@ - + @@ -54,10 +54,14 @@ + + + + - + diff --git a/tests/test_core/test_core_toodt.py b/tests/test_core/test_core_toodt.py index b34cb40d..4efe8b44 100644 --- a/tests/test_core/test_core_toodt.py +++ b/tests/test_core/test_core_toodt.py @@ -20,18 +20,19 @@ along with this program. If not, see . """ from __future__ import annotations -import pytest -import zipfile import xml.etree.ElementTree as ET +import zipfile from shutil import copyfile -from tools import ODT_IGNORE, cmpFiles +import pytest from novelwriter.common import xmlIndent from novelwriter.constants import nwHeadFmt -from novelwriter.core.toodt import ToOdt, ODTParagraphStyle, ODTTextStyle, XMLParagraph, _mkTag from novelwriter.core.project import NWProject +from novelwriter.core.toodt import ODTParagraphStyle, ODTTextStyle, ToOdt, XMLParagraph, _mkTag + +from tests.tools import ODT_IGNORE, cmpFiles XML_NS = [ ' xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"', @@ -132,7 +133,7 @@ def testCoreToOdt_TextFormatting(mockGUI): assert list(odt._mainPara.keys()) == [ "Text_20_body", "First_20_line_20_indent", "Text_20_Meta", "Title", "Separator", - "Heading_20_1", "Heading_20_2", "Heading_20_3", "Heading_20_4", "Header", + "Heading_20_1", "Heading_20_2", "Heading_20_3", "Heading_20_4", "Header", "Footnote", ] key = "55db6c1d22ff5aba93f0f67c8d4a857a26e2d3813dfbcba1ef7c0d424f501be5" @@ -145,51 +146,51 @@ def testCoreToOdt_TextFormatting(mockGUI): oStyle = ODTParagraphStyle("test") # No Text - odt.initDocument() - odt._addTextPar("Standard", oStyle, "") - assert xmlToText(odt._xText) == ( + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, "") + assert xmlToText(xTest) == ( '' '' '' ) # No Format - odt.initDocument() - odt._addTextPar("Standard", oStyle, "Hello World") + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, "Hello World") assert odt.errData == [] - assert xmlToText(odt._xText) == ( + assert xmlToText(xTest) == ( '' 'Hello World' '' ) # Heading Level None - odt.initDocument() - odt._addTextPar("Standard", oStyle, "Hello World", isHead=True) + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, "Hello World", isHead=True) assert odt.errData == [] - assert xmlToText(odt._xText) == ( + assert xmlToText(xTest) == ( '' 'Hello World' '' ) # Heading Level 1 - odt.initDocument() - odt._addTextPar("Standard", oStyle, "Hello World", isHead=True, oLevel="1") + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, "Hello World", isHead=True, oLevel="1") assert odt.errData == [] - assert xmlToText(odt._xText) == ( + assert xmlToText(xTest) == ( '' 'Hello World' '' ) # Formatted Text - odt.initDocument() text = "A bold word" - fmt = [(2, odt.FMT_B_B), (6, odt.FMT_B_E)] - odt._addTextPar("Standard", oStyle, text, tFmt=fmt) + fmt = [(2, odt.FMT_B_B, ""), (6, odt.FMT_B_E, "")] + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, text, tFmt=fmt) assert odt.errData == [] - assert xmlToText(odt._xText) == ( + assert xmlToText(xTest) == ( '' 'A bold ' 'word' @@ -197,25 +198,26 @@ def testCoreToOdt_TextFormatting(mockGUI): ) # Incorrectly Formatted Text - odt.initDocument() text = "A few words" - fmt = [(2, odt.FMT_B_B), (5, odt.FMT_B_E), (7, 99999)] - odt._addTextPar("Standard", oStyle, text, tFmt=fmt) + fmt = [(2, odt.FMT_B_B, ""), (5, odt.FMT_B_E, ""), (7, 99999, "")] + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, text, tFmt=fmt) assert odt.errData == ["Unknown format tag encountered"] - assert xmlToText(odt._xText) == ( + assert xmlToText(xTest) == ( '' 'A few ' 'words' '' ) + odt._errData = [] # Unclosed format - odt.initDocument() text = "A bold word" - fmt = [(2, odt.FMT_B_B)] - odt._addTextPar("Standard", oStyle, text, tFmt=fmt) + fmt = [(2, odt.FMT_B_B, "")] + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, text, tFmt=fmt) assert odt.errData == [] - assert xmlToText(odt._xText) == ( + assert xmlToText(xTest) == ( '' 'A ' 'bold word' @@ -223,12 +225,12 @@ def testCoreToOdt_TextFormatting(mockGUI): ) # Tabs and Breaks - odt.initDocument() text = "Hello\n\tWorld" fmt = [] - odt._addTextPar("Standard", oStyle, text, tFmt=fmt) + xTest = ET.Element(_mkTag("office", "text")) + odt._addTextPar(xTest, "Standard", oStyle, text, tFmt=fmt) assert odt.errData == [] - assert xmlToText(odt._xText) == ( + assert xmlToText(xTest) == ( '' 'HelloWorld' '' @@ -835,22 +837,28 @@ def testCoreToOdt_Format(mockGUI): project = NWProject() odt = ToOdt(project, isFlat=True) - assert odt._formatSynopsis("synopsis text", True) == ( - "Synopsis: synopsis text", [(0, ToOdt.FMT_B_B), (9, ToOdt.FMT_B_E)] + assert odt._formatSynopsis("synopsis text", [(9, ToOdt.FMT_STRIP, "")], True) == ( + "Synopsis: synopsis text", [ + (0, ToOdt.FMT_B_B, ""), (9, ToOdt.FMT_B_E, ""), (19, ToOdt.FMT_STRIP, "") + ] ) - assert odt._formatSynopsis("short text", False) == ( - "Short Description: short text", [(0, ToOdt.FMT_B_B), (18, ToOdt.FMT_B_E)] + assert odt._formatSynopsis("short text", [(6, ToOdt.FMT_STRIP, "")], False) == ( + "Short Description: short text", [ + (0, ToOdt.FMT_B_B, ""), (18, ToOdt.FMT_B_E, ""), (25, ToOdt.FMT_STRIP, "") + ] ) - assert odt._formatComments("comment text") == ( - "Comment: comment text", [(0, ToOdt.FMT_B_B), (8, ToOdt.FMT_B_E)] + assert odt._formatComments("comment text", [(8, ToOdt.FMT_STRIP, "")]) == ( + "Comment: comment text", [ + (0, ToOdt.FMT_B_B, ""), (8, ToOdt.FMT_B_E, ""), (17, ToOdt.FMT_STRIP, "") + ] ) assert odt._formatKeywords("") == ("", []) assert odt._formatKeywords("tag: Jane") == ( - "Tag: Jane", [(0, ToOdt.FMT_B_B), (4, ToOdt.FMT_B_E)] + "Tag: Jane", [(0, ToOdt.FMT_B_B, ""), (4, ToOdt.FMT_B_E, "")] ) assert odt._formatKeywords("char: Bod, Jane") == ( - "Characters: Bod, Jane", [(0, ToOdt.FMT_B_B), (11, ToOdt.FMT_B_E)] + "Characters: Bod, Jane", [(0, ToOdt.FMT_B_B, ""), (11, ToOdt.FMT_B_E, "")] ) # END Test testCoreToOdt_Format