lalala33rfs

Untitled

Nov 19th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. import io
  2. import unittest
  3. from contextlib import redirect_stdout
  4. from textwrap import dedent
  5. # from contextlib import contextmanager
  6. import contextlib
  7.  
  8. try:
  9. from src.task3 import contextmanager
  10. except ImportError as e:
  11. raise unittest.SkipTest("Task 3 is not complete yet")
  12.  
  13.  
  14. # @contextmanager
  15. @contextlib.contextmanager
  16. def foo():
  17. print("__enter__")
  18. try:
  19. yield 42
  20. except Exception as e:
  21. print(type(e))
  22. print(str(e))
  23. finally:
  24. print("__exit__")
  25. return
  26.  
  27.  
  28. class Task3Tests(unittest.TestCase):
  29. @staticmethod
  30. def prepare_string(string_value):
  31. # remove leading empty lines
  32. # and dedent leading spaces in every line of multiline string
  33. return dedent(string_value.lstrip('\n'))
  34.  
  35. def test_simple_case(self):
  36. expected = """
  37. __enter__
  38. 42
  39. <class 'ValueError'>
  40. OOOPS
  41. __exit__
  42. """
  43.  
  44. with io.StringIO() as buf, redirect_stdout(buf):
  45. with foo() as i:
  46. print(i)
  47. raise ValueError("OOOPS")
  48. res = buf.getvalue()
  49. self.assertEqual(self.prepare_string(expected), res)
Advertisement
Add Comment
Please, Sign In to add comment