ExpectedFunction.h

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 #ifndef UTILS__CALITKO_MOCKS__EXPECTED_FUNCTION_H
00024 #define UTILS__CALITKO_MOCKS__EXPECTED_FUNCTION_H
00025 
00026 #include "Imports.h"
00027 #include "BasicTypes.h"
00028 
00029 // Define some macros to help us define the template specializations:
00030 #include "HelperMacros.h"
00031 
00032 namespace Utils {
00033 namespace CalitkoMocks {
00034 
00035 class BoundFunctionBase;
00036 
00038 
00041 class ExpectedFunctionBase
00042 {
00043 public:
00044                     ExpectedFunctionBase();
00045     virtual         ~ExpectedFunctionBase();
00046 
00047     void            addCall (auto_ptr <BoundFunctionBase>);
00048     void            setContext (const std::string &expectationString_,
00049                                 const SourceLine &sourceLine_);
00050     void            verifyChecked();
00051     void            setNumberOfTimesShouldBeInvoked (int from, int to);
00052     bool            isLastExpectedCall();
00053     bool            wasVerified();
00054 
00055 protected:
00056     void            fail(const std::string &reason);
00057     void            callAll();
00058     void            stopWaiting(); // \todo kind of hack.
00059 
00060 private:
00061     Q_DISABLE_COPY (ExpectedFunctionBase)
00062 
00063     typedef std::list <BoundFunctionBase *> Calls;
00064 
00065     bool                        verifyCheckedWasCalled;
00066     Calls                       calls;
00067     std::string                 expectationString;
00068     SourceLine                  sourceLine;
00069     int                         minTimesInvoked;
00070     int                         maxTimesInvoked;
00071 protected:
00072     int                         timesInvoked;
00073 };
00074 
00076 
00112 template <typename Func = void()>
00113         class ExpectedFunction;
00114 
00116 
00167 template <typename Func, typename BoundFoo>
00168         class ExpectationChecker;
00169 
00171 #define DEFINE_EXPECTED_FUNCTION(N)                                         \
00172     template <typename R COMMA_ARGUMENTS_TYPE_LIST_##N (typename P)>        \
00173     class ExpectedFunction <R (ARGUMENTS_TYPE_LIST_##N(P))>                 \
00174         : public ExpectedFunctionBase                                       \
00175     {                                                                       \
00176         REFERENCE_OBJECT (ExpectedFunction)                                 \
00177     public:                                                                 \
00178         typedef ExpectedFunctionBase    Base;                               \
00179         typedef R                       ReturnType;                         \
00180                                                                             \
00181         ExpectedFunction (PARAMETER_LIST_##N)                               \
00182         : stubbedReturnValue (0) INITIALIZER_LIST_##N                       \
00183         {}                                                                  \
00184                                                                             \
00185         void willReturn (ReturnTypeWrapper <ReturnType> r)                  \
00186         { stubbedReturnValue.reset (new ReturnTypeWrapper <ReturnType>(r));}\
00187                                                                             \
00188         virtual ReturnType check (PARAMETER_LIST_##N)                       \
00189         { this->stopWaiting(); ++this->timesInvoked;                        \
00190           if (!IsSameType <ReturnType, void>::value                         \
00191               && (stubbedReturnValue.get() == 0))                           \
00192             this->fail ("willReturn() not called for");                     \
00193           if (!(COMPARE_VARIABLES_##N))                                     \
00194             this->fail ("willCall() expectation failed");                   \
00195           this->callAll();                                                  \
00196           return returnHelper (*stubbedReturnValue); }                      \
00197                                                                             \
00198     protected:                                                              \
00199         struct Dummy{};                                                     \
00200         ExpectedFunction (Dummy)                                            \
00201         : stubbedReturnValue (0) DEFAULT_INITIALIZER_LIST_##N               \
00202         {}                                                                  \
00203                                                                             \
00204     protected:                                                              \
00205         template <typename T> T                                             \
00206         returnHelper (const ReturnTypeWrapper <T> &r)                       \
00207         { return r.value; }                                                 \
00208                                                                             \
00209         void returnHelper (const ReturnTypeWrapper <void> &) {}             \
00210         auto_ptr <ReturnTypeWrapper <ReturnType> >                          \
00211                                             stubbedReturnValue;             \
00212         DECLARE_BOUND_VARIABLES_##N                                         \
00213     };                                                                      \
00214                                                                             \
00215     template <typename R COMMA_ARGUMENTS_TYPE_LIST_##N (typename P),        \
00216               typename BoundFoo>                                            \
00217     class ExpectationChecker <R (ARGUMENTS_TYPE_LIST_##N(P)), BoundFoo>     \
00218     : public ExpectedFunction <R (ARGUMENTS_TYPE_LIST_##N(P))>              \
00219     {                                                                       \
00220     public:                                                                 \
00221         typedef ExpectedFunction <R (ARGUMENTS_TYPE_LIST_##N(P))>           \
00222                                         Base;                               \
00223         typedef R                       ReturnType;                         \
00224                                                                             \
00225         ExpectationChecker (auto_ptr <BoundFoo> checker_)                   \
00226         : Base (typename Base::Dummy()), checker (checker_) {}              \
00227                                                                             \
00228         virtual ReturnType check (PARAMETER_LIST_##N)                       \
00229         { this->stopWaiting(); ++this->timesInvoked;                        \
00230           if (!IsSameType <ReturnType, void>::value                         \
00231               && (this->stubbedReturnValue.get() == 0))                     \
00232             this->fail ("willReturn() not called for");                     \
00233           if (!checker->check (ARGUMENTS_TYPE_LIST_##N(p)))                 \
00234             this->fail ("willCall() expectation failed");                   \
00235           this->callAll();                                                  \
00236           return this->returnHelper (*this->stubbedReturnValue); }          \
00237     private:                                                                \
00238         auto_ptr <BoundFoo>     checker;                                    \
00239     };
00240 
00241 DEFINE_EXPECTED_FUNCTION(0)
00242 DEFINE_EXPECTED_FUNCTION(1)
00243 DEFINE_EXPECTED_FUNCTION(2)
00244 DEFINE_EXPECTED_FUNCTION(3)
00245 DEFINE_EXPECTED_FUNCTION(4)
00246 DEFINE_EXPECTED_FUNCTION(5)
00247 DEFINE_EXPECTED_FUNCTION(6)
00248 DEFINE_EXPECTED_FUNCTION(7)
00249 DEFINE_EXPECTED_FUNCTION(8)
00250 DEFINE_EXPECTED_FUNCTION(9)
00251 
00252 #undef DEFINE_EXPECTED_FUNCTION
00253 
00254 } // namespace CalitkoMocks
00255 } // namespace Utils
00256 
00257 // Undefine the helper macros. Yes, including the file again undefs them all!
00258 #include "HelperMacros.h"
00259 
00260 #endif // UTILS__CALITKO_MOCKS__EXPECTED_FUNCTION_H