86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
"""GUI-freier Smoke-Test: I/O-Logik + App-Logik (ohne Dialoge).
|
|
|
|
Start: python smoke_test.py
|
|
Erwarteter Output: "OK"
|
|
"""
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
import tkinter as tk
|
|
|
|
import file_io
|
|
from file_io import FileLoadError, FileSaveError
|
|
|
|
from app import TextEditorApp
|
|
|
|
|
|
def test_file_io() -> None:
|
|
with tempfile.TemporaryDirectory() as d:
|
|
p = Path(d) / "test.txt"
|
|
file_io.save_file(p, "Hallo Welt\n2. Zeile")
|
|
assert Path(p).read_text(encoding="utf-8") == "Hallo Welt\n2. Zeile"
|
|
|
|
try:
|
|
file_io.load_file(Path(d) / "nicht_da.txt")
|
|
raise AssertionError("FileLoadError fehlte")
|
|
except FileLoadError:
|
|
pass
|
|
try:
|
|
file_io.save_file(Path(d) / "fehlender_pfad" / "x.txt", "x")
|
|
raise AssertionError("FileSaveError fehlte")
|
|
except FileSaveError:
|
|
pass
|
|
|
|
|
|
def test_app_logic() -> None:
|
|
root = tk.Tk()
|
|
app = TextEditorApp(root)
|
|
|
|
# Frischer Start: Titel, Status, keine Änderungen
|
|
assert app._modified is False
|
|
assert root.title().endswith("Texteditor")
|
|
assert "Ohne Titel" in root.title()
|
|
|
|
# Textingabe wird als ungespeicherte Änderung getrackt
|
|
# (root.update() ist nötig, damit das virtuelle <<Modified>>-Ereignis
|
|
# verarbeitet wird; the Handler resets the Tk-Flag afterwards, therefore
|
|
# only the Application-State is checked here)
|
|
app.text.insert("1.0", "Hallo")
|
|
root.update()
|
|
assert app._modified is True
|
|
assert root.title().startswith("*")
|
|
|
|
# Speichern (Datei-Pfad setzen, Speichern unter-Dialog überspringen)
|
|
with tempfile.TemporaryDirectory() as d:
|
|
target = Path(d) / "dokument.txt"
|
|
app.file_path = target
|
|
assert app.save_file() is True
|
|
saved = target.read_text(encoding="utf-8")
|
|
assert saved == "Hallo"
|
|
|
|
# Nach dem Speichern: keine ungespeicherten Änderungen, Titel ohne *
|
|
assert app._modified is False
|
|
assert not root.title().startswith("*")
|
|
assert app.file_path.name == "dokument.txt"
|
|
assert "dokument.txt" in root.title()
|
|
|
|
# After the Save a new Edit must become ungespeicherte again
|
|
# (only works because the Handler resets the <<Modified>>-Flag)
|
|
app.text.insert("end-1c", "!")
|
|
root.update()
|
|
assert app._modified is True
|
|
assert root.title().startswith("*")
|
|
|
|
# Inhalt setzen (Programm-Eingriff) zahlt nicht als unspeicherte Änderung
|
|
app._set_content("Neuer Inhalt")
|
|
assert app._modified is False
|
|
assert app.text.get("1.0", "end-1c") == "Neuer Inhalt"
|
|
|
|
root.destroy()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
test_file_io()
|
|
test_app_logic()
|
|
print("OK")
|