Document handling
New/Open/Save/Save as and Undo/Redo
This commit is contained in:
79
tests/unit/test_document_controller.py
Normal file
79
tests/unit/test_document_controller.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from PySide6.QtGui import QUndoCommand
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from bedit_gui.controllers.document_controller import DocumentController
|
||||
from bedit_gui.documents import Document
|
||||
from bedit_gui.views.dialogs.document_dialogs import SaveChangesChoice
|
||||
from bedit_gui.views.main_window import MainWindow
|
||||
|
||||
|
||||
class FakeDialogs:
|
||||
def __init__(self) -> None:
|
||||
self.open_path: Path | None = None
|
||||
self.save_path: Path | None = None
|
||||
self.save_choice = SaveChangesChoice.CANCEL
|
||||
self.errors: list[tuple[str, Exception]] = []
|
||||
|
||||
def choose_open_path(self, _current_path: Path | None) -> Path | None:
|
||||
return self.open_path
|
||||
|
||||
def choose_save_path(self, _current_path: Path | None) -> Path | None:
|
||||
return self.save_path
|
||||
|
||||
def ask_save_changes(self) -> SaveChangesChoice:
|
||||
return self.save_choice
|
||||
|
||||
def show_file_error(self, title: str, error: Exception) -> None:
|
||||
self.errors.append((title, error))
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def qt_app() -> QApplication:
|
||||
return QApplication.instance() or QApplication([])
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_new_document_respects_unsaved_changes(qt_app: QApplication) -> None:
|
||||
document = Document(qt_app)
|
||||
window = MainWindow()
|
||||
dialogs = FakeDialogs()
|
||||
controller = DocumentController(document, window, dialogs)
|
||||
original_id = document.model.id
|
||||
document.undo_stack.push(QUndoCommand("change"))
|
||||
|
||||
controller.new_document()
|
||||
assert document.model.id == original_id
|
||||
|
||||
dialogs.save_choice = SaveChangesChoice.DISCARD
|
||||
controller.new_document()
|
||||
assert document.model.id != original_id
|
||||
assert not document.modified
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_save_as_then_open_document(
|
||||
qt_app: QApplication,
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
document = Document(qt_app)
|
||||
window = MainWindow()
|
||||
dialogs = FakeDialogs()
|
||||
controller = DocumentController(document, window, dialogs)
|
||||
original_id = document.model.id
|
||||
path = tmp_path / "document.json"
|
||||
|
||||
dialogs.save_path = path
|
||||
assert controller.save_document_as()
|
||||
|
||||
document.new()
|
||||
dialogs.open_path = path
|
||||
controller.open_document()
|
||||
|
||||
assert document.model.id == original_id
|
||||
assert document.path == path
|
||||
assert not dialogs.errors
|
||||
37
tests/unit/test_gui_document.py
Normal file
37
tests/unit/test_gui_document.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from PySide6.QtGui import QUndoCommand
|
||||
|
||||
from bedit_gui.documents import Document
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.parametrize("suffix", [".beb", ".json"])
|
||||
def test_document_save_and_open(tmp_path: Path, suffix: str) -> None:
|
||||
path = tmp_path / f"document{suffix}"
|
||||
document = Document()
|
||||
original_id = document.model.id
|
||||
|
||||
document.save_as(path)
|
||||
document.new()
|
||||
assert document.model.id != original_id
|
||||
|
||||
document.open(path)
|
||||
|
||||
assert document.model.id == original_id
|
||||
assert document.path == path
|
||||
assert not document.modified
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_document_modified_state_uses_undo_stack() -> None:
|
||||
document = Document()
|
||||
|
||||
document.undo_stack.push(QUndoCommand("change"))
|
||||
assert document.modified
|
||||
|
||||
document.undo_stack.setClean()
|
||||
assert not document.modified
|
||||
Reference in New Issue
Block a user