1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
4 from __future__
import absolute_import
12 from gi
._compat
import PY2
, PY3
14 from gi
.repository
import Regress
17 class TestOverrides(unittest
.TestCase
):
19 def test_non_gi(self
):
24 gi
.overrides
.override(MyClass
)
25 self
.fail('unexpected success of overriding non-GI class')
26 except TypeError as e
:
27 self
.assertTrue('Can not override a type MyClass' in str(e
))
29 def test_separate_path(self
):
30 # Regress override is in tests/gi/overrides, separate from gi/overrides
31 # https://bugzilla.gnome.org/show_bug.cgi?id=680913
32 self
.assertEqual(Regress
.REGRESS_OVERRIDE
, 42)
34 def test_load_overrides(self
):
35 mod
= gi
.module
.get_introspection_module('GIMarshallingTests')
36 mod_override
= gi
.overrides
.load_overrides(mod
)
37 self
.assertTrue(mod_override
is not mod
)
38 self
.assertTrue(mod_override
._introspection
_module
is mod
)
39 self
.assertEqual(mod_override
.OVERRIDES_CONSTANT
, 7)
40 self
.assertEqual(mod
.OVERRIDES_CONSTANT
, 42)
42 def test_load_no_overrides(self
):
43 mod_key
= "gi.overrides.GIMarshallingTests"
44 had_mod
= mod_key
in sys
.modules
45 old_mod
= sys
.modules
.get(mod_key
)
47 # this makes override import fail
48 sys
.modules
[mod_key
] = None
49 mod
= gi
.module
.get_introspection_module('GIMarshallingTests')
50 mod_override
= gi
.overrides
.load_overrides(mod
)
51 self
.assertTrue(mod_override
is mod
)
53 del sys
.modules
[mod_key
]
55 sys
.modules
[mod_key
] = old_mod
58 class TestModule(unittest
.TestCase
):
61 def test_get_introspection_module_caching(self
):
62 # This test attempts to minimize side effects by
63 # using a DynamicModule directly instead of going though:
64 # from gi.repository import Foo
66 # Clear out introspection module cache before running this test.
67 old_modules
= gi
.module
._introspection
_modules
68 gi
.module
._introspection
_modules
= {}
70 mod_name
= 'GIMarshallingTests'
71 mod1
= gi
.module
.get_introspection_module(mod_name
)
72 mod2
= gi
.module
.get_introspection_module(mod_name
)
73 self
.assertTrue(mod1
is mod2
)
75 # Restore the previous cache
76 gi
.module
._introspection
_modules
= old_modules
78 def test_module_dependency_loading(self
):
79 # Difficult to because this generally need to run in isolation to make
80 # sure GIMarshallingTests has not yet been loaded. But we can do this with:
81 # make check TEST_NAMES=test_import_machinery.TestModule.test_module_dependency_loading
82 if 'gi.repository.Gio' in sys
.modules
:
85 from gi
.repository
import GIMarshallingTests
86 GIMarshallingTests
# PyFlakes
88 self
.assertIn('gi.repository.Gio', sys
.modules
)
89 self
.assertIn('gi.repository.GIMarshallingTests', sys
.modules
)
91 def test_static_binding_protection(self
):
92 # Importing old static bindings once gi has been imported should not
93 # crash but instead give back a dummy module which produces RuntimeErrors
95 with self
.assertRaises(AttributeError):
99 with self
.assertRaises(AttributeError):
103 with self
.assertRaises(AttributeError):
107 with self
.assertRaises(AttributeError):
111 with self
.assertRaises(AttributeError):
116 class TestImporter(unittest
.TestCase
):
117 def test_invalid_repository_module_name(self
):
118 with self
.assertRaises(ImportError) as context
:
119 from gi
.repository
import InvalidGObjectRepositoryModuleName
120 InvalidGObjectRepositoryModuleName
# pyflakes
122 exception_string
= str(context
.exception
)
124 self
.assertTrue('InvalidGObjectRepositoryModuleName' in exception_string
)
126 # The message of the custom exception in gi/importer.py is eaten in Python <3.3
127 if sys
.version_info
>= (3, 3):
128 self
.assertTrue('introspection typelib' in exception_string
)
130 def test_require_version_warning(self
):
131 check
= gi
.importer
._check
_require
_version
133 # make sure it doesn't fail at least
134 with
check("GLib", 1):
135 from gi
.repository
import GLib
138 # make sure the exception propagates
139 with self
.assertRaises(ImportError):
140 with
check("InvalidGObjectRepositoryModuleName", 1):
141 from gi
.repository
import InvalidGObjectRepositoryModuleName
142 InvalidGObjectRepositoryModuleName
144 def test_require_version_versiontype(self
):
146 with self
.assertRaises(ValueError):
147 gi
.require_version('GLib', 2.0)
149 # Test that unicode strings work in python 2
151 gi
.require_version('GLib', u
'2.0')
154 with self
.assertRaises(ValueError):
155 gi
.require_version('GLib', b
'2.0')
157 def test_require_versions(self
):
159 gi
.require_versions({'GLib': '2.0', 'Gio': '2.0', 'GObject': '2.0'})
160 from gi
.repository
import GLib
163 def test_get_import_stacklevel(self
):
164 gi
.importer
.get_import_stacklevel(import_hook
=True)
165 gi
.importer
.get_import_stacklevel(import_hook
=False)