CallDriver.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__CALL_DRIVER_H
00024 #define UTILS__CALITKO_MOCKS__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 Function> class ExpectedFunction;
00036 template <typename CallDriver, typename Function> class ExpectationDriver;
00037 
00039 
00061 template <typename Function>
00062 class CallDriver
00063 {
00064 public:
00065     typedef typename Function::ReturnType           ReturnType;
00066 
00067     CallDriver (auto_ptr <Function> foo) : f (foo), throwing (false) { }
00068     CallDriver (const CallDriver &other) : f (other.f), throwing (false) { }
00069 
00070     ~CallDriver()
00071     {
00079         if (f.get() != 0 and !throwing) {
00082             f->checkCalled();
00083         }
00084     }
00085 
00087 
00091     template <typename Foo>
00092     ExpectationDriver <CallDriver, ExpectedFunction <Foo> >
00093     willCall (ExpectationPair <ExpectedFunction <Foo> > expectation)
00094     {
00095         ExpectedFunction <Foo> *expectedFunction = expectation.function.get();
00096         expectation.list.add (expectation.function); // add the foo to the list
00097         f->addExpectation (expectedFunction);
00098         return ExpectationDriver <CallDriver, ExpectedFunction <Foo> >
00099             (*this, *expectedFunction);
00100     }
00101 
00105     template <typename Foo>
00106     ExpectationDriver <CallDriver, ExpectedFunction <Foo> >
00107     willCallMsg (ExpectationPair <ExpectedFunction <Foo> > expectation,
00108                  const std::string &expressionString,
00109                  const SourceLine &sourceLine)
00110     {
00111         expectation.function->setContext (expressionString, sourceLine);
00112         return willCall (expectation);
00113     }
00114 
00116     void willReturn (const ReturnTypeWrapper <ReturnType> &returnValue)
00117     {
00118         f->willReturn (returnValue);
00119         try {
00120             f->call();
00121         } catch (Exception &e) {
00122             throwing = true;
00123             throw;
00124         }
00125     }
00126 
00131     void willReturnMsg (const ReturnTypeWrapper <ReturnType> &returnValue,
00132                         const std::string &expressionString,
00133                         const SourceLine &sourceLine)
00134     {
00135         f->setContext (expressionString, sourceLine);
00136         willReturn (returnValue);
00137     }
00138 
00140     void returns()
00141     {
00142         willReturn (ReturnTypeWrapper <void>());
00143     }
00144 
00146     template <typename Foo>
00147     CallDriver &
00148     willEmit (ExpectationPair <ExpectedFunction <Foo> > expectation)
00149     {
00150         ExpectedFunction <Foo> *signalExpectation = expectation.function.get();
00151         expectation.list.add (expectation.function);
00152         f->addExpectation (signalExpectation);
00153         return *this;
00154     }
00155 
00160     template <typename Foo>
00161     CallDriver &
00162     willEmitMsg (ExpectationPair <ExpectedFunction <Foo> > expectation,
00163                  const std::string &expressionString,
00164                  const CPPUNIT_NS::SourceLine &sourceLine)
00165     {
00166         expectation.function->setContext (expressionString, sourceLine);
00167         return willEmit (expectation);
00168     }
00169 
00170 private:
00171     mutable auto_ptr <Function>     f;
00172     bool                            throwing;
00173 
00174     template <typename X, typename Y> friend class ExpectationDriver;
00175 };
00176 
00177 } // namespace CalitkoMocks
00178 } // namespace Utils
00179 
00180 #endif // UTILS__CALITKO_MOCKS__CALL_DRIVER_H