first commit
This commit is contained in:
commit
9b2f5b023f
9 files changed
+919
No files matched your search
@@ -0,0 +1,57 @@
|
||||
"""GUI-freier Test of the Highlighting-Logic (highlight.py).
|
||||
|
||||
Start: python highlight_test.py
|
||||
Expected Output: "OK"
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
from highlight import HIGHLIGHT_TAGS, detect_language, tokenize_python
|
||||
|
||||
SOURCE = 'def greet(name):\n # greeting\n msg = f"hi {name}"\n print(msg, 3.5)\n'
|
||||
|
||||
|
||||
def test_detect_language() -> None:
|
||||
assert detect_language(Path(r"C:\x\script.py")) == "python"
|
||||
assert detect_language("note.PY") == "python"
|
||||
assert detect_language("notes.txt") is None
|
||||
assert detect_language("readme") is None
|
||||
|
||||
|
||||
def test_tokenize_python() -> None:
|
||||
ranges = tokenize_python(SOURCE)
|
||||
|
||||
# Only known Tags are generated
|
||||
assert {tag for tag, _, _ in ranges} <= set(HIGHLIGHT_TAGS)
|
||||
|
||||
# 'def' is a Keyword, 'print' a Builtin (tokenize has no KEYWORD-Type)
|
||||
assert ("py_keyword", (1, 0), (1, 3)) in ranges
|
||||
assert ("py_builtin", (4, 4), (4, 9)) in ranges
|
||||
assert ("py_comment", (2, 4), (2, 14)) in ranges
|
||||
assert ("py_number", (4, 15), (4, 18)) in ranges
|
||||
|
||||
# f-Strings are highlighted as Strings (FSTRING_START/MIDDLE/END)
|
||||
assert any(tag == "py_string" for tag, _, _ in ranges)
|
||||
|
||||
# Plain Identifiers keep the default Color
|
||||
assert not any(tag in ("py_keyword", "py_builtin") and start == (3, 4) for tag, start, _ in ranges)
|
||||
|
||||
|
||||
def test_incomplete_source() -> None:
|
||||
"""The User types freely: broken Code must not raise, Tokens until
|
||||
there are still highlighted."""
|
||||
ranges = tokenize_python('x = (1,\ny = "abc\n')
|
||||
assert ("py_operator", (1, 2), (1, 3)) in ranges
|
||||
assert ("py_number", (1, 5), (1, 6)) in ranges
|
||||
|
||||
|
||||
def test_garbage_never_raises() -> None:
|
||||
for junk in ["Hello World", "C:\\temp\\file", "???", "def", "x = 1 \x0c\n", "a\x00b\n"]:
|
||||
tokenize_python(junk)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_detect_language()
|
||||
test_tokenize_python()
|
||||
test_incomplete_source()
|
||||
test_garbage_never_raises()
|
||||
print("OK")
|
||||
Reference in new issue
Block a user