Merge pull request #783 from hugovk/_util

Tests for _util.py
This commit is contained in:
wiredfool 2014-07-08 09:16:52 -07:00
commit 65357e1722
2 changed files with 86 additions and 0 deletions

View File

@ -3,20 +3,25 @@ import os
if bytes is str: if bytes is str:
def isStringType(t): def isStringType(t):
return isinstance(t, basestring) return isinstance(t, basestring)
def isPath(f): def isPath(f):
return isinstance(f, basestring) return isinstance(f, basestring)
else: else:
def isStringType(t): def isStringType(t):
return isinstance(t, str) return isinstance(t, str)
def isPath(f): def isPath(f):
return isinstance(f, (bytes, str)) return isinstance(f, (bytes, str))
# Checks if an object is a string, and that it points to a directory. # Checks if an object is a string, and that it points to a directory.
def isDirectory(f): def isDirectory(f):
return isPath(f) and os.path.isdir(f) return isPath(f) and os.path.isdir(f)
class deferred_error(object): class deferred_error(object):
def __init__(self, ex): def __init__(self, ex):
self.ex = ex self.ex = ex
def __getattr__(self, elt): def __getattr__(self, elt):
raise self.ex raise self.ex

81
Tests/test_util.py Normal file
View File

@ -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