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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 6 months 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 import weakref 3 import weakref
4 from test.support import run_unittest, gc_collect 4 from test.support import run_unittest, gc_collect
5 from test.script_helper import assert_python_ok 5 from test.script_helper import assert_python_ok
6 6
7 import sys 7 import sys
8 ModuleType = type(sys) 8 ModuleType = type(sys)
9 9
10 class FullLoader: 10 class FullLoader:
11 @classmethod 11 @classmethod
12 def module_repr(cls, m): 12 def module_repr(cls, m):
13 return "<module '{}' (crafted)>".format(m.__name__) 13 return "<module '{}' (crafted)>".format(m.__name__)
14 14
15 class BareLoader: 15 class BareLoader:
16 pass 16 pass
17 17
18 18
19 class ModuleTests(unittest.TestCase): 19 class ModuleTests(unittest.TestCase):
20 def test_uninitialized(self): 20 def test_uninitialized(self):
21 # An uninitialized module has no __dict__ or __name__, 21 # An uninitialized module has no __dict__ or __name__,
22 # and __doc__ is None 22 # and __doc__ is None
23 foo = ModuleType.__new__(ModuleType) 23 foo = ModuleType.__new__(ModuleType)
24 self.assertTrue(foo.__dict__ is None) 24 self.assertIsNone(foo.__dict__)
25 self.assertRaises(SystemError, dir, foo) 25 self.assertRaises(SystemError, dir, foo)
26 try: 26 try:
27 s = foo.__name__ 27 s = foo.__name__
28 self.fail("__name__ = %s" % repr(s)) 28 self.fail("__name__ = %s" % repr(s))
29 except AttributeError: 29 except AttributeError:
30 pass 30 pass
31 self.assertEqual(foo.__doc__, ModuleType.__doc__) 31 self.assertEqual(foo.__doc__, ModuleType.__doc__)
32 32
33 def test_no_docstring(self): 33 def test_no_docstring(self):
34 # Regularly initialized module, no docstring 34 # Regularly initialized module, no docstring
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 foo = ModuleType("foo", "foodoc\u1234") 67 foo = ModuleType("foo", "foodoc\u1234")
68 foo.bar = 42 68 foo.bar = 42
69 d = foo.__dict__ 69 d = foo.__dict__
70 foo.__init__("foo", "foodoc") 70 foo.__init__("foo", "foodoc")
71 self.assertEqual(foo.__name__, "foo") 71 self.assertEqual(foo.__name__, "foo")
72 self.assertEqual(foo.__doc__, "foodoc") 72 self.assertEqual(foo.__doc__, "foodoc")
73 self.assertEqual(foo.bar, 42) 73 self.assertEqual(foo.bar, 42)
74 self.assertEqual(foo.__dict__, 74 self.assertEqual(foo.__dict__,
75 {"__name__": "foo", "__doc__": "foodoc", "bar": 42, 75 {"__name__": "foo", "__doc__": "foodoc", "bar": 42,
76 "__loader__": None, "__package__": None, "__spec__": None}) 76 "__loader__": None, "__package__": None, "__spec__": None})
77 self.assertTrue(foo.__dict__ is d) 77 self.assertIs(foo.__dict__, d)
78 78
79 def test_dont_clear_dict(self): 79 def test_dont_clear_dict(self):
80 # See issue 7140. 80 # See issue 7140.
81 def f(): 81 def f():
82 foo = ModuleType("foo") 82 foo = ModuleType("foo")
83 foo.bar = 4 83 foo.bar = 4
84 return foo 84 return foo
85 gc_collect() 85 gc_collect()
86 self.assertEqual(f().__dict__["bar"], 4) 86 self.assertEqual(f().__dict__["bar"], 4)
87 87
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 213
214 # frozen and namespace module reprs are tested in importlib. 214 # frozen and namespace module reprs are tested in importlib.
215 215
216 216
217 def test_main(): 217 def test_main():
218 run_unittest(ModuleTests) 218 run_unittest(ModuleTests)
219 219
220 220
221 if __name__ == '__main__': 221 if __name__ == '__main__':
222 test_main() 222 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+