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

Side by Side Diff: Lib/test/test_functools.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_funcattrs.py ('k') | Lib/test/test_gc.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 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 self.assertEqual(p(b=2), ((), {'a':1, 'b':2})) 84 self.assertEqual(p(b=2), ((), {'a':1, 'b':2}))
85 # keyword args in the call override those in the partial object 85 # keyword args in the call override those in the partial object
86 self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2})) 86 self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2}))
87 87
88 def test_positional(self): 88 def test_positional(self):
89 # make sure positional arguments are captured correctly 89 # make sure positional arguments are captured correctly
90 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]: 90 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:
91 p = self.partial(capture, *args) 91 p = self.partial(capture, *args)
92 expected = args + ('x',) 92 expected = args + ('x',)
93 got, empty = p('x') 93 got, empty = p('x')
94 self.assertTrue(expected == got and empty == {}) 94 self.assertEqual(got, expected)
95 self.assertEqual(empty, {})
95 96
96 def test_keyword(self): 97 def test_keyword(self):
97 # make sure keyword arguments are captured correctly 98 # make sure keyword arguments are captured correctly
98 for a in ['a', 0, None, 3.5]: 99 for a in ['a', 0, None, 3.5]:
99 p = self.partial(capture, a=a) 100 p = self.partial(capture, a=a)
100 expected = {'a':a,'x':None} 101 expected = {'a':a,'x':None}
101 empty, got = p(x=None) 102 empty, got = p(x=None)
102 self.assertTrue(expected == got and empty == ()) 103 self.assertEqual(got, expected)
104 self.assertEqual(empty, ())
103 105
104 def test_no_side_effects(self): 106 def test_no_side_effects(self):
105 # make sure there are no side effects that affect subsequent calls 107 # make sure there are no side effects that affect subsequent calls
106 p = self.partial(capture, 0, a=1) 108 p = self.partial(capture, 0, a=1)
107 args1, kw1 = p(1, b=2) 109 args1, kw1 = p(1, b=2)
108 self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2}) 110 self.assertEqual(args1, (0,1))
111 self.assertEqual(kw1, {'a':1,'b':2})
109 args2, kw2 = p() 112 args2, kw2 = p()
110 self.assertTrue(args2 == (0,) and kw2 == {'a':1}) 113 self.assertEqual(args2, (0,))
114 self.assertEqual(kw2, {'a':1})
111 115
112 def test_error_propagation(self): 116 def test_error_propagation(self):
113 def f(x, y): 117 def f(x, y):
114 x / y 118 x / y
115 self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0)) 119 self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0))
116 self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0) 120 self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0)
117 self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0) 121 self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0)
118 self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1) 122 self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1)
119 123
120 def test_weakref(self): 124 def test_weakref(self):
(...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 self.assertEqual(hits, 0) 895 self.assertEqual(hits, 0)
892 self.assertEqual(misses, 0) 896 self.assertEqual(misses, 0)
893 897
894 domain = range(5) 898 domain = range(5)
895 for i in range(1000): 899 for i in range(1000):
896 x, y = choice(domain), choice(domain) 900 x, y = choice(domain), choice(domain)
897 actual = f(x, y) 901 actual = f(x, y)
898 expected = orig(x, y) 902 expected = orig(x, y)
899 self.assertEqual(actual, expected) 903 self.assertEqual(actual, expected)
900 hits, misses, maxsize, currsize = f.cache_info() 904 hits, misses, maxsize, currsize = f.cache_info()
901 self.assertTrue(hits > misses) 905 self.assertGreater(hits, misses)
902 self.assertEqual(hits + misses, 1000) 906 self.assertEqual(hits + misses, 1000)
903 self.assertEqual(currsize, 20) 907 self.assertEqual(currsize, 20)
904 908
905 f.cache_clear() # test clearing 909 f.cache_clear() # test clearing
906 hits, misses, maxsize, currsize = f.cache_info() 910 hits, misses, maxsize, currsize = f.cache_info()
907 self.assertEqual(hits, 0) 911 self.assertEqual(hits, 0)
908 self.assertEqual(misses, 0) 912 self.assertEqual(misses, 0)
909 self.assertEqual(currsize, 0) 913 self.assertEqual(currsize, 0)
910 f(x, y) 914 f(x, y)
911 hits, misses, maxsize, currsize = f.cache_info() 915 hits, misses, maxsize, currsize = f.cache_info()
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after
1568 import gc 1572 import gc
1569 counts = [None] * 5 1573 counts = [None] * 5
1570 for i in range(len(counts)): 1574 for i in range(len(counts)):
1571 support.run_unittest(*test_classes) 1575 support.run_unittest(*test_classes)
1572 gc.collect() 1576 gc.collect()
1573 counts[i] = sys.gettotalrefcount() 1577 counts[i] = sys.gettotalrefcount()
1574 print(counts) 1578 print(counts)
1575 1579
1576 if __name__ == '__main__': 1580 if __name__ == '__main__':
1577 test_main(verbose=True) 1581 test_main(verbose=True)
OLDNEW
« no previous file with comments | « Lib/test/test_funcattrs.py ('k') | Lib/test/test_gc.py » ('j') | no next file with comments »

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