ExpectedFunction.cpp

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 #include "Qt.h"
00024 #include "ExpectedFunction.h"
00025 #include "BoundFunction.h"
00026 #include "TesterApplication.h"
00027 #include "Imports.cpp"
00028 
00029 
00030 ExpectedFunctionBase::ExpectedFunctionBase()
00031  :  verifyCheckedWasCalled(), calls(),
00032     expectationString ("function name unavailable"), sourceLine(),
00033     minTimesInvoked (1), maxTimesInvoked (1), timesInvoked (0)
00034 {
00035 }
00036 
00037 ExpectedFunctionBase::~ExpectedFunctionBase()
00038 {
00039     // We DO own the registered BoundFunctions, so delete them:
00040     for (Calls::iterator it = calls.begin();
00041          it != calls.end(); ++it)
00042         delete *it;
00043 }
00044 
00045 void ExpectedFunctionBase::setContext (const std::string &expectationString_,
00046                                        const SourceLine &sourceLine_)
00047 {
00048     expectationString = expectationString_;
00049     sourceLine = sourceLine_;
00050 }
00051 
00052 void ExpectedFunctionBase::verifyChecked()
00053 {
00054     Q_ASSERT (verifyCheckedWasCalled == false);
00055     verifyCheckedWasCalled = true;
00056 
00057     if (timesInvoked < minTimesInvoked ||
00058         (maxTimesInvoked > -1 && timesInvoked > maxTimesInvoked)) {
00059         if (timesInvoked == 0)
00060             fail ("function not called");
00061         else
00062             fail ("function not called the expected numberOfTimes()");
00063     }
00064 }
00065 
00066 // That's for ExpectationsList to decide whether to ignore the expectation or
00067 // not, cause by the fact that call-more-than-once expectations practically
00068 // never get removed from ExpectationsList. Because expectations are verified
00069 // only when the CallDriver is destroyed, we can be sure that once it is
00070 // verified it is no longer needed.
00071 bool ExpectedFunctionBase::wasVerified()
00072 {
00073     return verifyCheckedWasCalled;
00074 }
00075 
00076 void ExpectedFunctionBase::setNumberOfTimesShouldBeInvoked (int from, int to)
00077 {
00078     minTimesInvoked = from;
00079     maxTimesInvoked = to;
00080 }
00081 
00082 bool ExpectedFunctionBase::isLastExpectedCall()
00083 {
00084     return maxTimesInvoked > 0 && timesInvoked + 1 == maxTimesInvoked;
00085 }
00086 
00087 void ExpectedFunctionBase::fail (const std::string &reason)
00088 {
00089     Asserter::failIf (
00090         true,
00091         Message (reason, expectationString),
00092         sourceLine);
00093 }
00094 
00095 void ExpectedFunctionBase::addCall (auto_ptr <BoundFunctionBase> call)
00096 {
00097     calls.push_back (call.get()); // allocate memory and store the pointer
00098     call.release(); // release ownership - no leak if exception thrown above
00099 }
00100 
00101 void ExpectedFunctionBase::callAll()
00102 {
00103     for (Calls::iterator it = calls.begin();
00104          it != calls.end(); ++it)
00105         (*it)->call();
00106 }
00107 
00109 void ExpectedFunctionBase::stopWaiting()
00110 {
00111     TesterApplication::exit (0);
00112 }