GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pythonnet/src/tests/test_module.py at python3 · fkarb/pythonnet Expand file tree/ Copy pathtest_module.pyMore file actions 389 lines (291 loc) · 13.4 KB / Copy pathtest_module.pyFile metadata and controls389 lines (291 loc) · 13.4 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 # =========================================================================== # This software is subject to the provisions of the Zope Public License, # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS # FOR A PARTICULAR PURPOSE. # =========================================================================== import clr clr.AddReference('Python.Test') clr.AddReference('System.Data') # testImplicitAssemblyLoad() passes on deprecation warning; perfect! # ##clr.AddReference('System.Windows.Forms') import sys, os, string, unittest, types, warnings from fnmatch import fnmatch import six if six.PY3: ClassType = type else: ClassType = types.ClassType class ModuleTests(unittest.TestCase): """Test CLR modules and the CLR import hook.""" def isCLRModule(self, object): return type(object).__name__ == 'ModuleObject' def isCLRRootModule(self, object): if six.PY3: # in Python 3 the clr module is a normal python module return object.__name__ == "clr" return type(object).__name__ == 'CLRModule' def isCLRClass(self, object): return type(object).__name__ == 'CLR Metatype' # for now def testAAAImportHookWorks(self): """Test that the import hook works correctly both using the included runtime and an external runtime. This must be the first test run in the unit tests!""" from System import String def test000importClr(self): import clr self.assertTrue(self.isCLRRootModule(clr)) def testPreloadVar(self): import clr self.assertTrue(clr.getPreload() is False, clr.getPreload()) clr.setPreload(False) self.assertTrue(clr.getPreload() is False, clr.getPreload()) try: clr.setPreload(True) self.assertTrue(clr.getPreload() is True, clr.getPreload()) clr.setPreload(0) self.assertTrue(clr.getPreload() is False, clr.getPreload()) clr.setPreload(1) self.assertTrue(clr.getPreload() is True, clr.getPreload()) import System.Configuration content = dir(System.Configuration) self.assertTrue(len(content) > 10, content) finally: clr.setPreload(False) def testModuleInterface(self): """Test the interface exposed by CLR module objects.""" import System self.assertEquals(type(System.__dict__), type({})) self.assertEquals(System.__name__, 'System') # the filename can be any module from the System namespace (eg System.Data.dll or System.dll) self.assertTrue(fnmatch(System.__file__, "*System*.dll")) self.assertTrue(System.__doc__.startswith("Namespace containing types from the following assemblies:")) self.assertTrue(self.isCLRClass(System.String)) self.assertTrue(self.isCLRClass(System.Int32)) def testSimpleImport(self): """Test simple import.""" import System self.assertTrue(self.isCLRModule(System)) self.assertTrue(System.__name__ == 'System') import sys self.assertTrue(type(sys) == types.ModuleType) self.assertTrue(sys.__name__ == 'sys') if six.PY3: import http.client as httplib self.assertTrue(type(httplib) == types.ModuleType) self.assertTrue(httplib.__name__ == 'http.client') else: import httplib self.assertTrue(type(httplib) == types.ModuleType) self.assertTrue(httplib.__name__ == 'httplib') def testSimpleImportWithAlias(self): """Test simple import with aliasing.""" import System as mySystem self.assertTrue(self.isCLRModule(mySystem)) self.assertTrue(mySystem.__name__ == 'System') import sys as mySys self.assertTrue(type(mySys) == types.ModuleType) self.assertTrue(mySys.__name__ == 'sys') if six.PY3: import http.client as myHttplib self.assertTrue(type(myHttplib) == types.ModuleType) self.assertTrue(myHttplib.__name__ == 'http.client') else: import httplib as myHttplib self.assertTrue(type(myHttplib) == types.ModuleType) self.assertTrue(myHttplib.__name__ == 'httplib') def testDottedNameImport(self): """Test dotted-name import.""" import System.Reflection self.assertTrue(self.isCLRModule(System.Reflection)) self.assertTrue(System.Reflection.__name__ == 'System.Reflection') import xml.dom self.assertTrue(type(xml.dom) == types.ModuleType) self.assertTrue(xml.dom.__name__ == 'xml.dom') def testMultipleDottedNameImport(self): """Test an import bug with multiple dotted imports.""" import System.Data self.assertTrue(self.isCLRModule(System.Data)) self.assertTrue(System.Data.__name__ == 'System.Data') import System.Data self.assertTrue(self.isCLRModule(System.Data)) self.assertTrue(System.Data.__name__ == 'System.Data') def testDottedNameImportWithAlias(self): """Test dotted-name import with aliasing.""" import System.Reflection as SysRef self.assertTrue(self.isCLRModule(SysRef)) self.assertTrue(SysRef.__name__ == 'System.Reflection') import xml.dom as myDom self.assertTrue(type(myDom) == types.ModuleType) self.assertTrue(myDom.__name__ == 'xml.dom') def testSimpleImportFrom(self): """Test simple 'import from'.""" from System import Reflection self.assertTrue(self.isCLRModule(Reflection)) self.assertTrue(Reflection.__name__ == 'System.Reflection') from xml import dom self.assertTrue(type(dom) == types.ModuleType) self.assertTrue(dom.__name__ == 'xml.dom') def testSimpleImportFromWithAlias(self): """Test simple 'import from' with aliasing.""" from System import Collections as Coll self.assertTrue(self.isCLRModule(Coll)) self.assertTrue(Coll.__name__ == 'System.Collections') from xml import dom as myDom self.assertTrue(type(myDom) == types.ModuleType) self.assertTrue(myDom.__name__ == 'xml.dom') def testDottedNameImportFrom(self): """Test dotted-name 'import from'.""" from System.Collections import Specialized self.assertTrue(self.isCLRModule(Specialized)) self.assertTrue( Specialized.__name__ == 'System.Collections.Specialized' ) from System.Collections.Specialized import StringCollection self.assertTrue(self.isCLRClass(StringCollection)) self.assertTrue(StringCollection.__name__ == 'StringCollection') from xml.dom import pulldom self.assertTrue(type(pulldom) == types.ModuleType) self.assertTrue(pulldom.__name__ == 'xml.dom.pulldom') from xml.dom.pulldom import PullDOM self.assertTrue(type(PullDOM) == ClassType) self.assertTrue(PullDOM.__name__ == 'PullDOM') def testDottedNameImportFromWithAlias(self): """Test dotted-name 'import from' with aliasing.""" from System.Collections import Specialized as Spec self.assertTrue(self.isCLRModule(Spec)) self.assertTrue(Spec.__name__ == 'System.Collections.Specialized') from System.Collections.Specialized import StringCollection as SC self.assertTrue(self.isCLRClass(SC)) self.assertTrue(SC.__name__ == 'StringCollection') from xml.dom import pulldom as myPulldom self.assertTrue(type(myPulldom) == types.ModuleType) self.assertTrue(myPulldom.__name__ == 'xml.dom.pulldom') from xml.dom.pulldom import PullDOM as myPullDOM self.assertTrue(type(myPullDOM) == ClassType) self.assertTrue(myPullDOM.__name__ == 'PullDOM') def testFromModuleImportStar(self): """Test from module import * behavior.""" count = len(locals().keys()) m = __import__('System.Xml', globals(), locals(), ['*']) self.assertTrue(m.__name__ == 'System.Xml') self.assertTrue(self.isCLRModule(m)) self.assertTrue(len(locals().keys()) > count + 1) def testImplicitAssemblyLoad(self): """Test implicit assembly loading via import.""" with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") # should trigger a DeprecationWarning as Microsoft.Build hasn't # been added as a reference yet (and should exist for mono) import Microsoft.Build self.assertEqual(len(w), 1) self.assertTrue(isinstance(w[0].message, DeprecationWarning)) with warnings.catch_warnings(record=True) as w: clr.AddReference("System.Windows.Forms") import System.Windows.Forms as Forms self.assertTrue(self.isCLRModule(Forms)) self.assertTrue(Forms.__name__ == 'System.Windows.Forms') from System.Windows.Forms import Form self.assertTrue(self.isCLRClass(Form)) self.assertTrue(Form.__name__ == 'Form') self.assertEqual(len(w), 0) def testExplicitAssemblyLoad(self): """Test explicit assembly loading using standard CLR tools.""" from System.Reflection import Assembly import System, sys assembly = Assembly.LoadWithPartialName('System.Data') self.assertTrue(assembly != None) import System.Data self.assertTrue('System.Data' in sys.modules) assembly = Assembly.LoadWithPartialName('SpamSpamSpamSpamEggsAndSpam') self.assertTrue(assembly == None) def testImplicitLoadAlreadyValidNamespace(self): """Test implicit assembly load over an already valid namespace.""" # In this case, the mscorlib assembly (loaded by default) defines # a number of types in the System namespace. There is also a System # assembly, which is _not_ loaded by default, which also contains # types in the System namespace. The desired behavior is for the # Python runtime to "do the right thing", allowing types from both # assemblies to be found in the System module implicitly. import System self.assertTrue(self.isCLRClass(System.UriBuilder)) def testImportNonExistantModule(self): """Test import failure for a non-existant module.""" def test(): import System.SpamSpamSpam self.assertTrue(ImportError, test) def testLookupNoNamespaceType(self): """Test lookup of types without a qualified namespace.""" import Python.Test import clr self.assertTrue(self.isCLRClass(clr.NoNamespaceType)) def testModuleLookupRecursion(self): """Test for recursive lookup handling.""" def test1(): from System import System self.assertTrue(ImportError, test1) def test2(): import System x = System.System self.assertTrue(AttributeError, test2) def testModuleGetAttr(self): """Test module getattr behavior.""" import System int_type = System.Int32 self.assertTrue(self.isCLRClass(int_type)) module = System.Xml self.assertTrue(self.isCLRModule(module)) def test(): spam = System.Spam self.assertTrue(AttributeError, test) def test(): spam = getattr(System, 1) self.assertTrue(TypeError, test) def testModuleAttrAbuse(self): """Test handling of attempts to set module attributes.""" # It would be safer to use a dict-proxy as the __dict__ for CLR # modules, but as of Python 2.3 some parts of the CPython runtime # like dir() will fail if a module dict is not a real dictionary. def test(): import System System.__dict__['foo'] = 0 return 1 self.assertTrue(test()) def testModuleTypeAbuse(self): """Test handling of attempts to break the module type.""" import System mtype = type(System) def test(): mtype.__getattribute__(0, 'spam') self.assertTrue(TypeError, test) def test(): mtype.__setattr__(0, 'spam', 1) self.assertTrue(TypeError, test) def test(): mtype.__repr__(0) self.assertTrue(TypeError, test) def test_ClrListAssemblies(self): from clr import ListAssemblies verbose = list(ListAssemblies(True)) short = list(ListAssemblies(False)) self.assertTrue(six.u('mscorlib') in short) self.assertTrue(six.u('System') in short) self.assertTrue(six.u('Culture=') in verbose[0]) self.assertTrue(six.u('Version=') in verbose[0]) def test_ClrAddReference(self): from clr import AddReference from System.IO import FileNotFoundException for name in ("System", "Python.Runtime"): assy = AddReference(name) assyName = assy.GetName().Name self.assertEqual(assyName, name) self.assertRaises(FileNotFoundException, AddReference, "somethingtotallysilly") def test_suite(): return unittest.makeSuite(ModuleTests) def main(): unittest.TextTestRunner().run(test_suite()) if __name__ == '__main__': main() |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |