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_MATCHBUILDER_H
00036 #define MOCKPP_MATCHBUILDER_H
00037
00038 #include <mockpp/mockpp.h>
00039
00040 #include <mockpp/compat/AssertionFailedError.h>
00041
00042 #include <mockpp/chaining/StubMatchersCollection.h>
00043
00044 #include <mockpp/builder/StubBuilder.h>
00045 #include <mockpp/builder/BuilderNamespace.h>
00046
00047 #include <mockpp/matcher/InvokedRecorder.h>
00048 #include <mockpp/matcher/InvokedAfterMatcher.h>
00049 #include <mockpp/matcher/InvokedBeforeMatcher.h>
00050 #include <mockpp/matcher/MatcherHolder.h>
00051
00052
00053 MOCKPP_NS_START
00054
00055
00059 class MatchBuilderAdapterBase
00060 {
00061 public:
00062
00066 virtual void *getBuilder() const = 0;
00067
00070 virtual ~MatchBuilderAdapterBase()
00071 {}
00072
00076 virtual void match( TypelessMatcher::AP customMatcher ) = 0;
00077 };
00078
00079
00084 template <typename R,
00085 typename I>
00086 class MatchBuilder : public StubBuilder<R, I>
00087 {
00088 public:
00089
00090 typedef AutoPointer<MatchBuilder<R, I> > AP;
00091
00097 virtual MatchBuilder &match( const MatcherHolder<I> &customMatcher ) = 0;
00098
00104 MatchBuilder<R, I>& after( const String &priorCallID )
00105 {
00106 this->setupOrderingAfterMatchers( *getBuilderNamespace(), priorCallID, priorCallID );
00107 return *this;
00108 }
00109
00115 MatchBuilder<R, I>& before( const String &subsequentCallID )
00116 {
00117 this->setupOrderingBeforeMatchers( *getBuilderNamespace(), subsequentCallID, subsequentCallID );
00118 return *this;
00119 }
00120
00127 MatchBuilder<R, I>& after( BuilderNamespace &otherMock, const String &priorCallID )
00128 {
00129 this->setupOrderingAfterMatchers( otherMock, priorCallID, priorCallID + MOCKPP_PCHAR( " on " ) + otherMock.getIdent() );
00130 return *this;
00131 }
00132
00139 MatchBuilder<R, I>& before( BuilderNamespace &otherMock, const String &subsequentCallID )
00140 {
00141 this->setupOrderingBeforeMatchers( otherMock, subsequentCallID, subsequentCallID + MOCKPP_PCHAR( " on " ) + otherMock.getIdent() );
00142 return *this;
00143 }
00144
00145 protected:
00146
00150 virtual BuilderNamespace *getBuilderNamespace() = 0;
00151
00155 virtual StubMatchersCollection<R, I> * getMocker() = 0;
00156
00157 private:
00158
00165 void setupOrderingAfterMatchers( BuilderNamespace &idTable,
00166 const String &priorCallID,
00167 const String &priorCallDescription )
00168 {
00169 MatchBuilderAdapterBase * priorCallBuilder = idTable.lookupID( priorCallID );
00170
00171 typename InvokedRecorder::AP priorCallRecorder ( new InvokedRecorder );
00172
00173 getMocker() ->addMatcher( new InvokedAfterMatcher<I>( priorCallRecorder.get(),
00174 priorCallDescription ) );
00175 if (priorCallBuilder != 0)
00176 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
00177 priorCallBuilder->match( (TypelessMatcher*)priorCallRecorder.release() );
00178 #else
00179 priorCallBuilder->match(priorCallRecorder);
00180 #endif
00181 else
00182 idTable.addPendingRecorder(priorCallID, priorCallRecorder);
00183 }
00184
00191 void setupOrderingBeforeMatchers( BuilderNamespace &idTable,
00192 const String &subsequentCallID,
00193 const String &subsequentCallDescription )
00194 {
00195 MatchBuilderAdapterBase * subsequentCallBuilder = idTable.lookupID( subsequentCallID );
00196
00197 typename InvokedRecorder::AP subsequentCallRecorder ( new InvokedRecorder );
00198
00199 getMocker() ->addMatcher( new InvokedBeforeMatcher<I>( subsequentCallRecorder.get(),
00200 subsequentCallDescription ) );
00201 if (subsequentCallBuilder != 0)
00202 subsequentCallBuilder->match( subsequentCallRecorder );
00203 else
00204 idTable.addPendingRecorder(subsequentCallID, subsequentCallRecorder);
00205 }
00206 };
00207
00208
00212 template <typename R,
00213 typename I>
00214 class MatchBuilderAdapter : public MatchBuilderAdapterBase
00215 {
00216 public:
00217
00222 MatchBuilderAdapter(MatchBuilder<R, I> *in_builder, bool in_shouldDelete = false)
00223 : builder(in_builder)
00224 , shouldDelete(in_shouldDelete)
00225 {}
00226
00229 virtual ~MatchBuilderAdapter()
00230 {
00231 if (shouldDelete)
00232 delete builder;
00233 builder = 0;
00234 }
00235
00239 virtual void match( typename TypelessMatcher::AP customMatcher )
00240 {
00241 builder->match (new TypelessMatcherAdapter<I>(customMatcher));
00242 }
00243
00247 virtual void *getBuilder() const
00248 {
00249 return builder;
00250 }
00251
00252 private:
00253
00254 MatchBuilder<R, I> *builder;
00255 bool shouldDelete;
00256 };
00257
00258
00259 MOCKPP_NS_END
00260
00261
00262 #endif // MOCKPP_MATCHBUILDER_H