PageRenderTime 22ms CodeModel.GetById 2ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_module.py

http://unladen-swallow.googlecode.com/
Python | 62 lines | 47 code | 8 blank | 7 comment | 1 complexity | 05705c00174f2958ce54d062b7c08477 MD5 | raw file
 1# Test the module type
 2import unittest
 3from test.test_support import run_unittest
 4
 5import sys
 6ModuleType = type(sys)
 7
 8class ModuleTests(unittest.TestCase):
 9    def test_uninitialized(self):
10        # An uninitialized module has no __dict__ or __name__,
11        # and __doc__ is None
12        foo = ModuleType.__new__(ModuleType)
13        self.failUnless(foo.__dict__ is None)
14        self.assertRaises(SystemError, dir, foo)
15        try:
16            s = foo.__name__
17            self.fail("__name__ = %s" % repr(s))
18        except AttributeError:
19            pass
20        self.assertEqual(foo.__doc__, ModuleType.__doc__)
21
22    def test_no_docstring(self):
23        # Regularly initialized module, no docstring
24        foo = ModuleType("foo")
25        self.assertEqual(foo.__name__, "foo")
26        self.assertEqual(foo.__doc__, None)
27        self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None})
28
29    def test_ascii_docstring(self):
30        # ASCII docstring
31        foo = ModuleType("foo", "foodoc")
32        self.assertEqual(foo.__name__, "foo")
33        self.assertEqual(foo.__doc__, "foodoc")
34        self.assertEqual(foo.__dict__,
35                         {"__name__": "foo", "__doc__": "foodoc"})
36
37    def test_unicode_docstring(self):
38        # Unicode docstring
39        foo = ModuleType("foo", u"foodoc\u1234")
40        self.assertEqual(foo.__name__, "foo")
41        self.assertEqual(foo.__doc__, u"foodoc\u1234")
42        self.assertEqual(foo.__dict__,
43                         {"__name__": "foo", "__doc__": u"foodoc\u1234"})
44
45    def test_reinit(self):
46        # Reinitialization should not replace the __dict__
47        foo = ModuleType("foo", u"foodoc\u1234")
48        foo.bar = 42
49        d = foo.__dict__
50        foo.__init__("foo", "foodoc")
51        self.assertEqual(foo.__name__, "foo")
52        self.assertEqual(foo.__doc__, "foodoc")
53        self.assertEqual(foo.bar, 42)
54        self.assertEqual(foo.__dict__,
55              {"__name__": "foo", "__doc__": "foodoc", "bar": 42})
56        self.failUnless(foo.__dict__ is d)
57
58def test_main():
59    run_unittest(ModuleTests)
60
61if __name__ == '__main__':
62    test_main()