Skip to content

Commit

Permalink
Update error package tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elijahsawyers committed Oct 24, 2019
1 parent 43fc217 commit 2311a83
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
31 changes: 31 additions & 0 deletions autostack/error/__tests__/mock_handle_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'''
Authors: Elijah Sawyers, Benjamin Sanders
Emails: elijahsawyers@gmail.com, ben.sanders97@gmail.com
Date: 10/23/2019
Overview: Mocks the handle_exception function.
'''


class MockHandleException:
'''
Mocks the handle_exception function.
'''

def __init__(self):
'''
Initializes a mock handle_exception with was_called set
to False and parameter set to None.
'''

self.was_called = False
self.parameter = None

def handle_exception(self, error):
'''
Sets was_called to True, indicating that the function was
called, and parameter is set to the error string passed into
the function.
'''

self.was_called = True
self.parameter = error
56 changes: 19 additions & 37 deletions autostack/error/__tests__/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
print_listening_for_errors,
)
from autostack.error.__tests__.mock_pipe import MockPipe
from autostack.error.__tests__.mock_handle_exception import MockHandleException


def test_listen_for_errors(monkeypatch):
Expand Down Expand Up @@ -64,27 +65,21 @@ def test_parse_output_for_error_non_error(monkeypatch):
'''

# 1. Given.
output = 'IndentationError: unexpected indent'
error_called_with = None
was_called = False

def mock_handle_exception(error):
nonlocal error_called_with
nonlocal was_called
was_called = True
error_called_with = error
mock_handle_exception = MockHandleException()

monkeypatch.setattr(
'autostack.error.handle_exception',
mock_handle_exception
mock_handle_exception.handle_exception
)

output = 'Not an error.'

# 2. When.
parse_output_for_error(output, None)

# 3. Then.
assert error_called_with == 'IndentationError'
assert was_called
assert not mock_handle_exception.parameter
assert not mock_handle_exception.was_called


def test_parse_output_for_error_with_error(monkeypatch):
Expand All @@ -94,27 +89,21 @@ def test_parse_output_for_error_with_error(monkeypatch):
'''

# 1. Given.
output = 'Not an error.'
error_called_with = None
was_called = False

def mock_handle_exception(error):
nonlocal error_called_with
nonlocal was_called
was_called = True
error_called_with = error
mock_handle_exception = MockHandleException()

monkeypatch.setattr(
'autostack.error.handle_exception',
mock_handle_exception
mock_handle_exception.handle_exception
)

output = 'IndentationError: unexpected indent'

# 2. When.
parse_output_for_error(output, None)

# 3. Then.
assert not error_called_with
assert not was_called
assert mock_handle_exception.parameter == 'IndentationError'
assert mock_handle_exception.was_called


def test_parse_output_for_error_traceback(monkeypatch):
Expand All @@ -124,27 +113,20 @@ def test_parse_output_for_error_traceback(monkeypatch):
'''

# 1. Given.
pipe = MockPipe([
mock_pipe = MockPipe([
' File "<stdin>", line 1, in <module>',
'NameError: name \'xyz\' is not defined'
])
mock_handle_exception = MockHandleException()
output = 'Traceback (most recent call last):'
error_called_with = None
was_called = False

def mock_handle_exception(error):
nonlocal error_called_with
nonlocal was_called
was_called = True
error_called_with = error

def mock_get_error_from_traceback(pipe):
# pylint: disable=unused-argument
return 'NameError'

monkeypatch.setattr(
'autostack.error.handle_exception',
mock_handle_exception
mock_handle_exception.handle_exception
)

monkeypatch.setattr(
Expand All @@ -153,11 +135,11 @@ def mock_get_error_from_traceback(pipe):
)

# 2. When.
parse_output_for_error(output, pipe)
parse_output_for_error(output, mock_pipe)

# 3. Then.
assert error_called_with == 'NameError'
assert was_called
assert mock_handle_exception.parameter == 'NameError'
assert mock_handle_exception.was_called


def test_error_solved_y(monkeypatch):
Expand Down

0 comments on commit 2311a83

Please sign in to comment.