DelayedCallDriverTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "../DelayedCallDriver.h"
00025 #include "../BoundFunction.h"
00026 #include "../CallDriver.h"
00027 #include "../ExpectationDriver.h"
00028 #include "../ExpectedFunction.h"
00029 #include "Imports.cpp"
00030 
00031 namespace Utils {
00032 namespace CalitkoMocks {
00033 namespace Testing {
00034 
00036 
00039 class DelayedCallDriverTest : public CppUnit::TestFixture
00040 {
00041     CPPUNIT_TEST_SUITE (DelayedCallDriverTest);
00042     CPPUNIT_TEST (testWillReturnOk);
00043     CPPUNIT_TEST_FAIL (testBoundFooWillCallNotCheckedFail);
00044     CPPUNIT_TEST (testWillCallOk);
00045     CPPUNIT_TEST_SUITE_END();
00046 
00047 public:
00048     bool fooOk;
00049     int foo (int a) { fooOk = true; return a * 2; }
00050     typedef BoundFunction <DelayedCallDriverTest, int (int)> fooType;
00051     typedef CallDriver <fooType> CallDriverFoo;
00052     typedef ExpectedFunction <bool ()> ExpectedFoo;
00053     typedef ExpectationDriver <CallDriverFoo, ExpectedFoo> ExpectationDriverFoo;
00054 
00056     void testWillReturnOk()
00057     {
00058         fooType delayedFoo (this, &DelayedCallDriverTest::foo, 1);
00059         /*  Note we pass a non-existing ExpectationDriver object but we know it
00060             will not be dereferenced. */
00061         DelayedCallDriver <ExpectationDriver <CallDriverFoo, ExpectedFoo>,
00062             fooType> driver (*(ExpectationDriverFoo *) 0, delayedFoo);
00063         fooOk = false;
00064         driver.willReturn (2);
00065         CPPUNIT_ASSERT (fooOk == false);
00066     }
00067 
00069 
00073     void testBoundFooWillCallNotCheckedFail()
00074     {
00075         typedef ExpectedFunction <void()> Foo;
00076         // The list owns the expected functions so it should be destroyed after
00077         // the call driver:
00078         ExpectationsList <Foo> list;
00079         fooType delayedFoo (this, &DelayedCallDriverTest::foo, 1);
00080         /*  Note we pass a non-existing ExpectationDriver object but we know it
00081             will not be dereferenced. */
00082         DelayedCallDriver <ExpectationDriver <CallDriverFoo, ExpectedFoo>,
00083             fooType> driver (*(ExpectationDriverFoo *) 0, delayedFoo);
00084         auto_ptr <Foo> expectation (new Foo());
00085         ExpectationPair <Foo> pair (list, expectation);
00086         // Add an expectation that we will not check:
00087         driver.willCall (pair);
00088         // Force the delayed function to be invoked and the expectation checked:
00089         delayedFoo.willReturn (2);
00090         delayedFoo.call(); // should throw
00091     }
00092 
00094     void testWillCallOk()
00095     {
00096         typedef ExpectedFunction <void()> Foo;
00097         // The list owns the expected functions so it should be destroyed after
00098         // the call driver:
00099         ExpectationsList <Foo> list;
00100         fooType delayedFoo (this, &DelayedCallDriverTest::foo, 1);
00101         typedef DelayedCallDriver <ExpectationDriver <CallDriverFoo,
00102             ExpectedFoo>, fooType> DelayedCallDriverType;
00103         /*  Note we pass a non-existing ExpectationDriver object but we know it
00104             will not be dereferenced. */
00105         DelayedCallDriverType driver (*(ExpectationDriverFoo *) 0, delayedFoo);
00106         auto_ptr <Foo> expectation (new Foo());
00107         ExpectationPair <Foo> pair (list, expectation);
00108         // Add an expectation that we will not check:
00109         ExpectationDriver <DelayedCallDriverType, ExpectedFunction <void ()> >
00110             newExpectation = driver.willCall (pair);
00111     }
00112 };
00113 
00114 CPPUNIT_TEST_SUITE_REGISTRATION (DelayedCallDriverTest);
00115 
00116 } // namespace Testing
00117 } // namespace CalitkoMocks
00118 } // namespace Utils