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

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

Issue 16510: Using appropriate checks in tests
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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_httpservers.py ('k') | Lib/test/test_io.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 import re 1 import re
2 import sys 2 import sys
3 import types 3 import types
4 import unittest 4 import unittest
5 import inspect 5 import inspect
6 import linecache 6 import linecache
7 import datetime 7 import datetime
8 import collections 8 import collections
9 import os 9 import os
10 import shutil 10 import shutil
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 def __init__(self, *args, **kwargs): 159 def __init__(self, *args, **kwargs):
160 unittest.TestCase.__init__(self, *args, **kwargs) 160 unittest.TestCase.__init__(self, *args, **kwargs)
161 161
162 git.abuse(7, 8, 9) 162 git.abuse(7, 8, 9)
163 163
164 def test_abuse_done(self): 164 def test_abuse_done(self):
165 self.istest(inspect.istraceback, 'git.ex[2]') 165 self.istest(inspect.istraceback, 'git.ex[2]')
166 self.istest(inspect.isframe, 'mod.fr') 166 self.istest(inspect.isframe, 'mod.fr')
167 167
168 def test_stack(self): 168 def test_stack(self):
169 self.assertTrue(len(mod.st) >= 5) 169 self.assertGreaterEqual(len(mod.st), 5)
170 self.assertEqual(revise(*mod.st[0][1:]), 170 self.assertEqual(revise(*mod.st[0][1:]),
171 (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0)) 171 (modfile, 16, 'eggs', [' st = inspect.stack()\n'], 0))
172 self.assertEqual(revise(*mod.st[1][1:]), 172 self.assertEqual(revise(*mod.st[1][1:]),
173 (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0)) 173 (modfile, 9, 'spam', [' eggs(b + d, c + f)\n'], 0))
174 self.assertEqual(revise(*mod.st[2][1:]), 174 self.assertEqual(revise(*mod.st[2][1:]),
175 (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0)) 175 (modfile, 43, 'argue', [' spam(a, b, c)\n'], 0))
176 self.assertEqual(revise(*mod.st[3][1:]), 176 self.assertEqual(revise(*mod.st[3][1:]),
177 (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0)) 177 (modfile, 39, 'abuse', [' self.argue(a, b, c)\n'], 0))
178 178
179 def test_trace(self): 179 def test_trace(self):
(...skipping 1894 matching lines...) Expand 10 before | Expand all | Expand 10 after
2074 test.__signature__ = sig.replace(parameters=new_params) 2074 test.__signature__ = sig.replace(parameters=new_params)
2075 self.assertEqual(str(inspect.signature(test)), 2075 self.assertEqual(str(inspect.signature(test)),
2076 '(<0>, *, b, **kwargs)') 2076 '(<0>, *, b, **kwargs)')
2077 2077
2078 def test_signature_replace_anno(self): 2078 def test_signature_replace_anno(self):
2079 def test() -> 42: 2079 def test() -> 42:
2080 pass 2080 pass
2081 2081
2082 sig = inspect.signature(test) 2082 sig = inspect.signature(test)
2083 sig = sig.replace(return_annotation=None) 2083 sig = sig.replace(return_annotation=None)
2084 self.assertIs(sig.return_annotation, None) 2084 self.assertIsNone(sig.return_annotation)
2085 sig = sig.replace(return_annotation=sig.empty) 2085 sig = sig.replace(return_annotation=sig.empty)
2086 self.assertIs(sig.return_annotation, sig.empty) 2086 self.assertIs(sig.return_annotation, sig.empty)
2087 sig = sig.replace(return_annotation=42) 2087 sig = sig.replace(return_annotation=42)
2088 self.assertEqual(sig.return_annotation, 42) 2088 self.assertEqual(sig.return_annotation, 42)
2089 self.assertEqual(sig, inspect.signature(test)) 2089 self.assertEqual(sig, inspect.signature(test))
2090 2090
2091 2091
2092 class TestParameterObject(unittest.TestCase): 2092 class TestParameterObject(unittest.TestCase):
2093 def test_signature_parameter_kinds(self): 2093 def test_signature_parameter_kinds(self):
2094 P = inspect.Parameter 2094 P = inspect.Parameter
2095 self.assertTrue(P.POSITIONAL_ONLY < P.POSITIONAL_OR_KEYWORD < \ 2095 self.assertTrue(P.POSITIONAL_ONLY < P.POSITIONAL_OR_KEYWORD < \
2096 P.VAR_POSITIONAL < P.KEYWORD_ONLY < P.VAR_KEYWORD) 2096 P.VAR_POSITIONAL < P.KEYWORD_ONLY < P.VAR_KEYWORD)
2097 2097
2098 self.assertEqual(str(P.POSITIONAL_ONLY), 'POSITIONAL_ONLY') 2098 self.assertEqual(str(P.POSITIONAL_ONLY), 'POSITIONAL_ONLY')
2099 self.assertTrue('POSITIONAL_ONLY' in repr(P.POSITIONAL_ONLY)) 2099 self.assertIn('POSITIONAL_ONLY', repr(P.POSITIONAL_ONLY))
2100 2100
2101 def test_signature_parameter_object(self): 2101 def test_signature_parameter_object(self):
2102 p = inspect.Parameter('foo', default=10, 2102 p = inspect.Parameter('foo', default=10,
2103 kind=inspect.Parameter.POSITIONAL_ONLY) 2103 kind=inspect.Parameter.POSITIONAL_ONLY)
2104 self.assertEqual(p.name, 'foo') 2104 self.assertEqual(p.name, 'foo')
2105 self.assertEqual(p.default, 10) 2105 self.assertEqual(p.default, 10)
2106 self.assertIs(p.annotation, p.empty) 2106 self.assertIs(p.annotation, p.empty)
2107 self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY) 2107 self.assertEqual(p.kind, inspect.Parameter.POSITIONAL_ONLY)
2108 2108
2109 with self.assertRaisesRegex(ValueError, 'invalid value'): 2109 with self.assertRaisesRegex(ValueError, 'invalid value'):
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
2161 self.assertEqual(p, p2) 2161 self.assertEqual(p, p2)
2162 2162
2163 p2 = p2.replace(name='bar') 2163 p2 = p2.replace(name='bar')
2164 self.assertEqual(p2.name, 'bar') 2164 self.assertEqual(p2.name, 'bar')
2165 self.assertNotEqual(p2, p) 2165 self.assertNotEqual(p2, p)
2166 2166
2167 with self.assertRaisesRegex(ValueError, 'not a valid parameter name'): 2167 with self.assertRaisesRegex(ValueError, 'not a valid parameter name'):
2168 p2 = p2.replace(name=p2.empty) 2168 p2 = p2.replace(name=p2.empty)
2169 2169
2170 p2 = p2.replace(name='foo', default=None) 2170 p2 = p2.replace(name='foo', default=None)
2171 self.assertIs(p2.default, None) 2171 self.assertIsNone(p2.default)
2172 self.assertNotEqual(p2, p) 2172 self.assertNotEqual(p2, p)
2173 2173
2174 p2 = p2.replace(name='foo', default=p2.empty) 2174 p2 = p2.replace(name='foo', default=p2.empty)
2175 self.assertIs(p2.default, p2.empty) 2175 self.assertIs(p2.default, p2.empty)
2176 2176
2177 2177
2178 p2 = p2.replace(default=42, kind=p2.POSITIONAL_OR_KEYWORD) 2178 p2 = p2.replace(default=42, kind=p2.POSITIONAL_OR_KEYWORD)
2179 self.assertEqual(p2.kind, p2.POSITIONAL_OR_KEYWORD) 2179 self.assertEqual(p2.kind, p2.POSITIONAL_OR_KEYWORD)
2180 self.assertNotEqual(p2, p) 2180 self.assertNotEqual(p2, p)
2181 2181
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
2536 TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases, 2536 TestDecorators, TestRetrievingSourceCode, TestOneliners, TestBuggyCases,
2537 TestInterpreterStack, TestClassesAndFunctions, TestPredicates, 2537 TestInterpreterStack, TestClassesAndFunctions, TestPredicates,
2538 TestGetcallargsFunctions, TestGetcallargsMethods, 2538 TestGetcallargsFunctions, TestGetcallargsMethods,
2539 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState, 2539 TestGetcallargsUnboundMethods, TestGetattrStatic, TestGetGeneratorState,
2540 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject, 2540 TestNoEOL, TestSignatureObject, TestSignatureBind, TestParameterObject,
2541 TestBoundArguments, TestGetClosureVars, TestUnwrap, TestMain 2541 TestBoundArguments, TestGetClosureVars, TestUnwrap, TestMain
2542 ) 2542 )
2543 2543
2544 if __name__ == "__main__": 2544 if __name__ == "__main__":
2545 test_main() 2545 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_httpservers.py ('k') | Lib/test/test_io.py » ('j') | no next file with comments »

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