Add test coverage
This commit is contained in:
@@ -622,6 +622,83 @@ def testToEPub_ConvertDirect(mockGUI: MockGuiMain):
|
||||
assert epub._sections[-1].text[-1] == "<p style='text-indent: 1.40em;'>Some text ...</p>"
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testToEPub_NavPage(mockGUI: MockGuiMain):
|
||||
"""Test the nav page table of contents."""
|
||||
project = NWProject()
|
||||
|
||||
def build(*docs: tuple[bool, str]) -> ToEPub:
|
||||
epub = ToEPub(project)
|
||||
epub.initDocument()
|
||||
epub.setPartitionFormat(nwHeadFmt.TITLE)
|
||||
epub.setChapterFormat(nwHeadFmt.TITLE)
|
||||
for isNovel, text in docs:
|
||||
epub._isNovel = isNovel
|
||||
epub._text = text
|
||||
epub.tokenizeText()
|
||||
epub.doConvert()
|
||||
epub.closeDocument()
|
||||
return epub
|
||||
|
||||
def navList(epub: ToEPub) -> str:
|
||||
text = epub._generateNavPage()
|
||||
return text[text.index('<ol epub:type="list">') + 22 : text.index("</nav>") - 6]
|
||||
|
||||
# No parts
|
||||
epub = build((True, "## Chapter One\n\n## Chapter Two\n\n"))
|
||||
epub.setNestedToC(True)
|
||||
assert navList(epub) == (
|
||||
'<li><a href="xhtml/chapter1.xhtml#sec_1">Chapter One</a></li>\n'
|
||||
'<li><a href="xhtml/chapter2.xhtml#sec_2">Chapter Two</a></li>\n'
|
||||
)
|
||||
|
||||
# With parts, ending inside a part
|
||||
text = (
|
||||
"## Prologue\n\n# Part One\n\n## Chapter One\n\n## Chapter Two\n\n"
|
||||
"# Part Two\n\n# Part Three\n\n## Chapter Three\n\n"
|
||||
)
|
||||
epub = build((True, text))
|
||||
assert navList(epub) == (
|
||||
'<li><a href="xhtml/chapter1.xhtml#sec_1">Prologue</a></li>\n'
|
||||
'<li><a href="xhtml/part1.xhtml#sec_2">Part One</a></li>\n'
|
||||
'<li><a href="xhtml/chapter2.xhtml#sec_3">Chapter One</a></li>\n'
|
||||
'<li><a href="xhtml/chapter3.xhtml#sec_4">Chapter Two</a></li>\n'
|
||||
'<li><a href="xhtml/part2.xhtml#sec_5">Part Two</a></li>\n'
|
||||
'<li><a href="xhtml/part3.xhtml#sec_6">Part Three</a></li>\n'
|
||||
'<li><a href="xhtml/chapter4.xhtml#sec_7">Chapter Three</a></li>\n'
|
||||
)
|
||||
|
||||
epub.setNestedToC(True)
|
||||
assert navList(epub) == (
|
||||
'<li><a href="xhtml/chapter1.xhtml#sec_1">Prologue</a></li>\n'
|
||||
'<li><a href="xhtml/part1.xhtml#sec_2">Part One</a>\n'
|
||||
"<ol>\n"
|
||||
'<li><a href="xhtml/chapter2.xhtml#sec_3">Chapter One</a></li>\n'
|
||||
'<li><a href="xhtml/chapter3.xhtml#sec_4">Chapter Two</a></li>\n'
|
||||
"</ol>\n"
|
||||
"</li>\n"
|
||||
'<li><a href="xhtml/part2.xhtml#sec_5">Part Two</a>\n'
|
||||
"</li>\n"
|
||||
'<li><a href="xhtml/part3.xhtml#sec_6">Part Three</a>\n'
|
||||
"<ol>\n"
|
||||
'<li><a href="xhtml/chapter4.xhtml#sec_7">Chapter Three</a></li>\n'
|
||||
"</ol>\n"
|
||||
"</li>\n"
|
||||
)
|
||||
|
||||
# Notes after a part are not nested
|
||||
epub = build((True, "# Part One\n\n## Chapter One\n\n"), (False, "#! Notes\n\n"))
|
||||
epub.setNestedToC(True)
|
||||
assert navList(epub) == (
|
||||
'<li><a href="xhtml/part1.xhtml#sec_1">Part One</a>\n'
|
||||
"<ol>\n"
|
||||
'<li><a href="xhtml/chapter1.xhtml#sec_2">Chapter One</a></li>\n'
|
||||
"</ol>\n"
|
||||
"</li>\n"
|
||||
'<li><a href="xhtml/notes1.xhtml#sec_3">Notes</a></li>\n'
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testToEPub_Save(mockGUI: MockGuiMain, fncPath: Path, tstPaths: TstPaths, ipsumText: list[str]):
|
||||
"""Test the save method of the ToEPub class."""
|
||||
|
||||
@@ -380,6 +380,47 @@ def testDocumentBuilder_DocX(mockGUI: MockGuiMain, prjLipsum: Path, fncPath: Pat
|
||||
assert zipfile.is_zipfile(docFile)
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testDocumentBuilder_EPub(mockGUI: MockGuiMain, prjLipsum: Path, fncPath: Path):
|
||||
"""Test building an EPub manuscript."""
|
||||
project = NWProject()
|
||||
project.openProject(prjLipsum)
|
||||
|
||||
build = BuildSettings()
|
||||
build.unpack(BUILD_CONF)
|
||||
build.setValue("epub.nestedToc", True)
|
||||
|
||||
docBuild = DocumentBuilder(project, build)
|
||||
docBuild.queueAll()
|
||||
|
||||
assert len(docBuild) == 19
|
||||
|
||||
# Check Build
|
||||
# ===========
|
||||
|
||||
docFile = fncPath / "Lorem Ipsum.epub"
|
||||
|
||||
count = 0
|
||||
error = []
|
||||
for _, success in docBuild.iterBuildDocument(docFile, nwBuildFmt.EPUB):
|
||||
count += 1 if success else 0
|
||||
if docBuild.error:
|
||||
error.append(docBuild.error)
|
||||
|
||||
assert count == 19
|
||||
assert error == []
|
||||
|
||||
assert docFile.is_file()
|
||||
assert zipfile.is_zipfile(docFile)
|
||||
with zipfile.ZipFile(docFile, mode="r") as zipObj:
|
||||
navText = zipObj.read("OEBPS/nav.xhtml").decode("utf-8")
|
||||
assert (
|
||||
'<li><a href="xhtml/part1.xhtml#sec_3">Part: Act One</a>\n'
|
||||
"<ol>\n"
|
||||
'<li><a href="xhtml/chapter2.xhtml#sec_4">Chapter: Chapter One</a></li>\n'
|
||||
) in navText
|
||||
|
||||
|
||||
@pytest.mark.core
|
||||
def testDocumentBuilder_PDF(mockGUI: MockGuiMain, prjLipsum: Path, fncPath: Path):
|
||||
"""Test building a PDF manuscript."""
|
||||
|
||||
@@ -1088,6 +1088,8 @@ def testGuiBuildSettings_FormatOutput(qtbot: QtBot, nwGUI: GuiMain):
|
||||
build.setValue("doc.pageHeaderEven", nwHeadFmt.DOC_AUTO_EVEN)
|
||||
build.setValue("doc.pageCountOffset", 0)
|
||||
|
||||
build.setValue("epub.nestedToc", False)
|
||||
|
||||
build.setValue("html.addStyles", False)
|
||||
build.setValue("html.preserveTabs", False)
|
||||
|
||||
@@ -1114,6 +1116,8 @@ def testGuiBuildSettings_FormatOutput(qtbot: QtBot, nwGUI: GuiMain):
|
||||
assert fmtTab.pageHeaderEven.isEnabled() is True
|
||||
assert fmtTab.btnPageHeaderEven.isEnabled() is True
|
||||
|
||||
assert fmtTab.nestedToc.isChecked() is False
|
||||
|
||||
assert fmtTab.htmlAddStyles.isChecked() is False
|
||||
assert fmtTab.htmlPreserveTabs.isChecked() is False
|
||||
|
||||
@@ -1127,6 +1131,7 @@ def testGuiBuildSettings_FormatOutput(qtbot: QtBot, nwGUI: GuiMain):
|
||||
assert fmtTab.lblMetaLanguage.text() == "British English"
|
||||
|
||||
# Toggle all
|
||||
fmtTab.nestedToc.setChecked(True)
|
||||
fmtTab.htmlAddStyles.setChecked(True)
|
||||
fmtTab.htmlPreserveTabs.setChecked(True)
|
||||
|
||||
@@ -1140,6 +1145,8 @@ def testGuiBuildSettings_FormatOutput(qtbot: QtBot, nwGUI: GuiMain):
|
||||
assert sBuild.getInt("doc.pageCountOffset") == 1
|
||||
assert sBuild.getStr("doc.metaLanguage") == "en-GB"
|
||||
|
||||
assert sBuild.getBool("epub.nestedToc") is True
|
||||
|
||||
assert sBuild.getBool("html.addStyles") is True
|
||||
assert sBuild.getBool("html.preserveTabs") is True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user