summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mocker.py12
-rwxr-xr-xtest.py18
2 files changed, 30 insertions, 0 deletions
diff --git a/mocker.py b/mocker.py
index 8523b21..9463702 100644
--- a/mocker.py
+++ b/mocker.py
@@ -562,6 +562,10 @@ class MockerBase(object):
self.call(lambda *args, **kwargs: value)
def generate(self, sequence):
+ """Last recorded event will return a generator with the given sequence.
+
+ @param sequence: Sequence of values to be generated.
+ """
def generate(*args, **kwargs):
for value in sequence:
yield value
@@ -880,6 +884,9 @@ class Mock(object):
def __setitem__(self, key, value):
return self.__mocker_act__("setitem", (key, value))
+ def __delitem__(self, key):
+ return self.__mocker_act__("delitem", (key,))
+
def __len__(self):
# MatchError is turned on an AttributeError so that list() and
# friends act properly when trying to get length hints on
@@ -991,6 +998,9 @@ class Action(object):
result = object[self.args[0]]
elif kind == "setitem":
result = object[self.args[0]] = self.args[1]
+ elif kind == "delitem":
+ del object[self.args[0]]
+ result = None
elif kind == "len":
result = len(object)
elif kind == "nonzero":
@@ -1080,6 +1090,8 @@ class Path(object):
elif action.kind == "setitem":
result = "%s[%r] = %r" % (result, action.args[0],
action.args[1])
+ elif action.kind == "delitem":
+ result = "del %s[%r]" % (result, action.args[0])
elif action.kind == "len":
result = "len(%s)" % result
elif action.kind == "nonzero":
diff --git a/test.py b/test.py
index 2d9475a..19b132b 100755
--- a/test.py
+++ b/test.py
@@ -1264,6 +1264,12 @@ class ActionTest(unittest.TestCase):
action.execute(obj)
self.assertEquals(obj, {"a": 1})
+ def test_execute_delitem(self):
+ obj = {"a": 1, "b": 2}
+ action = Action("delitem", ("a",), {})
+ action.execute(obj)
+ self.assertEquals(obj, {"b": 2})
+
def test_execute_len(self):
obj = [1, 2, 3]
action = Action("len", (), {})
@@ -1540,6 +1546,10 @@ class PathTest(unittest.TestCase):
path = Path(self.mock, None, [Action("setitem", ("key", "value"), {})])
self.assertEquals(str(path), "obj['key'] = 'value'")
+ def test_str_delitem(self):
+ path = Path(self.mock, None, [Action("delitem", ("key",), {})])
+ self.assertEquals(str(path), "del obj['key']")
+
def test_str_len(self):
path = Path(self.mock, None, [Action("len", (), {})])
self.assertEquals(str(path), "len(obj)")
@@ -1869,6 +1879,14 @@ class MockTest(unittest.TestCase):
self.assertEquals(path, self.mock.__mocker_path__ +
Action("setitem", ("key", "value"), {}))
+ def test_delitem(self):
+ del self.mock["key"]
+ (path,) = self.paths
+ self.assertEquals(type(path), Path)
+ self.assertTrue(path.parent_path is self.mock.__mocker_path__)
+ self.assertEquals(path, self.mock.__mocker_path__ +
+ Action("delitem", ("key",), {}))
+
def test_len(self):
self.assertEquals(len(self.mock), 42)
(path,) = self.paths