Skip to content

Commit

Permalink
fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Mar 4, 2024
1 parent 34fa045 commit 0a8cef3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion sure/__init__.py
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion sure/core.py
Expand Up @@ -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):
Expand Down
21 changes: 12 additions & 9 deletions sure/errors.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions sure/reporters/feature.py
Expand Up @@ -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):
Expand Down
40 changes: 29 additions & 11 deletions tests/unit/reporters/test_feature_reporter.py
Expand Up @@ -16,8 +16,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"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 (
Expand Down Expand Up @@ -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()

Expand All @@ -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"
),
]
)

Expand Down Expand Up @@ -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

Expand All @@ -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():
Expand Down

0 comments on commit 0a8cef3

Please sign in to comment.