CallDriverTest.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 "../CallDriver.h"
00025 #include "../ExpectationDriver.h"
00026 #include "../BoundFunction.h"
00027 #include "../ExpectedFunction.h"
00028 #include "Imports.cpp"
00029 
00030 namespace Utils {
00031 namespace CalitkoMocks {
00032 namespace Testing {
00033 
00035 
00039 class CallDriverTest : public CppUnit::TestFixture
00040 {
00041     CPPUNIT_TEST_SUITE (CallDriverTest);
00042     CPPUNIT_TEST (testWillReturnOk);
00043     CPPUNIT_TEST_FAIL (testWillReturnFail);
00044     CPPUNIT_TEST_FAIL (testBoundFooWillCallNotCheckedFail);
00045     CPPUNIT_TEST (testReturnsOk);
00046     CPPUNIT_TEST (testWillCallOk);
00047     CPPUNIT_TEST (testReturnsNotCalled);
00048     CPPUNIT_TEST (testMultipleChainedWillCallOk);
00049     CPPUNIT_TEST_SUITE_END();
00050 
00051 public:
00052     bool fooOk;
00053     int foo (int a) { fooOk = true; return a * 2; }
00054     typedef BoundFunction <CallDriverTest, int (int)> fooType;
00055 
00057     void testWillReturnOk()
00058     {
00059         CallDriver <fooType> driver (auto_ptr <fooType>
00060             (new fooType (this, &CallDriverTest::foo, 1)));
00061         fooOk = false;
00062         driver.willReturn (2);
00063         CPPUNIT_ASSERT (fooOk == true);
00064     }
00065 
00067     void testWillReturnFail()
00068     {
00069         CallDriver <fooType> driver (auto_ptr <fooType>
00070             (new fooType (this, &CallDriverTest::foo, 1)));
00071         fooOk = false;
00072         driver.willReturn (4); // this must throw
00073     }
00074 
00076 
00080     void testBoundFooWillCallNotCheckedFail()
00081     {
00082         typedef ExpectedFunction <void ()> Foo;
00083         // The list owns the expected functions so it should be destroyed after
00084         // the call driver:
00085         ExpectationsList <Foo> list;
00086         CallDriver <fooType> driver (auto_ptr <fooType>
00087             (new fooType (this, &CallDriverTest::foo, 1)));
00088         auto_ptr <Foo> foo (new Foo());
00089         ExpectationPair <Foo> pair (list, foo);
00090         driver.willCall (pair);
00091         driver.willReturn (2); // this must throw
00092     }
00093 
00094     bool foo2Ok;
00095     void foo2 (int a) { foo2Ok = true; }
00096     typedef BoundFunction <CallDriverTest, void (int)> foo2Type;
00098     void testReturnsOk()
00099     {
00100         CallDriver <foo2Type> driver (auto_ptr <foo2Type>
00101             (new foo2Type (this, &CallDriverTest::foo2, 1)));
00102         foo2Ok = false;
00103         driver.returns();
00104         CPPUNIT_ASSERT (foo2Ok == true);
00105     }
00106 
00108     void testWillCallOk()
00109     {
00110         typedef ExpectedFunction <void()> Foo;
00111         // The list owns the expected functions so it should be destroyed after
00112         // the call driver:
00113         ExpectationsList <Foo> list;
00114         CallDriver <foo2Type> driver (auto_ptr <foo2Type>
00115             (new foo2Type (this, &CallDriverTest::foo2, 1)));
00116         auto_ptr <Foo> foo (new Foo());
00117         ExpectationPair <Foo> pair (list, foo);
00118         ExpectationDriver <CallDriver <foo2Type>, ExpectedFunction <void()> >
00119             expectationDriver = driver.willCall (pair);
00120         list.take().check();
00121         driver.returns();
00122     }
00123 
00125     void testReturnsNotCalled()
00126     {
00127         try {
00128             CallDriver <foo2Type> driver (auto_ptr <foo2Type>
00129                 (new foo2Type (this, &CallDriverTest::foo2, 1)));
00130         } catch (CPPUNIT_NS::Exception &e) {
00131             CPPUNIT_ASSERT (e.message() == CPPUNIT_NS::Message (
00132                 "neither returns() nor willReturn() called for",
00133                 "function name unavailable"));
00134             CPPUNIT_ASSERT (e.sourceLine() == CPPUNIT_NS::SourceLine());
00135             return;
00136         }
00137     }
00138 
00140     void testMultipleChainedWillCallOk()
00141     {
00142         typedef ExpectedFunction <void (int)> Foo;
00143         ExpectationsList <Foo> list;
00144 
00145         auto_ptr <Foo> foo1 (new Foo (1));
00146         auto_ptr <Foo> foo2 (new Foo (2));
00147         auto_ptr <Foo> foo3 (new Foo (3));
00148 
00149         CallDriver <foo2Type> driver (auto_ptr <foo2Type>
00150             (new foo2Type (this, &CallDriverTest::foo2, 1)));
00151         driver
00152             .willCall (ExpectationPair <Foo> (list, foo1))
00153             .returns()
00154             .willCall (ExpectationPair <Foo> (list, foo2))
00155             .returns()
00156             .willCall (ExpectationPair <Foo> (list, foo3))
00157             .returns();
00158 
00159         list.take().check (1);
00160         list.take().check (2);
00161         list.take().check (3);
00162 
00163         driver.returns();
00164     }
00165 };
00166 
00167 CPPUNIT_TEST_SUITE_REGISTRATION (CallDriverTest);
00168 
00169 } // namespace Testing
00170 } // namespace CalitkoMocks
00171 } // namespace Utils