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_EXPECTATIONLIST_H
00031 #define MOCKPP_EXPECTATIONLIST_H
00032 
00033 #include <mockpp/mockpp.h> 
00034 
00035 #include MOCKPP_ALGORITHM_H
00036 #include MOCKPP_VECTOR_H
00037 
00038 #include <mockpp/AbstractExpectationCollection.h>
00039 #include <mockpp/util/AssertMo.h>
00040 
00041 
00042 MOCKPP_NS_START
00043 
00044 
00048 template <class T>
00049 class ExpectationList : public AbstractExpectationCollection<T>
00050 {
00051   public:
00052 
00057     ExpectationList(const String &name, VerifiableList *parent = 0)
00058       : AbstractExpectationCollection<T>(name, parent),
00059         expectNothing(false),
00060         haveActualValue(false)
00061     {
00062     }
00063 
00064 
00071     virtual void verify()
00072     {
00073       if( expectNothing)
00074       {
00075         String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected no value."));
00076         fmt << this->getVerifiableName();
00077         MOCKPP_ASSERT_FALSE_MESSAGE(fmt, haveActualValue );
00078       }
00079       else
00080       {
00081         if (!this->hasExpectations() )
00082           return;
00083 
00084         String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 expected a value."));
00085         fmt << this->getVerifiableName();
00086 
00087         MOCKPP_ASSERT_TRUE_MESSAGE(fmt, haveActualValue);
00088 
00089         fmt = mockpp_i18n(MOCKPP_PCHAR("%1 did not receive the expected item list."));
00090         fmt << this->getVerifiableName();
00091 
00092         MOCKPP_ASSERT_TRUE_MESSAGE(fmt, actualItems == expectedItems);
00093       }
00094     }
00095 
00096 
00100     void addActual(const T &actualItem)
00101     {
00102       actualItems.push_back(actualItem);
00103       haveActualValue = true;
00104 
00105       if (this->shouldCheckImmediately())
00106         checkImmediateValue(actualItem);
00107     }
00108 
00109 
00114     template <class I>
00115     void addActual(I items, I end)
00116     {
00117       for ( ; items != end; ++items)
00118         addActual(*items);
00119     }
00120 
00121 
00126     ExpectationList& addExpected(const T &expectedItem)
00127     {
00128       expectedItems.push_back(expectedItem);
00129       expectNothing = false;
00130       this->setHasExpectations();
00131       return *this;
00132     }
00133 
00134 
00138     void balanceActual()
00139     {
00140       if (actualItems.size() < expectedItems.size())
00141       {
00142         haveActualValue = true;
00143         actualItems.push_back(expectedItems[actualItems.size()]);
00144       }
00145     }
00146 
00147 
00153     template <class I>
00154     ExpectationList& addExpected(I items, I end)
00155     {
00156       for ( ; items != end; ++items)
00157         addExpected(*items);
00158       return *this;
00159     }
00160 
00161 
00165     virtual void clearActual()
00166     {
00167       haveActualValue = false;
00168       actualItems.clear();
00169     }
00170 
00171 
00175     virtual void reset()
00176     {
00177       this->clearFailOnVerify();
00178       clearActual();
00179       clearExpectation();
00180       expectNothing = false;
00181     }
00182 
00183 
00188     unsigned size() const
00189     {
00190       return expectedItems.size();
00191     }
00192 
00193 
00201     virtual void setExpectNothing()
00202     {
00203       expectNothing = true;
00204       clearExpectation();
00205       this->setHasExpectations();
00206     }
00207 
00208 
00209   protected:
00210 
00214     virtual void clearExpectation()
00215     {
00216       this->clearHasExpectations();
00217       expectedItems.clear();
00218     }
00219 
00220 
00227     void checkImmediateValue(const T &actualItem) const
00228     {
00229       unsigned itemsize = actualItems.size();
00230       String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 had different item sizes.\n")
00231                         MOCKPP_PCHAR("Expected %2 items but received %3 when adding %4."));
00232       fmt << this->getVerifiableName() << expectedItems.size() << itemsize << actualItem;
00233 
00234       MOCKPP_ASSERT_TRUE_MESSAGE(fmt, expectedItems.size() >= itemsize);
00235 
00236       fmt = mockpp_i18n(MOCKPP_PCHAR("%1 added item[%2] does not match. %3 != %4."));
00237       fmt << this->getVerifiableName() << itemsize << expectedItems[(itemsize - 1)] << actualItem;
00238 
00239       this->assertEquals(fmt, actualItem, expectedItems[(itemsize - 1)]);
00240     }
00241 
00242   private:
00243 
00244     MOCKPP_STL::vector<T> actualItems;
00245     MOCKPP_STL::vector<T> expectedItems;
00246     bool           expectNothing;
00247     bool           haveActualValue;
00248 };
00249 
00250 
00251 MOCKPP_NS_END
00252 
00253 
00254 #endif // MOCKPP_EXPECTATIONLIST_H