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

Delta Between Two Patch Sets: Lib/test/test_types.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 9 months ago
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_trace.py ('k') | Lib/test/test_unicode_file.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 locale 6 import locale
6 import sys 7 import sys
7 import types 8 import types
8 import unittest 9 import unittest
9 10
10 class TypesTests(unittest.TestCase): 11 class TypesTests(unittest.TestCase):
11 12
12 def test_truth_values(self): 13 def test_truth_values(self):
13 if None: self.fail('None is true instead of false') 14 if None: self.fail('None is true instead of false')
14 if 0: self.fail('0 is true instead of false') 15 if 0: self.fail('0 is true instead of false')
(...skipping 1055 matching lines...) Expand 10 before | Expand all | Expand 10 after
1070 ns1.spam = 5 1071 ns1.spam = 5
1071 self.assertEqual(vars(ns1), dict(spam=5)) 1072 self.assertEqual(vars(ns1), dict(spam=5))
1072 del ns1.spam 1073 del ns1.spam
1073 self.assertEqual(vars(ns1), {}) 1074 self.assertEqual(vars(ns1), {})
1074 1075
1075 def test_repr(self): 1076 def test_repr(self):
1076 ns1 = types.SimpleNamespace(x=1, y=2, w=3) 1077 ns1 = types.SimpleNamespace(x=1, y=2, w=3)
1077 ns2 = types.SimpleNamespace() 1078 ns2 = types.SimpleNamespace()
1078 ns2.x = "spam" 1079 ns2.x = "spam"
1079 ns2._y = 5 1080 ns2._y = 5
1080 1081 name = "namespace"
1081 self.assertEqual(repr(ns1), "namespace(w=3, x=1, y=2)") 1082
1082 self.assertEqual(repr(ns2), "namespace(_y=5, x='spam')") 1083 self.assertEqual(repr(ns1), "{name}(w=3, x=1, y=2)".format(name=name))
1084 self.assertEqual(repr(ns2), "{name}(_y=5, x='spam')".format(name=name))
1085
1086 def test_equal(self):
1087 ns1 = types.SimpleNamespace(x=1)
1088 ns2 = types.SimpleNamespace()
1089 ns2.x = 1
1090
1091 self.assertEqual(types.SimpleNamespace(), types.SimpleNamespace())
1092 self.assertEqual(ns1, ns2)
1093 self.assertNotEqual(ns2, types.SimpleNamespace())
1083 1094
1084 def test_nested(self): 1095 def test_nested(self):
1085 ns1 = types.SimpleNamespace(a=1, b=2) 1096 ns1 = types.SimpleNamespace(a=1, b=2)
1086 ns2 = types.SimpleNamespace() 1097 ns2 = types.SimpleNamespace()
1087 ns3 = types.SimpleNamespace(x=ns1) 1098 ns3 = types.SimpleNamespace(x=ns1)
1088 ns2.spam = ns1 1099 ns2.spam = ns1
1089 ns2.ham = '?' 1100 ns2.ham = '?'
1090 ns2.spam = ns3 1101 ns2.spam = ns3
1091 1102
1092 self.assertEqual(vars(ns1), dict(a=1, b=2)) 1103 self.assertEqual(vars(ns1), dict(a=1, b=2))
(...skipping 17 matching lines...) Expand all
1110 self.assertEqual(ns3.spam, ns2) 1121 self.assertEqual(ns3.spam, ns2)
1111 self.assertEqual(ns2.spam.spam, ns2) 1122 self.assertEqual(ns2.spam.spam, ns2)
1112 1123
1113 def test_recursive_repr(self): 1124 def test_recursive_repr(self):
1114 ns1 = types.SimpleNamespace(c='cookie') 1125 ns1 = types.SimpleNamespace(c='cookie')
1115 ns2 = types.SimpleNamespace() 1126 ns2 = types.SimpleNamespace()
1116 ns3 = types.SimpleNamespace(x=1) 1127 ns3 = types.SimpleNamespace(x=1)
1117 ns1.spam = ns1 1128 ns1.spam = ns1
1118 ns2.spam = ns3 1129 ns2.spam = ns3
1119 ns3.spam = ns2 1130 ns3.spam = ns2
1120 1131 name = "namespace"
1121 self.assertEqual(repr(ns1), 1132 repr1 = "{name}(c='cookie', spam={name}(...))".format(name=name)
1122 "namespace(c='cookie', spam=namespace(...))") 1133 repr2 = "{name}(spam={name}(spam={name}(...), x=1))".format(name=name)
1123 self.assertEqual(repr(ns2), 1134
1124 "namespace(spam=namespace(spam=namespace(...), x=1))") 1135 self.assertEqual(repr(ns1), repr1)
1136 self.assertEqual(repr(ns2), repr2)
1125 1137
1126 def test_as_dict(self): 1138 def test_as_dict(self):
1127 ns = types.SimpleNamespace(spam='spamspamspam') 1139 ns = types.SimpleNamespace(spam='spamspamspam')
1128 1140
1129 with self.assertRaises(TypeError): 1141 with self.assertRaises(TypeError):
1130 len(ns) 1142 len(ns)
1131 with self.assertRaises(TypeError): 1143 with self.assertRaises(TypeError):
1132 iter(ns) 1144 iter(ns)
1133 with self.assertRaises(TypeError): 1145 with self.assertRaises(TypeError):
1134 'spam' in ns 1146 'spam' in ns
1135 with self.assertRaises(TypeError): 1147 with self.assertRaises(TypeError):
1136 ns['spam'] 1148 ns['spam']
1137 1149
1138 def test_subclass(self): 1150 def test_subclass(self):
1139 class Spam(types.SimpleNamespace): 1151 class Spam(types.SimpleNamespace):
1140 pass 1152 pass
1141 1153
1142 spam = Spam(ham=8, eggs=9) 1154 spam = Spam(ham=8, eggs=9)
1143 1155
1144 self.assertIs(type(spam), Spam) 1156 self.assertIs(type(spam), Spam)
1145 self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9}) 1157 self.assertEqual(vars(spam), {'ham': 8, 'eggs': 9})
1146 1158
1159 def test_pickle(self):
1160 ns = types.SimpleNamespace(breakfast="spam", lunch="spam")
1161
1162 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1163 pname = "protocol {}".format(protocol)
1164 try:
1165 ns_pickled = pickle.dumps(ns, protocol)
1166 except TypeError as e:
1167 raise TypeError(pname) from e
1168 ns_roundtrip = pickle.loads(ns_pickled)
1169
1170 self.assertEqual(ns, ns_roundtrip, pname)
1171
1147 1172
1148 def test_main(): 1173 def test_main():
1149 run_unittest(TypesTests, MappingProxyTests, ClassCreationTests, 1174 run_unittest(TypesTests, MappingProxyTests, ClassCreationTests,
1150 SimpleNamespaceTests) 1175 SimpleNamespaceTests)
1151 1176
1152 if __name__ == '__main__': 1177 if __name__ == '__main__':
1153 test_main() 1178 test_main()
LEFTRIGHT

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