Commits

Kumar McMillan committed 7219a14

too many calls now raises an exception with remember_order()

  • Participants
  • Parent commits b0e9aca

Comments (0)

Files changed (3)

docs/using-fudge.rst

     >>> session.set_count(5)
     Traceback (most recent call last):
     ...
-    AssertionError: Call #1 was fake:session.set_count(5); Expected: #1 fake:session.get_count()[0], #2 fake:session.set_count(5), #3 fake:session.get_count()[1]
+    AssertionError: Call #1 was fake:session.set_count(5); Expected: #1 fake:session.get_count()[0], #2 fake:session.set_count(5), #3 fake:session.get_count()[1], end
     >>> fudge.clear_expectations()
     
 .. _Nose: http://somethingaboutorange.com/mrl/projects/nose/

fudge/__init__.py

     
     __str__ = __repr__
     
+    def _repr_call_list(self, call_list):
+        if not len(call_list):
+            return "no calls"
+        else:
+            stack = ["#%s %r" % (i+1,c) for i,c in enumerate(call_list)]
+            stack.append("end")
+            return ", ".join(stack)
+    
     def add_expected_call(self, call):
         self._call_order.append(call)
     
         self._actual_calls.append(call)
     
     def assert_order_met(self, finalize=False):
+        """assert that calls have been made in the right order."""
         error = None
-        if len(self._actual_calls) == 0:
+        actual_call_len = len(self._actual_calls)
+        expected_call_len = len(self._call_order)
+        
+        if actual_call_len == 0:
             error = "Not enough calls were made"
         else:
             for i,call in enumerate(self._call_order):
-                if len(self._actual_calls) < i+1:
+                if actual_call_len < i+1:
                     if not finalize:
                         # we're not done asserting calls so 
                         # forget about not having enough calls
                 if ac_call is not call:
                     error = "Call #%s was %r" % (i+1, ac_call)
                     break
+            
+            if not error:
+                if actual_call_len > expected_call_len:
+                    # only show the first extra call since this 
+                    # will be triggered before all calls are finished:
+                    error = "#%s %s was unexpected" % (
+                        expected_call_len+1,
+                        self._actual_calls[expected_call_len]
+                    )
+                
         if error:
             msg = "%s; Expected: %s" % (
-                    error, 
-                    ", ".join(["#%s %r" % (i+1,c) for i,c in enumerate(self._call_order)]))
+                    error, self._repr_call_list(self._call_order))
             raise AssertionError(msg)
     
     def reset_calls(self):
             >>> db.update()
             Traceback (most recent call last):
             ...
-            AssertionError: Call #1 was fake:db.update(); Expected: #1 fake:db.insert(), #2 fake:db.update()
+            AssertionError: Call #1 was fake:db.update(); Expected: #1 fake:db.insert(), #2 fake:db.update(), end
             >>> fudge.clear_expectations()
         
         When declaring multiple calls using :func:`fudge.Fake.next_call`, each subsequent call will be added 

fudge/tests/test_fudge.py

     def test_clear_calls_resets_call_order(self):
         exp_order = ExpectedCallOrder(self.fake)
         exp = ExpectedCall(self.fake, 'callMe', call_order=exp_order)
+        exp_order.add_expected_call(exp)
         self.reg.remember_expected_call_order(exp_order)
         
         exp()
     def test_verify_resets_call_order(self):
         exp_order = ExpectedCallOrder(self.fake)
         exp = ExpectedCall(self.fake, 'callMe', call_order=exp_order)
+        exp_order.add_expected_call(exp)
         self.reg.remember_expected_call_order(exp_order)
         
         exp()
         # two() not called but assertion is not finalized:
         call_order.assert_order_met(finalize=False)
     
-    def test_multiple_return_values_are_ignored(self):
+    def test_multiple_returns_affect_order(self):
         db = Fake("db")\
             .remember_order()\
             .expects("get_id").returns(1)\
         eq_(db.get_id(), 1)
         eq_(db.set_id(), None)
         eq_(db.get_id(), 2)
-        
         fudge.verify()
-        
+    
+    @raises(AssertionError)
+    def test_too_many_calls(self):
+        db = Fake("db")\
+            .remember_order()\
+            .expects("get_id").returns(1)\
+            .expects("set_id")
+        eq_(db.get_id(), 1)
+        eq_(db.set_id(), None)
+        # extra :
+        eq_(db.get_id(), 1)