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

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

Issue 16510: Using appropriate checks in tests
Patch Set: Created 6 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_tuple.py ('k') | Lib/test/test_unicodedata.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 locale 5 import locale
6 import sys 6 import sys
7 import types 7 import types
8 import unittest 8 import unittest
9 9
10 class TypesTests(unittest.TestCase): 10 class TypesTests(unittest.TestCase):
(...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 class dictmissing(dict): 616 class dictmissing(dict):
617 def __missing__(self, key): 617 def __missing__(self, key):
618 return "missing=%s" % key 618 return "missing=%s" % key
619 619
620 view = self.mappingproxy(dictmissing(x=1)) 620 view = self.mappingproxy(dictmissing(x=1))
621 self.assertEqual(view['x'], 1) 621 self.assertEqual(view['x'], 1)
622 self.assertEqual(view['y'], 'missing=y') 622 self.assertEqual(view['y'], 'missing=y')
623 self.assertEqual(view.get('x'), 1) 623 self.assertEqual(view.get('x'), 1)
624 self.assertEqual(view.get('y'), None) 624 self.assertEqual(view.get('y'), None)
625 self.assertEqual(view.get('y', 42), 42) 625 self.assertEqual(view.get('y', 42), 42)
626 self.assertTrue('x' in view) 626 self.assertIn('x', view)
627 self.assertFalse('y' in view) 627 self.assertNotIn('y', view)
628 628
629 def test_customdict(self): 629 def test_customdict(self):
630 class customdict(dict): 630 class customdict(dict):
631 def __contains__(self, key): 631 def __contains__(self, key):
632 if key == 'magic': 632 if key == 'magic':
633 return True 633 return True
634 else: 634 else:
635 return dict.__contains__(self, key) 635 return dict.__contains__(self, key)
636 636
637 def __iter__(self): 637 def __iter__(self):
(...skipping 15 matching lines...) Expand all
653 return 'values' 653 return 'values'
654 654
655 def __getitem__(self, key): 655 def __getitem__(self, key):
656 return "getitem=%s" % dict.__getitem__(self, key) 656 return "getitem=%s" % dict.__getitem__(self, key)
657 657
658 def get(self, key, default=None): 658 def get(self, key, default=None):
659 return "get=%s" % dict.get(self, key, 'default=%r' % default) 659 return "get=%s" % dict.get(self, key, 'default=%r' % default)
660 660
661 custom = customdict({'key': 'value'}) 661 custom = customdict({'key': 'value'})
662 view = self.mappingproxy(custom) 662 view = self.mappingproxy(custom)
663 self.assertTrue('key' in view) 663 self.assertIn('key', view)
664 self.assertTrue('magic' in view) 664 self.assertIn('magic', view)
665 self.assertFalse('xxx' in view) 665 self.assertNotIn('xxx', view)
666 self.assertEqual(view['key'], 'getitem=value') 666 self.assertEqual(view['key'], 'getitem=value')
667 self.assertRaises(KeyError, view.__getitem__, 'xxx') 667 self.assertRaises(KeyError, view.__getitem__, 'xxx')
668 self.assertEqual(tuple(view), ('iter',)) 668 self.assertEqual(tuple(view), ('iter',))
669 self.assertEqual(len(view), 500) 669 self.assertEqual(len(view), 500)
670 self.assertEqual(view.copy(), 'copy') 670 self.assertEqual(view.copy(), 'copy')
671 self.assertEqual(view.get('key'), 'get=value') 671 self.assertEqual(view.get('key'), 'get=value')
672 self.assertEqual(view.get('xxx'), 'get=default=None') 672 self.assertEqual(view.get('xxx'), 'get=default=None')
673 self.assertEqual(view.items(), 'items') 673 self.assertEqual(view.items(), 'items')
674 self.assertEqual(view.keys(), 'keys') 674 self.assertEqual(view.keys(), 'keys')
675 self.assertEqual(view.values(), 'values') 675 self.assertEqual(view.values(), 'values')
676 676
677 def test_chainmap(self): 677 def test_chainmap(self):
678 d1 = {'x': 1} 678 d1 = {'x': 1}
679 d2 = {'y': 2} 679 d2 = {'y': 2}
680 mapping = collections.ChainMap(d1, d2) 680 mapping = collections.ChainMap(d1, d2)
681 view = self.mappingproxy(mapping) 681 view = self.mappingproxy(mapping)
682 self.assertTrue('x' in view) 682 self.assertIn('x', view)
683 self.assertTrue('y' in view) 683 self.assertIn('y', view)
684 self.assertFalse('z' in view) 684 self.assertNotIn('z', view)
685 self.assertEqual(view['x'], 1) 685 self.assertEqual(view['x'], 1)
686 self.assertEqual(view['y'], 2) 686 self.assertEqual(view['y'], 2)
687 self.assertRaises(KeyError, view.__getitem__, 'z') 687 self.assertRaises(KeyError, view.__getitem__, 'z')
688 self.assertEqual(tuple(sorted(view)), ('x', 'y')) 688 self.assertEqual(tuple(sorted(view)), ('x', 'y'))
689 self.assertEqual(len(view), 2) 689 self.assertEqual(len(view), 2)
690 copy = view.copy() 690 copy = view.copy()
691 self.assertIsNot(copy, mapping) 691 self.assertIsNot(copy, mapping)
692 self.assertIsInstance(copy, collections.ChainMap) 692 self.assertIsInstance(copy, collections.ChainMap)
693 self.assertEqual(copy, mapping) 693 self.assertEqual(copy, mapping)
694 self.assertEqual(view.get('x'), 1) 694 self.assertEqual(view.get('x'), 1)
695 self.assertEqual(view.get('y'), 2) 695 self.assertEqual(view.get('y'), 2)
696 self.assertIsNone(view.get('z')) 696 self.assertIsNone(view.get('z'))
697 self.assertEqual(tuple(sorted(view.items())), (('x', 1), ('y', 2))) 697 self.assertEqual(tuple(sorted(view.items())), (('x', 1), ('y', 2)))
698 self.assertEqual(tuple(sorted(view.keys())), ('x', 'y')) 698 self.assertEqual(tuple(sorted(view.keys())), ('x', 'y'))
699 self.assertEqual(tuple(sorted(view.values())), (1, 2)) 699 self.assertEqual(tuple(sorted(view.values())), (1, 2))
700 700
701 def test_contains(self): 701 def test_contains(self):
702 view = self.mappingproxy(dict.fromkeys('abc')) 702 view = self.mappingproxy(dict.fromkeys('abc'))
703 self.assertTrue('a' in view) 703 self.assertIn('a', view)
704 self.assertTrue('b' in view) 704 self.assertIn('b', view)
705 self.assertTrue('c' in view) 705 self.assertIn('c', view)
706 self.assertFalse('xxx' in view) 706 self.assertNotIn('xxx', view)
707 707
708 def test_views(self): 708 def test_views(self):
709 mapping = {} 709 mapping = {}
710 view = self.mappingproxy(mapping) 710 view = self.mappingproxy(mapping)
711 keys = view.keys() 711 keys = view.keys()
712 values = view.values() 712 values = view.values()
713 items = view.items() 713 items = view.items()
714 self.assertEqual(list(keys), []) 714 self.assertEqual(list(keys), [])
715 self.assertEqual(list(values), []) 715 self.assertEqual(list(values), [])
716 self.assertEqual(list(items), []) 716 self.assertEqual(list(items), [])
(...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 self.assertIs(type(spam), Spam) 1144 self.assertIs(type(spam), Spam)
1145 self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9}) 1145 self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9})
1146 1146
1147 1147
1148 def test_main(): 1148 def test_main():
1149 run_unittest(TypesTests, MappingProxyTests, ClassCreationTests, 1149 run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
1150 SimpleNamespaceTests) 1150 SimpleNamespaceTests)
1151 1151
1152 if __name__ == '__main__': 1152 if __name__ == '__main__':
1153 test_main() 1153 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_tuple.py ('k') | Lib/test/test_unicodedata.py » ('j') | no next file with comments »

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