Mocks.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 
00041 #ifndef UTILS__CALITKO_MOCKS__MOCKS_H
00042 #define UTILS__CALITKO_MOCKS__MOCKS_H
00043 
00044 #include "Imports.h"
00045 #include "BoundFunction.h"
00046 #include "CallDriver.h"
00047 #include "DelayedCallDriver.h"
00048 #include "ExpectationDriver.h"
00049 #include "ExpectationsList.h"
00050 #include "ExpectedFunction.h"
00051 
00052 namespace Utils {
00053 namespace CalitkoMocks {
00054 
00055     template <typename Host, typename Func>
00056     CallDriver <BoundFunction <Host, Func> >
00057     call (auto_ptr <BoundFunction <Host, Func> > f)
00058     {
00059         return CallDriver <BoundFunction <Host, Func> > (f);
00060     }
00061 
00062     template <typename Host, typename Func>
00063     CallDriver <BoundFunction <Host, Func> >
00064     callMsg (auto_ptr <BoundFunction <Host, Func> > f,
00065              const std::string &expressionString,
00066              const SourceLine &sourceLine)
00067     {
00068         f->setContext (expressionString, sourceLine);
00069         return CallDriver <BoundFunction <Host, Func> > (f);
00070     }
00071 
00072     template <typename Checker>
00073     auto_ptr <ExpectedFunction <typename Checker::Function> >
00074     checker (const Checker &checker)
00075     {
00076         return auto_ptr <ExpectedFunction <typename Checker::Function> >
00077             (new ExpectationChecker <typename Checker::Function, Checker>
00078                 (auto_ptr <Checker> (new Checker (checker))));
00079     }
00080 
00081     struct MockBase
00082     {
00083         template <typename Host, typename Func>
00084         CallDriver <BoundFunction <Host, Func> >
00085         calls (auto_ptr <BoundFunction <Host, Func> > f)
00086         {
00087             return CallDriver <BoundFunction <Host, Func> > (f);
00088         }
00089 
00090         template <typename Host, typename Func>
00091         CallDriver <BoundFunction <Host, Func> >
00092         callsMsg (auto_ptr <BoundFunction <Host, Func> > f,
00093                  const std::string &expressionString,
00094                  const SourceLine &sourceLine)
00095         {
00096             f->setContext (expressionString, sourceLine);
00097             return CallDriver <BoundFunction <Host, Func> > (f);
00098         }
00099 
00100         void fail (const std::string &function)
00101         {
00102             Asserter::failIf (
00103                 true,
00104                 Message ("unexpected mock function called", function),
00105                 SourceLine());
00106         }
00107     };
00108 
00109     struct SignalWaitHelper
00110     {
00111         void processEvents (int maxTime);
00112     };
00113 
00114     CallDriver <BoundFunction <SignalWaitHelper, void (int)> >
00115     waitFor (int maxTime);
00116 
00117 
00118     template <typename T>
00119     void willBeEqual (T v1, T v2)
00120     {
00121         willBeEqualMsg (v1, v2, "unknown expression", SourceLine());
00122     }
00123 
00124     template <typename T>
00125     void willBeEqualMsg (T v1, T v2, const std::string &expectationString,
00126                         const SourceLine &sourceLine)
00127     {
00128         Asserter::failIf (!(v1 == v2),
00129                         Message ("willBeEqual() failed", expectationString),
00130                         sourceLine);
00131     }
00132 
00133     inline void willFail (const std::string &reason)
00134     {
00135         Asserter::failIf (true,
00136                         Message ("willFail() reached", reason),
00137                         SourceLine());
00138     }
00139 
00140 } // namespace CalitkoMocks
00141 } // namespace Utils
00142 
00143 
00145 #define willReturn(expr)    \
00146         willReturnMsg (expr, std::string ("Expression: " #expr), CPPUNIT_SOURCELINE())
00147 
00149 #define willCall(foo)       \
00150         willCallMsg (foo, std::string ("Function: " #foo), CPPUNIT_SOURCELINE())
00151 
00153 #define willEmit(foo)       \
00154         willEmitMsg (foo, std::string ("Function: " #foo), CPPUNIT_SOURCELINE())
00155 
00157 #define call(foo)       \
00158         callMsg (foo, std::string ("Function: " #foo), CPPUNIT_SOURCELINE())
00159 
00161 #define calls(foo)      \
00162         callsMsg (foo, std::string ("Function: " #foo), CPPUNIT_SOURCELINE())
00163 
00165 #define willBeEqual(a, b)                                   \
00166         willBeEqualMsg ((a), (b),                           \
00167                         std::string (#a ", " #b),           \
00168                         CPPUNIT_SOURCELINE())
00169 
00170 #endif // UTILS__CALITKO_MOCKS__MOCKS_H