00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef MOCKPP_VisitableMockMethod_H
00032 #define MOCKPP_VisitableMockMethod_H
00033
00034 #include <mockpp/mockpp.h>
00035
00036 #include <mockpp/visiting/VisitableMockObject.h>
00037
00038
00039
00040 MOCKPP_NS_START
00041
00042
00046 class MOCKPP_API_DECL0 VisitableMockMethodBase : public MockObject
00047 {
00048 public:
00049
00054 VisitableMockMethodBase(const String &name, VisitableMockObject *parent);
00055
00060 String getMethodName(bool fully = false) const;
00061
00065 MOCKPP_STL::string getMethodIdentifier() const;
00066
00070 VisitableMockObject *getVisitableMockObject() const;
00071
00072 protected:
00073
00076 void throwAvailableException() const
00077 {
00078 bool do_throw = true;
00079 if (throwablesInline)
00080 {
00081 unsigned num = throwableInsteadReturn.size();
00082 if (num != 0)
00083 {
00084 do_throw = throwableInsteadReturn[0];
00085 throwableInsteadReturn.erase(throwableInsteadReturn.begin());
00086 }
00087 }
00088
00089 if (do_throw && throwables.hasMoreObjects() != 0)
00090 {
00091 Throwable *thr = throwables.nextThrowableObject();
00092 if (thr != 0)
00093 thr->throw_me();
00094 }
00095
00096 if (defaultThrowable.get() != 0)
00097 defaultThrowable.get()->throw_me();
00098 }
00099
00100
00101
00102 public:
00103
00107 void unsetThrowablesInline()
00108 {
00109 MOCKPP_ASSERT_FALSE(this->getVisitableMockObject()->isActivated());
00110 this->throwablesInline = false;
00111 };
00112
00118 void addThrowable(Throwable *t)
00119 {
00120 MOCKPP_STD_NS::auto_ptr<Throwable> at (t);
00121 MOCKPP_ASSERT_FALSE(this->getVisitableMockObject()->isActivated());
00122 this->throwables.push_back(at.release());
00123 this->throwableInsteadReturn.push_back(true);
00124 }
00125
00132 template <class T>
00133 void addThrowable(const T &w, unsigned count = 1)
00134 {
00135 for ( ; count > 0; --count)
00136 addThrowable(make_throwable(w));
00137 }
00138
00144 void setDefaultThrowable(Throwable *t)
00145 {
00146 MOCKPP_STD_NS::auto_ptr<Throwable> at (t);
00147 MOCKPP_ASSERT_FALSE(this->getVisitableMockObject()->isActivated());
00148 this->defaultThrowable.take(at.release());
00149 }
00150
00153 virtual void reset()
00154 {
00155 this->defaultThrowable.reset();
00156 this->throwables.reset();
00157 this->throwableInsteadReturn.clear();
00158 this->throwablesInline = true;
00159 }
00160
00167 virtual void verify()
00168 {
00169 Throwable *dt = this->defaultThrowable.get();
00170 if (dt != 0)
00171 {
00172 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 is unused."));
00173 fmt << (this->getMethodName() + MOCKPP_PCHAR("/defaultThrowable"));
00174 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, dt->hasThrown());
00175 }
00176 }
00177
00178 protected:
00179
00180 VisitableMockObject *visitable;
00181 mutable ThrowableItem defaultThrowable;
00182 mutable ThrowableList throwables;
00183 mutable bool throwablesInline;
00184 mutable MOCKPP_STL::vector<bool> throwableInsteadReturn;
00185 };
00186
00187
00189
00190
00194 template <typename R>
00195 class VisitableMockReturningMethodBase : public VisitableMockMethodBase
00196 {
00197 public:
00198
00203 VisitableMockReturningMethodBase(const String &name, VisitableMockObject *parent)
00204 : VisitableMockMethodBase(name, parent)
00205 , returnValues(getMethodName() + MOCKPP_PCHAR("/returnValues"), this)
00206 {
00207 reset();
00208 }
00209
00215 void setDefaultReturnValue(const R &rv)
00216 {
00217 MOCKPP_ASSERT_FALSE(this->getVisitableMockObject()->isActivated());
00218 this->haveDefaultReturnValue = true;
00219 this->defaultReturnValue = rv;
00220 }
00221
00228 void addReturnValue(const R &rv, unsigned count = 1)
00229 {
00230 MOCKPP_ASSERT_FALSE(this->getVisitableMockObject()->isActivated());
00231 for ( ; count > 0; --count)
00232 {
00233 this->returnValues.addObjectToReturn(rv);
00234 this->throwableInsteadReturn.push_back(false);
00235 }
00236 }
00237
00240 virtual void reset()
00241 {
00242 VisitableMockMethodBase::reset();
00243 this->returnValues.reset();
00244 this->haveDefaultReturnValue = false;
00245 this->defaultReturnValueUsed = false;
00246 }
00247
00251 virtual void verify()
00252 {
00253 VisitableMockMethodBase::verify();
00254 if (this->haveDefaultReturnValue)
00255 {
00256 String fmt = mockpp_i18n(MOCKPP_PCHAR("%1 is unused."));
00257 fmt << (this->getMethodName() + MOCKPP_PCHAR("/defaultReturnValueUsed"));
00258 MOCKPP_ASSERT_TRUE_MESSAGE(fmt, this->defaultReturnValueUsed);
00259 }
00260 }
00261
00262 protected:
00263
00268 R determineReturnValue() const
00269 {
00270
00271 if (returnValues.hasMoreObjects())
00272 return returnValues.nextReturnObject();
00273
00274 MOCKPP_ASSERT_TRUE_MESSAGE(this->getMethodName(true) + MOCKPP_PCHAR("haveDefaultReturnValue != true"),
00275 haveDefaultReturnValue == true);
00276 defaultReturnValueUsed = true;
00277 return defaultReturnValue;
00278 }
00279
00280 protected:
00281
00282 mutable ReturnObjectList<R> returnValues;
00283 bool haveDefaultReturnValue;
00284 mutable bool defaultReturnValueUsed;
00285 R defaultReturnValue;
00286 };
00287
00288
00292 template <>
00293 class VisitableMockReturningMethodBase<void> : public VisitableMockMethodBase
00294 {
00295 public:
00296
00301 VisitableMockReturningMethodBase(const String &name, VisitableMockObject *parent)
00302 : VisitableMockMethodBase(name, parent)
00303 {}
00304 };
00305
00306
00307 MOCKPP_NS_END
00308
00309
00310 #endif // MOCKPP_VisitableMockMethod_H
00311