From 2311a836c78109f5f940dc20958faf1df2f850e6 Mon Sep 17 00:00:00 2001 From: Elijah Sawyers Date: Thu, 24 Oct 2019 08:22:44 -0500 Subject: [PATCH 1/2] Update error package tests --- .../error/__tests__/mock_handle_exception.py | 31 ++++++++++ autostack/error/__tests__/test_error.py | 56 +++++++------------ 2 files changed, 50 insertions(+), 37 deletions(-) create mode 100644 autostack/error/__tests__/mock_handle_exception.py diff --git a/autostack/error/__tests__/mock_handle_exception.py b/autostack/error/__tests__/mock_handle_exception.py new file mode 100644 index 0000000..6e6e229 --- /dev/null +++ b/autostack/error/__tests__/mock_handle_exception.py @@ -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 diff --git a/autostack/error/__tests__/test_error.py b/autostack/error/__tests__/test_error.py index c3bf922..3b98d7e 100644 --- a/autostack/error/__tests__/test_error.py +++ b/autostack/error/__tests__/test_error.py @@ -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): @@ -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): @@ -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): @@ -124,19 +113,12 @@ def test_parse_output_for_error_traceback(monkeypatch): ''' # 1. Given. - pipe = MockPipe([ + mock_pipe = MockPipe([ ' File "", line 1, in ', '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 @@ -144,7 +126,7 @@ def mock_get_error_from_traceback(pipe): monkeypatch.setattr( 'autostack.error.handle_exception', - mock_handle_exception + mock_handle_exception.handle_exception ) monkeypatch.setattr( @@ -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): From a885761ea4cc01792b4baf12736f0e34bf581416 Mon Sep 17 00:00:00 2001 From: Elijah Sawyers Date: Thu, 24 Oct 2019 10:23:11 -0500 Subject: [PATCH 2/2] More error package tests --- autostack/error/__init__.py | 13 +++++++++-- autostack/error/__tests__/test_error.py | 31 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/autostack/error/__init__.py b/autostack/error/__init__.py index be12cd8..504319d 100644 --- a/autostack/error/__init__.py +++ b/autostack/error/__init__.py @@ -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 "", line 1, in + 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() @@ -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) diff --git a/autostack/error/__tests__/test_error.py b/autostack/error/__tests__/test_error.py index 3b98d7e..0230aab 100644 --- a/autostack/error/__tests__/test_error.py +++ b/autostack/error/__tests__/test_error.py @@ -8,7 +8,7 @@ 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, @@ -122,6 +122,10 @@ def test_parse_output_for_error_traceback(monkeypatch): def mock_get_error_from_traceback(pipe): # pylint: disable=unused-argument + ''' + Mocks the get_error_from_traceback function. + ''' + return 'NameError' monkeypatch.setattr( @@ -142,6 +146,26 @@ def mock_get_error_from_traceback(pipe): 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 "", line 1, in ', + 'NameError: name \'xyz\' is not defined' + ]) + + # 2. When. + error = get_error_from_traceback(mock_pipe) + + # 3. Then. + assert error == 'NameError' + assert mock_pipe.get_readline_call_count() == 2 + + def test_error_solved_y(monkeypatch): ''' Ensures that when 'Y' is inputted, error_solved returns True. @@ -151,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' @@ -173,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' @@ -195,6 +221,8 @@ 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 @@ -202,6 +230,7 @@ def make_mock_input(): def mock_input(*args): # pylint: disable=unused-argument ''' + Mocks user input to be 'a' then 'Y'. ''' nonlocal call_count