00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef MOCKPP_ABSTRACTDYNAMICCHAININGMOCK_H
00036 #define MOCKPP_ABSTRACTDYNAMICCHAININGMOCK_H
00037
00038 #include <mockpp/mockpp.h>
00039
00040 #include <mockpp/chaining/DynamicChainingMock.h>
00041 #include <mockpp/chaining/DynamicChainingMockError.h>
00042 #include <mockpp/chaining/InvocationMocker.h>
00043 #include <mockpp/chaining/FIFOInvocationDispatcher.h>
00044
00045 #include <mockpp/stub/ReturnStub.h>
00046
00047 #include <mockpp/compat/Exception.h>
00048
00049
00050 MOCKPP_NS_START
00051
00052
00056 template <typename R,
00057 typename I>
00058 class AbstractDynamicChainingMockImpl : public DynamicChainingMock<R, I>
00059 {
00060 protected:
00061
00067 AbstractDynamicChainingMockImpl( typename InvocationDispatcher<R, I>::AP in_invocationDispatcher,
00068 const String &name,
00069 VerifiableList *parent)
00070 : DynamicChainingMock<R, I>(name, parent)
00071 , invocationDispatcher( in_invocationDispatcher )
00072 {}
00073
00074 public:
00075
00076 class Describer : public InvocationMocker<R, I>::DescriberBase
00077 {
00078 public:
00079
00083 virtual bool hasDescription()
00084 {
00085 return false;
00086 }
00087
00088
00096 virtual String describeTo( String &result,
00097 #ifdef MOCKPP_NO_TYPENAME_FOR_STL_NS
00098 const MOCKPP_STL::vector<InvocationMatcher<I>*> &matchers,
00099 #else
00100 const typename MOCKPP_STL::vector<InvocationMatcher<I>*> &matchers,
00101 #endif
00102 Stub<R, I> *stub,
00103 const String &name ) const
00104 {
00105 return result;
00106 }
00107
00108 #if defined( __BORLANDC__ ) || defined( _MSC_VER )
00109
00110
00111
00119 String describeTo( String &result,
00120 const typename MOCKPP_STL::vector<InvocationMatcher<I>*> &matchers,
00121 TypelessStub<R>* stub,
00122 const String &name ) const
00123 {
00124 return InvocationMocker<R, I>::DescriberBase::describeTo(result, matchers, adap.get(), name);
00125 }
00126
00127 #else
00128 using InvocationMocker<R, I>::DescriberBase::describeTo;
00129 #endif
00130
00131 };
00132
00136 virtual void verify()
00137 {
00138 forgetFailure();
00139
00140 MOCKPP_TRY
00141 {
00142 invocationDispatcher->verify();
00143 }
00144
00145 #ifndef MOCKPP_NO_EXCEPTIONS
00146 catch ( const AssertionFailedError &ex )
00147 {
00148 assertionFailed( __LINE__, __FILE__,
00149 MOCKPP_PCHAR( "mock object " )
00150 + getMockName()
00151 + MOCKPP_PCHAR( ": " )
00152 + ex.getMessage() );
00153 }
00154 #endif // MOCKPP_NO_EXCEPTIONS
00155 }
00156
00160 virtual String toString() const
00161 {
00162 return this->getVerifiableName();
00163 }
00164
00168 String getMockName() const
00169 {
00170 return this->getVerifiableName();
00171 }
00172
00176 virtual void setDefaultStub( const MOCKPP_NS::StubHolder<R, I> &newDefaultStub )
00177 {
00178 invocationDispatcher->setDefaultStub( newDefaultStub );
00179 }
00180
00184 virtual void addInvokable( typename Invokable<R, I>::AP invokable )
00185 {
00186 invocationDispatcher->add( invokable );
00187 }
00188
00191 virtual void reset()
00192 {
00193 invocationDispatcher->reset();
00194 forgetFailure();
00195 }
00196
00197 protected:
00198
00203 virtual R mockInvocation( const I &invocation ) = 0;
00204
00205 private:
00206
00209 void forgetFailure() const
00210 {
00211 failure.reset( 0 );
00212 }
00213
00214 protected:
00215
00216 mutable AutoPointer<AssertionFailedError> failure;
00217 typename InvocationDispatcher<R, I>::AP invocationDispatcher;
00218 };
00219
00220
00224 template <typename R,
00225 typename I>
00226 class AbstractDynamicChainingMock : public AbstractDynamicChainingMockImpl<R, I>
00227 {
00228 public:
00229
00235 AbstractDynamicChainingMock( typename InvocationDispatcher<R, I>::AP in_invocationDispatcher,
00236 const String &name,
00237 VerifiableList *parent)
00238 : AbstractDynamicChainingMockImpl<R,I>(in_invocationDispatcher, name, parent)
00239 {}
00240
00245 AbstractDynamicChainingMock( const String &name,
00246 VerifiableList *parent)
00247 : AbstractDynamicChainingMockImpl<R,I>(new FIFOInvocationDispatcher<R,I>, name, parent)
00248 {}
00249
00250 protected:
00251
00256 virtual R mockInvocation( const I &invocation )
00257 {
00258 if ( this->failure.get() != 0 )
00259 assertionFailed(this->failure->getSrcLine(),
00260 this->failure->getSrcFile(),
00261 this->failure->getMessage());
00262
00263 MOCKPP_TRY
00264 {
00265 return this->invocationDispatcher->dispatch( invocation );
00266 }
00267
00268 #ifndef MOCKPP_NO_EXCEPTIONS
00269 catch ( const AssertionFailedError & assertion )
00270 {
00271 DynamicChainingMockError<R, I> dme( this,
00272 invocation,
00273 this->invocationDispatcher.get(),
00274 assertion.getMessage() );
00275 this->failure.reset( new AssertionFailedError(__LINE__, __FILE__, dme.getMessage()));
00276 assertionFailed(this->failure->getSrcLine(),
00277 this->failure->getSrcFile(),
00278 this->failure->getMessage());
00279 }
00280 return R();
00281 #endif // MOCKPP_NO_EXCEPTIONS
00282 }
00283 };
00284
00285
00286 #ifndef MOCKPP_PTI_WEAKNESS // Partial Template Instantiation Weakness
00287
00288
00292 template <typename I>
00293 class AbstractDynamicChainingMock<void, I> : public AbstractDynamicChainingMockImpl<void, I>
00294 {
00295 public:
00296
00302 AbstractDynamicChainingMock( typename InvocationDispatcher<void, I>::AP in_invocationDispatcher,
00303 const String &name,
00304 VerifiableList *parent)
00305 : AbstractDynamicChainingMockImpl<void, I>(in_invocationDispatcher, name, parent)
00306 {}
00307
00312 AbstractDynamicChainingMock( const String &name,
00313 VerifiableList *parent)
00314 : AbstractDynamicChainingMockImpl<void, I>(new FIFOInvocationDispatcher<void,I>, name, parent)
00315 {}
00316
00317 protected:
00318
00322 virtual void mockInvocation( const I &invocation )
00323 {
00324 if ( this->failure.get() != 0 )
00325 MOCKPP_THROW(*this->failure.get());
00326
00327 MOCKPP_TRY
00328 {
00329 this->invocationDispatcher->dispatch( invocation );
00330 }
00331
00332 #ifndef MOCKPP_NO_EXCEPTIONS
00333 catch ( const AssertionFailedError & assertion )
00334 {
00335 DynamicChainingMockError<void, I> dme( this,
00336 invocation,
00337 this->invocationDispatcher.get(),
00338 assertion.getMessage() );
00339 this->failure.reset( new AssertionFailedError(__LINE__, __FILE__, dme.getMessage()));
00340 MOCKPP_THROW(*this->failure.get());
00341 }
00342 #endif // MOCKPP_NO_EXCEPTIONS
00343 }
00344 };
00345
00346
00347 #define MOCKPP_ABSTRACTDYNAMICCHAININGMOCK_PTI_IMPL(I)
00348 #define MOCKPP_ABSTRACTDYNAMICCHAININGMOCK_PTI_DECL(I)
00349
00350
00351 #else // MOCKPP_PTI_WEAKNESS Partial Template Instantiation Weakness
00352
00353
00354 #define MOCKPP_ABSTRACTDYNAMICCHAININGMOCK_PTI_DECL(I) \
00355 MOCKPP_NS_START \
00356 template<> \
00357 void MOCKPP_NS::AbstractDynamicChainingMock<void, I >; \
00358 }
00359
00360
00361 #define MOCKPP_ABSTRACTDYNAMICCHAININGMOCK_PTI_IMPL(I) \
00362 MOCKPP_NS_START \
00363 template<> \
00364 class AbstractDynamicChainingMock<void, I> : public AbstractDynamicChainingMockImpl<void, I> \
00365 { \
00366 public: \
00367 AbstractDynamicChainingMock( InvocationDispatcher<void, I>::AP in_invocationDispatcher, \
00368 const String &name, \
00369 VerifiableList *parent) \
00370 : AbstractDynamicChainingMockImpl<void, I>(in_invocationDispatcher, name, parent) \
00371 {} \
00372 \
00373 AbstractDynamicChainingMock( const String &name, \
00374 VerifiableList *parent) \
00375 : AbstractDynamicChainingMockImpl<void, I>(new FIFOInvocationDispatcher<void,I>, name, parent) \
00376 {} \
00377 \
00378 void mockInvocation( const I &invocation ) \
00379 { \
00380 if ( this->failure.get() != 0 ) \
00381 MOCKPP_THROW(*this->failure.get()); \
00382 \
00383 try \
00384 { \
00385 this->invocationDispatcher->dispatch( invocation ); \
00386 } \
00387 \
00388 catch ( const AssertionFailedError & assertion ) \
00389 { \
00390 DynamicChainingMockError<void, I > dme( this, \
00391 invocation, \
00392 this->invocationDispatcher.get(), \
00393 assertion.getMessage() ); \
00394 this->failure.reset( new AssertionFailedError(__LINE__, __FILE__, dme.getMessage())); \
00395 MOCKPP_THROW(*this->failure.get()); \
00396 } \
00397 } \
00398 }; \
00399 MOCKPP_NS_END
00400
00401
00402 #endif
00403
00404
00416 MOCKPP_NS_END
00417
00418
00419 #endif // MOCKPP_ABSTRACTDYNAMICCHAININGMOCK_H