e1af1f1d0b94b4dd471d1267fe05762a4d428cee
[platform/upstream/pygobject2.git] / tests / test_overrides.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
3
4 import unittest
5
6 import gi.overrides
7 import gi.module
8
9 try:
10     from gi.repository import Regress
11     Regress  # pyflakes
12 except ImportError:
13     Regress = None
14
15
16 class TestRegistry(unittest.TestCase):
17     def test_non_gi(self):
18         class MyClass:
19             pass
20
21         try:
22             gi.overrides.override(MyClass)
23             self.fail('unexpected success of overriding non-GI class')
24         except TypeError as e:
25             self.assertTrue('Can not override a type MyClass' in str(e))
26
27     @unittest.skipUnless(Regress, 'built without cairo support')
28     def test_separate_path(self):
29         # Regress override is in tests/gi/overrides, separate from gi/overrides
30         # https://bugzilla.gnome.org/show_bug.cgi?id=680913
31         self.assertEqual(Regress.REGRESS_OVERRIDE, 42)
32
33
34 class TestModule(unittest.TestCase):
35     # Tests for gi.module
36
37     def test_get_introspection_module_caching(self):
38         # This test attempts to minimize side effects by
39         # using a DynamicModule directly instead of going though:
40         # from gi.repository import Foo
41
42         # Clear out introspection module cache before running this test.
43         old_modules = gi.module._introspection_modules
44         gi.module._introspection_modules = {}
45
46         mod_name = 'GIMarshallingTests'
47         mod1 = gi.module.get_introspection_module(mod_name)
48         mod2 = gi.module.get_introspection_module(mod_name)
49         self.assertTrue(mod1 is mod2)
50
51         # Using a DynamicModule will use get_introspection_module internally
52         # in its _load method.
53         mod_overridden = gi.module.DynamicModule(mod_name)
54         mod_overridden._load()
55         self.assertTrue(mod1 is mod_overridden._introspection_module)
56
57         # Restore the previous cache
58         gi.module._introspection_modules = old_modules