Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tdd #217

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Prev Previous commit
context manager
Philipp committed Aug 21, 2020
commit f8514139bf578989944c2f4693958aceff9dcb82
1 change: 1 addition & 0 deletions some_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hola!
54 changes: 54 additions & 0 deletions test/context_managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

import unittest,io
class TDD_CONTEXT_MANAGERS(unittest.TestCase):
def test_context_managers(self):
with open('test/some_file', 'w') as opened_file:
opened_file.write('Hola!')
opened_file.close()
f = open('test/some_file')
data = f.read()
self.assertEqual(data,'Hola!')
f.close()
file = open('some_file', 'w')
try:
file.write('Hola!')
self.assertRaises(io.UnsupportedOperation, file.read)
finally:
file.close()
def test_manager_class(self):
this=self
class File(object):
def __init__(self, file_name, method):
self.file_obj = open(file_name, method)
def __enter__(self):
return self.file_obj
def __exit__(self, type, value, traceback):
this.assertEqual(self.file_obj.name,'test/demo.txt')
type and this.assertTrue(type is AttributeError)
self.file_obj.close()
return True
with File('test/demo.txt', 'w') as opened_file:
opened_file.write('Hola!')
with File('test/demo.txt', 'w') as opened_file:
opened_file.undefined_function('Hola!')
def test_manager_generator(self):
from contextlib import contextmanager

@contextmanager
def open_file(name):
f = open(name, 'w')
try:
yield f
finally:
f.close()
with open_file('test/some_file') as f:
f.write('hola!')
f.close()
f=open('test/some_file')
data = f.read()
self.assertEqual(data, 'hola!')
f.close()
if __name__ == '__main__':
unittest.main()


Empty file added test/demo.txt
Empty file.
1 change: 1 addition & 0 deletions test/some_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hola!