From 3f4d85dcad50401788ba904eeaceda9fa1bd3918 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 13 Oct 2022 18:18:08 +0200 Subject: [PATCH] Rewrite test for split dialog --- novelwriter/dialogs/docsplit.py | 7 +- tests/test_dialogs/test_dlg_docsplit.py | 254 ++++++------------------ 2 files changed, 60 insertions(+), 201 deletions(-) diff --git a/novelwriter/dialogs/docsplit.py b/novelwriter/dialogs/docsplit.py index 7ff206ee..f744f32e 100644 --- a/novelwriter/dialogs/docsplit.py +++ b/novelwriter/dialogs/docsplit.py @@ -186,16 +186,17 @@ class GuiDocSplit(QDialog): self._data = {} self._data["sHandle"] = sHandle + self.listBox.clear() + nwItem = self.theProject.tree[sHandle] if nwItem is None or not nwItem.isFileType(): - return False + return spLevel = self.splitLevel.currentData() if not self._text: inDoc = NWDoc(self.theProject, sHandle) self._text = (inDoc.readDocument() or "").splitlines() - self.listBox.clear() for lineNo, aLine in enumerate(self._text): onLine = -1 @@ -233,6 +234,6 @@ class GuiDocSplit(QDialog): newItem.setData(self.LABEL_ROLE, hLabel) self.listBox.addItem(newItem) - return True + return # END Class GuiDocSplit diff --git a/tests/test_dialogs/test_dlg_docsplit.py b/tests/test_dialogs/test_dlg_docsplit.py index 818c3d9e..b95d497c 100644 --- a/tests/test_dialogs/test_dlg_docsplit.py +++ b/tests/test_dialogs/test_dlg_docsplit.py @@ -19,22 +19,16 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ -import os import pytest -from mock import causeOSError -from tools import getGuiItem, readFile, writeFile, buildTestProject +from tools import C, 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.core.tree import NWTree -from novelwriter.core.document import NWDoc @pytest.mark.gui -@pytest.mark.skip def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): """Test the split document tool. """ @@ -46,207 +40,71 @@ def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): # Create a new project buildTestProject(nwGUI, fncProj) - # Handles for new objects - hNovelRoot = "0000000000008" - hChapterDir = "000000000000d" - hToSplit = "0000000000010" - hNewFolder = "0000000000021" - hPartition = "0000000000022" - hChapterOne = "0000000000023" - hSceneOne = "0000000000024" - hSceneTwo = "0000000000025" - hSceneThree = "0000000000026" - hSceneFour = "0000000000027" - hSceneFive = "0000000000028" + theProject = nwGUI.theProject + projTree = nwGUI.projView.projTree - # Add Project Content - nwGUI.switchFocus(nwWidget.TREE) - nwGUI.projView.projTree.clearSelection() - nwGUI.projView.projTree._getTreeItem(hNovelRoot).setSelected(True) - nwGUI.projView.projTree.newTreeItem(nwItemType.FILE) - - assert nwGUI.saveProject() is True - assert nwGUI.closeProject() is True - - tPartition = "# Nantucket" - tChapterOne = "## Chapter One\n\n% Chapter one comment" - tSceneOne = "### Scene One\n\nThere once was a man from Nantucket" - tSceneTwo = "### Scene Two\n\nWho kept all his cash in a bucket." - tSceneThree = "### Scene Three\n\n\tBut his daughter, named Nan, \n\tRan away with a man" - tSceneFour = "### Scene Four\n\nAnd as for the bucket, Nantucket." - tSceneFive = "#### The End\n\nend" - - tToSplit = ( - 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" + docText = ( + "Text\n\n" + "##! Prologue\n\n" + "Text\n\n" + "## Chapter One\n\n" + "### Scene One\n\n" + "Text\n\n" + "### Scene Two\n\n" + "Text\n\n" + "## Chapter Two\n\n" + "### Scene Three\n\n" + "Text\n\n" + "### Scene Four\n\n" + "Text\n\n" + "#! New Title\n\n" + "## New Chapter\n\n" + "### New Scene\n\n" + "#### New Section\n\n" ) - contentDir = os.path.join(fncProj, "content") - writeFile(os.path.join(contentDir, hToSplit+".nwd"), tToSplit) + hSplitDoc = theProject.newFile("Split Doc", C.hNovelRoot) + 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 - 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 = GuiDocSplit(nwGUI, hSplitDoc) 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() - 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 + # Changing to level 4, should reload and add the last section nwSplit.splitLevel.setCurrentIndex(3) - nwSplit.sourceItem = hToSplit - assert nwSplit._populateList() is True - assert nwSplit.listBox.count() == 7 + assert nwSplit.listBox.count() == 12 - # 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 - with monkeypatch.context() as mp: - mp.setattr(GuiDocSplit, "_doClose", lambda *a: None) - assert nwSplit._doSplit() is True - assert nwGUI.saveProject() + # Loading the dialog on a non-file item produces an empty list + nwSplit._loadContent(C.hNovelRoot) + assert nwSplit.listBox.count() == 0 - assert readFile(os.path.join(contentDir, hPartition+".nwd")) == ( - "%%%%~name: Nantucket\n" - "%%%%~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() + nwSplit.reject() + # qtbot.stop() # END Test testDlgSplit_Main