Commit f13b5801 by Chris Jerdonek

Merge 'issue_81' into development: closing issue #81.

parents 3e284008 ee579e21
...@@ -5,23 +5,14 @@ Unit tests of context.py. ...@@ -5,23 +5,14 @@ Unit tests of context.py.
""" """
from datetime import datetime
import unittest import unittest
from pystache.context import _NOT_FOUND from pystache.context import _NOT_FOUND
from pystache.context import _get_item from pystache.context import _get_value
from pystache.context import Context from pystache.context import Context
class TestCase(unittest.TestCase):
"""A TestCase class with support for assertIs()."""
# unittest.assertIs() is not available until Python 2.7:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
def assertIs(self, first, second):
self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second)))
class SimpleObject(object): class SimpleObject(object):
"""A sample class that does not define __getitem__().""" """A sample class that does not define __getitem__()."""
...@@ -33,7 +24,7 @@ class SimpleObject(object): ...@@ -33,7 +24,7 @@ class SimpleObject(object):
return "called..." return "called..."
class MappingObject(object): class DictLike(object):
"""A sample class that implements __getitem__() and __contains__().""" """A sample class that implements __getitem__() and __contains__()."""
...@@ -48,26 +39,36 @@ class MappingObject(object): ...@@ -48,26 +39,36 @@ class MappingObject(object):
return self._dict[key] return self._dict[key]
class GetItemTestCase(TestCase): class AssertIsMixin:
"""A mixin for adding assertIs() to a unittest.TestCase."""
# unittest.assertIs() is not available until Python 2.7:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
def assertIs(self, first, second):
self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second)))
"""Test context._get_item()."""
def assertNotFound(self, obj, key): class GetValueTests(unittest.TestCase, AssertIsMixin):
"""Test context._get_value()."""
def assertNotFound(self, item, key):
""" """
Assert that a call to _get_item() returns _NOT_FOUND. Assert that a call to _get_value() returns _NOT_FOUND.
""" """
self.assertIs(_get_item(obj, key), _NOT_FOUND) self.assertIs(_get_value(item, key), _NOT_FOUND)
### Case: obj is a dictionary. ### Case: the item is a dictionary.
def test_dictionary__key_present(self): def test_dictionary__key_present(self):
""" """
Test getting a key from a dictionary. Test getting a key from a dictionary.
""" """
obj = {"foo": "bar"} item = {"foo": "bar"}
self.assertEquals(_get_item(obj, "foo"), "bar") self.assertEquals(_get_value(item, "foo"), "bar")
def test_dictionary__callable_not_called(self): def test_dictionary__callable_not_called(self):
""" """
...@@ -77,98 +78,135 @@ class GetItemTestCase(TestCase): ...@@ -77,98 +78,135 @@ class GetItemTestCase(TestCase):
def foo_callable(self): def foo_callable(self):
return "bar" return "bar"
obj = {"foo": foo_callable} item = {"foo": foo_callable}
self.assertNotEquals(_get_item(obj, "foo"), "bar") self.assertNotEquals(_get_value(item, "foo"), "bar")
self.assertTrue(_get_item(obj, "foo") is foo_callable) self.assertTrue(_get_value(item, "foo") is foo_callable)
def test_dictionary__key_missing(self): def test_dictionary__key_missing(self):
""" """
Test getting a missing key from a dictionary. Test getting a missing key from a dictionary.
""" """
obj = {} item = {}
self.assertNotFound(obj, "missing") self.assertNotFound(item, "missing")
def test_dictionary__attributes_not_checked(self): def test_dictionary__attributes_not_checked(self):
""" """
Test that dictionary attributes are not checked. Test that dictionary attributes are not checked.
""" """
obj = {} item = {}
attr_name = "keys" attr_name = "keys"
self.assertEquals(getattr(obj, attr_name)(), []) self.assertEquals(getattr(item, attr_name)(), [])
self.assertNotFound(obj, attr_name) self.assertNotFound(item, attr_name)
### Case: obj does not implement __getitem__(). def test_dictionary__dict_subclass(self):
"""
Test that subclasses of dict are treated as dictionaries.
"""
class DictSubclass(dict): pass
item = DictSubclass()
item["foo"] = "bar"
self.assertEquals(_get_value(item, "foo"), "bar")
### Case: the item is an object.
def test_object__attribute_present(self): def test_object__attribute_present(self):
""" """
Test getting an attribute from an object. Test getting an attribute from an object.
""" """
obj = SimpleObject() item = SimpleObject()
self.assertEquals(_get_item(obj, "foo"), "bar") self.assertEquals(_get_value(item, "foo"), "bar")
def test_object__attribute_missing(self): def test_object__attribute_missing(self):
""" """
Test getting a missing attribute from an object. Test getting a missing attribute from an object.
""" """
obj = SimpleObject() item = SimpleObject()
self.assertNotFound(obj, "missing") self.assertNotFound(item, "missing")
def test_object__attribute_is_callable(self): def test_object__attribute_is_callable(self):
""" """
Test getting a callable attribute from an object. Test getting a callable attribute from an object.
""" """
obj = SimpleObject() item = SimpleObject()
self.assertEquals(_get_item(obj, "foo_callable"), "called...") self.assertEquals(_get_value(item, "foo_callable"), "called...")
### Case: obj implements __getitem__() (i.e. a "mapping object"). def test_object__non_built_in_type(self):
"""
Test getting an attribute from an instance of a type that isn't built-in.
def test_mapping__key_present(self):
""" """
Test getting a key from a mapping object. item = datetime(2012, 1, 2)
self.assertEquals(_get_value(item, "day"), 2)
def test_object__dict_like(self):
""" """
obj = MappingObject() Test getting a key from a dict-like object (an object that implements '__getitem__').
self.assertEquals(_get_item(obj, "foo"), "bar")
def test_mapping__key_missing(self):
""" """
Test getting a missing key from a mapping object. item = DictLike()
self.assertEquals(item["foo"], "bar")
self.assertNotFound(item, "foo")
### Case: the item is an instance of a built-in type.
def test_built_in_type__integer(self):
""" """
obj = MappingObject() Test getting from an integer.
self.assertNotFound(obj, "missing")
def test_mapping__get_attribute(self):
""" """
Test getting an attribute from a mapping object. class MyInt(int): pass
item1 = MyInt(10)
item2 = 10
self.assertEquals(item1.real, 10)
self.assertEquals(item2.real, 10)
self.assertEquals(_get_value(item1, 'real'), 10)
self.assertNotFound(item2, 'real')
def test_built_in_type__string(self):
""" """
obj = MappingObject() Test getting from a string.
key = "fuzz"
self.assertEquals(getattr(obj, key), "buzz")
# As desired, __getitem__()'s presence causes obj.fuzz not to be checked.
self.assertNotFound(obj, key)
def test_mapping_object__not_implementing_contains(self):
""" """
Test querying a mapping object that doesn't define __contains__(). class MyStr(str): pass
item1 = MyStr('abc')
item2 = 'abc'
self.assertEquals(item1.upper(), 'ABC')
self.assertEquals(item2.upper(), 'ABC')
self.assertEquals(_get_value(item1, 'upper'), 'ABC')
self.assertNotFound(item2, 'upper')
def test_built_in_type__list(self):
""" """
class Sample(object): Test getting from a list.
def __getitem__(self, key): """
return "bar" class MyList(list): pass
item1 = MyList([1, 2, 3])
item2 = [1, 2, 3]
self.assertEquals(item1.pop(), 3)
self.assertEquals(item2.pop(), 3)
obj = Sample() self.assertEquals(_get_value(item1, 'pop'), 2)
self.assertRaises(AttributeError, _get_item, obj, "foo") self.assertNotFound(item2, 'pop')
class ContextTests(TestCase): class ContextTests(unittest.TestCase, AssertIsMixin):
""" """
Test the Context class. Test the Context class.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment