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

66 lines
2.1 KiB
Python
Raw Normal View History

2017-10-13 11:18:51 -05:00
import unittest
import error_handling as er
class FileLike(object):
def __init__(self):
self.is_open = False
self.was_open = False
self.did_something = False
2017-10-13 11:18:51 -05:00
def open(self):
self.was_open = False
2017-10-13 11:31:52 -05:00
self.is_open = True
2017-10-13 11:18:51 -05:00
def close(self):
self.is_open = False
self.was_open = True
def __enter__(self):
self.open()
return self
2017-10-13 11:18:51 -05:00
def __exit__(self, *args):
self.close()
def do_something(self):
self.did_something = True
raise Exception()
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.assertRaises(Exception):
er.handle_error_by_throwing_exception()
def test_return_none(self):
self.assertEqual(1, er.handle_error_by_returning_none('1'),
'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.assertTrue(successful_result, 'Valid input should be successful')
self.assertEqual(1, result, 'Result of valid input should not be None')
failure_result, result = er.handle_error_by_returning_tuple('a')
self.assertFalse(failure_result,
'Invalid input should not be successful')
def test_filelike_objects_are_closed_on_exception(self):
filelike_object = FileLike()
2017-10-13 11:31:52 -05:00
with self.assertRaises(Exception):
2017-10-13 11:18:51 -05:00
er.filelike_objects_are_closed_on_exception(filelike_object)
self.assertFalse(filelike_object.is_open,
'filelike_object should be closed')
self.assertTrue(filelike_object.was_open,
'filelike_object should have been opened')
self.assertTrue(filelike_object.did_something,
'filelike_object should call do_something()')
2017-10-13 11:18:51 -05:00
if __name__ == '__main__':
unittest.main()