def test__str(self): context = Context() self.assertEquals(str(context), 'Context()') context = Context({'foo': 'bar'}) self.assertEquals(str(context), "Context({'foo': 'bar'},)") context = Context({'foo': 'bar'}, {'abc': 123}) self.assertEquals(str(context), "Context({'foo': 'bar'}, {'abc': 123})")
def test_get__fallback(self): """ Check that first-added stack items are queried on context misses. """ context = Context({"fuzz": "buzz"}, {"foo": "bar"}) self.assertEquals(context.get("fuzz"), "buzz")
def test_get__precedence(self): """ Test that get() respects the order of precedence (later items first). """ context = Context({"foo": "bar"}, {"foo": "buzz"}) self.assertEquals(context.get("foo"), "buzz")
def test_get__default(self): """ Test that get() respects the default value . """ context = Context() self.assertEquals(context.get("foo", "bar"), "bar")
def test_get__key_missing(self): """ Test getting a missing key. """ context = Context() self.assertTrue(context.get("foo") is None)
def test_get__key_present(self): """ Test getting a key. """ context = Context({"foo": "bar"}) self.assertEquals(context.get("foo"), "bar")
def test_create__context(self): """ Test passing a Context instance. """ obj = Context({'foo': 'bar'}) context = Context.create(obj) self.assertEquals(context.get('foo'), 'bar')
def test_top(self): key = "foo" context = Context({key: "bar"}, {key: "buzz"}) self.assertEquals(context.get(key), "buzz") top = context.top() self.assertEquals(top, {"foo": "buzz"}) # Make sure calling top() didn't remove the item from the stack. self.assertEquals(context.get(key), "buzz")
def test_push(self): """ Test push(). """ key = "foo" context = Context({key: "bar"}) self.assertEquals(context.get(key), "bar") context.push({key: "buzz"}) self.assertEquals(context.get(key), "buzz")
def test_pop(self): """ Test pop(). """ key = "foo" context = Context({key: "bar"}, {key: "buzz"}) self.assertEquals(context.get(key), "buzz") item = context.pop() self.assertEquals(item, {"foo": "buzz"}) self.assertEquals(context.get(key), "bar")
def test_copy(self): key = "foo" original = Context({key: "bar"}, {key: "buzz"}) self.assertEquals(original.get(key), "buzz") new = original.copy() # Confirm that the copy behaves the same. self.assertEquals(new.get(key), "buzz") # Change the copy, and confirm it is changed. new.pop() self.assertEquals(new.get(key), "bar") # Confirm the original is unchanged. self.assertEquals(original.get(key), "buzz")
def _assert_render(self, expected, template, *context, **kwargs): """ Test rendering the given template using the given context. """ partials = kwargs.get('partials') engine = kwargs.get('engine', self._engine()) if partials is not None: engine.load_partial = lambda key: unicode(partials[key]) context = Context(*context) actual = engine.render(template, context) assert_strings(test_case=self, actual=actual, expected=expected)
def test_init__many_elements(self): """ Check that passing more than two items to __init__() raises no exception. """ context = Context({}, {}, {})
def test_init__no_elements(self): """ Check that passing nothing to __init__() raises no exception. """ context = Context()