nilmdb/tests/test_mustclose.py
2013-01-02 00:00:05 -05:00

60 lines
1.1 KiB
Python

import nilmdb
from nilmdb.utils.printf import *
import nose
from nose.tools import *
from nose.tools import assert_raises
from test_helpers import *
import sys
import cStringIO
err = cStringIO.StringIO()
@nilmdb.utils.must_close(errorfile = err)
class Foo:
def __init__(self):
fprintf(err, "Init\n")
def __del__(self):
fprintf(err, "Deleting\n")
def close(self):
fprintf(err, "Closing\n")
@nilmdb.utils.must_close(errorfile = err)
class Bar:
pass
class TestMustClose(object):
def test(self):
# Note: this test might fail if the Python interpreter doesn't
# garbage collect the object (and call its __del__ function)
# right after a "del x".
x = Foo()
del x
eq_(err.getvalue(),
"Init\n"
"error: Foo.close() wasn't called!\n"
"Deleting\n")
err.truncate(0)
y = Foo()
y.close()
del y
eq_(err.getvalue(),
"Init\n"
"Closing\n"
"Deleting\n")
err.truncate(0)
z = Bar()
z.close()
del z
eq_(err.getvalue(), "")