Skip to content

Commit c64bf97

Browse files
Improve utests for lex_detect.py
1 parent c4b3aea commit c64bf97

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/robotide/editor/lex_detect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ def stc_lex_code(text: str) -> int:
653653
return stc.STC_LEX_NULL
654654
try:
655655
lex_code = eval(f"stc.{text}")
656-
except Exception:
656+
except AttributeError:
657657
lex_code = stc.STC_LEX_NULL
658658
return lex_code
659659

utest/editor/test_lex_detect.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
import unittest
1616

17+
from utest.resources import datafilereader
1718
from wx import stc
18-
from robotide.editor.lex_detect import detect
19+
from robotide.editor.lex_detect import detect, detect_from_file
1920

2021
PLUGIN_NAME = "Text Edit"
2122

@@ -82,6 +83,38 @@ def test_valid_detections(self):
8283
print(f"\nResults: {passed} passed, {failed} failed out of {len(tests)} tests")
8384
assert failed == 0
8485

86+
def test_invalid_detections(self):
87+
ftests = [ ("Cargo.toml", None), # stc.STC_LEX_TOML
88+
("app.dart", None), # stc.STC_LEX_DART
89+
("main.zig", None), # stc.STC_LEX_ZIG
90+
("game.gd", None), # stc.STC_LEX_GDSCRIPT
91+
("prog.nim", None), # stc.STC_LEX_NIM
92+
("app.jl", None), # stc.STC_LEX_JULIA
93+
# ASCIIDOC exists in Scintilla/wxWidgets but not in wxPython
94+
("readme.adoc", None) # stc.STC_LEX_ASCIIDOC
95+
]
96+
passed = failed = 0
97+
for fname, content in ftests:
98+
got = detect(fname, content)
99+
ok = got == stc.STC_LEX_NULL
100+
status = "OK" if ok else "FAIL"
101+
if not ok:
102+
print(f" [{status}] detect({fname!r}) → {got!r}")
103+
failed += 1
104+
else:
105+
passed += 1
106+
107+
print(f"\nResults: {passed} passed, {failed} failed out of {len(tests)} tests")
108+
assert failed == 0
109+
110+
def test_file_type_detections(self):
111+
result = detect_from_file(datafilereader.LIBRARY_WITH_SPACES_IN_PATH)
112+
assert result == stc.STC_LEX_PYTHON
113+
114+
def test_invalid_file_type_detections(self):
115+
result = detect_from_file(datafilereader.RESOURCES_DIR + "/nonexisting.jason")
116+
assert result == stc.STC_LEX_NULL
117+
85118

86119
if __name__ == '__main__':
87120
unittest.main()

0 commit comments

Comments
 (0)