diff --git a/tests/test_iotools.py b/tests/test_iotools.py new file mode 100644 index 0000000000000000000000000000000000000000..12084766f7b45d54c9e5847d64c60472a3413d11 --- /dev/null +++ b/tests/test_iotools.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import pytest +import zipfile + +from os.path import join + +from imcflibs.iotools import filehandle +from imcflibs.iotools import readtxt + +__author__ = "Niko Ehrenfeuchter" +__copyright__ = "Niko Ehrenfeuchter" +__license__ = "gpl3" + + +def test_filehandle(tmpdir): + tmpfile = tmpdir.join('testfile') + tmpname = str(tmpfile) + tmphandle = open(str(tmpfile), 'w') + print(type(tmphandle)) + assert type(tmpname) is str + assert type(tmphandle) is file + assert type(filehandle(tmpname)) is file + assert type(filehandle(tmphandle, 'w')) is file + + +def test_readtxt(tmpdir): + content = [ + u'lorem\n', + u'ipsum\n', + u'and some more\n', + u'dummy text\n', + ] + fh = tmpdir.mkdir("readtxt").join("content.txt") + fh.write(u''.join(content)) + print(fh.basename) + print(fh.dirname) + with zipfile.ZipFile(join(fh.dirname, 'archive.zip'), 'w') as zf: + zf.write(str(fh), arcname='content.txt') + print("wrote [%s] into [%s]" % (str(fh), zf.filename)) + + print(content) + fromfile = readtxt(str(fh)) + print(fromfile) + assert fromfile == content + + fromfile_flat = readtxt(str(fh), flat=True) + assert fromfile_flat == ''.join(content) + + print(join(fh.dirname, 'archive.zip')) + fromzip = readtxt('content.txt', join(fh.dirname, 'archive.zip')) + print(fromzip) + assert fromzip == content + + fromzip_flat = readtxt('content.txt', join(fh.dirname, 'archive.zip'), flat=True) + assert fromzip_flat == ''.join(content) diff --git a/tests/test_pathtools.py b/tests/test_pathtools.py index 07c681366797e1836c06f6b62a98199835e1f3c0..006368338e0c1f06d309882b4ad2cfc227bf8cd8 100644 --- a/tests/test_pathtools.py +++ b/tests/test_pathtools.py @@ -3,6 +3,7 @@ import pytest from imcflibs.pathtools import parse_path +from imcflibs.pathtools import jython_fiji_exists __author__ = "Niko Ehrenfeuchter" __copyright__ = "Niko Ehrenfeuchter" @@ -31,3 +32,16 @@ def test_parse_path(): assert path_to_dir['dname'] == 'foo' assert path_to_dir['ext'] == '' + +def test_parse_path_windows(): + path = r'C:\foo\bar' + parsed = parse_path(path) + + assert parsed['orig'] == path + assert parsed['full'] == r'C:/foo/bar' + assert parsed['fname'] == 'bar' + assert parsed['dname'] == 'foo' + + +def test_jython_fiji_exists(tmpdir): + assert jython_fiji_exists(str(tmpdir)) == True diff --git a/tests/test_strtools.py b/tests/test_strtools.py new file mode 100644 index 0000000000000000000000000000000000000000..c49a762a7ed288829b87e86a702e1d4b8671ae6e --- /dev/null +++ b/tests/test_strtools.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import pytest +import os + +from imcflibs.strtools import _is_string_like +from imcflibs.strtools import filename +from imcflibs.strtools import flatten + +__author__ = "Niko Ehrenfeuchter" +__copyright__ = "Niko Ehrenfeuchter" +__license__ = "gpl3" + + +def test__is_string_like(): + assert _is_string_like('foo') == True + assert _is_string_like(12345) == False + + +def test_filename_from_string(): + assert filename('test_file_name') == 'test_file_name' + + +def test_filename_from_handle(tmpdir): + path = str(tmpdir) + fhandle = tmpdir.join('foo.txt') + assert filename(fhandle) == os.path.join(path, 'foo.txt') + + +def test_flatten(): + assert flatten(('foo', 'bar')) == 'foobar' \ No newline at end of file