@@ -271,42 +271,46 @@ internal static CallPattern FromExpression(MocksRepository repository, Expressio
271271 callPattern . SetMethod ( method , checkCompatibility : true ) ;
272272
273273 //Finally, construct the arguments part of the call pattern.
274- bool hasParams = false ;
275- bool hasSingleValueInParams = false ;
276- if ( args != null && args . Length > 0 )
274+ using ( repository . StartArrangeArgMatching ( ) )
277275 {
278- var lastParameter = method . GetParameters ( ) . Last ( ) ;
279- if ( Attribute . IsDefined ( lastParameter , typeof ( ParamArrayAttribute ) ) && args . Last ( ) is NewArrayExpression )
280- {
281- hasParams = true ;
282- var paramsArg = ( NewArrayExpression ) args . Last ( ) ;
283- args = args . Take ( args . Length - 1 ) . Concat ( paramsArg . Expressions ) . ToArray ( ) ;
284- if ( paramsArg . Expressions . Count == 1 )
285- hasSingleValueInParams = true ;
286- }
276+ bool hasParams = false ;
277+ bool hasSingleValueInParams = false ;
287278
288- foreach ( var argument in args )
279+ if ( args != null && args . Length > 0 )
289280 {
290- callPattern . ArgumentMatchers . Add ( MocksRepository . CreateMatcherForArgument ( argument ) ) ;
291- }
281+ var lastParameter = method . GetParameters ( ) . Last ( ) ;
282+ if ( Attribute . IsDefined ( lastParameter , typeof ( ParamArrayAttribute ) ) && args . Last ( ) is NewArrayExpression )
283+ {
284+ hasParams = true ;
285+ var paramsArg = ( NewArrayExpression ) args . Last ( ) ;
286+ args = args . Take ( args . Length - 1 ) . Concat ( paramsArg . Expressions ) . ToArray ( ) ;
287+ if ( paramsArg . Expressions . Count == 1 )
288+ hasSingleValueInParams = true ;
289+ }
292290
293- if ( hasParams )
294- {
295- int paramsCount = method . GetParameters ( ) . Count ( ) ;
296- if ( hasSingleValueInParams )
291+ foreach ( var argument in args )
297292 {
298- IMatcher matcher = callPattern . ArgumentMatchers [ paramsCount - 1 ] ;
299- ITypedMatcher typeMatcher = matcher as ITypedMatcher ;
300- if ( typeMatcher != null && typeMatcher . Type != method . GetParameters ( ) . Last ( ) . ParameterType )
301- {
302- callPattern . ArgumentMatchers [ paramsCount - 1 ] = new ParamsMatcher ( new IMatcher [ ] { matcher } ) ;
303- }
293+ callPattern . ArgumentMatchers . Add ( MocksRepository . CreateMatcherForArgument ( argument ) ) ;
304294 }
305- else
295+
296+ if ( hasParams )
306297 {
307- IEnumerable < IMatcher > paramMatchers = callPattern . ArgumentMatchers . Skip ( paramsCount - 1 ) . Take ( callPattern . ArgumentMatchers . Count - paramsCount + 1 ) ;
308- callPattern . ArgumentMatchers = callPattern . ArgumentMatchers . Take ( paramsCount - 1 ) . ToList ( ) ;
309- callPattern . ArgumentMatchers . Add ( new ParamsMatcher ( paramMatchers . ToArray ( ) ) ) ;
298+ int paramsCount = method . GetParameters ( ) . Count ( ) ;
299+ if ( hasSingleValueInParams )
300+ {
301+ IMatcher matcher = callPattern . ArgumentMatchers [ paramsCount - 1 ] ;
302+ ITypedMatcher typeMatcher = matcher as ITypedMatcher ;
303+ if ( typeMatcher != null && typeMatcher . Type != method . GetParameters ( ) . Last ( ) . ParameterType )
304+ {
305+ callPattern . ArgumentMatchers [ paramsCount - 1 ] = new ParamsMatcher ( new IMatcher [ ] { matcher } ) ;
306+ }
307+ }
308+ else
309+ {
310+ IEnumerable < IMatcher > paramMatchers = callPattern . ArgumentMatchers . Skip ( paramsCount - 1 ) . Take ( callPattern . ArgumentMatchers . Count - paramsCount + 1 ) ;
311+ callPattern . ArgumentMatchers = callPattern . ArgumentMatchers . Take ( paramsCount - 1 ) . ToList ( ) ;
312+ callPattern . ArgumentMatchers . Add ( new ParamsMatcher ( paramMatchers . ToArray ( ) ) ) ;
313+ }
310314 }
311315 }
312316 }
@@ -319,7 +323,7 @@ internal static CallPattern FromExpression(MocksRepository repository, Expressio
319323 return callPattern ;
320324 }
321325
322- internal static CallPattern FromMethodBase ( object instance , MethodBase method , object [ ] arguments )
326+ internal static CallPattern FromMethodBase ( MocksRepository repository , object instance , MethodBase method , object [ ] arguments )
323327 {
324328 var callPattern = new CallPattern
325329 {
@@ -331,26 +335,34 @@ internal static CallPattern FromMethodBase(object instance, MethodBase method, o
331335
332336 callPattern . SetMethod ( method , checkCompatibility : true ) ;
333337
334- var parameters = method . GetParameters ( ) ;
335- if ( arguments == null || arguments . Length == 0 )
338+ using ( repository != null ? repository . StartArrangeArgMatching ( ) : null )
336339 {
337- callPattern . ArgumentMatchers . AddRange ( method . GetParameters ( ) . Select ( p => ( IMatcher ) new TypeMatcher ( p . ParameterType ) ) ) ;
338- }
339- else
340- {
341- if ( arguments . Length != method . GetParameters ( ) . Length )
340+ var parameters = method . GetParameters ( ) ;
341+ if ( arguments == null || arguments . Length == 0 )
342342 {
343- throw new MockException ( "Argument count mismatch." ) ;
343+ callPattern . ArgumentMatchers . AddRange ( method . GetParameters ( ) . Select ( p => ( IMatcher ) new TypeMatcher ( p . ParameterType ) ) ) ;
344344 }
345+ else
346+ {
347+ if ( arguments . Length != method . GetParameters ( ) . Length )
348+ {
349+ throw new MockException ( "Argument count mismatch." ) ;
350+ }
345351
346- callPattern . ArgumentMatchers . AddRange ( arguments . Select ( arg => MocksRepository . CreateMatcherForArgument ( arg ) ) ) ;
352+ callPattern . ArgumentMatchers . AddRange ( arguments . Select ( arg => MocksRepository . CreateMatcherForArgument ( arg ) ) ) ;
353+ }
347354 }
348355
349356 callPattern . AdjustForExtensionMethod ( ) ;
350357
351358 return callPattern ;
352359 }
353360
361+ internal static CallPattern FromMethodBase ( object instance , MethodBase method , object [ ] arguments )
362+ {
363+ return FromMethodBase ( null , instance , method , arguments ) ;
364+ }
365+
354366 internal static CallPattern FromAction ( MocksRepository repository , Action memberAction , bool dispatchToMethodMocks = false )
355367 {
356368 var callPattern = new CallPattern ( ) ;
@@ -365,7 +377,9 @@ internal static CallPattern FromAction(MocksRepository repository, Action member
365377 }
366378
367379 if ( lastInvocation == null )
380+ {
368381 throw new MockException ( "The specified action did not call a mocked method." ) ;
382+ }
369383
370384 callPattern . SetMethod ( lastInvocation . Method , checkCompatibility : true ) ;
371385 callPattern . InstanceMatcher = new ReferenceMatcher ( lastInvocation . Instance ) ;
@@ -374,11 +388,14 @@ internal static CallPattern FromAction(MocksRepository repository, Action member
374388 // one coming from a matcher, it is impossible to tell exactly which arguments are literal and which are matchers.
375389 // So, we assume that the user always first specifies some literal values, and then some matchers.
376390 // We assume that the user will never pass a literal after a matcher.
377- for ( int i = 0 ; i < lastInvocation . Args . Length ; ++ i )
391+ using ( repository . StartArrangeArgMatching ( ) )
378392 {
379- var indexInMatchers = i - ( lastInvocation . Args . Length - repository . MatchersInContext . Count ) ;
380- var matcher = indexInMatchers >= 0 ? repository . MatchersInContext [ indexInMatchers ] : new ValueMatcher ( lastInvocation . Args [ i ] ) ;
381- callPattern . ArgumentMatchers . Add ( matcher ) ;
393+ for ( int i = 0 ; i < lastInvocation . Args . Length ; ++ i )
394+ {
395+ var indexInMatchers = i - ( lastInvocation . Args . Length - repository . MatchersInContext . Count ) ;
396+ var matcher = indexInMatchers >= 0 ? repository . MatchersInContext [ indexInMatchers ] : new ValueMatcher ( lastInvocation . Args [ i ] ) ;
397+ callPattern . ArgumentMatchers . Add ( matcher ) ;
398+ }
382399 }
383400 repository . MatchersInContext . Clear ( ) ;
384401
0 commit comments