Files
texteditor/regression_highlight.py
T
2026-10-14 16:16:58 +02:00

90 lines
2.8 KiB
Python

"""Regressiontest for the Syntax-Highlighting and the <<Modified>>-Flag-Reset:
„Open“ of a .py-File, Language-Switch and Editing after a Save without Popups.
Start: python regression_highlight.py
Expected Output: "OK"
"""
import tkinter as tk
from pathlib import Path
import tempfile
import app as app_module
from app import TextEditorApp
from highlight import HIGHLIGHT_TAGS
SOURCE = "def greet(name):\n # greeting\n print(name, 3.5)\n"
def _highlighted_tags(text, probe: str) -> list[str]:
return [t for t in text.tag_names(probe) if t in HIGHLIGHT_TAGS]
def test_open_detects_language() -> None:
"""Opening a .py-File switches on the Highlighting automatically."""
with tempfile.TemporaryDirectory() as d:
target = Path(d) / "quelle.py"
target.write_text(SOURCE, encoding="utf-8")
# Dialog-Stub: the Filedialog of app.py gets replaced
app_module.filedialog.askopenfilename = lambda parent=None, **kwargs: str(target)
root = tk.Tk()
root.withdraw()
obj = TextEditorApp(root)
obj.open_file()
root.update()
assert obj.language == "python"
assert obj._modified is False
# 'def' at 1.1, '#' at 2.5, '3.5' at 3.16..3.19
assert "py_keyword" in _highlighted_tags(obj.text, "1.1")
assert "py_comment" in _highlighted_tags(obj.text, "2.6")
assert "py_number" in _highlighted_tags(obj.text, "3.17")
# Switch to Plain Text: all Highlighting-Tags must be gone
obj.use_plain_text()
for probe in ("1.1", "2.6", "3.12"):
assert _highlighted_tags(obj.text, probe) == []
# Switch back
obj.use_python()
assert "py_builtin" in _highlighted_tags(obj.text, "3.5")
root.destroy()
def test_dirty_flag_after_save() -> None:
"""Without the Flag-Reset in the Handler the Dirty-State is lost after
the first Save and the Highlighting stays outdated."""
root = tk.Tk()
root.withdraw()
obj = TextEditorApp(root)
with tempfile.TemporaryDirectory() as d:
target = Path(d) / "dok.py"
obj.file_path = target
obj.language = "python"
obj._set_content(SOURCE)
obj.text.insert("end-1c", "\nprint(1)")
root.update()
assert obj._modified is True
assert obj.save_file() is True
assert obj._modified is False
# A new Edit after the Save must become ungespeicherte again
obj.text.insert("end-1c", "\nprint(2)")
root.update()
assert obj._modified is True
assert root.title().startswith("*")
# and the Highlighting follows the new Input
assert "py_builtin" in _highlighted_tags(obj.text, "5.1")
root.destroy()
if __name__ == "__main__":
test_open_detects_language()
test_dirty_flag_after_save()
print("OK")