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_EXPECTATIONSET_H
00031 #define MOCKPP_EXPECTATIONSET_H
00032
00033 #include <mockpp/mockpp.h>
00034
00035 #include MOCKPP_ALGORITHM_H
00036 #include MOCKPP_SET_H
00037
00038 #include <mockpp/AbstractExpectationCollection.h>
00039 #include <mockpp/util/AssertMo.h>
00040
00041
00042 MOCKPP_NS_START
00043
00044
00049 template <class T>
00050 class ExpectationSet : public AbstractExpectationCollection<T>
00051 {
00052
00053 public:
00054
00059 ExpectationSet(const String &name, VerifiableList *parent = 0)
00060 : AbstractExpectationCollection<T>(name, parent),
00061 expectNothing(false),
00062 haveActualValue(false)
00063 {
00064 }
00065
00066
00070 void addActual(const T &actualItem)
00071 {
00072 actualItems.insert(actualItem);
00073 haveActualValue = true;
00074
00075 if (this->shouldCheckImmediately())
00076 checkImmediateValue(actualItem);
00077 }
00078
00079
00084 template <class I>
00085 void addActual(I items, I end)
00086 {
00087 for ( ; items != end; ++items)
00088 addActual(*items);
00089 }
00090
00091
00096 ExpectationSet& addExpected(const T &expectedItem)
00097 {
00098 expectNothing = false;
00099 expectedItems.insert(expectedItem);
00100 this->setHasExpectations();
00101 return *this;
00102 }
00103
00104
00110 template <class I>
00111 ExpectationSet& addExpected(I items, I end)
00112 {
00113 for ( ; items != end; ++items)
00114 addExpected(*items);
00115 return *this;
00116 }
00117
00118
00125 virtual void verify()
00126 {
00127 if( expectNothing)
00128 {
00129 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected no value."));
00130 fmt << getVerifiableName();
00131 MOCKPP_ASSERT_FALSE_MESSAGE(fmt, haveActualValue );
00132 }
00133 else
00134 {
00135 if (!this->hasExpectations() )
00136 return;
00137
00138 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected a value."));
00139 fmt << getVerifiableName();
00140
00141 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, haveActualValue );
00142
00143 fmt = mockpp_i18n(MOCKPP_PCHAR("%1 did not receive the expected number of collection items."));
00144 fmt << getVerifiableName();
00145
00146 MOCKPP_ASSERT_TRUE_MESSAGE(fmt,actualItems.size() == expectedItems.size());
00147
00148 fmt = mockpp_i18n(MOCKPP_PCHAR("%1 did not receive the expected item set."));
00149 fmt << getVerifiableName();
00150
00151 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, actualItems == expectedItems);
00152 }
00153 }
00154
00155
00163 virtual void setExpectNothing()
00164 {
00165 expectNothing = true;
00166 clearExpectation();
00167 this->setHasExpectations();
00168 }
00169
00170
00174 virtual void reset()
00175 {
00176 this->clearFailOnVerify();
00177 clearActual();
00178 clearExpectation();
00179 expectNothing = false;
00180 }
00181
00182
00186 virtual void clearActual()
00187 {
00188 haveActualValue = false;
00189 actualItems.clear();
00190 }
00191
00192
00193 AbstractExpectationCollection<T>::getVerifiableName;
00194
00195 protected:
00196
00200 virtual void clearExpectation()
00201 {
00202 expectedItems.clear();
00203 this->clearHasExpectations();
00204 }
00205
00206
00213 virtual void checkImmediateValue(const T &actualItem) const
00214 {
00215 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 did not receive an expected item.\nUnexpected: %2"));
00216 fmt << getVerifiableName() << actualItem;
00217
00218 MOCKPP_ASSERT_TRUE_MESSAGE(fmt,
00219 MOCKPP_STL::find(expectedItems.begin(), expectedItems.end(), actualItem) != expectedItems.end());
00220 }
00221
00222 private:
00223
00224 MOCKPP_STL::set<T> actualItems;
00225 MOCKPP_STL::set<T> expectedItems;
00226 bool expectNothing;
00227 bool haveActualValue;
00228 };
00229
00230
00231
00232 MOCKPP_NS_END
00233
00234
00235 #endif // MOCKPP_EXPECTATIONSET_H
00236