02-add-additional-matcher.patch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. commit d2cd9189374434ea6603b07c67b316c59e6d689f
  2. author: asatarin
  3. date: 2017-06-13T11:54:11+03:00
  4. revision: 2945097
  5. REVIEW:285653 Add additional capabilities to standard raises() matcher
  6. --- contrib/python/PyHamcrest/py2/hamcrest/core/core/raises.py (f026926efe42aecf93ac9fb70955b96fbdb35a67)
  7. +++ contrib/python/PyHamcrest/py2/hamcrest/core/core/raises.py (d2cd9189374434ea6603b07c67b316c59e6d689f)
  8. @@ -10,11 +10,13 @@ __license__ = "BSD, see License.txt"
  9. class Raises(BaseMatcher):
  10. - def __init__(self, expected, pattern=None):
  11. + def __init__(self, expected, pattern=None, matcher=None):
  12. self.pattern = pattern
  13. self.expected = expected
  14. self.actual = None
  15. self.function = None
  16. + self.matcher = matcher
  17. + self.actual_return_value = None
  18. def _matches(self, function):
  19. if not is_callable(function):
  20. @@ -26,18 +28,24 @@ class Raises(BaseMatcher):
  21. def _call_function(self, function):
  22. self.actual = None
  23. try:
  24. - function()
  25. + self.actual_return_value = function()
  26. except Exception:
  27. self.actual = sys.exc_info()[1]
  28. if isinstance(self.actual, self.expected):
  29. if self.pattern is not None:
  30. - return re.search(self.pattern, str(self.actual)) is not None
  31. - return True
  32. + return (
  33. + re.search(self.pattern, str(self.actual)) is not None
  34. + and (self.matcher is None or self.matcher.matches(self.actual))
  35. + )
  36. + return self.matcher is None or self.matcher.matches(self.actual)
  37. return False
  38. def describe_to(self, description):
  39. description.append_text('Expected a callable raising %s' % self.expected)
  40. + if self.matcher is not None:
  41. + description.append_text("\n and ")
  42. + description.append_description_of(self.matcher)
  43. def describe_mismatch(self, item, description):
  44. if not is_callable(item):
  45. @@ -51,15 +59,20 @@ class Raises(BaseMatcher):
  46. return
  47. if self.actual is None:
  48. - description.append_text('No exception raised.')
  49. - elif isinstance(self.actual, self.expected) and self.pattern is not None:
  50. - description.append_text('Correct assertion type raised, but the expected pattern ("%s") not found.' % self.pattern)
  51. - description.append_text('\n message was: "%s"' % str(self.actual))
  52. + description.append_text('No exception raised and actual return value = ')
  53. + description.append_value(self.actual_return_value)
  54. + elif isinstance(self.actual, self.expected):
  55. + if self.pattern is not None:
  56. + description.append_text('Correct assertion type raised, but the expected pattern ("%s") not found.' % self.pattern)
  57. + description.append_text('\n message was: "%s"' % str(self.actual))
  58. + if self.matcher is not None:
  59. + description.append_text("\nAdditional exception matcher: ")
  60. + self.matcher.describe_mismatch(self.actual, description)
  61. else:
  62. description.append_text('%s was raised instead' % type(self.actual))
  63. -def raises(exception, pattern=None):
  64. +def raises(exception, pattern=None, matcher=None):
  65. """Matches if the called function raised the expected exception.
  66. :param exception: The class of the expected exception
  67. @@ -75,7 +88,7 @@ def raises(exception, pattern=None):
  68. assert_that(calling(int).with_args('q'), raises(TypeError))
  69. assert_that(calling(parse, broken_input), raises(ValueError))
  70. """
  71. - return Raises(exception, pattern)
  72. + return Raises(exception, pattern, matcher)
  73. class DeferredCallable(object):