Document handling

New/Open/Save/Save as and Undo/Redo
This commit is contained in:
2026-07-26 13:41:23 +02:00
parent 38b8ee34ff
commit 4c3b8b4b6d
12 changed files with 506 additions and 4 deletions

View 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