summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2007-12-09 13:53:01 -0200
committerGustavo Niemeyer <gustavo@niemeyer.net>2007-12-09 13:53:01 -0200
commit195f0d884b7c4ad16271a9b0f17e6cecfbfba169 (patch)
treeada8a38956aa3416568a5832af046701efd88a63
parent7ba4e6a4eff5896808e7b95e84744cd97a963c07 (diff)
downloadmocker-195f0d884b7c4ad16271a9b0f17e6cecfbfba169.tar.gz
When the expression executed isn't exactly the same as the
recorded events (e.g. when parameter matchers are used), show in the error message the real expression run, to aid in debugging.
-rw-r--r--NEWS4
-rw-r--r--mocker.py20
-rwxr-xr-xtest.py26
3 files changed, 43 insertions, 7 deletions
diff --git a/NEWS b/NEWS
index a852ca6..e735ebe 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,10 @@
- Fixed problem when requesting order on similar expressions. The
second expression might not be accepted.
+- When the expression executed isn't exactly the same as the
+ recorded events (e.g. when parameter matchers are used), show in
+ the error message the real expression run, to aid in debugging.
+
0.9.3 (2007-11-24)
==================
diff --git a/mocker.py b/mocker.py
index 0ee163b..813c0b8 100644
--- a/mocker.py
+++ b/mocker.py
@@ -684,13 +684,17 @@ class MockerBase(object):
recorder(self, event)
return Mock(self, path)
else:
- # First run unsatisfied events, then ones not previously run.
- events = [(event.satisfied()*2 + event.has_run(), event)
- for event in self._events]
- events.sort()
- for key, event in events:
- if event.matches(path):
- return event.run(path)
+ # First run unsatisfied events, then ones not previously run. We
+ # put the index in the ordering tuple instead of the actual event
+ # because we want a stable sort (ordering between 2 events is
+ # undefined).
+ events = self._events
+ order = [(events[i].satisfied()*2 + events[i].has_run(), i)
+ for i in range(len(events))]
+ order.sort()
+ for weight, i in order:
+ if events[i].matches(path):
+ return events[i].run(path)
raise MatchError(ERROR_PREFIX + "Unexpected expression: %s" % path)
def get_recorders(cls, self):
@@ -1505,6 +1509,8 @@ class Event(object):
result = task_result
if errors:
message = [str(self.path)]
+ if str(path) != message[0]:
+ message.append("- Run: %s" % path)
for error in errors:
lines = error.splitlines()
message.append("- " + lines.pop(0))
diff --git a/test.py b/test.py
index 10285aa..ca6e097 100755
--- a/test.py
+++ b/test.py
@@ -2624,6 +2624,31 @@ class EventTest(TestCase):
self.assertEquals(calls, [42, 42, 42])
def test_run_errors(self):
+ """When the path representation isn't the same it's shown up."""
+ class MyTask(object):
+ def __init__(self, id, failed):
+ self.id = id
+ self.failed = failed
+ def run(self, path):
+ if self.failed:
+ raise AssertionError("%d failed" % self.id)
+ event = Event("i.am.a.path")
+ event.add_task(MyTask(1, True))
+ event.add_task(MyTask(2, False))
+ event.add_task(MyTask(3, True))
+
+ try:
+ event.run("i.am.a.path")
+ except AssertionError, e:
+ message = os.linesep.join(["i.am.a.path",
+ "- 1 failed",
+ "- 3 failed"])
+ self.assertEquals(str(e), message)
+ else:
+ self.fail("AssertionError not raised")
+
+ def test_run_errors_with_different_path_representation(self):
+ """When the path representation isn't the same it's shown up."""
class MyTask(object):
def __init__(self, id, failed):
self.id = id
@@ -2640,6 +2665,7 @@ class EventTest(TestCase):
event.run(42)
except AssertionError, e:
message = os.linesep.join(["i.am.a.path",
+ "- Run: 42", # <==
"- 1 failed",
"- 3 failed"])
self.assertEquals(str(e), message)