00001 00008 /************************************************************************** 00009 00010 begin : Mon Dec 30 2002 00011 copyright : (C) 2002-2010 by Ewald Arnold 00012 email : mockpp at ewald-arnold dot de 00013 00014 This program is free software; you can redistribute it and/or modify 00015 it under the terms of the GNU Lesser General Public License as 00016 published by the Free Software Foundation; either version 2 of the License, 00017 or (at your option) any later version. 00018 00019 This program is distributed in the hope that it will be useful, 00020 but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU Lesser General Public License 00025 along with this program; if not, write to the Free Software 00026 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00027 00028 **/ 00029 00030 #define MOCKPP_NEED_EXPORTS 00031 #include <mockpp/mockpp.h> // always first 00032 00033 #include <mockpp/ExpectationCounter.h> 00034 00035 #include <mockpp/compat/Formatter.h> 00036 00037 MOCKPP_NS_START 00038 00039 00040 MOCKPP_API_IMPL0 00041 ExpectationCounter::ExpectationCounter(const String &name, VerifiableList *parent) 00042 : AbstractExpectation<unsigned>(name, parent) 00043 { 00044 expectNothing = false; 00045 myExpectedMinCalls = 0; 00046 myExpectedMaxCalls = 0; 00047 myActualCalls = 0; 00048 } 00049 00050 00051 MOCKPP_API_IMPL(void) ExpectationCounter::clearActual() 00052 { 00053 myActualCalls = 0; 00054 } 00055 00056 00057 MOCKPP_API_IMPL(void) ExpectationCounter::clearExpectation() 00058 { 00059 clearHasExpectations(); 00060 } 00061 00062 00063 MOCKPP_API_IMPL(void) ExpectationCounter::reset() 00064 { 00065 clearActual(); 00066 clearExpectation(); 00067 this->clearHasExpectations(); 00068 } 00069 00070 00071 MOCKPP_API_IMPL(unsigned) ExpectationCounter::getActual() const 00072 { 00073 return myActualCalls; 00074 } 00075 00076 00077 MOCKPP_API_IMPL(void) ExpectationCounter::inc() 00078 { 00079 myActualCalls++; 00080 if (shouldCheckImmediately()) 00081 { 00082 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was called %2 times but should not be called more than %3 times.")); 00083 00084 fmt << getVerifiableName() << myActualCalls << myExpectedMaxCalls; 00085 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myActualCalls <= myExpectedMaxCalls); 00086 } 00087 } 00088 00089 00090 MOCKPP_API_IMPL(void) 00091 ExpectationCounter::setExpected(unsigned expectedMinCalls, unsigned expectedMaxCalls) 00092 { 00093 expectNothing = false; 00094 MOCKPP_ASSERT_TRUE(expectedMinCalls <= expectedMaxCalls); 00095 myExpectedMinCalls = expectedMinCalls; 00096 myExpectedMaxCalls = expectedMaxCalls; 00097 setHasExpectations(); 00098 } 00099 00100 00101 MOCKPP_API_IMPL(void) ExpectationCounter::setExpected(unsigned expectedCalls) 00102 00103 { 00104 setExpected(expectedCalls, expectedCalls); 00105 } 00106 00107 00108 MOCKPP_API_IMPL(void) ExpectationCounter::setExpectNothing() 00109 { 00110 myExpectedMinCalls = 0; 00111 myExpectedMaxCalls = 0; 00112 setHasExpectations(); 00113 expectNothing = true; 00114 } 00115 00116 00117 MOCKPP_API_IMPL(void) ExpectationCounter::verify() 00118 { 00119 if (!this->hasExpectations() ) 00120 return; 00121 00122 if( expectNothing ) 00123 { 00124 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected no value.")); 00125 fmt << this->getVerifiableName(); 00126 00127 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myActualCalls == 0 ); 00128 } 00129 else 00130 { 00131 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was called %2 times which is less than the expected count of %3.")); 00132 fmt << getVerifiableName() << myActualCalls << myExpectedMinCalls; 00133 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myExpectedMinCalls <= myActualCalls); 00134 00135 fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was called %2 times which is more than the expected count of %3.")); 00136 fmt << getVerifiableName() << myActualCalls << myExpectedMaxCalls; 00137 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myExpectedMaxCalls >= myActualCalls); 00138 } 00139 } 00140 00141 00142 MOCKPP_NS_END