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

Side by Side Diff: Lib/test/test_types.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_trace.py ('k') | Lib/test/test_unicode_file.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 -- part 6, built-in types 1 # Python test set -- part 6, built-in types
2 2
3 from test.support import run_unittest, run_with_locale 3 from test.support import run_unittest, run_with_locale
4 import collections 4 import collections
5 import pickle 5 import pickle
6 import locale 6 import locale
7 import sys 7 import sys
8 import types 8 import types
9 import unittest 9 import unittest
10 10
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 class dictmissing(dict): 617 class dictmissing(dict):
618 def __missing__(self, key): 618 def __missing__(self, key):
619 return "missing=%s" % key 619 return "missing=%s" % key
620 620
621 view = self.mappingproxy(dictmissing(x=1)) 621 view = self.mappingproxy(dictmissing(x=1))
622 self.assertEqual(view['x'], 1) 622 self.assertEqual(view['x'], 1)
623 self.assertEqual(view['y'], 'missing=y') 623 self.assertEqual(view['y'], 'missing=y')
624 self.assertEqual(view.get('x'), 1) 624 self.assertEqual(view.get('x'), 1)
625 self.assertEqual(view.get('y'), None) 625 self.assertEqual(view.get('y'), None)
626 self.assertEqual(view.get('y', 42), 42) 626 self.assertEqual(view.get('y', 42), 42)
627 self.assertTrue('x' in view) 627 self.assertIn('x', view)
628 self.assertFalse('y' in view) 628 self.assertNotIn('y', view)
629 629
630 def test_customdict(self): 630 def test_customdict(self):
631 class customdict(dict): 631 class customdict(dict):
632 def __contains__(self, key): 632 def __contains__(self, key):
633 if key == 'magic': 633 if key == 'magic':
634 return True 634 return True
635 else: 635 else:
636 return dict.__contains__(self, key) 636 return dict.__contains__(self, key)
637 637
638 def __iter__(self): 638 def __iter__(self):
(...skipping 15 matching lines...) Expand all
654 return 'values' 654 return 'values'
655 655
656 def __getitem__(self, key): 656 def __getitem__(self, key):
657 return "getitem=%s" % dict.__getitem__(self, key) 657 return "getitem=%s" % dict.__getitem__(self, key)
658 658
659 def get(self, key, default=None): 659 def get(self, key, default=None):
660 return "get=%s" % dict.get(self, key, 'default=%r' % default) 660 return "get=%s" % dict.get(self, key, 'default=%r' % default)
661 661
662 custom = customdict({'key': 'value'}) 662 custom = customdict({'key': 'value'})
663 view = self.mappingproxy(custom) 663 view = self.mappingproxy(custom)
664 self.assertTrue('key' in view) 664 self.assertIn('key', view)
665 self.assertTrue('magic' in view) 665 self.assertIn('magic', view)
666 self.assertFalse('xxx' in view) 666 self.assertNotIn('xxx', view)
667 self.assertEqual(view['key'], 'getitem=value') 667 self.assertEqual(view['key'], 'getitem=value')
668 self.assertRaises(KeyError, view.__getitem__, 'xxx') 668 self.assertRaises(KeyError, view.__getitem__, 'xxx')
669 self.assertEqual(tuple(view), ('iter',)) 669 self.assertEqual(tuple(view), ('iter',))
670 self.assertEqual(len(view), 500) 670 self.assertEqual(len(view), 500)
671 self.assertEqual(view.copy(), 'copy') 671 self.assertEqual(view.copy(), 'copy')
672 self.assertEqual(view.get('key'), 'get=value') 672 self.assertEqual(view.get('key'), 'get=value')
673 self.assertEqual(view.get('xxx'), 'get=default=None') 673 self.assertEqual(view.get('xxx'), 'get=default=None')
674 self.assertEqual(view.items(), 'items') 674 self.assertEqual(view.items(), 'items')
675 self.assertEqual(view.keys(), 'keys') 675 self.assertEqual(view.keys(), 'keys')
676 self.assertEqual(view.values(), 'values') 676 self.assertEqual(view.values(), 'values')
677 677
678 def test_chainmap(self): 678 def test_chainmap(self):
679 d1 = {'x': 1} 679 d1 = {'x': 1}
680 d2 = {'y': 2} 680 d2 = {'y': 2}
681 mapping = collections.ChainMap(d1, d2) 681 mapping = collections.ChainMap(d1, d2)
682 view = self.mappingproxy(mapping) 682 view = self.mappingproxy(mapping)
683 self.assertTrue('x' in view) 683 self.assertIn('x', view)
684 self.assertTrue('y' in view) 684 self.assertIn('y', view)
685 self.assertFalse('z' in view) 685 self.assertNotIn('z', view)
686 self.assertEqual(view['x'], 1) 686 self.assertEqual(view['x'], 1)
687 self.assertEqual(view['y'], 2) 687 self.assertEqual(view['y'], 2)
688 self.assertRaises(KeyError, view.__getitem__, 'z') 688 self.assertRaises(KeyError, view.__getitem__, 'z')
689 self.assertEqual(tuple(sorted(view)), ('x', 'y')) 689 self.assertEqual(tuple(sorted(view)), ('x', 'y'))
690 self.assertEqual(len(view), 2) 690 self.assertEqual(len(view), 2)
691 copy = view.copy() 691 copy = view.copy()
692 self.assertIsNot(copy, mapping) 692 self.assertIsNot(copy, mapping)
693 self.assertIsInstance(copy, collections.ChainMap) 693 self.assertIsInstance(copy, collections.ChainMap)
694 self.assertEqual(copy, mapping) 694 self.assertEqual(copy, mapping)
695 self.assertEqual(view.get('x'), 1) 695 self.assertEqual(view.get('x'), 1)
696 self.assertEqual(view.get('y'), 2) 696 self.assertEqual(view.get('y'), 2)
697 self.assertIsNone(view.get('z')) 697 self.assertIsNone(view.get('z'))
698 self.assertEqual(tuple(sorted(view.items())), (('x', 1), ('y', 2))) 698 self.assertEqual(tuple(sorted(view.items())), (('x', 1), ('y', 2)))
699 self.assertEqual(tuple(sorted(view.keys())), ('x', 'y')) 699 self.assertEqual(tuple(sorted(view.keys())), ('x', 'y'))
700 self.assertEqual(tuple(sorted(view.values())), (1, 2)) 700 self.assertEqual(tuple(sorted(view.values())), (1, 2))
701 701
702 def test_contains(self): 702 def test_contains(self):
703 view = self.mappingproxy(dict.fromkeys('abc')) 703 view = self.mappingproxy(dict.fromkeys('abc'))
704 self.assertTrue('a' in view) 704 self.assertIn('a', view)
705 self.assertTrue('b' in view) 705 self.assertIn('b', view)
706 self.assertTrue('c' in view) 706 self.assertIn('c', view)
707 self.assertFalse('xxx' in view) 707 self.assertNotIn('xxx', view)
708 708
709 def test_views(self): 709 def test_views(self):
710 mapping = {} 710 mapping = {}
711 view = self.mappingproxy(mapping) 711 view = self.mappingproxy(mapping)
712 keys = view.keys() 712 keys = view.keys()
713 values = view.values() 713 values = view.values()
714 items = view.items() 714 items = view.items()
715 self.assertEqual(list(keys), []) 715 self.assertEqual(list(keys), [])
716 self.assertEqual(list(values), []) 716 self.assertEqual(list(values), [])
717 self.assertEqual(list(items), []) 717 self.assertEqual(list(items), [])
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
1169 1169
1170 self.assertEqual(ns, ns_roundtrip, pname) 1170 self.assertEqual(ns, ns_roundtrip, pname)
1171 1171
1172 1172
1173 def test_main(): 1173 def test_main():
1174 run_unittest(TypesTests, MappingProxyTests, ClassCreationTests, 1174 run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
1175 SimpleNamespaceTests) 1175 SimpleNamespaceTests)
1176 1176
1177 if __name__ == '__main__': 1177 if __name__ == '__main__':
1178 test_main() 1178 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_trace.py ('k') | Lib/test/test_unicode_file.py » ('j') | no next file with comments »

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