ExpectationDriver.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__EXPECTATION_DRIVER_H
00024 #define UTILS__CALITKO_MOCKS__EXPECTATION_DRIVER_H
00025 
00026 #include "Imports.h"
00027 #include "BasicTypes.h"
00028 
00029 namespace Utils {
00030 namespace CalitkoMocks {
00031 
00032 template <typename Host, typename Function> class BoundFunction;
00033 template <typename Function> class CallDriver;
00034 template <typename ExpectationDrv, typename Function> class DelayedCallDriver;
00035 
00037 
00054 template <typename OwningCallDriver, typename Function>
00055 class ExpectationDriver
00056 {
00057 public:
00058     typedef typename Function::ReturnType           ReturnType;
00059 
00060     ExpectationDriver (OwningCallDriver &owningCallDriver_,
00061                        Function &expectedFunction_)
00062         : owningCallDriver (owningCallDriver_),
00063           expectedFunction (expectedFunction_)
00064     {
00065     }
00066 
00068 
00074     template <typename H, typename T>
00075     DelayedCallDriver <ExpectationDriver, BoundFunction <H, T> >
00076     willCall (auto_ptr <BoundFunction <H, T> > f)
00077     {
00078         DelayedCallDriver <ExpectationDriver, BoundFunction <H, T> >
00079                                     delayedCall (*this, *(f.get()));
00080         expectedFunction.addCall (
00081             auto_ptr <typename BoundFunction <H, T>::Base> (f.get()));
00082         f.release();
00083         return delayedCall;
00084     }
00085 
00089     template <typename H, typename T>
00090     DelayedCallDriver <ExpectationDriver, BoundFunction <H, T> >
00091     willCallMsg (auto_ptr <BoundFunction <H, T> > f,
00092                  const std::string &expressionString,
00093                  const SourceLine &sourceLine)
00094     {
00095         f->setContext (expressionString, sourceLine);
00096         return willCall (f);
00097     }
00098 
00100 
00104     OwningCallDriver & willReturn (ReturnTypeWrapper <ReturnType> r)
00105     {
00106         expectedFunction.willReturn (r);
00107         return owningCallDriver;
00108     }
00109 
00115     OwningCallDriver & willReturnMsg (ReturnTypeWrapper <ReturnType> r,
00116                                     const std::string &,
00117                                     const SourceLine &)
00118     {
00119         return willReturn (r);
00120     }
00121 
00124     OwningCallDriver & returns()
00125     {
00126         return willReturn (ReturnTypeWrapper <void>());
00127     }
00128 
00129     ExpectationDriver & numberOfTimes (int from)
00130     {
00131         expectedFunction.setNumberOfTimesShouldBeInvoked (from, from);
00132         return *this;
00133     }
00134 
00135     ExpectationDriver & numberOfTimes (int from, int to)
00136     {
00137         expectedFunction.setNumberOfTimesShouldBeInvoked (from, to);
00138         return *this;
00139     }
00140 
00141 private:
00142     OwningCallDriver                    &owningCallDriver;
00143     Function                            &expectedFunction;
00144 };
00145 
00146 } // namespace CalitkoMocks
00147 } // namespace Utils
00148 
00149 #endif // UTILS__CALITKO_MOCKS__EXPECTATION_DRIVER_H