Skip to content

Commit

Permalink
Merge pull request #32 from autostack-team/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
elijahsawyers authored Oct 24, 2019
2 parents 43fc217 + a885761 commit 82fee50
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 40 deletions.
13 changes: 11 additions & 2 deletions autostack/error/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ def parse_output_for_error(output, pipe):

def get_error_from_traceback(pipe):
'''
TODO: Write docstring.
Gets the error description from a traceback.
e.g.:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'xyz' is not defined
would return 'NameError'.
Parameter {File} pipe: the pipe to read the traceback from.
Returns {str}: the error description.
'''

output = pipe.readline()
Expand All @@ -90,7 +99,7 @@ def handle_exception(exception):
TODO: Write docstring.
'''

for post in accepted_posts(exception[:-1]):
for post in accepted_posts(exception):
# Display Stack Overflow posts for the error.
print_accepted_post(post)

Expand Down
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
87 changes: 49 additions & 38 deletions autostack/error/__tests__/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
from autostack.error import (
listen_for_errors,
parse_output_for_error,
# get_error_from_traceback,
get_error_from_traceback,
# handle_exception,
error_solved,
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,24 @@ 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
'''
Mocks the get_error_from_traceback function.
'''

return 'NameError'

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

monkeypatch.setattr(
Expand All @@ -153,11 +139,31 @@ 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 mock_handle_exception.parameter == 'NameError'
assert mock_handle_exception.was_called


def test_get_error_from_traceback():
'''
Ensures that the error description is returned from a
traceback.
'''

# 1. Given.
mock_pipe = MockPipe([
' File "<stdin>", line 1, in <module>',
'NameError: name \'xyz\' is not defined'
])

# 2. When.
error = get_error_from_traceback(mock_pipe)

# 3. Then.
assert error_called_with == 'NameError'
assert was_called
assert error == 'NameError'
assert mock_pipe.get_readline_call_count() == 2


def test_error_solved_y(monkeypatch):
Expand All @@ -169,6 +175,7 @@ def test_error_solved_y(monkeypatch):
def mock_input(*args):
# pylint: disable=unused-argument
'''
Mocks user input to be 'Y'.
'''

return 'Y'
Expand All @@ -191,6 +198,7 @@ def test_error_solved_n(monkeypatch):
def mock_input(*args):
# pylint: disable=unused-argument
'''
Mocks user input to be 'n'.
'''

return 'n'
Expand All @@ -213,13 +221,16 @@ def test_error_solved_invalid_input(capsys, monkeypatch):
# 1. Given.
def make_mock_input():
'''
Creates the mock function to mock user intput, and the
input is different based on the call count.
'''

call_count = 0

def mock_input(*args):
# pylint: disable=unused-argument
'''
Mocks user input to be 'a' then 'Y'.
'''

nonlocal call_count
Expand Down

0 comments on commit 82fee50

Please sign in to comment.