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

Side by Side Diff: Lib/unittest/case.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 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/turtle.py ('k') | Lib/unittest/test/test_case.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 """Test case implementation""" 1 """Test case implementation"""
2 2
3 import sys 3 import sys
4 import functools 4 import functools
5 import difflib 5 import difflib
6 import pprint 6 import pprint
7 import re 7 import re
8 import warnings 8 import warnings
9 import collections 9 import collections
10 10
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 return 209 return
210 # Now we simply try to choose a helpful failure message 210 # Now we simply try to choose a helpful failure message
211 if first_matching is not None: 211 if first_matching is not None:
212 self._raiseFailure('"{}" does not match "{}"'.format( 212 self._raiseFailure('"{}" does not match "{}"'.format(
213 self.expected_regex.pattern, str(first_matching))) 213 self.expected_regex.pattern, str(first_matching)))
214 if self.obj_name: 214 if self.obj_name:
215 self._raiseFailure("{} not triggered by {}".format(exc_name, 215 self._raiseFailure("{} not triggered by {}".format(exc_name,
216 self.obj_name)) 216 self.obj_name))
217 else: 217 else:
218 self._raiseFailure("{} not triggered".format(exc_name)) 218 self._raiseFailure("{} not triggered".format(exc_name))
219
220
221 class _TypeEqualityDict(object):
222
223 def __init__(self, testcase):
224 self.testcase = testcase
225 self._store = {}
226
227 def __setitem__(self, key, value):
228 self._store[key] = value
229
230 def __getitem__(self, key):
231 value = self._store[key]
232 if isinstance(value, str):
233 return getattr(self.testcase, value)
234 return value
235
236 def get(self, key, default=None):
237 if key in self._store:
238 return self[key]
239 return default
219 240
220 241
221 class TestCase(object): 242 class TestCase(object):
222 """A class whose instances are single test cases. 243 """A class whose instances are single test cases.
223 244
224 By default, the test code itself should be placed in a method named 245 By default, the test code itself should be placed in a method named
225 'runTest'. 246 'runTest'.
226 247
227 If the fixture may be used for many test cases, create as 248 If the fixture may be used for many test cases, create as
228 many test methods as are needed. When instantiating such a TestCase 249 many test methods as are needed. When instantiating such a TestCase
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 # but not an *incorrect* or missing method name 303 # but not an *incorrect* or missing method name
283 raise ValueError("no such test method in %s: %s" % 304 raise ValueError("no such test method in %s: %s" %
284 (self.__class__, methodName)) 305 (self.__class__, methodName))
285 else: 306 else:
286 self._testMethodDoc = testMethod.__doc__ 307 self._testMethodDoc = testMethod.__doc__
287 self._cleanups = [] 308 self._cleanups = []
288 309
289 # Map types to custom assertEqual functions that will compare 310 # Map types to custom assertEqual functions that will compare
290 # instances of said type in more detail to generate a more useful 311 # instances of said type in more detail to generate a more useful
291 # error message. 312 # error message.
292 self._type_equality_funcs = {} 313 self._type_equality_funcs = _TypeEqualityDict(self)
293 self.addTypeEqualityFunc(dict, 'assertDictEqual') 314 self.addTypeEqualityFunc(dict, 'assertDictEqual')
294 self.addTypeEqualityFunc(list, 'assertListEqual') 315 self.addTypeEqualityFunc(list, 'assertListEqual')
295 self.addTypeEqualityFunc(tuple, 'assertTupleEqual') 316 self.addTypeEqualityFunc(tuple, 'assertTupleEqual')
296 self.addTypeEqualityFunc(set, 'assertSetEqual') 317 self.addTypeEqualityFunc(set, 'assertSetEqual')
297 self.addTypeEqualityFunc(frozenset, 'assertSetEqual') 318 self.addTypeEqualityFunc(frozenset, 'assertSetEqual')
298 self.addTypeEqualityFunc(str, 'assertMultiLineEqual') 319 self.addTypeEqualityFunc(str, 'assertMultiLineEqual')
299 320
300 def addTypeEqualityFunc(self, typeobj, function): 321 def addTypeEqualityFunc(self, typeobj, function):
301 """Add a type specific assertEqual style function to compare a type. 322 """Add a type specific assertEqual style function to compare a type.
302 323
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 # subclasses are not intended to be compared in detail to their super 636 # subclasses are not intended to be compared in detail to their super
616 # class instances using a type equality func. This means testing 637 # class instances using a type equality func. This means testing
617 # subtypes won't automagically use the detailed comparison. Callers 638 # subtypes won't automagically use the detailed comparison. Callers
618 # should use their type specific assertSpamEqual method to compare 639 # should use their type specific assertSpamEqual method to compare
619 # subclasses if the detailed comparison is desired and appropriate. 640 # subclasses if the detailed comparison is desired and appropriate.
620 # See the discussion in http://bugs.python.org/issue2578. 641 # See the discussion in http://bugs.python.org/issue2578.
621 # 642 #
622 if type(first) is type(second): 643 if type(first) is type(second):
623 asserter = self._type_equality_funcs.get(type(first)) 644 asserter = self._type_equality_funcs.get(type(first))
624 if asserter is not None: 645 if asserter is not None:
625 if isinstance(asserter, str):
626 asserter = getattr(self, asserter)
627 return asserter 646 return asserter
628 647
629 return self._baseAssertEqual 648 return self._baseAssertEqual
630 649
631 def _baseAssertEqual(self, first, second, msg=None): 650 def _baseAssertEqual(self, first, second, msg=None):
632 """The default assertEqual implementation, not type specific.""" 651 """The default assertEqual implementation, not type specific."""
633 if not first == second: 652 if not first == second:
634 standardMsg = '%s != %s' % (safe_repr(first), safe_repr(second)) 653 standardMsg = '%s != %s' % (safe_repr(first), safe_repr(second))
635 msg = self._formatMessage(msg, standardMsg) 654 msg = self._formatMessage(msg, standardMsg)
636 raise self.failureException(msg) 655 raise self.failureException(msg)
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 1226
1208 def __repr__(self): 1227 def __repr__(self):
1209 return "<%s tec=%s>" % (strclass(self.__class__), 1228 return "<%s tec=%s>" % (strclass(self.__class__),
1210 self._testFunc) 1229 self._testFunc)
1211 1230
1212 def shortDescription(self): 1231 def shortDescription(self):
1213 if self._description is not None: 1232 if self._description is not None:
1214 return self._description 1233 return self._description
1215 doc = self._testFunc.__doc__ 1234 doc = self._testFunc.__doc__
1216 return doc and doc.split("\n")[0].strip() or None 1235 return doc and doc.split("\n")[0].strip() or None
OLDNEW
« no previous file with comments | « Lib/turtle.py ('k') | Lib/unittest/test/test_case.py » ('j') | no next file with comments »

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