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

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

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 9 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_funcattrs.py ('k') | Lib/test/test_gc.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 import abc 1 import abc
2 import collections 2 import collections
3 from itertools import permutations 3 from itertools import permutations
4 import pickle 4 import pickle
5 from random import choice 5 from random import choice
6 import sys 6 import sys
7 from test import support 7 from test import support
8 import unittest 8 import unittest
9 from weakref import proxy 9 from weakref import proxy
10 10
(...skipping 24 matching lines...) Expand all
35 ((1, 2, 3, 4), dict(a=10, b=30, c=40))) 35 ((1, 2, 3, 4), dict(a=10, b=30, c=40)))
36 p = self.partial(map, lambda x: x*10) 36 p = self.partial(map, lambda x: x*10)
37 self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40]) 37 self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40])
38 38
39 def test_attributes(self): 39 def test_attributes(self):
40 p = self.partial(capture, 1, 2, a=10, b=20) 40 p = self.partial(capture, 1, 2, a=10, b=20)
41 # attributes should be readable 41 # attributes should be readable
42 self.assertEqual(p.func, capture) 42 self.assertEqual(p.func, capture)
43 self.assertEqual(p.args, (1, 2)) 43 self.assertEqual(p.args, (1, 2))
44 self.assertEqual(p.keywords, dict(a=10, b=20)) 44 self.assertEqual(p.keywords, dict(a=10, b=20))
45 # attributes should not be writable
46 if not isinstance(self.partial, type):
47 return
48 self.assertRaises(AttributeError, setattr, p, 'func', map)
49 self.assertRaises(AttributeError, setattr, p, 'args', (1, 2))
50 self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2) )
51
52 p = self.partial(hex)
53 try:
54 del p.__dict__
55 except TypeError:
56 pass
57 else:
58 self.fail('partial object allowed __dict__ to be deleted')
59 45
60 def test_argument_checking(self): 46 def test_argument_checking(self):
61 self.assertRaises(TypeError, self.partial) # need at least a func ar g 47 self.assertRaises(TypeError, self.partial) # need at least a func ar g
62 try: 48 try:
63 self.partial(2)() 49 self.partial(2)()
64 except TypeError: 50 except TypeError:
65 pass 51 pass
66 else: 52 else:
67 self.fail('First arg not checked for callability') 53 self.fail('First arg not checked for callability')
68 54
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 self.assertEqual(join(data), '0123456789') 134 self.assertEqual(join(data), '0123456789')
149 join = self.partial(''.join) 135 join = self.partial(''.join)
150 self.assertEqual(join(data), '0123456789') 136 self.assertEqual(join(data), '0123456789')
151 137
152 138
153 @unittest.skipUnless(c_functools, 'requires the C _functools module') 139 @unittest.skipUnless(c_functools, 'requires the C _functools module')
154 class TestPartialC(TestPartial, unittest.TestCase): 140 class TestPartialC(TestPartial, unittest.TestCase):
155 if c_functools: 141 if c_functools:
156 partial = c_functools.partial 142 partial = c_functools.partial
157 143
144 def test_attributes_unwritable(self):
145 # attributes should not be writable
146 p = self.partial(capture, 1, 2, a=10, b=20)
147 self.assertRaises(AttributeError, setattr, p, 'func', map)
148 self.assertRaises(AttributeError, setattr, p, 'args', (1, 2))
149 self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2) )
150
151 p = self.partial(hex)
152 try:
153 del p.__dict__
154 except TypeError:
155 pass
156 else:
157 self.fail('partial object allowed __dict__ to be deleted')
158
158 def test_repr(self): 159 def test_repr(self):
159 args = (object(), object()) 160 args = (object(), object())
160 args_repr = ', '.join(repr(a) for a in args) 161 args_repr = ', '.join(repr(a) for a in args)
161 kwargs = {'a': object(), 'b': object()} 162 #kwargs = {'a': object(), 'b': object()}
163 kwargs = {'a': object()}
162 kwargs_repr = ', '.join("%s=%r" % (k, v) for k, v in kwargs.items()) 164 kwargs_repr = ', '.join("%s=%r" % (k, v) for k, v in kwargs.items())
163 if self.partial is c_functools.partial: 165 if self.partial is c_functools.partial:
164 name = 'functools.partial' 166 name = 'functools.partial'
165 else: 167 else:
166 name = self.partial.__name__ 168 name = self.partial.__name__
167 169
168 f = self.partial(capture) 170 f = self.partial(capture)
169 self.assertEqual('{}({!r})'.format(name, capture), 171 self.assertEqual('{}({!r})'.format(name, capture),
170 repr(f)) 172 repr(f))
171 173
(...skipping 949 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 self.assertIs(g.dispatch(object), g.dispatch(str)) 1123 self.assertIs(g.dispatch(object), g.dispatch(str))
1122 # Note: in the assert above this is not g. 1124 # Note: in the assert above this is not g.
1123 # @singledispatch returns the wrapper. 1125 # @singledispatch returns the wrapper.
1124 1126
1125 def test_wrapping_attributes(self): 1127 def test_wrapping_attributes(self):
1126 @functools.singledispatch 1128 @functools.singledispatch
1127 def g(obj): 1129 def g(obj):
1128 "Simple test" 1130 "Simple test"
1129 return "Test" 1131 return "Test"
1130 self.assertEqual(g.__name__, "g") 1132 self.assertEqual(g.__name__, "g")
1131 self.assertEqual(g.__doc__, "Simple test") 1133 if sys.flags.optimize < 2:
1134 self.assertEqual(g.__doc__, "Simple test")
1132 1135
1133 @unittest.skipUnless(decimal, 'requires _decimal') 1136 @unittest.skipUnless(decimal, 'requires _decimal')
1134 @support.cpython_only 1137 @support.cpython_only
1135 def test_c_classes(self): 1138 def test_c_classes(self):
1136 @functools.singledispatch 1139 @functools.singledispatch
1137 def g(obj): 1140 def g(obj):
1138 return "base" 1141 return "base"
1139 @g.register(decimal.DecimalException) 1142 @g.register(decimal.DecimalException)
1140 def _(obj): 1143 def _(obj):
1141 return obj.args 1144 return obj.args
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
1569 import gc 1572 import gc
1570 counts = [None] * 5 1573 counts = [None] * 5
1571 for i in range(len(counts)): 1574 for i in range(len(counts)):
1572 support.run_unittest(*test_classes) 1575 support.run_unittest(*test_classes)
1573 gc.collect() 1576 gc.collect()
1574 counts[i] = sys.gettotalrefcount() 1577 counts[i] = sys.gettotalrefcount()
1575 print(counts) 1578 print(counts)
1576 1579
1577 if __name__ == '__main__': 1580 if __name__ == '__main__':
1578 test_main(verbose=True) 1581 test_main(verbose=True)
LEFTRIGHT

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