00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #define MOCKPP_NEED_EXPORTS
00031 #include <mockpp/mockpp.h>
00032
00033 #include MOCKPP_ALGORITHM_H
00034
00035 #include <mockpp/TrackingCounter.h>
00036 #include <mockpp/VerifiableList.h>
00037
00038 #include <mockpp/compat/Asserter.h>
00039
00040 #include <mockpp/compat/Formatter.h>
00041
00042 MOCKPP_NS_START
00043
00044
00045 MOCKPP_API_IMPL0
00046 TrackingCounterBase::TrackingCounterBase(const String &name, VerifiableList *parent)
00047 : AbstractExpectation<unsigned>(name, parent)
00048 {
00049 expectNothing = false;
00050 myExpectedMinVal = 0;
00051 myExpectedMaxVal = 0;
00052 }
00053
00054
00055 MOCKPP_API_IMPL(unsigned) TrackingCounterBase::getExpectedMinValue() const
00056 {
00057 return myExpectedMinVal;
00058 }
00059
00060
00061 MOCKPP_API_IMPL(unsigned) TrackingCounterBase::getExpectedMaxValue() const
00062 {
00063 return myExpectedMaxVal;
00064 }
00065
00066
00067 MOCKPP_API_IMPL(void) TrackingCounterBase::clearExpectation()
00068 {
00069 clearHasExpectations();
00070 }
00071
00072
00073 MOCKPP_API_IMPL(void) TrackingCounterBase::reset()
00074 {
00075 clearActual();
00076 clearExpectation();
00077 this->clearHasExpectations();
00078 }
00079
00080
00081 MOCKPP_API_IMPL(void)
00082 TrackingCounterBase::setExpected(unsigned expectedMinValue, unsigned expectedMaxValue)
00083 {
00084 expectNothing = false;
00085 MOCKPP_ASSERT_TRUE(expectedMinValue <= expectedMaxValue);
00086 myExpectedMinVal = expectedMinValue;
00087 myExpectedMaxVal = expectedMaxValue;
00088 setHasExpectations();
00089 }
00090
00091
00092 MOCKPP_API_IMPL(void) TrackingCounterBase::setExpected(unsigned expectedValue)
00093 {
00094 setExpected(expectedValue, expectedValue);
00095 }
00096
00097
00098 MOCKPP_API_IMPL(bool) TrackingCounterBase::isExpectNothing() const
00099 {
00100 return expectNothing;
00101 }
00102
00103
00104 MOCKPP_API_IMPL(void) TrackingCounterBase::setExpectNothing()
00105 {
00106 myExpectedMinVal = 0;
00107 myExpectedMaxVal = 0;
00108 setHasExpectations();
00109 expectNothing = true;
00110 }
00111
00112
00114
00115
00116 MOCKPP_API_IMPL0 TrackingCounterMaster::TrackingCounterMaster(const String &name, VerifiableList *parent)
00117 : TrackingCounterBase(name, parent)
00118 {
00119 myActualValue = 0;
00120 }
00121
00122
00123 MOCKPP_API_IMPL(void) TrackingCounterMaster::verify()
00124 {
00125 for (unsigned i = 0; i < clients.size(); ++i)
00126 clients[i]->verify();
00127
00128 if (!this->hasExpectations() )
00129 return;
00130
00131 if( isExpectNothing() )
00132 {
00133 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected no value."));
00134 fmt << this->getVerifiableName();
00135
00136 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myActualValue == 0 );
00137 }
00138 else
00139 {
00140 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was tagged %2 times which is less than the expected count of %3."));
00141 fmt << getVerifiableName() << myActualValue << getExpectedMinValue();
00142 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, getExpectedMinValue() <= myActualValue);
00143
00144 fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was tagged %2 times which is more than the expected count of %3."));
00145 fmt << getVerifiableName() << myActualValue << getExpectedMaxValue();
00146 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, getExpectedMaxValue() >= myActualValue);
00147 }
00148 }
00149
00150
00151 MOCKPP_API_IMPL(unsigned) TrackingCounterMaster::numClients () const
00152 {
00153 return clients.size();
00154 }
00155
00156
00157 MOCKPP_API_IMPL(void) TrackingCounterMaster::addClient (TrackingCounterClient* vf)
00158 {
00159 if (vf != 0)
00160 clients.push_back(vf);
00161 }
00162
00163
00164 MOCKPP_API_IMPL(void) TrackingCounterMaster::removeClient(TrackingCounterClient* vf)
00165 {
00166 MOCKPP_STL::vector<TrackingCounterClient*>::iterator it =
00167 MOCKPP_STL::find (clients.begin(), clients.end(), vf);
00168
00169 if(it != clients.end())
00170 clients.erase(it);
00171 }
00172
00173
00174 MOCKPP_API_IMPL(unsigned) TrackingCounterMaster::getActual() const
00175 {
00176 return myActualValue;
00177 }
00178
00179
00180 MOCKPP_API_IMPL(void) TrackingCounterMaster::clearActual()
00181 {
00182 myActualValue = 0;
00183 for (unsigned i = 0; i < clients.size(); ++i)
00184 clients[i]->clearActual();
00185 }
00186
00187
00188 MOCKPP_API_IMPL(unsigned) TrackingCounterMaster::increment()
00189 {
00190 myActualValue++;
00191 if (shouldCheckImmediately())
00192 {
00193 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was tagged %2 times but should not be called more than %3 times."));
00194
00195 fmt << getVerifiableName() << myActualValue << getExpectedMaxValue();
00196 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myActualValue <= getExpectedMaxValue());
00197 }
00198
00199 return myActualValue;
00200 }
00201
00202
00204
00205
00206 MOCKPP_API_IMPL0 TrackingCounterClient::TrackingCounterClient(const String &name, TrackingCounterMaster &in_master)
00207 : TrackingCounterBase(name, in_master.getParent())
00208 {
00209 master = &in_master;
00210 master->addClient(this);
00211 clearActual();
00212 }
00213
00214
00215 MOCKPP_API_IMPL0 TrackingCounterClient::~TrackingCounterClient()
00216 {
00217 master->removeClient(this);
00218 }
00219
00220
00221 MOCKPP_API_IMPL(void) TrackingCounterClient::clearActual()
00222 {
00223 myActualValue = 0;
00224 countervalues.clear();
00225 }
00226
00227
00228 MOCKPP_API_IMPL(unsigned) TrackingCounterClient::getActual() const
00229 {
00230 return myActualValue;
00231 }
00232
00233
00234 MOCKPP_API_IMPL(unsigned) TrackingCounterClient::getTag(unsigned index) const
00235 {
00236 MOCKPP_ASSERT_TRUE(index < countervalues.size());
00237 return countervalues[index];
00238 }
00239
00240
00241 MOCKPP_API_IMPL(unsigned) TrackingCounterClient::numTags() const
00242 {
00243
00244 return countervalues.size();
00245 }
00246
00247
00248 MOCKPP_API_IMPL(void) TrackingCounterClient::verify()
00249 {
00250 if (!this->hasExpectations() )
00251 return;
00252
00253 if( isExpectNothing() )
00254 {
00255 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected no value."));
00256 fmt << this->getVerifiableName();
00257
00258 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myActualValue == 0 );
00259 }
00260 else
00261 {
00262 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was last tagged with %2 which is less than the expected count of %3."));
00263 fmt << getVerifiableName() << myActualValue << getExpectedMinValue();
00264 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, getExpectedMinValue() <= myActualValue);
00265
00266 fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was last tagged with %2 which is more than the expected count of %3."));
00267 fmt << getVerifiableName() << myActualValue << getExpectedMaxValue();
00268 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, getExpectedMaxValue() >= myActualValue);
00269 }
00270 }
00271
00272
00273 MOCKPP_API_IMPL(unsigned) TrackingCounterClient::inc()
00274 {
00275 myActualValue = master->increment();
00276 countervalues.push_back(myActualValue);
00277 if (shouldCheckImmediately())
00278 {
00279 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 was tagged with %2 but should not be tagged more than %3 times."));
00280
00281 fmt << getVerifiableName() << myActualValue << getExpectedMaxValue();
00282 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, myActualValue <= getExpectedMaxValue());
00283 }
00284 return myActualValue;
00285 }
00286
00287
00288 MOCKPP_NS_END