diff --git a/PIL/_util.py b/PIL/_util.py index eb5c2c242..51c6f6887 100644 --- a/PIL/_util.py +++ b/PIL/_util.py @@ -3,20 +3,25 @@ import os if bytes is str: def isStringType(t): return isinstance(t, basestring) + def isPath(f): return isinstance(f, basestring) else: def isStringType(t): return isinstance(t, str) + def isPath(f): return isinstance(f, (bytes, str)) + # Checks if an object is a string, and that it points to a directory. def isDirectory(f): return isPath(f) and os.path.isdir(f) + class deferred_error(object): def __init__(self, ex): self.ex = ex + def __getattr__(self, elt): raise self.ex diff --git a/Tests/test_util.py b/Tests/test_util.py new file mode 100644 index 000000000..a547c6bd6 --- /dev/null +++ b/Tests/test_util.py @@ -0,0 +1,81 @@ +from helper import unittest, PillowTestCase + +from PIL import _util + + +class TestUtil(PillowTestCase): + + def test_is_string_type(self): + # Arrange + color = "red" + + # Act + it_is = _util.isStringType(color) + + # Assert + self.assertTrue(it_is) + + def test_is_not_string_type(self): + # Arrange + color = (255, 0, 0) + + # Act + it_is_not = _util.isStringType(color) + + # Assert + self.assertFalse(it_is_not) + + def test_is_path(self): + # Arrange + fp = "filename.ext" + + # Act + it_is = _util.isStringType(fp) + + # Assert + self.assertTrue(it_is) + + def test_is_not_path(self): + # Arrange + filename = self.tempfile("temp.ext") + fp = open(filename, 'w').close() + + # Act + it_is_not = _util.isPath(fp) + + # Assert + self.assertFalse(it_is_not) + + def test_is_directory(self): + # Arrange + directory = "Tests" + + # Act + it_is = _util.isDirectory(directory) + + # Assert + self.assertTrue(it_is) + + def test_is_not_directory(self): + # Arrange + text = "abc" + + # Act + it_is_not = _util.isDirectory(text) + + # Assert + self.assertFalse(it_is_not) + + def test_deferred_error(self): + # Arrange + + # Act + thing = _util.deferred_error(ValueError("Some error text")) + + # Assert + self.assertRaises(ValueError, lambda: thing.some_attr) + +if __name__ == '__main__': + unittest.main() + +# End of file