72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
"""Regressionstest f"r askyesnocancel-Multiple-Values-Bug:
|
||
simuliert "Öffnen dr"cken bei ungespeicherten "Anderungen" ohne Popups.
|
||
|
||
Start: python regression_open.py
|
||
Erwarteter Output: "OK"
|
||
"""
|
||
import tkinter as tk
|
||
import tkinter.messagebox
|
||
import tkinter.filedialog
|
||
from pathlib import Path
|
||
import tempfile
|
||
|
||
from app import TextEditorApp, APP_TITLE # nur Import-Zeit-Kontrolle
|
||
|
||
# Dialoge stubben, damit der Test ohne Popups l"uft
|
||
askyes_calls = []
|
||
open_paths = [None] # [0] = gew"hlte Ziel-Datei oder None (abbrechen)
|
||
|
||
|
||
def fake_askyesnocancel(title=None, message=None, **options):
|
||
askyes_calls.append({"title": title, "message": message, **options})
|
||
# Standard-Antwort des Tests: "Ja" speichern
|
||
return True
|
||
|
||
|
||
def fake_askopenfilename(parent=None, **kwargs):
|
||
return open_paths[0] or ""
|
||
|
||
|
||
tkinter.messagebox.askyesnocancel = fake_askyesnocancel
|
||
tkinter.filedialog.askopenfilename = fake_askopenfilename
|
||
# app.py importiert "from tkinter import messagebox" – dort patchen:
|
||
import app
|
||
app.messagebox.askyesnocancel = fake_askyesnocancel
|
||
app.filedialog.askopenfilename = fake_askopenfilename
|
||
|
||
|
||
def test_open_with_unsaved_changes() -> None:
|
||
root = tk.Tk()
|
||
app_obj = TextEditorApp(root)
|
||
|
||
# ungespeicherte "Anderungen eingeben
|
||
app_obj.text.insert("1.0", "Wichtige Arbeit")
|
||
root.update()
|
||
assert app_obj._modified is True
|
||
|
||
# "Öffnen" triggern – m"uss ohne Crash die Abfrage zeigen und
|
||
# die Datei "offnen
|
||
with tempfile.TemporaryDirectory() as d:
|
||
target = Path(d) / "quelle.txt"
|
||
target.write_text("Inhalt der Quelldatei", encoding="utf-8")
|
||
open_paths[0] = str(target)
|
||
app_obj.open_file()
|
||
root.update()
|
||
|
||
# Abfrage wurde genau einmal korrekt aufgerufen
|
||
assert len(askyes_calls) == 1, askyes_calls
|
||
call = askyes_calls[0]
|
||
assert call["title"] == "Änderungen speichern?"
|
||
assert "noch nicht gespeichert" in call["message"]
|
||
assert call["parent"] is root
|
||
# und die Datei wurde "offnet
|
||
assert app_obj.file_path == target
|
||
assert app_obj.text.get("1.0", "end-1c") == "Inhalt der Quelldatei"
|
||
assert app_obj._modified is False
|
||
root.destroy()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
test_open_with_unsaved_changes()
|
||
print("OK")
|