From 8758bac40a14703eb5748249c577eb9f3541269f Mon Sep 17 00:00:00 2001 From: James Westman Date: Mon, 19 Dec 2022 13:53:52 -0600 Subject: [PATCH] tests: Test XML outputs Load the outputs of the tests in Gtk.Builder and make sure they work. Some of them don't and need to be fixed. Others will require a bit more work to set up callbacks, templates, etc. --- tests/samples/action_widgets.blp | 2 +- tests/samples/action_widgets.ui | 4 +-- tests/samples/numbers.blp | 3 ++- tests/samples/numbers.ui | 2 +- tests/samples/object_prop.blp | 6 ++--- tests/samples/object_prop.ui | 10 ++++---- tests/test_samples.py | 43 ++++++++++++++++++++++++-------- 7 files changed, 46 insertions(+), 24 deletions(-) diff --git a/tests/samples/action_widgets.blp b/tests/samples/action_widgets.blp index 34293a0..2d4d6ae 100644 --- a/tests/samples/action_widgets.blp +++ b/tests/samples/action_widgets.blp @@ -1,6 +1,6 @@ using Gtk 4.0; -template MyDialog : Dialog { +Dialog { [action response=cancel] Button cancel_button { label: _("Cancel"); diff --git a/tests/samples/action_widgets.ui b/tests/samples/action_widgets.ui index 8c41bb2..91b6e64 100644 --- a/tests/samples/action_widgets.ui +++ b/tests/samples/action_widgets.ui @@ -1,7 +1,7 @@ - + diff --git a/tests/samples/numbers.blp b/tests/samples/numbers.blp index 6364dd2..9ac25dd 100644 --- a/tests/samples/numbers.blp +++ b/tests/samples/numbers.blp @@ -2,6 +2,7 @@ using Gtk 4.0; Gtk.Label { xalign: .5; - margin-end: 1_000_000; + height-request: 1_000_000; margin-top: 0x30; + } diff --git a/tests/samples/numbers.ui b/tests/samples/numbers.ui index bb70bd8..03dee06 100644 --- a/tests/samples/numbers.ui +++ b/tests/samples/numbers.ui @@ -3,7 +3,7 @@ 0.5 - 1000000 + 1000000 48 diff --git a/tests/samples/object_prop.blp b/tests/samples/object_prop.blp index eaccfd9..b7270ac 100644 --- a/tests/samples/object_prop.blp +++ b/tests/samples/object_prop.blp @@ -1,7 +1,7 @@ using Gtk 4.0; -template TestTemplate : Label { - test-property: Button { - label: "Hello, world!"; +Range { + adjustment: Adjustment { + lower: 10; }; } diff --git a/tests/samples/object_prop.ui b/tests/samples/object_prop.ui index 46a62d9..5224073 100644 --- a/tests/samples/object_prop.ui +++ b/tests/samples/object_prop.ui @@ -1,11 +1,11 @@ - + diff --git a/tests/test_samples.py b/tests/test_samples.py index e63c36c..c745226 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -20,9 +20,13 @@ import difflib # I love Python from pathlib import Path -import traceback import unittest +import gi + +gi.require_version("Gtk", "4.0") +from gi.repository import Gtk + from blueprintcompiler import tokenizer, parser, decompiler from blueprintcompiler.completions import complete from blueprintcompiler.errors import PrintableError, MultipleErrors, CompileError @@ -40,7 +44,8 @@ class TestSamples(unittest.TestCase): for i in range(len(text)): list(complete(ast, tokens, i)) - def assert_sample(self, name): + def assert_sample(self, name, skip_run=False): + print(f'assert_sample("{name}", skip_run={skip_run})') try: with open((Path(__file__).parent / f"samples/{name}.blp").resolve()) as f: blueprint = f.read() @@ -70,7 +75,12 @@ class TestSamples(unittest.TestCase): e.pretty_print(name + ".blp", blueprint) raise AssertionError() + # Make sure the sample runs + if not skip_run: + Gtk.Builder.new_from_string(actual, -1) + def assert_sample_error(self, name): + print(f'assert_sample_error("{name}")') try: with open( (Path(__file__).parent / f"sample_errors/{name}.blp").resolve() @@ -115,6 +125,7 @@ class TestSamples(unittest.TestCase): raise AssertionError("Expected a compiler error, but none was emitted") def assert_decompile(self, name): + print(f'assert_decompile("{name}")') try: with open((Path(__file__).parent / f"samples/{name}.blp").resolve()) as f: expected = f.read() @@ -140,27 +151,37 @@ class TestSamples(unittest.TestCase): self.assert_sample("combo_box_text") self.assert_sample("comments") self.assert_sample("enum") - self.assert_sample("expr_lookup") + self.assert_sample("expr_lookup", skip_run=True) # TODO: Fix self.assert_sample("file_filter") - self.assert_sample("flags") + self.assert_sample("flags", skip_run=True) # TODO: Fix self.assert_sample("id_prop") self.assert_sample("layout") - self.assert_sample("menu") + self.assert_sample("menu", skip_run=True) # TODO: Fix self.assert_sample("numbers") self.assert_sample("object_prop") - self.assert_sample("parseable") + self.assert_sample( + "parseable", skip_run=True + ) # The image resource doesn't exist self.assert_sample("property") - self.assert_sample("signal") + self.assert_sample("signal", skip_run=True) # The callback doesn't exist self.assert_sample("size_group") self.assert_sample("string_list") self.assert_sample("strings") self.assert_sample("style") - self.assert_sample("template") - self.assert_sample("template_no_parent") + self.assert_sample( + "template", skip_run=True + ) # The template class doesn't exist + self.assert_sample( + "template_no_parent", skip_run=True + ) # The template class doesn't exist self.assert_sample("translated") - self.assert_sample("typeof") + self.assert_sample( + "typeof", skip_run=True + ) # The custom object type doesn't exist self.assert_sample("uint") - self.assert_sample("unchecked_class") + self.assert_sample( + "unchecked_class", skip_run=True + ) # The custom object type doesn't exist self.assert_sample("using") def test_sample_errors(self):