Commits

David Villa Alises committed db5a70e

- wished features in wishes.py
- demo.py to show expectation reports

Comments (0)

Files changed (5)

+../pydoubles-carlosble/report-test.py

pyDoubles/core.py

 Defined expectations were not satisfied.
 Registered calls are:
 %s
-But expectations are:
+But expected invocation is:
    %s(%s)%s -> None'''
 
         return template % (
             args_str,
             self._readable_kwargs(kwargs_str))
 
-#        return "RECORDED calls were: << %s >>, \n EXPECTED call is << (args = %s), (keyword args = %s) >>" % (
-#            self.pool_render.render_invocations_to_method(invocation.method_name),
-##                str(self.report.method_info(invocation.method_name)),
-#                args_str, self._readable_kwargs(kwargs_str))
-
     def _some_call_matches_this_assertion(self, expected_invocation):
         matching_invocations = self.method_pool.matching_invocations_by_args(
             expected_invocation)

pyDoubles/framework.py

 Defined expectations were not satisfied.
 This call was not expected:
    %s
-But expectations are:
+Expectations were:
 %s'''
 
         invocation = MethodInvocation(self.invoked_method_name, args, kwargs)
 Defined expectations were not satisfied.
 Registered calls are:
 %s
-But expectations are:
+But expectations were:
 %s'''
 
         if not self.stubs.repositories_are_equivalent(self.satisfied_expectations):

pyDoublesTests/reports.py

 
 from unittest import TestCase
 
+from nose.tools import nottest
+
 from pyDoubles.framework import empty_spy, empty_mock
 from pyDoubles.framework import assert_that_was_called, expect_call
 from pyDoubles.framework import ArgsDontMatch, UnexpectedBehavior
+from pyDoubles.framework import MethodInvocation
+from pyDoubles.core import _MethodPool_, PoolRender, ANY_ARG
 
 
-def create_report(template, given_invocations, expectations):
-    def format(calls):
-        if not calls:
-            return "   No one"
-        return '   ' + str.join('\n   ', calls)
+class PoolRenderTest(TestCase):
+    def setUp(self):
+        self.pool = _MethodPool_()
+        self.render = PoolRender(self.pool, indent=0)
 
-    given_invocations = given_invocations or []
-    expectations = expectations or []
+    def add_invocation(self, name, args=None, kwargs=None, output=None):
+        self.pool.add_invocation(
+            MethodInvocation(name, args, kwargs, output))
 
-    message = template % (
-        format(given_invocations),
-        format(expectations))
+    def test_int_arg_method_returning_int(self):
+        self.add_invocation('foo', (1,), output=1)
 
-    return message.strip()
+        self.assertEquals(
+            self.render.render_all_invocations(),
+            'foo(1) -> 1')
 
+    @nottest
+    def test_ANY_arg_method_returning_none(self):
+        self.add_invocation('foo', (ANY_ARG,))
 
-def create_missing_invocations_report(registered=None, expectations=None):
-    TEMPLATE = '''
+        self.assertEquals(
+            self.render.render_all_invocations(),
+            'foo(ANY) -> None')
+
+
+class ReportMixin:
+    @classmethod
+    def create_report(cls, template, given_invocations, expectations):
+        def format(calls):
+            if not calls:
+                return "   No one"
+            return '   ' + str.join('\n   ', calls)
+
+        given_invocations = given_invocations or []
+        expectations = expectations or []
+
+        message = template % (
+            format(given_invocations),
+            format(expectations))
+
+        return message.strip()
+
+    @classmethod
+    def create_missing_invocations_report(cls, registered=None, expectations=None):
+        TEMPLATE = '''
 Defined expectations were not satisfied.
 Registered calls are:
 %s
-But expectations are:
+But expectations were:
 %s'''
 
-    return create_report(TEMPLATE, registered, expectations)
+        return cls.create_report(TEMPLATE, registered, expectations)
 
+    @classmethod
+    def create_spy_missing_invocation_report(cls, registered=None, expectation=None):
+        TEMPLATE = '''
+Defined expectations were not satisfied.
+Registered calls are:
+%s
+But expected invocation is:
+%s'''
+        return cls.create_report(TEMPLATE, registered, [expectation])
 
-def create_unexpected_invocation_report(registered=None, expectations=None):
-    TEMPLATE = '''
+    @classmethod
+    def create_unexpected_invocation_report(cls, registered=None, expectations=None):
+        TEMPLATE = '''
 Defined expectations were not satisfied.
 This call was not expected:
 %s
