blob: a9713f5cd4d583d182fab6d2feaf878aece4d18d [file] [log] [blame]
Guido van Rossum4114a4a2001-10-18 18:49:37 +00001# Test the frozen module defined in frozen.c.
2
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test.support import captured_stdout, run_unittest
Thomas Woutersed03b412007-08-28 21:37:11 +00004import unittest
Guido van Rossum4114a4a2001-10-18 18:49:37 +00005import sys, os
6
Thomas Woutersed03b412007-08-28 21:37:11 +00007class FrozenTests(unittest.TestCase):
8 def test_frozen(self):
Victor Stinnerc77b9312011-05-16 16:29:35 +02009 with captured_stdout() as stdout:
10 try:
11 import __hello__
12 except ImportError as x:
13 self.fail("import __hello__ failed:" + str(x))
14 self.assertEqual(__hello__.initialized, True)
15 self.assertEqual(len(dir(__hello__)), 6, dir(__hello__))
16 self.assertEqual(stdout.getvalue(), 'Hello world!\n')
Guido van Rossum4114a4a2001-10-18 18:49:37 +000017
Victor Stinnerc77b9312011-05-16 16:29:35 +020018 with captured_stdout() as stdout:
19 try:
20 import __phello__
21 except ImportError as x:
22 self.fail("import __phello__ failed:" + str(x))
23 self.assertEqual(__phello__.initialized, True)
24 if not "__phello__.spam" in sys.modules:
25 self.assertEqual(len(dir(__phello__)), 7, dir(__phello__))
26 else:
27 self.assertEqual(len(dir(__phello__)), 8, dir(__phello__))
28 self.assertEqual(__phello__.__path__, [__phello__.__name__])
29 self.assertEqual(stdout.getvalue(), 'Hello world!\n')
Guido van Rossum4114a4a2001-10-18 18:49:37 +000030
Victor Stinnerc77b9312011-05-16 16:29:35 +020031 with captured_stdout() as stdout:
32 try:
33 import __phello__.spam
34 except ImportError as x:
35 self.fail("import __phello__.spam failed:" + str(x))
36 self.assertEqual(__phello__.spam.initialized, True)
37 self.assertEqual(len(dir(__phello__.spam)), 6)
38 self.assertEqual(len(dir(__phello__)), 8)
39 self.assertEqual(stdout.getvalue(), 'Hello world!\n')
Guido van Rossum4114a4a2001-10-18 18:49:37 +000040
Christian Heimesf19169f2007-11-12 19:19:07 +000041 try:
42 import __phello__.foo
43 except ImportError:
44 pass
45 else:
46 self.fail("import __phello__.foo should have failed")
Thomas Woutersed03b412007-08-28 21:37:11 +000047
Christian Heimesdae2a892008-04-19 00:55:37 +000048 if sys.platform != "mac": # On the Mac this import does succeed.
49 try:
50 import __phello__.foo
51 except ImportError:
52 pass
53 else:
54 self.fail("import __phello__.foo should have failed")
55
56 del sys.modules['__hello__']
57 del sys.modules['__phello__']
58 del sys.modules['__phello__.spam']
59
Thomas Woutersed03b412007-08-28 21:37:11 +000060def test_main():
61 run_unittest(FrozenTests)
Guido van Rossum3b7210d2007-10-15 00:25:56 +000062
63if __name__ == "__main__":
64 test_main()