mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
tests: Test that docs & completions don't crash
This commit is contained in:
parent
6576e02837
commit
3f37380c25
4 changed files with 21 additions and 5 deletions
|
@ -24,7 +24,7 @@ from ..errors import CompileError, MultipleErrors
|
||||||
from ..completions_utils import *
|
from ..completions_utils import *
|
||||||
from .. import decompiler as decompile
|
from .. import decompiler as decompile
|
||||||
from ..decompiler import DecompileCtx, decompiler
|
from ..decompiler import DecompileCtx, decompiler
|
||||||
from ..gir import StringType, BoolType, IntType, FloatType, GirType
|
from ..gir import StringType, BoolType, IntType, FloatType, GirType, Enumeration
|
||||||
from ..lsp_utils import Completion, CompletionItemKind, SemanticToken, SemanticTokenType
|
from ..lsp_utils import Completion, CompletionItemKind, SemanticToken, SemanticTokenType
|
||||||
from ..parse_tree import *
|
from ..parse_tree import *
|
||||||
from ..parser_utils import *
|
from ..parser_utils import *
|
||||||
|
|
|
@ -94,11 +94,12 @@ def get_types(gir):
|
||||||
}
|
}
|
||||||
|
|
||||||
def _get_docs(gir, name):
|
def _get_docs(gir, name):
|
||||||
return (
|
if gir_type := (
|
||||||
gir.get_type("AccessibleProperty", "Gtk").members.get(name)
|
gir.get_type("AccessibleProperty", "Gtk").members.get(name)
|
||||||
or gir.get_type("AccessibleRelation", "Gtk").members.get(name)
|
or gir.get_type("AccessibleRelation", "Gtk").members.get(name)
|
||||||
or gir.get_type("AccessibleState", "Gtk").members.get(name)
|
or gir.get_type("AccessibleState", "Gtk").members.get(name)
|
||||||
).doc
|
):
|
||||||
|
return gir_type.doc
|
||||||
|
|
||||||
|
|
||||||
class A11yProperty(BaseTypedAttribute):
|
class A11yProperty(BaseTypedAttribute):
|
||||||
|
|
|
@ -112,10 +112,10 @@ class Flag(AstNode):
|
||||||
@docs()
|
@docs()
|
||||||
def docs(self):
|
def docs(self):
|
||||||
type = self.parent.parent.value_type
|
type = self.parent.parent.value_type
|
||||||
|
if not isinstance(type, Enumeration):
|
||||||
|
return
|
||||||
if member := type.members.get(self.tokens["value"]):
|
if member := type.members.get(self.tokens["value"]):
|
||||||
return member.doc
|
return member.doc
|
||||||
else:
|
|
||||||
return type.doc
|
|
||||||
|
|
||||||
@validate()
|
@validate()
|
||||||
def validate_for_type(self):
|
def validate_for_type(self):
|
||||||
|
|
|
@ -24,12 +24,21 @@ import traceback
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from blueprintcompiler import tokenizer, parser, decompiler
|
from blueprintcompiler import tokenizer, parser, decompiler
|
||||||
|
from blueprintcompiler.completions import complete
|
||||||
from blueprintcompiler.errors import PrintableError, MultipleErrors, CompileError
|
from blueprintcompiler.errors import PrintableError, MultipleErrors, CompileError
|
||||||
from blueprintcompiler.tokenizer import Token, TokenType, tokenize
|
from blueprintcompiler.tokenizer import Token, TokenType, tokenize
|
||||||
from blueprintcompiler import utils
|
from blueprintcompiler import utils
|
||||||
|
|
||||||
|
|
||||||
class TestSamples(unittest.TestCase):
|
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):
|
def assert_sample(self, name):
|
||||||
try:
|
try:
|
||||||
with open((Path(__file__).parent / f"samples/{name}.blp").resolve()) as f:
|
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())
|
diff = difflib.unified_diff(expected.splitlines(), actual.splitlines())
|
||||||
print("\n".join(diff))
|
print("\n".join(diff))
|
||||||
raise AssertionError()
|
raise AssertionError()
|
||||||
|
|
||||||
|
self.assert_docs_dont_crash(blueprint, ast)
|
||||||
|
self.assert_completions_dont_crash(blueprint, ast, tokens)
|
||||||
except PrintableError as e: # pragma: no cover
|
except PrintableError as e: # pragma: no cover
|
||||||
e.pretty_print(name + ".blp", blueprint)
|
e.pretty_print(name + ".blp", blueprint)
|
||||||
raise AssertionError()
|
raise AssertionError()
|
||||||
|
@ -67,6 +79,9 @@ class TestSamples(unittest.TestCase):
|
||||||
tokens = tokenizer.tokenize(blueprint)
|
tokens = tokenizer.tokenize(blueprint)
|
||||||
ast, errors, warnings = parser.parse(tokens)
|
ast, errors, warnings = parser.parse(tokens)
|
||||||
|
|
||||||
|
self.assert_docs_dont_crash(blueprint, ast)
|
||||||
|
self.assert_completions_dont_crash(blueprint, ast, tokens)
|
||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
raise errors
|
raise errors
|
||||||
if len(ast.errors):
|
if len(ast.errors):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue