diff --git a/sure/__init__.py b/sure/__init__.py index dd15784..d414289 100644 --- a/sure/__init__.py +++ b/sure/__init__.py @@ -1128,7 +1128,7 @@ def __getattr__(self, name): assert_that = AssertionBuilder("assert_that") it = AssertionBuilder("it") expect = AssertionBuilder("expect") -expects = AssertionBuilder("expect") +expects = AssertionBuilder("expects") that = AssertionBuilder("that") the = AssertionBuilder("the") these = AssertionBuilder("these") diff --git a/sure/core.py b/sure/core.py index f0da38d..c9ea571 100644 --- a/sure/core.py +++ b/sure/core.py @@ -206,7 +206,7 @@ def compare(self): X = list(X) if isinstance(Y, MockCallListType): - X = list(Y) + Y = list(Y) c = self.get_context() if self.is_complex(X) and type(X) is type(Y): diff --git a/sure/errors.py b/sure/errors.py index 9fd4186..6a1567b 100644 --- a/sure/errors.py +++ b/sure/errors.py @@ -48,24 +48,24 @@ def get_stack_frames(): ) +def xor(lhs, rhs): + return lhs ^ rhs + + def get_most_recent_call_frame() -> traceback.FrameSummary: stack = get_stack_frames() return stack[-1] +def exit_code(codeword: str) -> int: + return reduce(xor, list(map(ord, codeword))) + + def treat_error(error: Exception, location: Optional[TestLocation] = None) -> Exception: manager = ExceptionManager(error, location) return manager.perform_handoff() -def xor(lhs, rhs): - return lhs ^ rhs - - -def exit_code(codeword: str) -> int: - return reduce(xor, list(map(ord, codeword))) - - class BaseSureError(Exception): def __init__(self, message): self.message = str(message) @@ -112,7 +112,7 @@ def __init__(self, scenario_result: ScenarioResult): class ExitError(ImmediateExit): - def __init__(self, context: RuntimeContext, result: ScenarioResult, report:bool = True): + def __init__(self, context: RuntimeContext, result: ScenarioResult, report: bool = True): if report: context.reporter.on_error(context, result) return super().__init__(exit_code("ERROR")) @@ -132,6 +132,9 @@ def __init__(self, context, exception: Exception): super().__init__(f"InternalRuntimeError: {exception}") context.reporter.on_internal_runtime_error(context, self) + def __str__(self): + return self.traceback + class WrongUsageError(BaseSureError): """raised when :class:`~sure.AssertionBuilder` is used diff --git a/sure/reporters/feature.py b/sure/reporters/feature.py index fba581e..a4c831f 100644 --- a/sure/reporters/feature.py +++ b/sure/reporters/feature.py @@ -148,11 +148,11 @@ def on_error(self, test: Scenario, result: ScenarioResult): self.failures.append(test) self.reported_errors.append(fullstack) - def on_internal_runtime_error(self, context: RuntimeContext, error: ErrorStack): + def on_internal_runtime_error(self, context: RuntimeContext, error: InternalRuntimeError): if isinstance(error.exception, SpecialSyntaxDisabledError): self.sh.bold_yellow(f"\n{' ' * self.indentation} {error.exception}") else: - self.sh.bold_red(error.location_specific_error()) + self.sh.bold_red(str(error)) sys.exit(error.code) def on_finish(self, context: RuntimeContext): diff --git a/tests/unit/reporters/test_feature_reporter.py b/tests/unit/reporters/test_feature_reporter.py index 0ceb57e..af8ba01 100644 --- a/tests/unit/reporters/test_feature_reporter.py +++ b/tests/unit/reporters/test_feature_reporter.py @@ -16,8 +16,8 @@ # along with this program. If not, see . "unit tests for :mod:`sure.reporters.feature`" import sys -from unittest.mock import patch, call -from unittest.mock import Mock as Spy +from mock import patch, call +from mock import Mock as Spy from sure import expects from sure.runner import Runner from sure.runtime import ( @@ -316,7 +316,7 @@ def test_feature_reporter_on_error(): def contrive_exception_info(): """contrives an exception to retrieve a list of traceback objects""" try: - sys.exc_info(sys) + raise SystemError("loudhighpitch") except Exception as e: return e, sys.exc_info() @@ -341,15 +341,15 @@ def contrive_exception_info(): expects(sh.mock_calls).to.equal( [ call.reset("\n"), + call.bold_red("Error SystemError('loudhighpitch')\n"), call.bold_red( - "Error TypeError('sys.exc_info() takes no arguments (1 given)')\n" - ), - call.bold_red( - f' File "{collapse_path(__file__)}", line 319, in contrive_exception_info\n sys.exc_info(sys)\n' + f' File \"{collapse_path(__file__)}\", line 319, in contrive_exception_info\n raise SystemError("loudhighpitch")\n' ), call.reset(" "), call.reset("\n"), - call.bold_red(f"{collapse_path(__file__)}:316\n"), + call.bold_red( + f"{collapse_path(__file__)}:316\n" + ), ] ) @@ -383,7 +383,13 @@ def test_feature_reporter_on_internal_runtime_error(exit): "FeatureReporter.on_internal_runtime_error() displays InternalRuntimeError in red" reporter = FeatureReporter(stub(Runner)) - context = stub(RuntimeContext, name="runtime-context-stub-name", reporter=reporter) + options = RuntimeOptions(immediate=True, reap_warnings=True) + context = stub( + RuntimeContext, + name="runtime-context-stub-name", + reporter=reporter, + options=options, + ) sh = Spy(name="Shell") reporter.sh = sh @@ -399,8 +405,20 @@ def contrive_special_syntax_disabled_error(): reporter.on_internal_runtime_error(context, error) - expects(sh.mock_calls).to.equal([call.bold_red(f' File "{collapse_path(__file__)}", line 392, in contrive_special_syntax_disabled_error\n raise InternalRuntimeError(context, RuntimeError("fail"))\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n')]) - exit.assert_called_once_with(exit_code(str(exc))) + expects(sh.mock_calls).to.equal( + [ + call.bold_red("NoneType: None\n"), + call.bold_red( + f' File "{collapse_path(__file__)}", line 398, in contrive_special_syntax_disabled_error\n raise InternalRuntimeError(context, RuntimeError("fail"))\n' + ), + ] + ) + expects(exit.mock_calls).to.equal( + [ + call(exit_code(str(exc))), + call(exit_code(str(exc))), + ] + ) def test_feature_reporter_on_finish():