From 8a2caaf3829fc7ec94a98d845b0cc57a579e2942 Mon Sep 17 00:00:00 2001 From: James Westman Date: Sat, 23 Oct 2021 00:33:21 -0500 Subject: [PATCH] tests: Add parser test --- tests/test_parser.py | 111 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 tests/test_parser.py diff --git a/tests/test_parser.py b/tests/test_parser.py new file mode 100644 index 0000000..a2e635d --- /dev/null +++ b/tests/test_parser.py @@ -0,0 +1,111 @@ +# test_parser.py +# +# Copyright 2021 James Westman +# +# This file is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this program. If not, see . +# +# SPDX-License-Identifier: LGPL-3.0-or-later + + +import unittest + +from gtkblueprinttool.ast import * +from gtkblueprinttool.errors import PrintableError +from gtkblueprinttool.tokenizer import tokenize +from gtkblueprinttool.parser import parse + + +class TestParser(unittest.TestCase): + def test_parser(self): + f = """ + using Gtk 4.0; + + template MyAppWindow : Gtk.ApplicationWindow { + title: _("Hello, world!"); + app-defined-prop: 10.5; + + [titlebar] + Gtk.HeaderBar header_bar { + } + + Gtk.Button { + clicked => on_clicked() swapped; + } + } + + Label { + label: "Text"; + notify::visible => on_notify_visible(); + } + + Box {} + """ + + tokens = tokenize(f) + ui = parse(tokens) + self.assertIsInstance(ui, UI) + self.assertEqual(len(ui.errors), 0) + + self.assertIsInstance(ui.gtk_directive, GtkDirective) + self.assertEqual(ui.gtk_directive.version, "4.0") + + self.assertEqual(len(ui.templates), 1) + template = ui.templates[0] + self.assertEqual(template.name, "MyAppWindow") + self.assertEqual(template.parent_namespace, "Gtk") + self.assertEqual(template.parent_class, "ApplicationWindow") + + self.assertEqual(len(template.object_content.properties), 2) + prop = template.object_content.properties[0] + self.assertEqual(prop.name, "title") + self.assertEqual(prop.value, "Hello, world!") + self.assertTrue(prop.translatable) + prop = template.object_content.properties[1] + self.assertEqual(prop.name, "app-defined-prop") + self.assertEqual(prop.value, 10.5) + self.assertFalse(prop.translatable) + + self.assertEqual(len(template.object_content.children), 2) + child = template.object_content.children[0] + self.assertEqual(child.child_type, "titlebar") + self.assertEqual(child.object.id, "header_bar") + self.assertEqual(child.object.namespace, "Gtk") + self.assertEqual(child.object.class_name, "HeaderBar") + child = template.object_content.children[1] + self.assertIsNone(child.child_type) + self.assertIsNone(child.object.id) + self.assertEqual(child.object.namespace, "Gtk") + self.assertEqual(child.object.class_name, "Button") + self.assertEqual(len(child.object.object_content.signals), 1) + signal = child.object.object_content.signals[0] + self.assertEqual(signal.name, "clicked") + self.assertEqual(signal.handler, "on_clicked") + self.assertTrue(signal.swapped) + self.assertIsNone(signal.detail_name) + + self.assertEqual(len(ui.objects), 2) + obj = ui.objects[0] + self.assertIsNone(obj.namespace) + self.assertEqual(obj.class_name, "Label") + self.assertEqual(len(obj.object_content.properties), 1) + prop = obj.object_content.properties[0] + self.assertEqual(prop.name, "label") + self.assertEqual(prop.value, "Text") + self.assertFalse(prop.translatable) + self.assertEqual(len(obj.object_content.signals), 1) + signal = obj.object_content.signals[0] + self.assertEqual(signal.name, "notify") + self.assertEqual(signal.handler, "on_notify_visible") + self.assertEqual(signal.detail_name, "visible") + self.assertFalse(signal.swapped)