tests: Test that docs & completions don't crash

This commit is contained in:
James Westman 2022-03-19 17:57:49 -05:00
parent 6576e02837
commit 3f37380c25
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
4 changed files with 21 additions and 5 deletions

View file

@ -24,12 +24,21 @@ import traceback
import unittest
from blueprintcompiler import tokenizer, parser, decompiler
from blueprintcompiler.completions import complete
from blueprintcompiler.errors import PrintableError, MultipleErrors, CompileError
from blueprintcompiler.tokenizer import Token, TokenType, tokenize
from blueprintcompiler import utils
class TestSamples(unittest.TestCase):
def assert_docs_dont_crash(self, text, ast):
for i in range(len(text)):
ast.get_docs(i)
def assert_completions_dont_crash(self, text, ast, tokens):
for i in range(len(text)):
list(complete(ast, tokens, i))
def assert_sample(self, name):
try:
with open((Path(__file__).parent / f"samples/{name}.blp").resolve()) as f:
@ -52,6 +61,9 @@ class TestSamples(unittest.TestCase):
diff = difflib.unified_diff(expected.splitlines(), actual.splitlines())
print("\n".join(diff))
raise AssertionError()
self.assert_docs_dont_crash(blueprint, ast)
self.assert_completions_dont_crash(blueprint, ast, tokens)
except PrintableError as e: # pragma: no cover
e.pretty_print(name + ".blp", blueprint)
raise AssertionError()
@ -67,6 +79,9 @@ class TestSamples(unittest.TestCase):
tokens = tokenizer.tokenize(blueprint)
ast, errors, warnings = parser.parse(tokens)
self.assert_docs_dont_crash(blueprint, ast)
self.assert_completions_dont_crash(blueprint, ast, tokens)
if errors:
raise errors
if len(ast.errors):