Files
python/exercises/practice/error-handling/error_handling_test.py

56 lines
2.3 KiB
Python
Raw Normal View History

2017-10-13 11:18:51 -05:00
import unittest
import error_handling as er
from test_utils import FileLike
2017-10-13 11:31:52 -05:00
2017-10-13 11:18:51 -05:00
class ErrorHandlingTest(unittest.TestCase):
def test_throw_exception(self):
with self.assertRaisesWithMessage(Exception):
2017-10-13 11:18:51 -05:00
er.handle_error_by_throwing_exception()
def test_return_none(self):
self.assertEqual(er.handle_error_by_returning_none('1'), 1,
2017-10-13 11:18:51 -05:00
'Result of valid input should not be None')
2017-10-13 11:31:52 -05:00
self.assertIsNone(er.handle_error_by_returning_none('a'),
'Result of invalid input should be None')
2017-10-13 11:18:51 -05:00
def test_return_tuple(self):
successful_result, result = er.handle_error_by_returning_tuple('1')
self.assertIs(successful_result, True,
'Valid input should be successful')
self.assertEqual(result, 1, 'Result of valid input should not be None')
2017-10-13 11:18:51 -05:00
failure_result, result = er.handle_error_by_returning_tuple('a')
self.assertIs(failure_result, False,
'Invalid input should not be successful')
2017-10-13 11:18:51 -05:00
def test_filelike_objects_are_closed_on_exception(self):
filelike_object = FileLike(fail_something=True)
with self.assertRaisesWithMessage(Exception):
2017-10-13 11:18:51 -05:00
er.filelike_objects_are_closed_on_exception(filelike_object)
self.assertIs(filelike_object.is_open, False,
'filelike_object should be closed')
self.assertIs(filelike_object.was_open, True,
'filelike_object should have been opened')
self.assertIs(filelike_object.did_something, True,
'filelike_object should call do_something()')
2017-10-13 11:18:51 -05:00
def test_filelike_objects_are_closed_without_exception(self):
filelike_object = FileLike(fail_something=False)
er.filelike_objects_are_closed_on_exception(filelike_object)
self.assertIs(filelike_object.is_open, False,
'filelike_object should be closed')
self.assertIs(filelike_object.was_open, True,
'filelike_object should have been opened')
self.assertIs(filelike_object.did_something, True,
'filelike_object should call do_something()')
# Utility functions
def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")
2017-10-13 11:18:51 -05:00
if __name__ == '__main__':
unittest.main()