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;
template MyDialog : Dialog {
Dialog {
[action response=cancel]
Button cancel_button {
label: _("Cancel");

View file

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

View file

@ -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;
}

View file

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

View file

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

View file

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

View file

@ -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):