Rewrite test for split dialog

This commit is contained in:
Veronica Berglyd Olsen
2022-10-13 18:18:08 +02:00
parent 02976c5f12
commit 3f4d85dcad
2 changed files with 60 additions and 201 deletions
+4 -3
View File
@@ -186,16 +186,17 @@ class GuiDocSplit(QDialog):
self._data = {} self._data = {}
self._data["sHandle"] = sHandle self._data["sHandle"] = sHandle
self.listBox.clear()
nwItem = self.theProject.tree[sHandle] nwItem = self.theProject.tree[sHandle]
if nwItem is None or not nwItem.isFileType(): if nwItem is None or not nwItem.isFileType():
return False return
spLevel = self.splitLevel.currentData() spLevel = self.splitLevel.currentData()
if not self._text: if not self._text:
inDoc = NWDoc(self.theProject, sHandle) inDoc = NWDoc(self.theProject, sHandle)
self._text = (inDoc.readDocument() or "").splitlines() self._text = (inDoc.readDocument() or "").splitlines()
self.listBox.clear()
for lineNo, aLine in enumerate(self._text): for lineNo, aLine in enumerate(self._text):
onLine = -1 onLine = -1
@@ -233,6 +234,6 @@ class GuiDocSplit(QDialog):
newItem.setData(self.LABEL_ROLE, hLabel) newItem.setData(self.LABEL_ROLE, hLabel)
self.listBox.addItem(newItem) self.listBox.addItem(newItem)
return True return
# END Class GuiDocSplit # END Class GuiDocSplit
+56 -198
View File
@@ -19,22 +19,16 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
""" """
import os
import pytest import pytest
from mock import causeOSError from tools import C, buildTestProject
from tools import getGuiItem, readFile, writeFile, buildTestProject
from PyQt5.QtWidgets import QAction, QMessageBox from PyQt5.QtWidgets import QMessageBox
from novelwriter.enum import nwItemType, nwWidget
from novelwriter.dialogs import GuiDocSplit, GuiEditLabel from novelwriter.dialogs import GuiDocSplit, GuiEditLabel
from novelwriter.core.tree import NWTree
from novelwriter.core.document import NWDoc
@pytest.mark.gui @pytest.mark.gui
@pytest.mark.skip
def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd):
"""Test the split document tool. """Test the split document tool.
""" """
@@ -46,207 +40,71 @@ def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd):
# Create a new project # Create a new project
buildTestProject(nwGUI, fncProj) buildTestProject(nwGUI, fncProj)
# Handles for new objects theProject = nwGUI.theProject
hNovelRoot = "0000000000008" projTree = nwGUI.projView.projTree
hChapterDir = "000000000000d"
hToSplit = "0000000000010"
hNewFolder = "0000000000021"
hPartition = "0000000000022"
hChapterOne = "0000000000023"
hSceneOne = "0000000000024"
hSceneTwo = "0000000000025"
hSceneThree = "0000000000026"
hSceneFour = "0000000000027"
hSceneFive = "0000000000028"
# Add Project Content docText = (
nwGUI.switchFocus(nwWidget.TREE) "Text\n\n"
nwGUI.projView.projTree.clearSelection() "##! Prologue\n\n"
nwGUI.projView.projTree._getTreeItem(hNovelRoot).setSelected(True) "Text\n\n"
nwGUI.projView.projTree.newTreeItem(nwItemType.FILE) "## Chapter One\n\n"
"### Scene One\n\n"
assert nwGUI.saveProject() is True "Text\n\n"
assert nwGUI.closeProject() is True "### Scene Two\n\n"
"Text\n\n"
tPartition = "# Nantucket" "## Chapter Two\n\n"
tChapterOne = "## Chapter One\n\n% Chapter one comment" "### Scene Three\n\n"
tSceneOne = "### Scene One\n\nThere once was a man from Nantucket" "Text\n\n"
tSceneTwo = "### Scene Two\n\nWho kept all his cash in a bucket." "### Scene Four\n\n"
tSceneThree = "### Scene Three\n\n\tBut his daughter, named Nan, \n\tRan away with a man" "Text\n\n"
tSceneFour = "### Scene Four\n\nAnd as for the bucket, Nantucket." "#! New Title\n\n"
tSceneFive = "#### The End\n\nend" "## New Chapter\n\n"
"### New Scene\n\n"
tToSplit = ( "#### New Section\n\n"
f"{tPartition}\n\n{tChapterOne}\n\n"
f"{tSceneOne}\n\n{tSceneTwo}\n\n"
f"{tSceneThree}\n\n{tSceneFour}\n\n"
f"{tSceneFive}\n\n"
) )
contentDir = os.path.join(fncProj, "content") hSplitDoc = theProject.newFile("Split Doc", C.hNovelRoot)
writeFile(os.path.join(contentDir, hToSplit+".nwd"), tToSplit) theProject.writeNewFile(hSplitDoc, 1, True, docText)
projTree.revealNewTreeItem(hSplitDoc, nHandle=C.hNovelRoot, wordCount=True)
assert nwGUI.openProject(fncProj) is True docText = f"# Split Doc\n\n{docText}"
# Open the Split tool nwSplit = GuiDocSplit(nwGUI, hSplitDoc)
nwGUI.switchFocus(nwWidget.TREE)
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hToSplit).setSelected(True)
monkeypatch.setattr(GuiDocSplit, "exec_", lambda *a: None)
nwGUI.mainMenu.aSplitDoc.activate(QAction.Trigger)
qtbot.waitUntil(lambda: getGuiItem("GuiDocSplit") is not None, timeout=1000)
nwSplit = getGuiItem("GuiDocSplit")
assert isinstance(nwSplit, GuiDocSplit)
nwSplit.show() nwSplit.show()
qtbot.wait(50) qtbot.addWidget(nwSplit)
# Populate List # By default, only up to level three headinsg should be listed
# ============= assert nwSplit.splitLevel.currentData() == 3
assert nwSplit.listBox.count() == 11
nwSplit.listBox.clear() # Changing to level 4, should reload and add the last section
assert nwSplit.listBox.count() == 0
# No item selected
nwSplit.sourceItem = None
nwGUI.projView.projTree.clearSelection()
assert nwSplit._populateList() is False
assert nwSplit.listBox.count() == 0
# Non-existing item
with monkeypatch.context() as mp:
mp.setattr(NWTree, "__getitem__", lambda *a: None)
nwSplit.sourceItem = None
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hToSplit).setSelected(True)
assert nwSplit._populateList() is False
assert nwSplit.listBox.count() == 0
# Select a non-file
nwSplit.sourceItem = None
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hChapterDir).setSelected(True)
assert nwSplit._populateList() is False
assert nwSplit.listBox.count() == 0
# Error when reading documents
with monkeypatch.context() as mp:
mp.setattr(NWDoc, "readDocument", lambda *a: None)
nwSplit.sourceItem = hToSplit
assert nwSplit._populateList() is False
assert nwSplit.listBox.count() == 0
# Read properly, and check split levels
# Level 1
nwSplit.splitLevel.setCurrentIndex(0)
nwSplit.sourceItem = hToSplit
assert nwSplit._populateList() is True
assert nwSplit.listBox.count() == 1
# Level 2
nwSplit.splitLevel.setCurrentIndex(1)
nwSplit.sourceItem = hToSplit
assert nwSplit._populateList() is True
assert nwSplit.listBox.count() == 2
# Level 3
nwSplit.splitLevel.setCurrentIndex(2)
nwSplit.sourceItem = hToSplit
assert nwSplit._populateList() is True
assert nwSplit.listBox.count() == 6
# Level 4
nwSplit.splitLevel.setCurrentIndex(3) nwSplit.splitLevel.setCurrentIndex(3)
nwSplit.sourceItem = hToSplit assert nwSplit.listBox.count() == 12
assert nwSplit._populateList() is True
assert nwSplit.listBox.count() == 7
# Split Document data, text = nwSplit.getData()
# ============== assert text == docText.splitlines()
assert data["sHandle"] == hSplitDoc
assert data["spLevel"] == 4
assert data["intoFolder"] is True
assert data["docHierarchy"] is True
assert data["headerList"][0] == (0, 1, "Split Doc")
assert data["headerList"][1] == (4, 2, "Prologue")
assert data["headerList"][2] == (8, 2, "Chapter One")
assert data["headerList"][3] == (10, 3, "Scene One")
assert data["headerList"][4] == (14, 3, "Scene Two")
assert data["headerList"][5] == (18, 2, "Chapter Two")
assert data["headerList"][6] == (20, 3, "Scene Three")
assert data["headerList"][7] == (24, 3, "Scene Four")
assert data["headerList"][8] == (28, 1, "New Title")
assert data["headerList"][9] == (30, 2, "New Chapter")
assert data["headerList"][10] == (32, 3, "New Scene")
assert data["headerList"][11] == (34, 4, "New Section")
# Test a proper split first # Loading the dialog on a non-file item produces an empty list
with monkeypatch.context() as mp: nwSplit._loadContent(C.hNovelRoot)
mp.setattr(GuiDocSplit, "_doClose", lambda *a: None) assert nwSplit.listBox.count() == 0
assert nwSplit._doSplit() is True
assert nwGUI.saveProject()
assert readFile(os.path.join(contentDir, hPartition+".nwd")) == ( nwSplit.reject()
"%%%%~name: Nantucket\n" # qtbot.stop()
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
) % (hNewFolder, hPartition, tPartition)
assert readFile(os.path.join(contentDir, hChapterOne+".nwd")) == (
"%%%%~name: Chapter One\n"
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
) % (hNewFolder, hChapterOne, tChapterOne)
assert readFile(os.path.join(contentDir, hSceneOne+".nwd")) == (
"%%%%~name: Scene One\n"
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
) % (hNewFolder, hSceneOne, tSceneOne)
assert readFile(os.path.join(contentDir, hSceneTwo+".nwd")) == (
"%%%%~name: Scene Two\n"
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
) % (hNewFolder, hSceneTwo, tSceneTwo)
assert readFile(os.path.join(contentDir, hSceneThree+".nwd")) == (
"%%%%~name: Scene Three\n"
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
) % (hNewFolder, hSceneThree, tSceneThree)
assert readFile(os.path.join(contentDir, hSceneFour+".nwd")) == (
"%%%%~name: Scene Four\n"
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
) % (hNewFolder, hSceneFour, tSceneFour)
assert readFile(os.path.join(contentDir, hSceneFive+".nwd")) == (
"%%%%~name: The End\n"
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
) % (hNewFolder, hSceneFive, tSceneFive)
# OS error
with monkeypatch.context() as mp:
mp.setattr("builtins.open", causeOSError)
assert nwSplit._doSplit() is False
# Select to not split
with monkeypatch.context() as mp:
mp.setattr(QMessageBox, "question", lambda *a: QMessageBox.No)
assert nwSplit._doSplit() is False
# Clear the list
nwSplit.listBox.clear()
assert nwSplit._doSplit() is False
# Can't find sourcv item
with monkeypatch.context() as mp:
mp.setattr(NWTree, "__getitem__", lambda *a: None)
assert nwSplit._doSplit() is False
# No source item set
nwSplit.sourceItem = None
assert nwSplit._doSplit() is False
# Close
nwSplit._doClose()
# qtbot.stopForInteraction()
# END Test testDlgSplit_Main # END Test testDlgSplit_Main