1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
12 from gi.repository import Regress
18 class TestOverrides(unittest.TestCase):
20 def test_non_gi(self):
25 gi.overrides.override(MyClass)
26 self.fail('unexpected success of overriding non-GI class')
27 except TypeError as e:
28 self.assertTrue('Can not override a type MyClass' in str(e))
30 @unittest.skipUnless(Regress, 'built without cairo support')
31 def test_separate_path(self):
32 # Regress override is in tests/gi/overrides, separate from gi/overrides
33 # https://bugzilla.gnome.org/show_bug.cgi?id=680913
34 self.assertEqual(Regress.REGRESS_OVERRIDE, 42)
36 def test_load_overrides(self):
37 mod = gi.module.get_introspection_module('GIMarshallingTests')
38 mod_override = gi.overrides.load_overrides(mod)
39 self.assertTrue(mod_override is not mod)
40 self.assertTrue(mod_override._introspection_module is mod)
41 self.assertEqual(mod_override.OVERRIDES_CONSTANT, 7)
42 self.assertEqual(mod.OVERRIDES_CONSTANT, 42)
44 def test_load_no_overrides(self):
45 mod_key = "gi.overrides.GIMarshallingTests"
46 had_mod = mod_key in sys.modules
47 old_mod = sys.modules.get(mod_key)
49 # this makes override import fail
50 sys.modules[mod_key] = None
51 mod = gi.module.get_introspection_module('GIMarshallingTests')
52 mod_override = gi.overrides.load_overrides(mod)
53 self.assertTrue(mod_override is mod)
55 del sys.modules[mod_key]
57 sys.modules[mod_key] = old_mod
60 class TestModule(unittest.TestCase):
63 def test_get_introspection_module_caching(self):
64 # This test attempts to minimize side effects by
65 # using a DynamicModule directly instead of going though:
66 # from gi.repository import Foo
68 # Clear out introspection module cache before running this test.
69 old_modules = gi.module._introspection_modules
70 gi.module._introspection_modules = {}
72 mod_name = 'GIMarshallingTests'
73 mod1 = gi.module.get_introspection_module(mod_name)
74 mod2 = gi.module.get_introspection_module(mod_name)
75 self.assertTrue(mod1 is mod2)
77 # Restore the previous cache
78 gi.module._introspection_modules = old_modules
80 def test_module_dependency_loading(self):
81 # Difficult to because this generally need to run in isolation to make
82 # sure GIMarshallingTests has not yet been loaded. But we can do this with:
83 # make check TEST_NAMES=test_import_machinery.TestModule.test_module_dependency_loading
84 if 'gi.repository.Gio' in sys.modules:
87 from gi.repository import GIMarshallingTests
88 GIMarshallingTests # PyFlakes
90 self.assertIn('gi.repository.Gio', sys.modules)
91 self.assertIn('gi.repository.GIMarshallingTests', sys.modules)
93 def test_static_binding_protection(self):
94 # Importing old static bindings once gi has been imported should not
95 # crash but instead give back a dummy module which produces RuntimeErrors
97 with self.assertRaises(AttributeError):
101 with self.assertRaises(AttributeError):
105 with self.assertRaises(AttributeError):
109 with self.assertRaises(AttributeError):
113 with self.assertRaises(AttributeError):
118 class TestImporter(unittest.TestCase):
119 def test_invalid_repository_module_name(self):
120 with self.assertRaises(ImportError) as context:
121 from gi.repository import InvalidGObjectRepositoryModuleName
122 InvalidGObjectRepositoryModuleName # pyflakes
124 exception_string = str(context.exception)
126 self.assertTrue('InvalidGObjectRepositoryModuleName' in exception_string)
128 # The message of the custom exception in gi/importer.py is eaten in Python <3.3
129 if sys.version_info < (3, 3):
130 self.assertTrue('introspection typelib' not in exception_string)
132 self.assertTrue('introspection typelib' in exception_string)
134 def test_require_version_warning(self):
135 check = gi.importer._check_require_version
137 # make sure it doesn't fail at least
138 with check("GLib", 1):
139 from gi.repository import GLib
142 # make sure the exception propagates
143 with self.assertRaises(ImportError):
144 with check("InvalidGObjectRepositoryModuleName", 1):
145 from gi.repository import InvalidGObjectRepositoryModuleName
146 InvalidGObjectRepositoryModuleName
148 def test_require_versions(self):
150 gi.require_versions({'GLib': '2.0', 'Gio': '2.0', 'GObject': '2.0'})
151 from gi.repository import GLib
154 def test_get_import_stacklevel(self):
155 gi.importer.get_import_stacklevel(import_hook=True)
156 gi.importer.get_import_stacklevel(import_hook=False)