DelayedCallDriver.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__DELAYED_CALL_DRIVER_H
00024 #define UTILS__CALITKO_MOCKS__DELAYED_CALL_DRIVER_H
00025 
00026 #include "Imports.h"
00027 #include "BasicTypes.h"
00028 #include "ExpectedFunction.h"
00029 #include "ExpectationsList.h"
00030 
00031 namespace Utils {
00032 namespace CalitkoMocks {
00033 
00034 class ExpectedFunctionBase;
00035 template <typename CallDriver, typename Function> class ExpectationDriver;
00036 
00056 template <typename OuterExpectationDriver, typename Function>
00057 class DelayedCallDriver
00058 {
00059 public:
00060     typedef typename Function::ReturnType           ReturnType;
00061 
00062     DelayedCallDriver (OuterExpectationDriver &expectationDriver_, Function &f_)
00063         : f (f_), outerExpectationDriver (expectationDriver_) {}
00064 
00066 
00070     template <typename Foo>
00071     ExpectationDriver <DelayedCallDriver, ExpectedFunction <Foo> >
00072     willCall (ExpectationPair <ExpectedFunction <Foo> > expectation)
00073     {
00074         ExpectedFunction <Foo> *expectedFunction = expectation.function.get();
00075         expectation.list.add (expectation.function); // add the foo to the list
00076         f.addExpectation (expectedFunction);
00077         return ExpectationDriver <DelayedCallDriver, ExpectedFunction <Foo> >
00078             (*this, *expectedFunction);
00079     }
00080 
00084     template <typename Foo>
00085     ExpectationDriver <DelayedCallDriver, ExpectedFunction <Foo> >
00086     willCallMsg (ExpectationPair <ExpectedFunction <Foo> > expectation,
00087                  const std::string &expressionString,
00088                  const SourceLine &sourceLine)
00089     {
00090         expectation.function->setContext (expressionString, sourceLine);
00091         return willCall (expectation);
00092     }
00093 
00095     OuterExpectationDriver &
00096     willReturn (const ReturnTypeWrapper <ReturnType> &returnValue)
00097     {
00098         f.willReturn (returnValue);
00099         return outerExpectationDriver;
00100     }
00101 
00106     OuterExpectationDriver &
00107     willReturnMsg (const ReturnTypeWrapper <ReturnType> &returnValue,
00108                    const std::string &expressionString,
00109                    const SourceLine &sourceLine)
00110     {
00111         f.setContext (expressionString, sourceLine);
00112         return willReturn (returnValue);
00113     }
00114 
00116     OuterExpectationDriver & returns()
00117     {
00118         return willReturn (ReturnTypeWrapper <void>());
00119     }
00120 
00122     template <typename Foo>
00123     DelayedCallDriver &
00124     willEmit (ExpectationPair <ExpectedFunction <Foo> > expectation)
00125     {
00126         ExpectedFunction <Foo> *signalExpectation = expectation.function.get();
00127         expectation.list.add (expectation.function);
00128         f.addExpectation (signalExpectation);
00129         return *this;
00130     }
00131 
00136     template <typename Foo>
00137     DelayedCallDriver &
00138     willEmitMsg (ExpectationPair <ExpectedFunction <Foo> > expectation,
00139                  const std::string &expressionString,
00140                  const SourceLine &sourceLine)
00141     {
00142         expectation.function->setContext (expressionString, sourceLine);
00143         return willEmit (expectation);
00144     }
00145 
00146 protected:
00147     Function                    &f;
00148     OuterExpectationDriver      &outerExpectationDriver;
00149 };
00150 
00151 } // namespace CalitkoMocks
00152 } // namespace Utils
00153 
00154 #endif // UTILS__CALITKO_MOCKS__DELAYED_CALL_DRIVER_H