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.
This commit is contained in:
James Westman 2022-12-19 13:53:52 -06:00
parent 219891584c
commit 8758bac40a
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
7 changed files with 46 additions and 24 deletions

View file

@ -1,6 +1,6 @@
using Gtk 4.0; using Gtk 4.0;
template MyDialog : Dialog { Dialog {
[action response=cancel] [action response=cancel]
Button cancel_button { Button cancel_button {
label: _("Cancel"); label: _("Cancel");

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<requires lib="gtk" version="4.0"/> <requires lib="gtk" version="4.0"/>
<template class="MyDialog" parent="GtkDialog"> <object class="GtkDialog">
<child type="action"> <child type="action">
<object class="GtkButton" id="cancel_button"> <object class="GtkButton" id="cancel_button">
<property name="label" translatable="true">Cancel</property> <property name="label" translatable="true">Cancel</property>
@ -22,7 +22,7 @@
<action-widget response="9">custom_response_button</action-widget> <action-widget response="9">custom_response_button</action-widget>
<action-widget response="ok" default="True">ok_button</action-widget> <action-widget response="ok" default="True">ok_button</action-widget>
</action-widgets> </action-widgets>
</template> </object>
<object class="GtkInfoBar"> <object class="GtkInfoBar">
<child type="action"> <child type="action">
<object class="GtkButton" id="ok_info_button"> <object class="GtkButton" id="ok_info_button">

View file

@ -2,6 +2,7 @@ using Gtk 4.0;
Gtk.Label { Gtk.Label {
xalign: .5; xalign: .5;
margin-end: 1_000_000; height-request: 1_000_000;
margin-top: 0x30; margin-top: 0x30;
} }

View file

@ -3,7 +3,7 @@
<requires lib="gtk" version="4.0"/> <requires lib="gtk" version="4.0"/>
<object class="GtkLabel"> <object class="GtkLabel">
<property name="xalign">0.5</property> <property name="xalign">0.5</property>
<property name="margin-end">1000000</property> <property name="height-request">1000000</property>
<property name="margin-top">48</property> <property name="margin-top">48</property>
</object> </object>
</interface> </interface>

View file

@ -1,7 +1,7 @@
using Gtk 4.0; using Gtk 4.0;
template TestTemplate : Label { Range {
test-property: Button { adjustment: Adjustment {
label: "Hello, world!"; lower: 10;
}; };
} }

View file

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<requires lib="gtk" version="4.0"/> <requires lib="gtk" version="4.0"/>
<template class="TestTemplate" parent="GtkLabel"> <object class="GtkRange">
<property name="test-property"> <property name="adjustment">
<object class="GtkButton"> <object class="GtkAdjustment">
<property name="label">Hello, world!</property> <property name="lower">10</property>
</object> </object>
</property> </property>
</template> </object>
</interface> </interface>

View file

@ -20,9 +20,13 @@
import difflib # I love Python import difflib # I love Python
from pathlib import Path from pathlib import Path
import traceback
import unittest import unittest
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
from blueprintcompiler import tokenizer, parser, decompiler from blueprintcompiler import tokenizer, parser, decompiler
from blueprintcompiler.completions import complete from blueprintcompiler.completions import complete
from blueprintcompiler.errors import PrintableError, MultipleErrors, CompileError from blueprintcompiler.errors import PrintableError, MultipleErrors, CompileError
@ -40,7 +44,8 @@ class TestSamples(unittest.TestCase):
for i in range(len(text)): for i in range(len(text)):
list(complete(ast, tokens, i)) 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: 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:
blueprint = f.read() blueprint = f.read()
@ -70,7 +75,12 @@ class TestSamples(unittest.TestCase):
e.pretty_print(name + ".blp", blueprint) e.pretty_print(name + ".blp", blueprint)
raise AssertionError() raise AssertionError()
# Make sure the sample runs
if not skip_run:
Gtk.Builder.new_from_string(actual, -1)
def assert_sample_error(self, name): def assert_sample_error(self, name):
print(f'assert_sample_error("{name}")')
try: try:
with open( with open(
(Path(__file__).parent / f"sample_errors/{name}.blp").resolve() (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") raise AssertionError("Expected a compiler error, but none was emitted")
def assert_decompile(self, name): def assert_decompile(self, name):
print(f'assert_decompile("{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:
expected = f.read() expected = f.read()
@ -140,27 +151,37 @@ class TestSamples(unittest.TestCase):
self.assert_sample("combo_box_text") self.assert_sample("combo_box_text")
self.assert_sample("comments") self.assert_sample("comments")
self.assert_sample("enum") 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("file_filter")
self.assert_sample("flags") self.assert_sample("flags", skip_run=True) # TODO: Fix
self.assert_sample("id_prop") self.assert_sample("id_prop")
self.assert_sample("layout") self.assert_sample("layout")
self.assert_sample("menu") self.assert_sample("menu", skip_run=True) # TODO: Fix
self.assert_sample("numbers") self.assert_sample("numbers")
self.assert_sample("object_prop") 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("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("size_group")
self.assert_sample("string_list") self.assert_sample("string_list")
self.assert_sample("strings") self.assert_sample("strings")
self.assert_sample("style") self.assert_sample("style")
self.assert_sample("template") self.assert_sample(
self.assert_sample("template_no_parent") "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("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("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") self.assert_sample("using")
def test_sample_errors(self): def test_sample_errors(self):