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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 6 years, 5 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_bufio.py ('k') | Lib/test/test_bytes.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 # Python test set -- built-in functions 1 # Python test set -- built-in functions
2 2
3 import ast 3 import ast
4 import builtins 4 import builtins
5 import collections 5 import collections
6 import io 6 import io
7 import locale 7 import locale
8 import os 8 import os
9 import pickle 9 import pickle
10 import platform 10 import platform
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 self.assertRaises(ImportError, __import__, 'spamspam') 146 self.assertRaises(ImportError, __import__, 'spamspam')
147 self.assertRaises(TypeError, __import__, 1, 2, 3, 4) 147 self.assertRaises(TypeError, __import__, 1, 2, 3, 4)
148 self.assertRaises(ValueError, __import__, '') 148 self.assertRaises(ValueError, __import__, '')
149 self.assertRaises(TypeError, __import__, 'sys', name='sys') 149 self.assertRaises(TypeError, __import__, 'sys', name='sys')
150 150
151 def test_abs(self): 151 def test_abs(self):
152 # int 152 # int
153 self.assertEqual(abs(0), 0) 153 self.assertEqual(abs(0), 0)
154 self.assertEqual(abs(1234), 1234) 154 self.assertEqual(abs(1234), 1234)
155 self.assertEqual(abs(-1234), 1234) 155 self.assertEqual(abs(-1234), 1234)
156 self.assertTrue(abs(-sys.maxsize-1) > 0) 156 self.assertGreater(abs(-sys.maxsize-1), 0)
157 # float 157 # float
158 self.assertEqual(abs(0.0), 0.0) 158 self.assertEqual(abs(0.0), 0.0)
159 self.assertEqual(abs(3.14), 3.14) 159 self.assertEqual(abs(3.14), 3.14)
160 self.assertEqual(abs(-3.14), 3.14) 160 self.assertEqual(abs(-3.14), 3.14)
161 # str 161 # str
162 self.assertRaises(TypeError, abs, 'a') 162 self.assertRaises(TypeError, abs, 'a')
163 # bool 163 # bool
164 self.assertEqual(abs(True), 1) 164 self.assertEqual(abs(True), 1)
165 self.assertEqual(abs(False), 0) 165 self.assertEqual(abs(False), 0)
166 # other 166 # other
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 # Issue #9804: surrogates should be joined even for printable 229 # Issue #9804: surrogates should be joined even for printable
230 # wide characters (UCS-2 builds). 230 # wide characters (UCS-2 builds).
231 self.assertEqual(ascii('\U0001d121'), "'\\U0001d121'") 231 self.assertEqual(ascii('\U0001d121'), "'\\U0001d121'")
232 # All together 232 # All together
233 s = "'\0\"\n\r\t abcd\x85é\U00012fff\uD800\U0001D121xxx." 233 s = "'\0\"\n\r\t abcd\x85é\U00012fff\uD800\U0001D121xxx."
234 self.assertEqual(ascii(s), 234 self.assertEqual(ascii(s),
235 r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'""") 235 r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'""")
236 236
237 def test_neg(self): 237 def test_neg(self):
238 x = -sys.maxsize-1 238 x = -sys.maxsize-1
239 self.assertTrue(isinstance(x, int)) 239 self.assertIsInstance(x, int)
240 self.assertEqual(-x, sys.maxsize+1) 240 self.assertEqual(-x, sys.maxsize+1)
241 241
242 def test_callable(self): 242 def test_callable(self):
243 self.assertTrue(callable(len)) 243 self.assertTrue(callable(len))
244 self.assertFalse(callable("a")) 244 self.assertFalse(callable("a"))
245 self.assertTrue(callable(callable)) 245 self.assertTrue(callable(callable))
246 self.assertTrue(callable(lambda x, y: x + y)) 246 self.assertTrue(callable(lambda x, y: x + y))
247 self.assertFalse(callable(__builtins__)) 247 self.assertFalse(callable(__builtins__))
248 def f(): pass 248 def f(): pass
249 self.assertTrue(callable(f)) 249 self.assertTrue(callable(f))
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 self.assertEqual(chr(0x000FFFFF), "\U000FFFFF") 289 self.assertEqual(chr(0x000FFFFF), "\U000FFFFF")
290 self.assertEqual(chr(0x00100000), "\U00100000") 290 self.assertEqual(chr(0x00100000), "\U00100000")
291 self.assertEqual(chr(0x00100001), "\U00100001") 291 self.assertEqual(chr(0x00100001), "\U00100001")
292 self.assertEqual(chr(0x0010FFFE), "\U0010FFFE") 292 self.assertEqual(chr(0x0010FFFE), "\U0010FFFE")
293 self.assertEqual(chr(0x0010FFFF), "\U0010FFFF") 293 self.assertEqual(chr(0x0010FFFF), "\U0010FFFF")
294 self.assertRaises(ValueError, chr, -1) 294 self.assertRaises(ValueError, chr, -1)
295 self.assertRaises(ValueError, chr, 0x00110000) 295 self.assertRaises(ValueError, chr, 0x00110000)
296 self.assertRaises((OverflowError, ValueError), chr, 2**32) 296 self.assertRaises((OverflowError, ValueError), chr, 2**32)
297 297
298 def test_cmp(self): 298 def test_cmp(self):
299 self.assertTrue(not hasattr(builtins, "cmp")) 299 self.assertFalse(hasattr(builtins, "cmp"))
300 300
301 def test_compile(self): 301 def test_compile(self):
302 compile('print(1)\n', '', 'exec') 302 compile('print(1)\n', '', 'exec')
303 bom = b'\xef\xbb\xbf' 303 bom = b'\xef\xbb\xbf'
304 compile(bom + b'print(1)\n', '', 'exec') 304 compile(bom + b'print(1)\n', '', 'exec')
305 compile(source='pass', filename='?', mode='exec') 305 compile(source='pass', filename='?', mode='exec')
306 compile(dont_inherit=0, filename='tmp', source='0', mode='eval') 306 compile(dont_inherit=0, filename='tmp', source='0', mode='eval')
307 compile('pass', '?', dont_inherit=1, mode='exec') 307 compile('pass', '?', dont_inherit=1, mode='exec')
308 compile(memoryview(b"text"), "name", "exec") 308 compile(memoryview(b"text"), "name", "exec")
309 self.assertRaises(TypeError, compile) 309 self.assertRaises(TypeError, compile)
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 self.bar = "wow" 393 self.bar = "wow"
394 f = Foo() 394 f = Foo()
395 self.assertNotIn("__repr__", dir(f)) 395 self.assertNotIn("__repr__", dir(f))
396 self.assertIn("bar", dir(f)) 396 self.assertIn("bar", dir(f))
397 397
398 # dir(obj_using __dir__) 398 # dir(obj_using __dir__)
399 class Foo(object): 399 class Foo(object):
400 def __dir__(self): 400 def __dir__(self):
401 return ["kan", "ga", "roo"] 401 return ["kan", "ga", "roo"]
402 f = Foo() 402 f = Foo()
403 self.assertTrue(dir(f) == ["ga", "kan", "roo"]) 403 self.assertEqual(dir(f), ["ga", "kan", "roo"])
404 404
405 # dir(obj__dir__tuple) 405 # dir(obj__dir__tuple)
406 class Foo(object): 406 class Foo(object):
407 def __dir__(self): 407 def __dir__(self):
408 return ("b", "c", "a") 408 return ("b", "c", "a")
409 res = dir(Foo()) 409 res = dir(Foo())
410 self.assertIsInstance(res, list) 410 self.assertIsInstance(res, list)
411 self.assertTrue(res == ["a", "b", "c"]) 411 self.assertEqual(res, ["a", "b", "c"])
412 412
413 # dir(obj__dir__not_sequence) 413 # dir(obj__dir__not_sequence)
414 class Foo(object): 414 class Foo(object):
415 def __dir__(self): 415 def __dir__(self):
416 return 7 416 return 7
417 f = Foo() 417 f = Foo()
418 self.assertRaises(TypeError, dir, f) 418 self.assertRaises(TypeError, dir, f)
419 419
420 # dir(traceback) 420 # dir(traceback)
421 try: 421 try:
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
628 self.assertEqual(list(filter(None, (1, 2))), [1, 2]) 628 self.assertEqual(list(filter(None, (1, 2))), [1, 2])
629 self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4]) 629 self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4])
630 self.assertRaises(TypeError, list, filter(42, (1, 2))) 630 self.assertRaises(TypeError, list, filter(42, (1, 2)))
631 631
632 def test_filter_pickle(self): 632 def test_filter_pickle(self):
633 f1 = filter(filter_char, "abcdeabcde") 633 f1 = filter(filter_char, "abcdeabcde")
634 f2 = filter(filter_char, "abcdeabcde") 634 f2 = filter(filter_char, "abcdeabcde")
635 self.check_iter_pickle(f1, list(f2)) 635 self.check_iter_pickle(f1, list(f2))
636 636
637 def test_getattr(self): 637 def test_getattr(self):
638 self.assertTrue(getattr(sys, 'stdout') is sys.stdout) 638 self.assertIs(getattr(sys, 'stdout'), sys.stdout)
639 self.assertRaises(TypeError, getattr, sys, 1) 639 self.assertRaises(TypeError, getattr, sys, 1)
640 self.assertRaises(TypeError, getattr, sys, 1, "foo") 640 self.assertRaises(TypeError, getattr, sys, 1, "foo")
641 self.assertRaises(TypeError, getattr) 641 self.assertRaises(TypeError, getattr)
642 self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode)) 642 self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode))
643 # unicode surrogates are not encodable to the default encoding (utf8) 643 # unicode surrogates are not encodable to the default encoding (utf8)
644 self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E") 644 self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E")
645 645
646 def test_hasattr(self): 646 def test_hasattr(self):
647 self.assertTrue(hasattr(sys, 'stdout')) 647 self.assertTrue(hasattr(sys, 'stdout'))
648 self.assertRaises(TypeError, hasattr, sys, 1) 648 self.assertRaises(TypeError, hasattr, sys, 1)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 def test_isinstance(self): 709 def test_isinstance(self):
710 class C: 710 class C:
711 pass 711 pass
712 class D(C): 712 class D(C):
713 pass 713 pass
714 class E: 714 class E:
715 pass 715 pass
716 c = C() 716 c = C()
717 d = D() 717 d = D()
718 e = E() 718 e = E()
719 self.assertTrue(isinstance(c, C)) 719 self.assertIsInstance(c, C)
720 self.assertTrue(isinstance(d, C)) 720 self.assertIsInstance(d, C)
721 self.assertTrue(not isinstance(e, C)) 721 self.assertNotIsInstance(e, C)
722 self.assertTrue(not isinstance(c, D)) 722 self.assertNotIsInstance(c, D)
723 self.assertTrue(not isinstance('foo', E)) 723 self.assertNotIsInstance('foo', E)
724 self.assertRaises(TypeError, isinstance, E, 'foo') 724 self.assertRaises(TypeError, isinstance, E, 'foo')
725 self.assertRaises(TypeError, isinstance) 725 self.assertRaises(TypeError, isinstance)
726 726
727 def test_issubclass(self): 727 def test_issubclass(self):
728 class C: 728 class C:
729 pass 729 pass
730 class D(C): 730 class D(C):
731 pass 731 pass
732 class E: 732 class E:
733 pass 733 pass
734 c = C() 734 c = C()
735 d = D() 735 d = D()
736 e = E() 736 e = E()
737 self.assertTrue(issubclass(D, C)) 737 self.assertTrue(issubclass(D, C))
738 self.assertTrue(issubclass(C, C)) 738 self.assertTrue(issubclass(C, C))
739 self.assertTrue(not issubclass(C, D)) 739 self.assertFalse(issubclass(C, D))
740 self.assertRaises(TypeError, issubclass, 'foo', E) 740 self.assertRaises(TypeError, issubclass, 'foo', E)
741 self.assertRaises(TypeError, issubclass, E, 'foo') 741 self.assertRaises(TypeError, issubclass, E, 'foo')
742 self.assertRaises(TypeError, issubclass) 742 self.assertRaises(TypeError, issubclass)
743 743
744 def test_len(self): 744 def test_len(self):
745 self.assertEqual(len('123'), 3) 745 self.assertEqual(len('123'), 3)
746 self.assertEqual(len(()), 0) 746 self.assertEqual(len(()), 0)
747 self.assertEqual(len((1, 2, 3, 4)), 4) 747 self.assertEqual(len((1, 2, 3, 4)), 4)
748 self.assertEqual(len([1, 2, 3, 4]), 4) 748 self.assertEqual(len([1, 2, 3, 4]), 4)
749 self.assertEqual(len({}), 0) 749 self.assertEqual(len({}), 0)
(...skipping 823 matching lines...) Expand 10 before | Expand all | Expand 10 after
1573 counts = [None] * 5 1573 counts = [None] * 5
1574 for i in range(len(counts)): 1574 for i in range(len(counts)):
1575 run_unittest(*test_classes) 1575 run_unittest(*test_classes)
1576 gc.collect() 1576 gc.collect()
1577 counts[i] = sys.gettotalrefcount() 1577 counts[i] = sys.gettotalrefcount()
1578 print(counts) 1578 print(counts)
1579 1579
1580 1580
1581 if __name__ == "__main__": 1581 if __name__ == "__main__":
1582 test_main(verbose=True) 1582 test_main(verbose=True)
OLDNEW
« no previous file with comments | « Lib/test/test_bufio.py ('k') | Lib/test/test_bytes.py » ('j') | no next file with comments »

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