From 7bc20fe2d1225bebb2f4ec1faabf6a70e4e5a4e7 Mon Sep 17 00:00:00 2001 From: Mikhail Fludkov Date: Thu, 8 Feb 2018 12:49:01 +0100 Subject: tests: Make tests run without Gtk/Gdk installed Python evaluates the code inside @unittest.skipIf decorator during import time. Because Gtk is not installed it crashes while evaluating 'Gtk._version' where Gtk=None. --- tests/test_overrides_gtk.py | 50 +++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'tests/test_overrides_gtk.py') diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py index f15e717d..338ca72c 100644 --- a/tests/test_overrides_gtk.py +++ b/tests/test_overrides_gtk.py @@ -17,8 +17,10 @@ from gi.repository import GLib, GObject try: from gi.repository import Gtk, GdkPixbuf, Gdk PyGTKDeprecationWarning = Gtk.PyGTKDeprecationWarning + Gtk_version = Gtk._version except ImportError: Gtk = None + Gtk_version = None PyGTKDeprecationWarning = None GdkPixbuf = None Gdk = None @@ -76,7 +78,7 @@ class TestGtk(unittest.TestCase): labels = [x for x in box] self.assertEqual(labels, [label, label2]) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_actions(self): self.assertEqual(Gtk.Action, gi.overrides.Gtk.Action) action = Gtk.Action(name="test", label="Test", tooltip="Test Action", stock_id=Gtk.STOCK_COPY) @@ -93,7 +95,7 @@ class TestGtk(unittest.TestCase): self.assertEqual(action.get_stock_id(), Gtk.STOCK_COPY) self.assertEqual(action.get_current_value(), 1) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_actiongroup(self): self.assertEqual(Gtk.ActionGroup, gi.overrides.Gtk.ActionGroup) @@ -135,7 +137,7 @@ class TestGtk(unittest.TestCase): expected_results.remove(a) action.activate() - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_uimanager(self): self.assertEqual(Gtk.UIManager, gi.overrides.Gtk.UIManager) ui = Gtk.UIManager() @@ -155,7 +157,7 @@ class TestGtk(unittest.TestCase): self.assertEqual(ag, groups[-2]) self.assertEqual(ag2, groups[-1]) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_uimanager_nonascii(self): ui = Gtk.UIManager() ui.add_ui_from_string(b''.decode('UTF-8')) @@ -198,7 +200,7 @@ class TestGtk(unittest.TestCase): self.assertEqual(Gtk.Dialog, gi.overrides.Gtk.Dialog) self.assertEqual(Gtk.FileChooserDialog, gi.overrides.Gtk.FileChooserDialog) self.assertEqual(Gtk.RecentChooserDialog, gi.overrides.Gtk.RecentChooserDialog) - if Gtk._version != "4.0": + if Gtk_version != "4.0": self.assertEqual(Gtk.ColorSelectionDialog, gi.overrides.Gtk.ColorSelectionDialog) self.assertEqual(Gtk.FontSelectionDialog, gi.overrides.Gtk.FontSelectionDialog) @@ -306,7 +308,7 @@ class TestGtk(unittest.TestCase): self.assertEqual(dialog.get_property('secondary-text'), '2nd markup') self.assertTrue(dialog.get_property('secondary-use-markup')) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_color_selection_dialog(self): dialog = Gtk.ColorSelectionDialog(title="color selection dialog test") self.assertTrue(isinstance(dialog, Gtk.Dialog)) @@ -334,7 +336,7 @@ class TestGtk(unittest.TestCase): action = dialog.get_property('action') self.assertEqual(Gtk.FileChooserAction.OPEN, action) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_font_selection_dialog(self): dialog = Gtk.FontSelectionDialog(title="font selection dialog test") self.assertTrue(isinstance(dialog, Gtk.Dialog)) @@ -371,7 +373,7 @@ class TestGtk(unittest.TestCase): self.assertTrue(isinstance(button, Gtk.Container)) self.assertTrue(isinstance(button, Gtk.Widget)) - if Gtk._version != "4.0": + if Gtk_version != "4.0": # Using stock items causes hard warning in devel versions of GTK+. with capture_glib_warnings(allow_warnings=True): button = Gtk.Button.new_from_stock(Gtk.STOCK_CLOSE) @@ -469,7 +471,7 @@ class TestGtk(unittest.TestCase): adjustment = Gtk.Adjustment(1, -1, 3, 0, 0, 0, value=2) self.adjustment_check(adjustment, value=2, lower=-1, upper=3) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_table(self): table = Gtk.Table() self.assertTrue(isinstance(table, Gtk.Table)) @@ -531,7 +533,7 @@ class TestGtk(unittest.TestCase): widget.drag_source_add_uri_targets() widget.drag_source_set_icon_name("_About") widget.drag_source_set_icon_pixbuf(GdkPixbuf.Pixbuf()) - if Gtk._version != "4.0": + if Gtk_version != "4.0": widget.drag_source_set_icon_stock(Gtk.STOCK_ABOUT) widget.drag_source_get_target_list() widget.drag_source_set_target_list(None) @@ -540,7 +542,7 @@ class TestGtk(unittest.TestCase): # these methods cannot be called because they require a valid drag on # a real GdkWindow. So we only check that they exist and are callable. - if Gtk._version != "4.0": + if Gtk_version != "4.0": self.assertTrue(hasattr(widget, 'drag_dest_set_proxy')) self.assertTrue(hasattr(widget, 'drag_get_data')) @@ -577,7 +579,7 @@ class TestGtk(unittest.TestCase): treeview.enable_model_drag_dest(mixed_target_list, Gdk.DragAction.DEFAULT | Gdk.DragAction.MOVE) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_scrollbar(self): adjustment = Gtk.Adjustment() @@ -600,7 +602,7 @@ class TestGtk(unittest.TestCase): iconview = Gtk.IconView(model=model) self.assertEqual(iconview.props.model, model) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_toolbutton(self): # PyGTK compat @@ -623,7 +625,7 @@ class TestGtk(unittest.TestCase): self.assertEqual(button.props.label, 'mylabel') self.assertEqual(button.props.icon_widget, icon) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_iconset(self): Gtk.IconSet() pixbuf = GdkPixbuf.Pixbuf() @@ -639,7 +641,7 @@ class TestGtk(unittest.TestCase): self.assertEqual(viewport.props.vadjustment, vadjustment) self.assertEqual(viewport.props.hadjustment, hadjustment) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_stock_lookup(self): stock_item = Gtk.stock_lookup('gtk-ok') self.assertEqual(type(stock_item), Gtk.StockItem) @@ -655,7 +657,7 @@ class TestGtk(unittest.TestCase): GLib.idle_add(Gtk.main_quit, 'hello') Gtk.main() - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_widget_render_icon(self): button = Gtk.Button(label='OK') pixbuf = button.render_icon(Gtk.STOCK_OK, Gtk.IconSize.BUTTON) @@ -663,7 +665,7 @@ class TestGtk(unittest.TestCase): @unittest.skipUnless(Gtk, 'Gtk not available') -@unittest.skipIf(Gtk._version == "4.0", "not in gtk4") +@unittest.skipIf(Gtk_version == "4.0", "not in gtk4") class TestWidget(unittest.TestCase): def test_style_get_property_gvalue(self): button = Gtk.Button() @@ -2002,7 +2004,7 @@ class TestTextBuffer(unittest.TestCase): @unittest.skipUnless(Gtk, 'Gtk not available') class TestContainer(unittest.TestCase): - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_child_set_property(self): box = Gtk.Box() child = Gtk.Button() @@ -2014,7 +2016,7 @@ class TestContainer(unittest.TestCase): box.child_get_property(child, 'padding', value) self.assertEqual(value.get_int(), 42) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_child_get_property_gvalue(self): box = Gtk.Box() child = Gtk.Button() @@ -2024,7 +2026,7 @@ class TestContainer(unittest.TestCase): box.child_get_property(child, 'padding', value) self.assertEqual(value.get_int(), 42) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_child_get_property_return_with_explicit_gvalue(self): box = Gtk.Box() child = Gtk.Button() @@ -2034,7 +2036,7 @@ class TestContainer(unittest.TestCase): result = box.child_get_property(child, 'padding', value) self.assertEqual(result, 42) - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_child_get_property_return_with_implicit_gvalue(self): box = Gtk.Box() child = Gtk.Button() @@ -2046,14 +2048,14 @@ class TestContainer(unittest.TestCase): def test_child_get_property_error(self): box = Gtk.Box() child = Gtk.Button() - if Gtk._version == "4.0": + if Gtk_version == "4.0": box.pack_start(child, expand=False, fill=True) else: box.pack_start(child, expand=False, fill=True, padding=42) with self.assertRaises(ValueError): box.child_get_property(child, 'not-a-valid-child-property') - @unittest.skipIf(Gtk._version == "4.0", "not in gtk4") + @unittest.skipIf(Gtk_version == "4.0", "not in gtk4") def test_child_get_and_set(self): box = Gtk.Box() child = Gtk.Button() @@ -2070,7 +2072,7 @@ class TestContainer(unittest.TestCase): self.assertEqual(fill, False) self.assertEqual(padding, 21) - @unittest.skipIf(Gtk._version != "4.0", "only in gtk4") + @unittest.skipIf(Gtk_version != "4.0", "only in gtk4") def test_child_get_and_set_gtk4(self): # padding got removed in gtk4 box = Gtk.Box() -- cgit v1.2.1