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

Delta Between Two Patch Sets: Lib/test/test_module.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 1 month ago
Right Patch Set: Created 5 years, 10 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_minidom.py ('k') | Lib/test/_test_multiprocessing.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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:
(...skipping 19 matching lines...) Expand all
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
35 foo = ModuleType("foo") 35 foo = ModuleType("foo")
36 self.assertEqual(foo.__name__, "foo") 36 self.assertEqual(foo.__name__, "foo")
37 self.assertEqual(foo.__doc__, None) 37 self.assertEqual(foo.__doc__, None)
38 self.assertIs(foo.__loader__, None) 38 self.assertIs(foo.__loader__, None)
39 self.assertIs(foo.__package__, None) 39 self.assertIs(foo.__package__, None)
40 self.assertIs(foo.__spec__, None)
40 self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None, 41 self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None,
41 "__loader__": None, "__package__": None} ) 42 "__loader__": None, "__package__": None,
43 "__spec__": None})
42 44
43 def test_ascii_docstring(self): 45 def test_ascii_docstring(self):
44 # ASCII docstring 46 # ASCII docstring
45 foo = ModuleType("foo", "foodoc") 47 foo = ModuleType("foo", "foodoc")
46 self.assertEqual(foo.__name__, "foo") 48 self.assertEqual(foo.__name__, "foo")
47 self.assertEqual(foo.__doc__, "foodoc") 49 self.assertEqual(foo.__doc__, "foodoc")
48 self.assertEqual(foo.__dict__, 50 self.assertEqual(foo.__dict__,
49 {"__name__": "foo", "__doc__": "foodoc", 51 {"__name__": "foo", "__doc__": "foodoc",
50 "__loader__": None, "__package__": None}) 52 "__loader__": None, "__package__": None,
53 "__spec__": None})
51 54
52 def test_unicode_docstring(self): 55 def test_unicode_docstring(self):
53 # Unicode docstring 56 # Unicode docstring
54 foo = ModuleType("foo", "foodoc\u1234") 57 foo = ModuleType("foo", "foodoc\u1234")
55 self.assertEqual(foo.__name__, "foo") 58 self.assertEqual(foo.__name__, "foo")
56 self.assertEqual(foo.__doc__, "foodoc\u1234") 59 self.assertEqual(foo.__doc__, "foodoc\u1234")
57 self.assertEqual(foo.__dict__, 60 self.assertEqual(foo.__dict__,
58 {"__name__": "foo", "__doc__": "foodoc\u1234", 61 {"__name__": "foo", "__doc__": "foodoc\u1234",
59 "__loader__": None, "__package__": None}) 62 "__loader__": None, "__package__": None,
63 "__spec__": None})
60 64
61 def test_reinit(self): 65 def test_reinit(self):
62 # Reinitialization should not replace the __dict__ 66 # Reinitialization should not replace the __dict__
63 foo = ModuleType("foo", "foodoc\u1234") 67 foo = ModuleType("foo", "foodoc\u1234")
64 foo.bar = 42 68 foo.bar = 42
65 d = foo.__dict__ 69 d = foo.__dict__
66 foo.__init__("foo", "foodoc") 70 foo.__init__("foo", "foodoc")
67 self.assertEqual(foo.__name__, "foo") 71 self.assertEqual(foo.__name__, "foo")
68 self.assertEqual(foo.__doc__, "foodoc") 72 self.assertEqual(foo.__doc__, "foodoc")
69 self.assertEqual(foo.bar, 42) 73 self.assertEqual(foo.bar, 42)
70 self.assertEqual(foo.__dict__, 74 self.assertEqual(foo.__dict__,
71 {"__name__": "foo", "__doc__": "foodoc", "bar": 42, 75 {"__name__": "foo", "__doc__": "foodoc", "bar": 42,
72 "__loader__": None, "__package__": None}) 76 "__loader__": None, "__package__": None, "__spec__": None})
73 self.assertIs(foo.__dict__, d) 77 self.assertIs(foo.__dict__, d)
74 78
75 def test_dont_clear_dict(self): 79 def test_dont_clear_dict(self):
76 # See issue 7140. 80 # See issue 7140.
77 def f(): 81 def f():
78 foo = ModuleType("foo") 82 foo = ModuleType("foo")
79 foo.bar = 4 83 foo.bar = 4
80 return foo 84 return foo
81 gc_collect() 85 gc_collect()
82 self.assertEqual(f().__dict__["bar"], 4) 86 self.assertEqual(f().__dict__["bar"], 4)
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 # Yes, a class not an instance. 184 # Yes, a class not an instance.
181 m.__loader__ = FullLoader 185 m.__loader__ = FullLoader
182 m.__file__ = '/tmp/foo.py' 186 m.__file__ = '/tmp/foo.py'
183 self.assertEqual(repr(m), "<module 'foo' (crafted)>") 187 self.assertEqual(repr(m), "<module 'foo' (crafted)>")
184 188
185 def test_module_repr_builtin(self): 189 def test_module_repr_builtin(self):
186 self.assertEqual(repr(sys), "<module 'sys' (built-in)>") 190 self.assertEqual(repr(sys), "<module 'sys' (built-in)>")
187 191
188 def test_module_repr_source(self): 192 def test_module_repr_source(self):
189 r = repr(unittest) 193 r = repr(unittest)
190 self.assertEqual(r[:25], "<module 'unittest' from '") 194 starts_with = "<module 'unittest' from '"
191 self.assertEqual(r[-13:], "__init__.py'>") 195 ends_with = "__init__.py'>"
196 self.assertEqual(r[:len(starts_with)], starts_with,
197 '{!r} does not start with {!r}'.format(r, starts_with))
198 self.assertEqual(r[-len(ends_with):], ends_with,
199 '{!r} does not end with {!r}'.format(r, ends_with))
192 200
193 def test_module_finalization_at_shutdown(self): 201 def test_module_finalization_at_shutdown(self):
194 # Module globals and builtins should still be available during shutdown 202 # Module globals and builtins should still be available during shutdown
195 rc, out, err = assert_python_ok("-c", "from test import final_a") 203 rc, out, err = assert_python_ok("-c", "from test import final_a")
196 self.assertFalse(err) 204 self.assertFalse(err)
197 lines = out.splitlines() 205 lines = out.splitlines()
198 self.assertEqual(set(lines), { 206 self.assertEqual(set(lines), {
199 b"x = a", 207 b"x = a",
200 b"x = b", 208 b"x = b",
201 b"final_a.x = a", 209 b"final_a.x = a",
202 b"final_b.x = b", 210 b"final_b.x = b",
203 b"len = len", 211 b"len = len",
204 b"shutil.rmtree = rmtree"}) 212 b"shutil.rmtree = rmtree"})
205 213
206 # frozen and namespace module reprs are tested in importlib. 214 # frozen and namespace module reprs are tested in importlib.
207 215
208 216
209 def test_main(): 217 def test_main():
210 run_unittest(ModuleTests) 218 run_unittest(ModuleTests)
211 219
212 220
213 if __name__ == '__main__': 221 if __name__ == '__main__':
214 test_main() 222 test_main()
LEFTRIGHT

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