Skip to content

Commit

Permalink
tests for interaction of NamedStream with os.path functions
Browse files Browse the repository at this point in the history
(see discussion at #652)
  • Loading branch information
orbeckst committed Jan 22, 2016
1 parent 6e7c7da commit 3ad14f9
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions testsuite/MDAnalysisTests/test_streamio.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def setUp(self):
self.textname = "jabberwock.txt"
self.numtextlines = len(self.text)

def testClosing(self):
def test_closing(self):
obj = cStringIO.StringIO("".join(self.text))
ns = util.NamedStream(obj, self.textname, close=True)
assert_equal(ns.closed, False)
ns.close()
assert_equal(ns.closed, True)

def testClosingForce(self):
def test_closing_force(self):
obj = cStringIO.StringIO("".join(self.text))
ns = util.NamedStream(obj, self.textname)
assert_equal(ns.closed, False)
Expand All @@ -102,7 +102,7 @@ def testClosingForce(self):
ns.close(force=True)
assert_equal(ns.closed, True)

def testcStringIO_read(self):
def test_cStringIO_read(self):
obj = cStringIO.StringIO("".join(self.text))
ns = util.NamedStream(obj, self.textname)
assert_equal(ns.name, self.textname)
Expand All @@ -112,7 +112,7 @@ def testcStringIO_read(self):
assert_equal(len(ns.readlines()), self.numtextlines)
ns.close(force=True)

def testFile_read(self):
def test_File_read(self):
obj = open(self.filename, 'r')
ns = util.NamedStream(obj, self.filename)
assert_equal(ns.name, self.filename)
Expand All @@ -122,7 +122,7 @@ def testFile_read(self):
assert_equal(len(ns.readlines()), self.numlines)
ns.close(force=True)

def testcStringIO_write(self):
def test_cStringIO_write(self):
obj = cStringIO.StringIO()
ns = util.NamedStream(obj, self.textname)
ns.writelines(self.text)
Expand All @@ -134,7 +134,7 @@ def testcStringIO_write(self):
assert_equal(ns.read(20), "".join(self.text)[:20])
ns.close(force=True)

def testFile_write(self):
def test_File_write(self):
fd, outfile = tempfile.mkstemp(suffix=".txt")
os.close(fd)
try:
Expand All @@ -155,6 +155,34 @@ def testFile_write(self):
os.unlink(outfile)
except OSError:
pass
class TestNamedStream_filename_behavior(object):
textname = "jabberwock.txt"
# note: no setUp() because classes with generators would run it
# *for each generated test* and we need it for the generator method

def test_ospath_funcs(self):
obj = cStringIO.StringIO()
ns = util.NamedStream(obj, self.textname)

funcs = ("abspath", "basename", "dirname", "normpath",
"relpath", "split", "splitext")
def _test_func(funcname, fn=self.textname, ns=ns):
func = getattr(os.path, funcname)
reference = func(fn)
value = func(ns)
assert_equal(value, reference,
err_msg=("os.path.{0}() does not work with "
"NamedStream").format(funcname))
# join not included because of different call signature
def _test_join(func="join", fn=self.textname, ns=ns, path="/tmp/MDAnalysisTests"):
reference = os.path.join(path, fn)
value = os.path.join(path, ns)
assert_equal(value, reference,
err_msg=("os.path.join() does not work with "
"NamedStream"))
for func in funcs:
yield _test_func, func
yield _test_join, "join"


class _StreamData(object):
Expand Down

0 comments on commit 3ad14f9

Please sign in to comment.