| Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 1 | # Test the frozen module defined in frozen.c. | 
 | 2 |  | 
| Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test.support import captured_stdout, run_unittest | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 4 | import unittest | 
| Georg Brandl | 1b37e87 | 2010-03-14 10:45:50 +0000 | [diff] [blame] | 5 | import sys | 
| Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 6 |  | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 7 | class FrozenTests(unittest.TestCase): | 
 | 8 |     def test_frozen(self): | 
| Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 9 |         try: | 
 | 10 |             import __hello__ | 
 | 11 |         except ImportError as x: | 
 | 12 |             self.fail("import __hello__ failed:" + str(x)) | 
 | 13 |         self.assertEqual(__hello__.initialized, True) | 
| Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 14 |         self.assertEqual(len(dir(__hello__)), 7, dir(__hello__)) | 
| Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 15 |  | 
| Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 16 |         try: | 
 | 17 |             import __phello__ | 
 | 18 |         except ImportError as x: | 
 | 19 |             self.fail("import __phello__ failed:" + str(x)) | 
 | 20 |         self.assertEqual(__phello__.initialized, True) | 
 | 21 |         if not "__phello__.spam" in sys.modules: | 
| Christian Heimes | cbf3b5c | 2007-12-03 21:02:03 +0000 | [diff] [blame] | 22 |             self.assertEqual(len(dir(__phello__)), 8, dir(__phello__)) | 
| Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 23 |         else: | 
 | 24 |             self.assertEqual(len(dir(__phello__)), 9, dir(__phello__)) | 
| Benjamin Peterson | d968e27 | 2008-11-05 22:48:33 +0000 | [diff] [blame] | 25 |         self.assertEquals(__phello__.__path__, [__phello__.__name__]) | 
| Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 26 |  | 
| Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 27 |         try: | 
 | 28 |             import __phello__.spam | 
 | 29 |         except ImportError as x: | 
 | 30 |             self.fail("import __phello__.spam failed:" + str(x)) | 
 | 31 |         self.assertEqual(__phello__.spam.initialized, True) | 
| Barry Warsaw | 28a691b | 2010-04-17 00:19:56 +0000 | [diff] [blame] | 32 |         self.assertEqual(len(dir(__phello__.spam)), 7) | 
 | 33 |         self.assertEqual(len(dir(__phello__)), 9) | 
| Guido van Rossum | 4114a4a | 2001-10-18 18:49:37 +0000 | [diff] [blame] | 34 |  | 
| Christian Heimes | f19169f | 2007-11-12 19:19:07 +0000 | [diff] [blame] | 35 |         try: | 
 | 36 |             import __phello__.foo | 
 | 37 |         except ImportError: | 
 | 38 |             pass | 
 | 39 |         else: | 
 | 40 |             self.fail("import __phello__.foo should have failed") | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 41 |  | 
| Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 42 |             try: | 
 | 43 |                 import __phello__.foo | 
 | 44 |             except ImportError: | 
 | 45 |                 pass | 
 | 46 |             else: | 
 | 47 |                 self.fail("import __phello__.foo should have failed") | 
| Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 48 |  | 
 | 49 |         del sys.modules['__hello__'] | 
 | 50 |         del sys.modules['__phello__'] | 
 | 51 |         del sys.modules['__phello__.spam'] | 
 | 52 |  | 
| Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 53 | def test_main(): | 
 | 54 |     run_unittest(FrozenTests) | 
| Guido van Rossum | 3b7210d | 2007-10-15 00:25:56 +0000 | [diff] [blame] | 55 |  | 
 | 56 | if __name__ == "__main__": | 
 | 57 |     test_main() |