00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef MOCKPP_EXPECTATIONCONGLOMERATION_H
00031 #define MOCKPP_EXPECTATIONCONGLOMERATION_H
00032
00033 #include <mockpp/mockpp.h>
00034
00035 #include MOCKPP_ALGORITHM_H
00036 #include MOCKPP_SET_H
00037 #include MOCKPP_VECTOR_H
00038
00039 #include <mockpp/AbstractExpectationCollection.h>
00040 #include <mockpp/util/AssertMo.h>
00041 #include <mockpp/ExpectationBoundary.h>
00042
00043
00044 MOCKPP_NS_START
00045
00046
00051 template <class T>
00052 class ExpectationConglomeration : public AbstractExpectationCollection<T>
00053 {
00054
00055 public:
00056
00061 ExpectationConglomeration(const String &name, VerifiableList *parent = 0)
00062 : AbstractExpectationCollection<T>(name, parent),
00063 expectNothing(false),
00064 haveActualValue(false)
00065 {
00066 }
00067
00068
00072 void addActual(const T &actualItem)
00073 {
00074 actualItems.insert(actualItem);
00075 haveActualValue = true;
00076
00077 if (this->shouldCheckImmediately())
00078 checkImmediateValue(actualItem);
00079 }
00080
00081
00086 template <class I>
00087 void addActual(I items, I end)
00088 {
00089 for ( ; items != end; ++items)
00090 addActual(*items);
00091 }
00092
00093
00098 ExpectationConglomeration& addExpected(const T &expectedItem)
00099 {
00100 expectedSingleItems.push_back(expectedItem);
00101 expectNothing = false;
00102 this->setHasExpectations();
00103 return *this;
00104 }
00105
00106
00112 ExpectationConglomeration& addExpectedBoundary(const T &lower, const T &upper)
00113 {
00114 expectedBoundaryItems.push_back(Boundary (lower, upper));
00115 expectNothing = false;
00116 this->setHasExpectations();
00117 return *this;
00118 }
00119
00120
00126 template <class I>
00127 ExpectationConglomeration& addExpected(I items, I end)
00128 {
00129 for ( ; items != end; ++items)
00130 addExpected(*items);
00131 return *this;
00132 }
00133
00134
00137 virtual void reset()
00138 {
00139 this->clearFailOnVerify();
00140 clearActual();
00141 clearExpectation();
00142 expectNothing = false;
00143 }
00144
00145
00149 virtual void clearActual()
00150 {
00151 haveActualValue = false;
00152 actualItems.clear();
00153 }
00154
00155
00162 virtual void verify()
00163 {
00164 if( expectNothing)
00165 {
00166 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected no value."));
00167 fmt << getVerifiableName();
00168 MOCKPP_ASSERT_FALSE_MESSAGE(fmt, haveActualValue );
00169 }
00170 else
00171 {
00172 if (!this->hasExpectations() )
00173 return;
00174
00175 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected a value."));
00176 fmt << this->getVerifiableName();
00177 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, haveActualValue );
00178
00179 fmt = mockpp_i18n(MOCKPP_PCHAR("%1 did not receive the expected item set."));
00180 fmt << this->getVerifiableName();
00181 typename MOCKPP_STL::set<T>::const_iterator it;
00182 for (it = actualItems.begin(); it != actualItems.end(); ++it)
00183 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, contains(*it));
00184 }
00185 }
00186
00187
00195 virtual void setExpectNothing()
00196 {
00197 expectNothing = true;
00198 clearExpectation();
00199 this->setHasExpectations();
00200 }
00201
00202
00208 bool contains(const T &val) const
00209 {
00210 if (MOCKPP_STL::find(expectedSingleItems.begin(), expectedSingleItems.end(), val) != expectedSingleItems.end())
00211 return true;
00212
00213 typename MOCKPP_STL::vector<Boundary>::const_iterator it;
00214 for (it = expectedBoundaryItems.begin(); it != expectedBoundaryItems.end(); ++it)
00215 if ( (*it).contains(val))
00216 return true;
00217
00218 return false;
00219 }
00220
00221
00222 AbstractExpectationCollection<T>::getVerifiableName;
00223
00224 protected:
00225
00229 virtual void clearExpectation()
00230 {
00231 this->clearHasExpectations();
00232 expectedBoundaryItems.clear();
00233 expectedSingleItems.clear();
00234 }
00235
00236
00243 virtual void checkImmediateValue(const T &actualItem) const
00244 {
00245 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 did not receive an expected item.\nUnexpected: %2"));
00246 fmt << getVerifiableName() << actualItem;
00247
00248 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, contains(actualItem));
00249 }
00250
00251
00252 private:
00253
00254 class Boundary
00255 {
00256 public:
00257
00258 Boundary (const T &low, const T &up)
00259 : lower(low)
00260 , upper(up)
00261 {
00262 }
00263
00264 bool contains(const T &val) const
00265 {
00266 return val <= upper && val >= lower;
00267 }
00268
00269 private:
00270
00271 T lower;
00272 T upper;
00273 };
00274
00275 MOCKPP_STL::set<T> actualItems;
00276 MOCKPP_STL::vector<T> expectedSingleItems;
00277 MOCKPP_STL::vector<Boundary> expectedBoundaryItems;
00278 bool expectNothing;
00279 bool haveActualValue;
00280 };
00281
00282
00283
00284 MOCKPP_NS_END
00285
00286
00287 #endif // MOCKPP_EXPECTATIONCONGLOMERATION_H
00288