mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
tests: Add some error handling tests
This commit is contained in:
parent
241668fb94
commit
43d442a6df
11 changed files with 29 additions and 5 deletions
|
@ -39,6 +39,8 @@ class UI(AstNode):
|
|||
try:
|
||||
gir_ctx.add_namespace(self.children[GtkDirective][0].gir_namespace)
|
||||
except CompileError as e:
|
||||
e.start = self.children[GtkDirective][0].group.start
|
||||
e.end = self.children[GtkDirective][0].group.end
|
||||
self._gir_errors.append(e)
|
||||
|
||||
for i in self.children[Import]:
|
||||
|
@ -46,6 +48,8 @@ class UI(AstNode):
|
|||
if i.gir_namespace is not None:
|
||||
gir_ctx.add_namespace(i.gir_namespace)
|
||||
except CompileError as e:
|
||||
e.start = i.group.tokens["namespace"].start
|
||||
e.end = i.group.tokens["version"].end
|
||||
self._gir_errors.append(e)
|
||||
|
||||
return gir_ctx
|
||||
|
@ -67,8 +71,11 @@ class UI(AstNode):
|
|||
@validate()
|
||||
def at_most_one_template(self):
|
||||
if len(self.children[Template]) > 1:
|
||||
raise CompileError(f"Only one template may be defined per file, but this file contains {len(self.templates)}",
|
||||
self.children[Template][1].group.start)
|
||||
for template in self.children[Template][1:]:
|
||||
raise CompileError(
|
||||
f"Only one template may be defined per file, but this file contains {len(self.children[Template])}",
|
||||
template.group.tokens["name"].start, template.group.tokens["name"].end,
|
||||
)
|
||||
|
||||
|
||||
@validate()
|
||||
|
@ -96,7 +103,7 @@ class GtkDirective(AstNode):
|
|||
def gtk_version(self):
|
||||
if self.tokens["version"] not in ["4.0"]:
|
||||
err = CompileError("Only GTK 4 is supported")
|
||||
if self.version.startswith("4"):
|
||||
if self.tokens["version"].startswith("4"):
|
||||
err.hint("Expected the GIR version, not an exact version number. Use `using Gtk 4.0;`.")
|
||||
else:
|
||||
err.hint("Expected `using Gtk 4.0;`")
|
||||
|
|
|
@ -50,7 +50,7 @@ def get_namespace(namespace, version):
|
|||
break
|
||||
|
||||
if filename not in _namespace_cache:
|
||||
raise CompileError(f"Namespace `{namespace}-{version}` could not be found.")
|
||||
raise CompileError(f"Namespace {namespace}-{version} could not be found")
|
||||
|
||||
return _namespace_cache[filename]
|
||||
|
||||
|
|
2
tests/sample_errors/conflicting_namespaces.blp
Normal file
2
tests/sample_errors/conflicting_namespaces.blp
Normal file
|
@ -0,0 +1,2 @@
|
|||
using Gtk 4.0;
|
||||
using Gtk 3.0;
|
1
tests/sample_errors/conflicting_namespaces.err
Normal file
1
tests/sample_errors/conflicting_namespaces.err
Normal file
|
@ -0,0 +1 @@
|
|||
2,7,7,Namespace Gtk-3.0 can't be imported because version 4.0 was imported earlier
|
4
tests/sample_errors/two_templates.blp
Normal file
4
tests/sample_errors/two_templates.blp
Normal file
|
@ -0,0 +1,4 @@
|
|||
using Gtk 4.0;
|
||||
|
||||
template ClassName : Gtk.Button {}
|
||||
template ClassName2 : Gtk.Button {}
|
1
tests/sample_errors/two_templates.err
Normal file
1
tests/sample_errors/two_templates.err
Normal file
|
@ -0,0 +1 @@
|
|||
4,10,10,Only one template may be defined per file, but this file contains 2
|
1
tests/sample_errors/using_gtk_3.blp
Normal file
1
tests/sample_errors/using_gtk_3.blp
Normal file
|
@ -0,0 +1 @@
|
|||
using Gtk 3.0;
|
1
tests/sample_errors/using_gtk_3.err
Normal file
1
tests/sample_errors/using_gtk_3.err
Normal file
|
@ -0,0 +1 @@
|
|||
1,11,3,Only GTK 4 is supported
|
2
tests/sample_errors/using_invalid_namespace.blp
Normal file
2
tests/sample_errors/using_invalid_namespace.blp
Normal file
|
@ -0,0 +1,2 @@
|
|||
using Gtk 4.0;
|
||||
using NotARealNamespace 2.0;
|
1
tests/sample_errors/using_invalid_namespace.err
Normal file
1
tests/sample_errors/using_invalid_namespace.err
Normal file
|
@ -0,0 +1 @@
|
|||
2,7,17,Namespace NotARealNamespace-2.0 could not be found
|
|
@ -73,7 +73,7 @@ class TestSamples(unittest.TestCase):
|
|||
def error_str(error):
|
||||
line, col = utils.idx_to_pos(error.start, blueprint)
|
||||
len = error.end - error.start
|
||||
return ",".join([str(line + 1), str(col), str(len), error.message])
|
||||
return ",".join([str(line + 1), str(col + 1), str(len), error.message])
|
||||
|
||||
if isinstance(e, CompileError):
|
||||
actual = error_str(e)
|
||||
|
@ -103,4 +103,8 @@ class TestSamples(unittest.TestCase):
|
|||
|
||||
|
||||
def test_sample_errors(self):
|
||||
self.assert_sample_error("conflicting_namespaces")
|
||||
self.assert_sample_error("duplicate_obj_id")
|
||||
self.assert_sample_error("two_templates")
|
||||
self.assert_sample_error("using_gtk_3")
|
||||
self.assert_sample_error("using_invalid_namespace")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue