Example #1
0
    def test_dictionary__callable_not_called(self):
        """
        Test that callable values are returned as-is (and in particular not called).

        """
        def foo_callable(self):
            return "bar"

        item = {"foo": foo_callable}
        self.assertNotEqual(_get_value(item, "foo"), "bar")
        self.assertTrue(_get_value(item, "foo") is foo_callable)
Example #2
0
    def test_dictionary__callable_not_called(self):
        """
        Test that callable values are returned as-is (and in particular not called).

        """
        def foo_callable(self):
            return "bar"

        item = {"foo": foo_callable}
        self.assertNotEquals(_get_value(item, "foo"), "bar")
        self.assertTrue(_get_value(item, "foo") is foo_callable)
Example #3
0
    def test_built_in_type__integer(self):
        """
        Test getting from an integer.

        """
        class MyInt(int): pass

        item1 = MyInt(10)
        item2 = 10

        try:
            item2.real
        except AttributeError:
            # Then skip this unit test.  The numeric type hierarchy was
            # added only in Python 2.6, in which case integers inherit
            # from complex numbers the "real" attribute, etc:
            #
            #   http://docs.python.org/library/numbers.html
            #
            return

        self.assertEquals(item1.real, 10)
        self.assertEquals(item2.real, 10)

        self.assertEquals(_get_value(item1, 'real'), 10)
        self.assertNotFound(item2, 'real')
Example #4
0
    def test_dictionary__key_present(self):
        """
        Test getting a key from a dictionary.

        """
        item = {"foo": "bar"}
        self.assertEqual(_get_value(item, "foo"), "bar")
Example #5
0
    def test_object__attribute_is_callable(self):
        """
        Test getting a callable attribute from an object.

        """
        item = SimpleObject()
        self.assertEqual(_get_value(item, "foo_callable"), "called...")
Example #6
0
    def test_dictionary__key_present(self):
        """
        Test getting a key from a dictionary.

        """
        item = {"foo": "bar"}
        self.assertEquals(_get_value(item, "foo"), "bar")
Example #7
0
    def test_object__non_built_in_type(self):
        """
        Test getting an attribute from an instance of a type that isn't built-in.

        """
        item = datetime(2012, 1, 2)
        self.assertEquals(_get_value(item, "day"), 2)
Example #8
0
    def test_object__attribute_present(self):
        """
        Test getting an attribute from an object.

        """
        item = SimpleObject()
        self.assertEquals(_get_value(item, "foo"), "bar")
Example #9
0
    def test_object__attribute_is_callable(self):
        """
        Test getting a callable attribute from an object.

        """
        item = SimpleObject()
        self.assertEquals(_get_value(item, "foo_callable"), "called...")
Example #10
0
    def test_object__attribute_present(self):
        """
        Test getting an attribute from an object.

        """
        item = SimpleObject()
        self.assertEqual(_get_value(item, "foo"), "bar")
Example #11
0
    def test_object__non_built_in_type(self):
        """
        Test getting an attribute from an instance of a type that isn't built-in.

        """
        item = datetime(2012, 1, 2)
        self.assertEqual(_get_value(item, "day"), 2)
Example #12
0
    def test_dictionary__mapping_implementation(self):
        """
        Test that registered implementations of collections.Mapping are treated as dictionaries.

        See https://github.com/defunkt/pystache/pull/144

        """
        class MappingSubclass(collections.Mapping):
            def __init__(self, *args, **kwargs):
                self._mapping = dict(*args, **kwargs)
                super(MappingSubclass, self).__init__()

            def __getitem__(self, key):
                return self._mapping[key]

            def __iter__(self):
                return iter(self._mapping)

            def __len__(self):
                return len(self._mapping)

        class RegisteredMapping(object):
            def __init__(self, *args, **kwargs):
                self._mapping = dict(*args, **kwargs)
                super(RegisteredMapping, self).__init__()

            def __getitem__(self, key):
                return self._mapping[key]

            def __iter__(self):
                return iter(self._mapping)

            def __len__(self):
                return len(self._mapping)

        collections.Mapping.register(RegisteredMapping)

        item = MappingSubclass(foo="bar")
        self.assertEqual(_get_value(item, "foo"), "bar")

        item = RegisteredMapping(foo="bar")
        self.assertEqual(_get_value(item, "foo"), "bar")
Example #13
0
    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.assertEqual(_get_value(item, "foo"), "bar")
Example #14
0
    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.assertEqual(_get_value(item, "foo"), "bar")
Example #15
0
    def test_built_in_type__list(self):
        """
        Test getting from a list.

        """
        class MyList(list): pass

        item1 = MyList([1, 2, 3])
        item2 = [1, 2, 3]

        self.assertEqual(item1.pop(), 3)
        self.assertEqual(item2.pop(), 3)

        self.assertEqual(_get_value(item1, 'pop'), 2)
        self.assertNotFound(item2, 'pop')

        # get list items by index
        self.assertEqual(_get_value(item2, '0'), 1)

        # Don't throw errors if we pass a non-int to a list.
        self.assertNotFound(item2, 'numberone')
Example #16
0
    def test_built_in_type__list(self):
        """
        Test getting from a list.

        """
        class MyList(list):
            pass

        item1 = MyList([1, 2, 3])
        item2 = [1, 2, 3]

        self.assertEqual(item1.pop(), 3)
        self.assertEqual(item2.pop(), 3)

        self.assertEqual(_get_value(item1, 'pop'), 2)
        self.assertNotFound(item2, 'pop')

        # get list items by index
        self.assertEqual(_get_value(item2, '0'), 1)

        # Don't throw errors if we pass a non-int to a list.
        self.assertNotFound(item2, 'numberone')
Example #17
0
    def test_built_in_type__list(self):
        """
        Test getting from a list.

        """
        class MyList(list): pass

        item1 = MyList([1, 2, 3])
        item2 = [1, 2, 3]

        self.assertEqual(item1.pop(), 3)
        self.assertEqual(item2.pop(), 3)

        self.assertEqual(_get_value(item1, 'pop'), 2)
        self.assertNotFound(item2, 'pop')
Example #18
0
    def test_built_in_type__string(self):
        """
        Test getting from a string.

        """
        class MyStr(str): pass

        item1 = MyStr('abc')
        item2 = 'abc'

        self.assertEqual(item1.upper(), 'ABC')
        self.assertEqual(item2.upper(), 'ABC')

        self.assertEqual(_get_value(item1, 'upper'), 'ABC')
        self.assertNotFound(item2, 'upper')
Example #19
0
    def test_built_in_type__integer(self):
        """
        Test getting from an integer.

        """
        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')
Example #20
0
    def test_built_in_type__list(self):
        """
        Test getting from a list.

        """
        class MyList(list): pass

        item1 = MyList([1, 2, 3])
        item2 = [1, 2, 3]

        self.assertEqual(item1.pop(), 3)
        self.assertEqual(item2.pop(), 3)

        self.assertEqual(_get_value(item1, 'pop'), 2)
        self.assertNotFound(item2, 'pop')
Example #21
0
    def test_built_in_type__string(self):
        """
        Test getting from a string.

        """
        class MyStr(str): pass

        item1 = MyStr('abc')
        item2 = 'abc'

        self.assertEqual(item1.upper(), 'ABC')
        self.assertEqual(item2.upper(), 'ABC')

        self.assertEqual(_get_value(item1, 'upper'), 'ABC')
        self.assertNotFound(item2, 'upper')
Example #22
0
    def test_built_in_type__integer(self):
        """
        Test getting from an integer.

        """
        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')
Example #23
0
    def test_built_in_type__string(self):
        """
        Test getting from a string.

        """

        class MyStr(str):
            pass

        item1 = MyStr("abc")
        item2 = "abc"

        self.assertEqual(item1.upper(), "ABC")
        self.assertEqual(item2.upper(), "ABC")

        self.assertEqual(_get_value(item1, "upper"), "ABC")
        self.assertNotFound(item2, "upper")
Example #24
0
    def test_object__property__raising_exception(self):
        """
        Test getting a property that raises an exception.

        """
        class Foo(object):
            @property
            def bar(self):
                return 1

            @property
            def baz(self):
                raise ValueError("test")

        foo = Foo()
        self.assertEqual(_get_value(foo, 'bar'), 1)
        self.assertNotFound(foo, 'missing')
        self.assertRaises(ValueError, _get_value, foo, 'baz')
Example #25
0
    def test_object__property__raising_exception(self):
        """
        Test getting a property that raises an exception.

        """
        class Foo(object):

            @property
            def bar(self):
                return 1

            @property
            def baz(self):
                raise ValueError("test")

        foo = Foo()
        self.assertEqual(_get_value(foo, 'bar'), 1)
        self.assertNotFound(foo, 'missing')
        self.assertRaises(ValueError, _get_value, foo, 'baz')
Example #26
0
    def test_built_in_type__integer(self):
        """
        Test getting from an integer.

        """
        class MyInt(int): pass

        cust_int = MyInt(10)
        pure_int = 10

        # We have to use a built-in method like __neg__ because "public"
        # attributes like "real" were not added to Python until Python 2.6,
        # when the numeric type hierarchy was added:
        #
        #   http://docs.python.org/library/numbers.html
        #
        self.assertEqual(cust_int.__neg__(), -10)
        self.assertEqual(pure_int.__neg__(), -10)

        self.assertEqual(_get_value(cust_int, '__neg__'), -10)
        self.assertNotFound(pure_int, '__neg__')
Example #27
0
    def test_built_in_type__integer(self):
        """
        Test getting from an integer.

        """
        class MyInt(int): pass

        cust_int = MyInt(10)
        pure_int = 10

        # We have to use a built-in method like __neg__ because "public"
        # attributes like "real" were not added to Python until Python 2.6,
        # when the numeric type hierarchy was added:
        #
        #   http://docs.python.org/library/numbers.html
        #
        self.assertEqual(cust_int.__neg__(), -10)
        self.assertEqual(pure_int.__neg__(), -10)

        self.assertEqual(_get_value(cust_int, '__neg__'), -10)
        self.assertNotFound(pure_int, '__neg__')
Example #28
0
    def assertNotFound(self, item, key):
        """
        Assert that a call to _get_value() returns _NOT_FOUND.

        """
        self.assertIs(_get_value(item, key), _NOT_FOUND)
Example #29
0
    def assertNotFound(self, item, key):
        """
        Assert that a call to _get_value() returns _NOT_FOUND.

        """
        self.assertIs(_get_value(item, key), _NOT_FOUND)