Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(30465)

Side by Side Diff: Lib/test/test_module.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_minidom.py ('k') | Lib/test/test_multiprocessing.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Test the module type 1 # Test the module type
2 import unittest 2 import unittest
3 from test.support import run_unittest, gc_collect 3 from test.support import run_unittest, gc_collect
4 4
5 import sys 5 import sys
6 ModuleType = type(sys) 6 ModuleType = type(sys)
7 7
8 class FullLoader: 8 class FullLoader:
9 @classmethod 9 @classmethod
10 def module_repr(cls, m): 10 def module_repr(cls, m):
11 return "<module '{}' (crafted)>".format(m.__name__) 11 return "<module '{}' (crafted)>".format(m.__name__)
12 12
13 class BareLoader: 13 class BareLoader:
14 pass 14 pass
15 15
16 16
17 class ModuleTests(unittest.TestCase): 17 class ModuleTests(unittest.TestCase):
18 def test_uninitialized(self): 18 def test_uninitialized(self):
19 # An uninitialized module has no __dict__ or __name__, 19 # An uninitialized module has no __dict__ or __name__,
20 # and __doc__ is None 20 # and __doc__ is None
21 foo = ModuleType.__new__(ModuleType) 21 foo = ModuleType.__new__(ModuleType)
22 self.assertTrue(foo.__dict__ is None) 22 self.assertIsNone(foo.__dict__)
23 self.assertRaises(SystemError, dir, foo) 23 self.assertRaises(SystemError, dir, foo)
24 try: 24 try:
25 s = foo.__name__ 25 s = foo.__name__
26 self.fail("__name__ = %s" % repr(s)) 26 self.fail("__name__ = %s" % repr(s))
27 except AttributeError: 27 except AttributeError:
28 pass 28 pass
29 self.assertEqual(foo.__doc__, ModuleType.__doc__) 29 self.assertEqual(foo.__doc__, ModuleType.__doc__)
30 30
31 def test_no_docstring(self): 31 def test_no_docstring(self):
32 # Regularly initialized module, no docstring 32 # Regularly initialized module, no docstring
(...skipping 22 matching lines...) Expand all
55 # Reinitialization should not replace the __dict__ 55 # Reinitialization should not replace the __dict__
56 foo = ModuleType("foo", "foodoc\u1234") 56 foo = ModuleType("foo", "foodoc\u1234")
57 foo.bar = 42 57 foo.bar = 42
58 d = foo.__dict__ 58 d = foo.__dict__
59 foo.__init__("foo", "foodoc") 59 foo.__init__("foo", "foodoc")
60 self.assertEqual(foo.__name__, "foo") 60 self.assertEqual(foo.__name__, "foo")
61 self.assertEqual(foo.__doc__, "foodoc") 61 self.assertEqual(foo.__doc__, "foodoc")
62 self.assertEqual(foo.bar, 42) 62 self.assertEqual(foo.bar, 42)
63 self.assertEqual(foo.__dict__, 63 self.assertEqual(foo.__dict__,
64 {"__name__": "foo", "__doc__": "foodoc", "bar": 42}) 64 {"__name__": "foo", "__doc__": "foodoc", "bar": 42})
65 self.assertTrue(foo.__dict__ is d) 65 self.assertIs(foo.__dict__, d)
66 66
67 @unittest.expectedFailure 67 @unittest.expectedFailure
68 def test_dont_clear_dict(self): 68 def test_dont_clear_dict(self):
69 # See issue 7140. 69 # See issue 7140.
70 def f(): 70 def f():
71 foo = ModuleType("foo") 71 foo = ModuleType("foo")
72 foo.bar = 4 72 foo.bar = 4
73 return foo 73 return foo
74 gc_collect() 74 gc_collect()
75 self.assertEqual(f().__dict__["bar"], 4) 75 self.assertEqual(f().__dict__["bar"], 4)
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 169
170 # frozen and namespace module reprs are tested in importlib. 170 # frozen and namespace module reprs are tested in importlib.
171 171
172 172
173 def test_main(): 173 def test_main():
174 run_unittest(ModuleTests) 174 run_unittest(ModuleTests)
175 175
176 176
177 if __name__ == '__main__': 177 if __name__ == '__main__':
178 test_main() 178 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_minidom.py ('k') | Lib/test/test_multiprocessing.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+