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):