Add test coverage

This commit is contained in:
Veronica Berglyd Olsen
2024-02-26 22:40:32 +01:00
parent 5fa6c2ad99
commit eda054ae11
5 changed files with 157 additions and 64 deletions
+36 -14
View File
@@ -29,8 +29,8 @@ from novelwriter.core.project import NWProject
@pytest.mark.core
def testCoreToHtml_ConvertFormat(mockGUI):
"""Test the tokenizer and converter chain using the ToHtml class."""
def testCoreToHtml_ConvertHeaders(mockGUI):
"""Test header formats in the ToHtml class."""
project = NWProject()
html = ToHtml(project)
@@ -129,6 +129,19 @@ def testCoreToHtml_ConvertFormat(mockGUI):
html.doConvert()
assert html.result == "<h2><a name='T0001'></a>Heading Two</h2>\n"
# END Test testCoreToHtml_ConvertHeaders
@pytest.mark.core
def testCoreToHtml_ConvertParagraphs(mockGUI):
"""Test paragraph formats in the ToHtml class."""
project = NWProject()
html = ToHtml(project)
html._isNovel = True
html._isNote = False
html._isFirst = True
# Paragraphs
# ==========
@@ -141,6 +154,19 @@ def testCoreToHtml_ConvertFormat(mockGUI):
"<del>strikethrough</del> text</strong> here</p>\n"
)
# Shortcodes
html._text = (
"Some [b]bold[/b], [i]italic[/i], [s]strike[/s], [u]underline[/u], [m]mark[/m], "
"super[sup]script[/sup], sub[sub]script[/sub] here\n"
)
html.tokenizeText()
html.doConvert()
assert html.result == (
"<p>Some <strong>bold</strong>, <em>italic</em>, <del>strike</del>, "
"<span style='text-decoration: underline;'>underline</span>, <mark>mark</mark>, "
"super<sup>script</sup>, sub<sub>script</sub> here</p>\n"
)
# Text w/Hard Break
html._text = "Line one \nLine two \nLine three\n"
html.tokenizeText()
@@ -206,17 +232,13 @@ def testCoreToHtml_ConvertFormat(mockGUI):
html.tokenizeText()
html.doConvert()
assert html.result == (
"<h2>"
"<a name='T0001'></a>Chapter</h2>\n"
"<p style='margin-bottom: 0;'>"
"<span class='tags'>Point of View:</span> <a href='#tag_Bod'>Bod</a>"
"</p>\n"
"<p style='margin-bottom: 0; margin-top: 0;'>"
"<span class='tags'>Plot:</span> <a href='#tag_Main'>Main</a>"
"</p>\n"
"<p style='margin-top: 0;'>"
"<span class='tags'>Locations:</span> <a href='#tag_Europe'>Europe</a>"
"</p>\n"
"<h1 style='page-break-before: always;'>Chapter</h1>\n"
"<p style='margin-bottom: 0;'><span class='tags'>Point of View:</span> "
"<a href='#tag_Bod'>Bod</a></p>\n"
"<p style='margin-bottom: 0; margin-top: 0;'><span class='tags'>Plot:</span> "
"<a href='#tag_Main'>Main</a></p>\n"
"<p style='margin-top: 0;'><span class='tags'>Locations:</span> "
"<a href='#tag_Europe'>Europe</a></p>\n"
)
# Preview Mode
@@ -234,7 +256,7 @@ def testCoreToHtml_ConvertFormat(mockGUI):
"text</b> here</p>\n"
)
# END Test testCoreToHtml_ConvertFormat
# END Test testCoreToHtml_ConvertParagraphs
@pytest.mark.core
+5
View File
@@ -638,6 +638,11 @@ def testCoreToken_ExtractFormats(mockGUI):
assert text == "Text with underline in it."
assert fmt == [(10, tokens.FMT_U_B), (19, tokens.FMT_U_E)]
# Plain mark
text, fmt = tokens._extractFormats("Text with [m]highlight[/m] in it.")
assert text == "Text with highlight in it."
assert fmt == [(10, tokens.FMT_M_B), (19, tokens.FMT_M_E)]
# Plain superscript
text, fmt = tokens._extractFormats("Text with super[sup]script[/sup] in it.")
assert text == "Text with superscript in it."
+40 -10
View File
@@ -29,16 +29,11 @@ from novelwriter.core.project import NWProject
@pytest.mark.core
def testCoreToMarkdown_ConvertFormat(mockGUI):
"""Test the tokenizer and converter chain using the ToMarkdown
class.
"""
def testCoreToMarkdown_ConvertHeaders(mockGUI):
"""Test header formats in the ToMarkdown class."""
project = NWProject()
toMD = ToMarkdown(project)
# Headers
# =======
toMD._isNovel = True
toMD._isNote = False
toMD._isFirst = True
@@ -79,8 +74,18 @@ def testCoreToMarkdown_ConvertFormat(mockGUI):
toMD.doConvert()
assert toMD.result == "## Prologue\n\n"
# Paragraphs
# ==========
# END Test testCoreToMarkdown_ConvertHeaders
@pytest.mark.core
def testCoreToMarkdown_ConvertParagraphs(mockGUI):
"""Test paragraph formats in the ToMarkdown class."""
project = NWProject()
toMD = ToMarkdown(project)
toMD._isNovel = True
toMD._isNote = False
toMD._isFirst = True
# Text for Extended Markdown
toMD.setExtendedMarkdown()
@@ -100,6 +105,31 @@ def testCoreToMarkdown_ConvertFormat(mockGUI):
"Some **nested bold and _italic_ and strikethrough text** here\n\n"
)
# Shortcodes for Extended Markdown
toMD.setExtendedMarkdown()
toMD._text = (
"Some [b]bold[/b], [i]italic[/i], [s]strike[/s], [u]underline[/u], [m]mark[/m], "
"super[sup]script[/sup], sub[sub]script[/sub] here\n"
)
toMD.tokenizeText()
toMD.doConvert()
assert toMD.result == (
"Some **bold**, _italic_, ~~strike~~, underline, ==mark==, "
"super^script^, sub~script~ here\n\n"
)
# Shortcodes for Standard Markdown
toMD.setStandardMarkdown()
toMD._text = (
"Some [b]bold[/b], [i]italic[/i], [s]strike[/s], [u]underline[/u], [m]mark[/m], "
"super[sup]script[/sup], sub[sub]script[/sub] here\n"
)
toMD.tokenizeText()
toMD.doConvert()
assert toMD.result == (
"Some **bold**, _italic_, strike, underline, mark, superscript, subscript here\n\n"
)
# Text w/Hard Break
toMD._text = "Line one \nLine two \nLine three\n"
toMD.tokenizeText()
@@ -160,7 +190,7 @@ def testCoreToMarkdown_ConvertFormat(mockGUI):
"**Locations:** Europe\n\n"
)
# END Test testCoreToMarkdown_ConvertFormat
# END Test testCoreToMarkdown_ConvertParagraphs
@pytest.mark.core
+69 -40
View File
@@ -237,22 +237,12 @@ def testCoreToOdt_TextFormatting(mockGUI):
@pytest.mark.core
def testCoreToOdt_Convert(mockGUI):
def testCoreToOdt_ConvertHeaders(mockGUI):
"""Test the converter of the ToOdt class."""
project = NWProject()
odt = ToOdt(project, isFlat=True)
odt._isNovel = True
def getStyle(styleName):
for aSet in odt._autoPara.values():
if aSet[0] == styleName:
return aSet[1]
return None
# Headers
# =======
# Header 1
odt._text = "# Title\n"
odt.tokenizeText()
@@ -331,8 +321,21 @@ def testCoreToOdt_Convert(mockGUI):
'</office:text>'
)
# Paragraphs
# ==========
# END Test testCoreToOdt_ConvertHeaders
@pytest.mark.core
def testCoreToOdt_ConvertParagraphs(mockGUI):
"""Test the converter of the ToOdt class."""
project = NWProject()
odt = ToOdt(project, isFlat=True)
odt._isNovel = True
def getStyle(styleName):
for aSet in odt._autoPara.values():
if aSet[0] == styleName:
return aSet[1]
return None
# Nested Markdown Text
odt._text = "Some ~~nested **bold** and _italics_ text~~ text."
@@ -372,7 +375,7 @@ def testCoreToOdt_Convert(mockGUI):
'</office:text>'
)
# Nested Shortcode Text, Super/Subscript
# Shortcode Text, Super/Subscript
odt._text = "Some super[sup]script[/sup] and sub[sub]script[/sub] text."
odt.tokenizeText()
odt.initDocument()
@@ -387,6 +390,21 @@ def testCoreToOdt_Convert(mockGUI):
'</office:text>'
)
# Shortcode Text, Underline/Highlight
odt._text = "Some [u]underlined and [m]highlighted[/m][/u] text."
odt.tokenizeText()
odt.initDocument()
odt.doConvert()
odt.closeDocument()
assert odt.errData == []
assert xmlToText(odt._xText) == (
'<office:text>'
'<text:p text:style-name="Text_20_body">Some '
'<text:span text:style-name="T7">underlined and </text:span>'
'<text:span text:style-name="T8">highlighted</text:span> text.</text:p>'
'</office:text>'
)
# Hard Break
odt._text = "Some text.\nNext line\n"
odt.tokenizeText()
@@ -422,7 +440,7 @@ def testCoreToOdt_Convert(mockGUI):
assert odt.errData == []
assert xmlToText(odt._xText) == (
'<office:text>'
'<text:p text:style-name="Text_20_body">Some <text:span text:style-name="T7">'
'<text:p text:style-name="Text_20_body">Some <text:span text:style-name="T9">'
'bold<text:tab />text</text:span></text:p>'
'</office:text>'
)
@@ -467,13 +485,13 @@ def testCoreToOdt_Convert(mockGUI):
assert xmlToText(odt._xText) == (
'<office:text>'
'<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T7">'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T9">'
'Point of View:</text:span> Jane</text:p>'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T7">'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T9">'
'Synopsis:</text:span> So it begins</text:p>'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T7">'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T9">'
'Short Description:</text:span> Then what</text:p>'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T7">'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T9">'
'Comment:</text:span> A plain comment</text:p>'
'</office:text>'
)
@@ -535,26 +553,26 @@ def testCoreToOdt_Convert(mockGUI):
assert xmlToText(odt._xText) == (
'<office:text>'
'<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
'<text:p text:style-name="P3"><text:span text:style-name="T7">'
'<text:p text:style-name="P1"><text:span text:style-name="T9">'
'Point of View:</text:span> Jane</text:p>'
'<text:p text:style-name="P4"><text:span text:style-name="T7">'
'<text:p text:style-name="P2"><text:span text:style-name="T9">'
'Characters:</text:span> John</text:p>'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T7">'
'<text:p text:style-name="Text_20_Meta"><text:span text:style-name="T9">'
'Plot:</text:span> Main</text:p>'
'<text:p text:style-name="P5">Right align</text:p>'
'<text:p text:style-name="P3">Right align</text:p>'
'<text:p text:style-name="Text_20_body">Left Align</text:p>'
'<text:p text:style-name="P6">Centered</text:p>'
'<text:p text:style-name="P7">Left indent</text:p>'
'<text:p text:style-name="P8">Right indent</text:p>'
'<text:p text:style-name="P4">Centered</text:p>'
'<text:p text:style-name="P5">Left indent</text:p>'
'<text:p text:style-name="P6">Right indent</text:p>'
'</office:text>'
)
assert getStyle("P3")._pAttr["margin-bottom"] == ["fo", "0.000cm"] # type: ignore
assert getStyle("P4")._pAttr["margin-bottom"] == ["fo", "0.000cm"] # type: ignore
assert getStyle("P4")._pAttr["margin-top"] == ["fo", "0.000cm"] # type: ignore
assert getStyle("P5")._pAttr["text-align"] == ["fo", "right"] # type: ignore
assert getStyle("P6")._pAttr["text-align"] == ["fo", "center"] # type: ignore
assert getStyle("P7")._pAttr["margin-left"] == ["fo", "1.693cm"] # type: ignore
assert getStyle("P8")._pAttr["margin-right"] == ["fo", "1.693cm"] # type: ignore
assert getStyle("P1")._pAttr["margin-bottom"] == ["fo", "0.000cm"] # type: ignore
assert getStyle("P2")._pAttr["margin-bottom"] == ["fo", "0.000cm"] # type: ignore
assert getStyle("P2")._pAttr["margin-top"] == ["fo", "0.000cm"] # type: ignore
assert getStyle("P3")._pAttr["text-align"] == ["fo", "right"] # type: ignore
assert getStyle("P4")._pAttr["text-align"] == ["fo", "center"] # type: ignore
assert getStyle("P5")._pAttr["margin-left"] == ["fo", "1.693cm"] # type: ignore
assert getStyle("P6")._pAttr["margin-right"] == ["fo", "1.693cm"] # type: ignore
# Justified
odt._text = (
@@ -573,11 +591,11 @@ def testCoreToOdt_Convert(mockGUI):
'<office:text>'
'<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene</text:h>'
'<text:p text:style-name="Text_20_body">Regular paragraph</text:p>'
'<text:p text:style-name="P9">with<text:line-break />break</text:p>'
'<text:p text:style-name="P9">Left Align</text:p>'
'<text:p text:style-name="P7">with<text:line-break />break</text:p>'
'<text:p text:style-name="P7">Left Align</text:p>'
'</office:text>'
)
assert getStyle("P9")._pAttr["text-align"] == ["fo", "left"] # type: ignore
assert getStyle("P7")._pAttr["text-align"] == ["fo", "left"] # type: ignore
# Page Breaks
odt._text = (
@@ -593,9 +611,9 @@ def testCoreToOdt_Convert(mockGUI):
assert odt.errData == []
assert xmlToText(odt._xText) == (
'<office:text>'
'<text:h text:style-name="P2" text:outline-level="2">Chapter One</text:h>'
'<text:h text:style-name="P8" text:outline-level="2">Chapter One</text:h>'
'<text:p text:style-name="Text_20_body">Text</text:p>'
'<text:h text:style-name="P2" text:outline-level="2">Chapter Two</text:h>'
'<text:h text:style-name="P8" text:outline-level="2">Chapter Two</text:h>'
'<text:p text:style-name="Text_20_body">Text</text:p>'
'</office:text>'
)
@@ -612,12 +630,12 @@ def testCoreToOdt_Convert(mockGUI):
assert odt.errData == []
assert xmlToText(odt._xText) == (
'<office:text>'
'<text:p text:style-name="Text_20_body">Test text **<text:span text:style-name="T8">'
'<text:p text:style-name="Text_20_body">Test text **<text:span text:style-name="T10">'
'bold</text:span>** and more.</text:p>'
'</office:text>'
)
# END Test testCoreToOdt_Convert
# END Test testCoreToOdt_ConvertParagraphs
@pytest.mark.core
@@ -1081,6 +1099,17 @@ def testCoreToOdt_ODTTextStyle():
txtStyle.setFontStyle("stuff")
assert txtStyle._tAttr["font-style"] == ["fo", None]
# Background Color
assert txtStyle._tAttr["background-color"] == ["fo", None]
txtStyle.setBackgroundColor("stuff")
assert txtStyle._tAttr["background-color"] == ["fo", None]
txtStyle.setBackgroundColor("012345")
assert txtStyle._tAttr["background-color"] == ["fo", None]
txtStyle.setBackgroundColor("#012345")
assert txtStyle._tAttr["background-color"] == ["fo", "#012345"]
txtStyle.setBackgroundColor("stuff")
assert txtStyle._tAttr["background-color"] == ["fo", None]
# Text Position
assert txtStyle._tAttr["text-position"] == ["style", None]
txtStyle.setTextPosition("stuff")
+7
View File
@@ -491,6 +491,13 @@ def testGuiEditor_Actions(qtbot, nwGUI, projPath, ipsumText, mockRnd):
assert nwGUI.docEditor.docAction(nwDocAction.UNDO) is True
assert nwGUI.docEditor.getText() == text
# Mark
nwGUI.docEditor.setCursorPosition(46)
assert nwGUI.docEditor.docAction(nwDocAction.SC_MARK) is True
assert nwGUI.docEditor.getText() == text.replace("consectetur", "[m]consectetur[/m]")
assert nwGUI.docEditor.docAction(nwDocAction.UNDO) is True
assert nwGUI.docEditor.getText() == text
# Superscript
nwGUI.docEditor.setCursorPosition(46)
assert nwGUI.docEditor.docAction(nwDocAction.SC_SUP) is True