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

Side by Side Diff: Lib/test/test_builtin.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_buffer.py ('k') | Lib/test/test_bz2.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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 # Issue #9804: surrogates should be joined even for printable 231 # Issue #9804: surrogates should be joined even for printable
232 # wide characters (UCS-2 builds). 232 # wide characters (UCS-2 builds).
233 self.assertEqual(ascii('\U0001d121'), "'\\U0001d121'") 233 self.assertEqual(ascii('\U0001d121'), "'\\U0001d121'")
234 # All together 234 # All together
235 s = "'\0\"\n\r\t abcd\x85é\U00012fff\uD800\U0001D121xxx." 235 s = "'\0\"\n\r\t abcd\x85é\U00012fff\uD800\U0001D121xxx."
236 self.assertEqual(ascii(s), 236 self.assertEqual(ascii(s),
237 r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'""") 237 r"""'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud800\U0001d121xxx.'""")
238 238
239 def test_neg(self): 239 def test_neg(self):
240 x = -sys.maxsize-1 240 x = -sys.maxsize-1
241 self.assertTrue(isinstance(x, int)) 241 self.assertIsInstance(x, int)
242 self.assertEqual(-x, sys.maxsize+1) 242 self.assertEqual(-x, sys.maxsize+1)
243 243
244 def test_callable(self): 244 def test_callable(self):
245 self.assertTrue(callable(len)) 245 self.assertTrue(callable(len))
246 self.assertFalse(callable("a")) 246 self.assertFalse(callable("a"))
247 self.assertTrue(callable(callable)) 247 self.assertTrue(callable(callable))
248 self.assertTrue(callable(lambda x, y: x + y)) 248 self.assertTrue(callable(lambda x, y: x + y))
249 self.assertFalse(callable(__builtins__)) 249 self.assertFalse(callable(__builtins__))
250 def f(): pass 250 def f(): pass
251 self.assertTrue(callable(f)) 251 self.assertTrue(callable(f))
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 self.assertEqual(chr(0x000FFFFF), "\U000FFFFF") 291 self.assertEqual(chr(0x000FFFFF), "\U000FFFFF")
292 self.assertEqual(chr(0x00100000), "\U00100000") 292 self.assertEqual(chr(0x00100000), "\U00100000")
293 self.assertEqual(chr(0x00100001), "\U00100001") 293 self.assertEqual(chr(0x00100001), "\U00100001")
294 self.assertEqual(chr(0x0010FFFE), "\U0010FFFE") 294 self.assertEqual(chr(0x0010FFFE), "\U0010FFFE")
295 self.assertEqual(chr(0x0010FFFF), "\U0010FFFF") 295 self.assertEqual(chr(0x0010FFFF), "\U0010FFFF")
296 self.assertRaises(ValueError, chr, -1) 296 self.assertRaises(ValueError, chr, -1)
297 self.assertRaises(ValueError, chr, 0x00110000) 297 self.assertRaises(ValueError, chr, 0x00110000)
298 self.assertRaises((OverflowError, ValueError), chr, 2**32) 298 self.assertRaises((OverflowError, ValueError), chr, 2**32)
299 299
300 def test_cmp(self): 300 def test_cmp(self):
301 self.assertTrue(not hasattr(builtins, "cmp")) 301 self.assertFalse(hasattr(builtins, "cmp"))
302 302
303 def test_compile(self): 303 def test_compile(self):
304 compile('print(1)\n', '', 'exec') 304 compile('print(1)\n', '', 'exec')
305 bom = b'\xef\xbb\xbf' 305 bom = b'\xef\xbb\xbf'
306 compile(bom + b'print(1)\n', '', 'exec') 306 compile(bom + b'print(1)\n', '', 'exec')
307 compile(source='pass', filename='?', mode='exec') 307 compile(source='pass', filename='?', mode='exec')
308 compile(dont_inherit=0, filename='tmp', source='0', mode='eval') 308 compile(dont_inherit=0, filename='tmp', source='0', mode='eval')
309 compile('pass', '?', dont_inherit=1, mode='exec') 309 compile('pass', '?', dont_inherit=1, mode='exec')
310 compile(memoryview(b"text"), "name", "exec") 310 compile(memoryview(b"text"), "name", "exec")
311 self.assertRaises(TypeError, compile) 311 self.assertRaises(TypeError, compile)
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 self.bar = "wow" 395 self.bar = "wow"
396 f = Foo() 396 f = Foo()
397 self.assertNotIn("__repr__", dir(f)) 397 self.assertNotIn("__repr__", dir(f))
398 self.assertIn("bar", dir(f)) 398 self.assertIn("bar", dir(f))
399 399
400 # dir(obj_using __dir__) 400 # dir(obj_using __dir__)
401 class Foo(object): 401 class Foo(object):
402 def __dir__(self): 402 def __dir__(self):
403 return ["kan", "ga", "roo"] 403 return ["kan", "ga", "roo"]
404 f = Foo() 404 f = Foo()
405 self.assertTrue(dir(f) == ["ga", "kan", "roo"]) 405 self.assertEqual(dir(f), ["ga", "kan", "roo"])
406 406
407 # dir(obj__dir__tuple) 407 # dir(obj__dir__tuple)
408 class Foo(object): 408 class Foo(object):
409 def __dir__(self): 409 def __dir__(self):
410 return ("b", "c", "a") 410 return ("b", "c", "a")
411 res = dir(Foo()) 411 res = dir(Foo())
412 self.assertIsInstance(res, list) 412 self.assertIsInstance(res, list)
413 self.assertTrue(res == ["a", "b", "c"]) 413 self.assertEqual(res, ["a", "b", "c"])
414 414
415 # dir(obj__dir__not_sequence) 415 # dir(obj__dir__not_sequence)
416 class Foo(object): 416 class Foo(object):
417 def __dir__(self): 417 def __dir__(self):
418 return 7 418 return 7
419 f = Foo() 419 f = Foo()
420 self.assertRaises(TypeError, dir, f) 420 self.assertRaises(TypeError, dir, f)
421 421
422 # dir(traceback) 422 # dir(traceback)
423 try: 423 try:
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
633 self.assertEqual(list(filter(None, (1, 2))), [1, 2]) 633 self.assertEqual(list(filter(None, (1, 2))), [1, 2])
634 self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4]) 634 self.assertEqual(list(filter(lambda x: x>=3, (1, 2, 3, 4))), [3, 4])
635 self.assertRaises(TypeError, list, filter(42, (1, 2))) 635 self.assertRaises(TypeError, list, filter(42, (1, 2)))
636 636
637 def test_filter_pickle(self): 637 def test_filter_pickle(self):
638 f1 = filter(filter_char, "abcdeabcde") 638 f1 = filter(filter_char, "abcdeabcde")
639 f2 = filter(filter_char, "abcdeabcde") 639 f2 = filter(filter_char, "abcdeabcde")
640 self.check_iter_pickle(f1, list(f2)) 640 self.check_iter_pickle(f1, list(f2))
641 641
642 def test_getattr(self): 642 def test_getattr(self):
643 self.assertTrue(getattr(sys, 'stdout') is sys.stdout) 643 self.assertIs(getattr(sys, 'stdout'), sys.stdout)
644 self.assertRaises(TypeError, getattr, sys, 1) 644 self.assertRaises(TypeError, getattr, sys, 1)
645 self.assertRaises(TypeError, getattr, sys, 1, "foo") 645 self.assertRaises(TypeError, getattr, sys, 1, "foo")
646 self.assertRaises(TypeError, getattr) 646 self.assertRaises(TypeError, getattr)
647 self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode)) 647 self.assertRaises(AttributeError, getattr, sys, chr(sys.maxunicode))
648 # unicode surrogates are not encodable to the default encoding (utf8) 648 # unicode surrogates are not encodable to the default encoding (utf8)
649 self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E") 649 self.assertRaises(AttributeError, getattr, 1, "\uDAD1\uD51E")
650 650
651 def test_hasattr(self): 651 def test_hasattr(self):
652 self.assertTrue(hasattr(sys, 'stdout')) 652 self.assertTrue(hasattr(sys, 'stdout'))
653 self.assertRaises(TypeError, hasattr, sys, 1) 653 self.assertRaises(TypeError, hasattr, sys, 1)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 def test_isinstance(self): 714 def test_isinstance(self):
715 class C: 715 class C:
716 pass 716 pass
717 class D(C): 717 class D(C):
718 pass 718 pass
719 class E: 719 class E:
720 pass 720 pass
721 c = C() 721 c = C()
722 d = D() 722 d = D()
723 e = E() 723 e = E()
724 self.assertTrue(isinstance(c, C)) 724 self.assertIsInstance(c, C)
725 self.assertTrue(isinstance(d, C)) 725 self.assertIsInstance(d, C)
726 self.assertTrue(not isinstance(e, C)) 726 self.assertNotIsInstance(e, C)
727 self.assertTrue(not isinstance(c, D)) 727 self.assertNotIsInstance(c, D)
728 self.assertTrue(not isinstance('foo', E)) 728 self.assertNotIsInstance('foo', E)
729 self.assertRaises(TypeError, isinstance, E, 'foo') 729 self.assertRaises(TypeError, isinstance, E, 'foo')
730 self.assertRaises(TypeError, isinstance) 730 self.assertRaises(TypeError, isinstance)
731 731
732 def test_issubclass(self): 732 def test_issubclass(self):
733 class C: 733 class C:
734 pass 734 pass
735 class D(C): 735 class D(C):
736 pass 736 pass
737 class E: 737 class E:
738 pass 738 pass
739 c = C() 739 c = C()
740 d = D() 740 d = D()
741 e = E() 741 e = E()
742 self.assertTrue(issubclass(D, C)) 742 self.assertTrue(issubclass(D, C))
743 self.assertTrue(issubclass(C, C)) 743 self.assertTrue(issubclass(C, C))
744 self.assertTrue(not issubclass(C, D)) 744 self.assertFalse(issubclass(C, D))
745 self.assertRaises(TypeError, issubclass, 'foo', E) 745 self.assertRaises(TypeError, issubclass, 'foo', E)
746 self.assertRaises(TypeError, issubclass, E, 'foo') 746 self.assertRaises(TypeError, issubclass, E, 'foo')
747 self.assertRaises(TypeError, issubclass) 747 self.assertRaises(TypeError, issubclass)
748 748
749 def test_len(self): 749 def test_len(self):
750 self.assertEqual(len('123'), 3) 750 self.assertEqual(len('123'), 3)
751 self.assertEqual(len(()), 0) 751 self.assertEqual(len(()), 0)
752 self.assertEqual(len((1, 2, 3, 4)), 4) 752 self.assertEqual(len((1, 2, 3, 4)), 4)
753 self.assertEqual(len([1, 2, 3, 4]), 4) 753 self.assertEqual(len([1, 2, 3, 4]), 4)
754 self.assertEqual(len({}), 0) 754 self.assertEqual(len({}), 0)
(...skipping 837 matching lines...) Expand 10 before | Expand all | Expand 10 after
1592 data = 'The quick Brown fox Jumped over The lazy Dog'.split() 1592 data = 'The quick Brown fox Jumped over The lazy Dog'.split()
1593 self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0) 1593 self.assertRaises(TypeError, sorted, data, None, lambda x,y: 0)
1594 1594
1595 def load_tests(loader, tests, pattern): 1595 def load_tests(loader, tests, pattern):
1596 from doctest import DocTestSuite 1596 from doctest import DocTestSuite
1597 tests.addTest(DocTestSuite(builtins)) 1597 tests.addTest(DocTestSuite(builtins))
1598 return tests 1598 return tests
1599 1599
1600 if __name__ == "__main__": 1600 if __name__ == "__main__":
1601 unittest.main() 1601 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_buffer.py ('k') | Lib/test/test_bz2.py » ('j') | no next file with comments »

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