-But expectations are:
+Expectations were:
 %s'''
 
-    return create_report(TEMPLATE, registered, expectations)
+        return cls.create_report(TEMPLATE, registered, expectations)
 
-
-class ReportMixin:
     def get_exception_message_when_call(self, func):
         with self.assertRaises(UnexpectedBehavior) as cm:
             func()
         self.mock = empty_mock()
 
     def test_expect_none_but_someting_unexpected_called(self):
-        expected_report = create_unexpected_invocation_report(
+        expected_report = self.create_unexpected_invocation_report(
             registered = ["unexpected() -> None"])
 
         actual_report = self.get_exception_message_when_call(
         # then
         self.assertEquals(
             actual_report,
-            create_missing_invocations_report(
+            self.create_missing_invocations_report(
                 expectations = ["foo() -> None"]))
 
     def test_expect_2_void_methods_but_nothing_called(self):
         # then
         self.assertEquals(
             actual_report,
-            create_missing_invocations_report(expectations = [
+            self.create_missing_invocations_report(expectations = [
                     "foo() -> None",
                     "bar() -> None"]))
 
         # then
         self.assertEquals(
             actual_report,
-            create_missing_invocations_report(
+            self.create_missing_invocations_report(
                 expectations = ["foo(1, 2) -> 1"]))
 
     def test_except_method_with_2_str_args_returning_str_but_nothing_called(self):
         # then
         self.assertEquals(
             actual_report,
-            create_missing_invocations_report(
+            self.create_missing_invocations_report(
                 expectations = ["foo('a', 'b') -> 'c'"]))
 
     def test_except_method_with_2_kwargs_returning_dict_but_nothing_called(self):
         actual_report = self.get_mock_report(self.mock)
 
         # then
-        expected_report = create_missing_invocations_report(
+        expected_report = self.create_missing_invocations_report(
             expectations = ["foo(color='red', num=1) -> {'key': 1}"])
 
 #        print actual_report
         foo = ["foo() -> None"]
         bar = ["bar() -> None"]
 
-        expected_report = create_missing_invocations_report(
+        expected_report = self.create_missing_invocations_report(
             registered = foo + bar,
             expectations = 2 * foo + 2 * bar)
 
         self.assertEquals(actual_report, expected_report)
 
 
-class SpyReportTest(TestCase):
+class SpyReportTest(TestCase, ReportMixin):
     def test_1_method(self):
         # given
         spy = empty_spy()
         with self.assertRaises(ArgsDontMatch) as cm:
             assert_that_was_called(spy.foo).with_args(3)
 
-        expected_report = create_missing_invocations_report(
+        expected_report = self.create_spy_missing_invocation_report(
                 registered = ["foo(1) -> None", "foo(2) -> None"],
-                expectations = ["foo(3) -> None"])
+                expectation = "foo(3) -> None")
 
 #        print expected_report
 #        print cm.exception.message
+#!/bin/bash --
+# -*- coding:utf-8; tab-width:4; mode:shell-script -*-
+
+from unittest import TestCase
+
+from pyDoubles.framework import empty_mock, mock
+from pyDoubles.framework import expect_call, UnexpectedBehavior
+
+from unit import Collaborator
+import reports
+
+
+class MockReportTest(TestCase, reports.ReportMixin):
+    def setUp(self):
+        self.mock = mock(Collaborator())
+
+    def test_recorded_call_return_value_is_displayed(self):
+        expected_report = self.create_unexpected_invocation_report(
+            registered = ["hello() -> 'hello'"])
+
+        actual_report = self.get_exception_message_when_call(
+            self.mock.hello)
+
+        self.assertEquals(actual_report, expected_report)
+
+
+class MockTests(TestCase):
+    def setUp(self):
+        self.mock = mock(Collaborator())
+
+    def test_check_kwargs_keys(self):
+        with self.assertRaises(WrongSignature):
+            expect_call(self.mock.kwarg_method).with_args(wrong_key=True)
+
+    def test_missing_method_expectation(self):
+        with self.assertRaises(MissingMethod):
+            self.mock.wrong()
+
+    def test_invocation_order_different_methods_does_not_matter(self):
+        expect_call(self.mock.hello)
+        expect_call(self.mock.something)
+
+        self.mock.something()
+        self.mock.hello()
+
+        self.mock.assert_expectations()
+
+    def test_invocation_order_same_method_does_not_matter(self):
+        expect_call(self.mock.one_arg_method).with_args(1)
+        expect_call(self.mock.one_arg_method).with_args(2)
+
+        self.mock.one_arg_method(2)
+        self.mock.one_arg_method(1)
+
+        self.mock.assert_expectations()
+
+    def test_invocation_order_same_method_different_kwargs_matters(self):
+        expect_call(self.mock.kwarg_method).with_args(key_param=1)
+        expect_call(self.mock.kwarg_method).with_args(key_param=2)
+
+        self.mock.kwarg_method(key_param=2)
+        self.mock.kwarg_method(key_param=1)
+
+        self.mock.assert_expectations()
+
+
+class StrictOrderMock(TestCase):
+    def setUp(self):
+        self.mock = strict_mock(Collaborator())
+
+    def test_invocation_order_different_methods_matters(self):
+        expect_call(self.mock.hello)
+        expect_call(self.mock.something)
+
+        self.mock.something()
+        self.mock.hello()
+
+        self.failUnlessRaises(UnexpectedBehavior,
+            self.mock.assert_expectations)
+
+    def test_invocation_order_same_method_different_args_matters(self):
+        expect_call(self.mock.one_arg_method).with_args(1)
+        expect_call(self.mock.one_arg_method).with_args(2)
+
+        self.mock.one_arg_method(2)
+        self.mock.one_arg_method(1)
+
+        self.failUnlessRaises(UnexpectedBehavior,
+            self.mock.assert_expectations)
+
+    def test_invocation_order_same_method_different_kwargs_matters(self):
+        expect_call(self.mock.kwarg_method).with_args(key_param=1)
+        expect_call(self.mock.kwarg_method).with_args(key_param=2)
+
+        self.mock.kwarg_method(key_param=2)
+        self.mock.kwarg_method(key_param=1)
+
+        self.failUnlessRaises(UnexpectedBehavior,
+            self.mock.assert_expectations)