Commit 33b26577 by Chris Jerdonek

Implemented (and tested) Context.top() and Context.copy().

parent 7a7d886e
...@@ -137,8 +137,29 @@ class Context(object): ...@@ -137,8 +137,29 @@ class Context(object):
return default return default
def push(self, item): def push(self, item):
"""
Push an item onto the stack.
"""
self._stack.append(item) self._stack.append(item)
def pop(self): def pop(self):
"""
Pop an item off of the stack, and return it.
"""
return self._stack.pop() return self._stack.pop()
def top(self):
"""
Return the item last added to the stack.
"""
return self._stack[-1]
def copy(self):
"""
Return a copy of this instance.
"""
return Context(*self._stack)
...@@ -242,3 +242,27 @@ class ContextTestCase(TestCase): ...@@ -242,3 +242,27 @@ class ContextTestCase(TestCase):
self.assertEquals(item, {"foo": "buzz"}) self.assertEquals(item, {"foo": "buzz"})
self.assertEquals(context.get(key), "bar") self.assertEquals(context.get(key), "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_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")
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