Thread: [Mockpp-commits] mockpp/mockpp/tests ChainableMockObjectPolymorphism_test.cpp,1.2,1.3
Brought to you by:
ewald-arnold
From: Ewald A. <ewa...@us...> - 2005-10-02 21:45:21
|
Update of /cvsroot/mockpp/mockpp/mockpp/tests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32089/mockpp/tests Modified Files: ChainableMockObjectPolymorphism_test.cpp Log Message: Index: ChainableMockObjectPolymorphism_test.cpp =================================================================== RCS file: /cvsroot/mockpp/mockpp/mockpp/tests/ChainableMockObjectPolymorphism_test.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- ChainableMockObjectPolymorphism_test.cpp 26 Sep 2005 18:24:55 -0000 1.2 +++ ChainableMockObjectPolymorphism_test.cpp 1 Oct 2005 16:24:35 -0000 1.3 @@ -1,217 +1,262 @@ -/*************************************************************************** - ChainableMockObjectPolymorphism_test.cpp - - - unit tests for MockVisitor class and polymorphic parameter methods - ------------------- - begin : Tue Sep 20 2004 - copyright : (C) 2005 contributed by Mathieu Champlon - email : mockpp at ewald-arnold dot de - - $Id$ - - ***************************************************************************/ - -/************************************************************************** - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2 of the License, - * or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - ***************************************************************************/ - -#define MOCKPP_ENABLE_DEFAULT_FORMATTER - -#include <mockpp/mockpp.h> // always first - -#ifndef HAVE_CPPUNIT - -# warning CppUnit not available at compile time - -#else - -#include <mockpp/ChainableMockObject.h> - -#include <mockpp/chaining/ChainingMockObjectSupport.h> -#include <mockpp/chaining/Invocation.h> - -#include <mockpp/constraint/OutBound.h> - -#include <cppunit/extensions/HelperMacros.h> - - -class MockVisitorPolymorphism_test : public CppUnit::TestFixture -{ - public: - - CPPUNIT_TEST_SUITE( MockVisitorPolymorphism_test ); - - CPPUNIT_TEST(test_visitor_retrieves_parameter1); - CPPUNIT_TEST(test_visitor_retrieves_parameter2); - - CPPUNIT_TEST_SUITE_END(); - - public: - - void test_visitor_retrieves_parameter1(); - void test_visitor_retrieves_parameter2(); -}; - - -CPPUNIT_TEST_SUITE_REGISTRATION(MockVisitorPolymorphism_test); - -// let's suppose we have an interface defined like this : -class Parameter -{ -public: - - // this method won't be part of the test - // it's only to show that this class is not a simple value object - virtual void doSomething() = 0; -}; - -// this interface will be mocked for the test -class Visitor -{ -public: - - virtual void visit( const Parameter & ) = 0; -}; - -// this is the class under test -class UnderTest -{ -public: - - // it takes a Parameter as a configuration component - UnderTest( const Parameter ¶meter ) - : parameter_( parameter ) - {} - - // this is the method we are about to test - void accept( Visitor &visitor ) - { - visitor.visit( parameter_ ); - } - -private: - - const Parameter& parameter_; -}; - - -// we need a mock for the Parameter in order to build an UnderTest instance -class MockParameter1 : public mockpp::ChainableMockObject, - public Parameter -{ - public: - - MockParameter1(const mockpp::String &name) - : mockpp::ChainableMockObject(name, 0) - , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE0(doSomething) - {} - - MOCKPP_VOID_CHAINABLE0(MockParameter1, doSomething); -}; - -// we need a mock for the Parameter in order to build an UnderTest instance -class MockParameter2 : public mockpp::ChainableMockObject, public Parameter -{ -public: - - MockParameter2(const mockpp::String &name) - : mockpp::ChainableMockObject(name, 0) - , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE0(doSomething) - {} - - MOCKPP_VOID_CHAINABLE0(MockParameter2, doSomething); -}; - -// we mock the Visitor in order to call the apply method of UnderTest with it -class MockVisitor1 : public mockpp::ChainableMockObject, - public Visitor -{ - public: - - MockVisitor1(const mockpp::String &name) - : mockpp::ChainableMockObject(name, 0) - , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE1(visit) - {} - - // here is the first problem as Parameter is just an interface - MOCKPP_VOID_CHAINABLE_EXT1(MockVisitor1, visit, const Parameter &, - , Parameter); -}; - - -// we mock the Visitor in order to call the apply method of UnderTest with it -class MockVisitor2 : public mockpp::ChainableMockObject, - public Visitor -{ - public: - - MockVisitor2(const mockpp::String &name) - : mockpp::ChainableMockObject(name, 0) - , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE1(visit) - {} - - // here is the first problem as Parameter is just an interface - MOCKPP_VOID_CHAINABLE_EXT1(MockVisitor2, visit, const Parameter &, - , MockParameter2); -}; - - -void MockVisitorPolymorphism_test::test_visitor_retrieves_parameter1() -{ - // we create our class under test - MockParameter1 mp1(MOCKPP_PCHAR("mock-parameter1")); - UnderTest ut1(mp1); - - // we program the mock visitor as we expect - MockVisitor1 mv(MOCKPP_PCHAR("mock-visitor")); - MOCKPP_CHAINER_FOR(MockVisitor1, visit) chain(&mv); - // here is the second problem as we would like to verify that the - // call is made with precisely 'mp' and nothing else - chain.stubs(mockpp::once()) - .with(mockpp::same<Parameter>(mp1)); - - // we trigger the tested method - ut1.accept(mv); - - // and finally verify the expectation - mv.verify(); -} - - -void MockVisitorPolymorphism_test::test_visitor_retrieves_parameter2() -{ - // we create our class under test - MockParameter2 mp2(MOCKPP_PCHAR("mock-parameter2")); - UnderTest ut2(mp2); - - // we program the mock visitor as we expect - MockVisitor2 mv(MOCKPP_PCHAR("mock-visitor")); - MOCKPP_CHAINER_FOR(MockVisitor2, visit) chain(&mv); - // here is the second problem as we would like to verify that the - // call is made with precisely 'mp' and nothing else - chain.stubs(mockpp::once()) - .with(mockpp::same(mp2)); - - // we trigger the tested method - ut2.accept(mv); - - // and finally verify the expectation - mv.verify(); -} - - -#endif // HAVE_CPPUNIT +/*************************************************************************** + ChainableMockObjectPolymorphism_test.cpp + - + unit tests for MockVisitor class and polymorphic parameter methods + ------------------- + begin : Tue Sep 20 2004 + copyright : (C) 2005 contributed by Mathieu Champlon + email : mockpp at ewald-arnold dot de + + $Id$ + + ***************************************************************************/ + +/************************************************************************** + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2 of the License, + * or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + ***************************************************************************/ + +#define MOCKPP_ENABLE_DEFAULT_FORMATTER + +#include <mockpp/mockpp.h> // always first + +// #include <iostream> + +#ifndef HAVE_CPPUNIT + +# warning CppUnit not available at compile time + +#else + +#include <mockpp/ChainableMockObject.h> + +#include <mockpp/chaining/ChainingMockObjectSupport.h> +#include <mockpp/chaining/Invocation.h> + +#include <cppunit/extensions/HelperMacros.h> + + +class MockVisitorPolymorphism_test : public CppUnit::TestFixture +{ + public: + + CPPUNIT_TEST_SUITE( MockVisitorPolymorphism_test ); + + CPPUNIT_TEST(test_visitor_retrieves_parameter); + CPPUNIT_TEST(test_visitor_retrieves_mock_parameter); + + CPPUNIT_TEST_SUITE_END(); + + public: + + void test_visitor_retrieves_mock_parameter(); + void test_visitor_retrieves_parameter(); +}; + + +CPPUNIT_TEST_SUITE_REGISTRATION(MockVisitorPolymorphism_test); + + +class Parameter +{ +public: + + virtual void doSomething() = 0; +}; + + +class Visitor +{ +public: + + virtual void visit1( const Parameter & ) = 0; + virtual void visit2( const Parameter &, int ) = 0; + virtual void visit3( const Parameter &, const Parameter &, const Parameter & ) = 0; +}; + + +class UnderTest +{ +public: + + UnderTest( const Parameter &in_parameter1, + const Parameter &in_parameter2, + const Parameter &in_parameter3 ) + : parameter1( in_parameter1 ) + , parameter2( in_parameter2 ) + , parameter3( in_parameter3 ) + {} + + UnderTest( const Parameter &in_parameter1) + : parameter1( in_parameter1 ) + , parameter2( in_parameter1 ) + , parameter3( in_parameter1 ) + {} + + void accept1( Visitor &visitor ) + { +// std::cout << "accept1\n"; + visitor.visit1( parameter1 ); + } + + void accept3( Visitor &visitor ) + { +// std::cout << "accept3\n"; + visitor.visit3( parameter1, parameter2, parameter3 ); + } + +private: + + const Parameter& parameter1; + const Parameter& parameter2; + const Parameter& parameter3; +}; + + +class MockParameter : public mockpp::ChainableMockObject, + public Parameter +{ +public: + + MockParameter(const mockpp::String &name) + : mockpp::ChainableMockObject(name, 0) + , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE0(doSomething) + {} + + MOCKPP_VOID_CHAINABLE0(MockParameter, doSomething); + + unsigned a; +}; + + +#ifdef MOCKPP_USE_INVOCATION_EQUALS + +template <> +bool mockpp::invocationComparison<MockParameter>(const MockParameter &left, + const MockParameter &right) +{ + std::cout << "invocationComparison<MockParameter>\n"; + return left.a == right.a; +} + + +template <> +bool mockpp::invocationComparison<Parameter>(const Parameter &left, + const Parameter &right) +{ + std::cout << "invocationComparison<Parameter>\n"; + return mockpp::isSameComparison(left, right); +} + +#endif // MOCKPP_USE_INVOCATION_EQUALS + + +class MockVisitor1 : public mockpp::ChainableMockObject, + public Visitor +{ +public: + + MockVisitor1(const mockpp::String &name) + : mockpp::ChainableMockObject(name, 0) + , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE1(visit1) + , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE2(visit2) + , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE3(visit3) + {} + + MOCKPP_VOID_CHAINABLE_EXT1(MockVisitor1, visit1, const Parameter &, + , Parameter); + + MOCKPP_VOID_CHAINABLE_EXT2(MockVisitor1, visit2, const Parameter &, int, + , Parameter , int); + + MOCKPP_VOID_CHAINABLE_EXT3(MockVisitor1, visit3, const Parameter &, const Parameter &, const Parameter &, + , Parameter, Parameter, Parameter); +}; + + +void MockVisitorPolymorphism_test::test_visitor_retrieves_parameter() +{ + MockParameter mp1(MOCKPP_PCHAR("mock-parameter1")); + MockParameter mp2(MOCKPP_PCHAR("mock-parameter2")); + +// mp1 == mp2; <== does not work! missing operator== + + UnderTest ut1(mp1); + MockVisitor1 mv1(MOCKPP_PCHAR("mock-visitor1")); + MOCKPP_CHAINER_FOR(MockVisitor1, visit1) chain1(&mv1); + chain1.expects(mockpp::once()) + .with(mockpp::same<Parameter>(mp1)); + ut1.accept1(mv1); + + UnderTest ut3(mp1, mp2, mp1); + MockVisitor1 mv3(MOCKPP_PCHAR("mock-visitor3")); + MOCKPP_CHAINER_FOR(MockVisitor1, visit3) chain3(&mv3); + chain3.expects(mockpp::once()) + .with(mockpp::same<Parameter>(mp1), + mockpp::logic_not<Parameter>(mockpp::same<Parameter>(mp1)), + mockpp::same<Parameter>(mp1)); + chain3.expects(mockpp::once()) + .with(mockpp::same<Parameter>(mp1), + mockpp::same<Parameter>(mp2), + mockpp::same<Parameter>(mp1)); + ut3.accept3(mv3); + ut3.accept3(mv3); + + mv1.verify(); + mv3.verify(); +} + + +class MockVisitor2 : public mockpp::ChainableMockObject, + public Visitor +{ +public: + + MockVisitor2(const mockpp::String &name) + : mockpp::ChainableMockObject(name, 0) + , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE1(visit1) + , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE2(visit2) + , MOCKPP_CONSTRUCT_MEMBERS_FOR_CHAINABLE3(visit3) + {} + + MOCKPP_VOID_CHAINABLE_EXT1(MockVisitor2, visit1, const Parameter &, + , Parameter); + + MOCKPP_VOID_CHAINABLE_EXT2(MockVisitor2, visit2, const Parameter &, int, + , Parameter , int); + + MOCKPP_VOID_CHAINABLE_EXT3(MockVisitor2, visit3, const Parameter &, const Parameter &, const Parameter &, + , Parameter, Parameter, Parameter); +}; + + +void MockVisitorPolymorphism_test::test_visitor_retrieves_mock_parameter() +{ + MockParameter mp(MOCKPP_PCHAR("mock-parameter")); + UnderTest ut(mp); + + MockVisitor2 mv(MOCKPP_PCHAR("mock-visitor")); + MOCKPP_CHAINER_FOR(MockVisitor2, visit1) chain(&mv); + chain.expects(mockpp::once()) + .with(mockpp::same<Parameter>(mp)); + + ut.accept1(mv); + + mv.verify(); +} + + +#endif // HAVE_CPPUNIT |