public GenericToNonGenericMatcherProxyWithDescribe(IArgumentMatcher <T> matcher) : base(matcher) { if (matcher is not IDescribeNonMatches) { throw new SubstituteInternalException("Should implement IDescribeNonMatches type."); } }
/// <summary> /// Pushes an argument matcher in the current <see cref="CallContext{Queue{IArgumentMatcher}}"/> /// and returns a default value for <typeparamref name="T"/>. /// </summary> public static T Push <T>(IArgumentMatcher matcher) { CallContext <Queue <IArgumentMatcher> > .GetData(() => new Queue <IArgumentMatcher>()) .Enqueue(matcher); return(default(T)); }
public GenericToNonGenericMatcherProxyWithDescribe(IArgumentMatcher <T> matcher) : base(matcher) { if (matcher as IDescribeNonMatches == null) { throw new ArgumentException("Should implement IDescribeNonMatches type."); } }
private ImmutableList <IArgumentMatcher> CreateAllArgumentsMatchers() { // The purpose of this method is to add argument matchers // for arguments that were specified by passing non-default values. // In other words we transform: // _component.MethodCall(1, Arg.Any<int>(), 5) // into: // _component.MethodCall(Arg.Is(1), Arg.Any<int>(), Arg.Is(5)) // // Missing argument matchers are replaced by MissingArgumentMatcherPlaceholder instances. // Also notice that missing argument matchers will be used quite often, // every *non-setup* method call with a default value will trigger // creation of a missing argument matcher. var allArgumentsMatchers = ImmutableList.CreateBuilder <IArgumentMatcher>(); var explicitMatchers = new Queue <IArgumentMatcher>(_explicitMatchers); for (int i = 0; i < _methodCall.PassedArguments.Count; i++) { Type parameterType = _methodCall.CalledMethod.ParameterTypes[i]; object argumentValue = _methodCall.PassedArguments[i]; // TODO: To extension method on type, isDefaultValue IArgumentMatcher matcher = ReflectionUtils.IsDefaultValue(parameterType, argumentValue) ? explicitMatchers.DequeueOrElse(() => new MissingArgumentMatcherPlaceholder(_methodCall, i)) : CreateMatcherFromArgumentValue(parameterType, argumentValue); allArgumentsMatchers.Add(matcher); } return(allArgumentsMatchers.ToImmutable()); }
private static bool MatchesImpl <T>(T argumentValue, IArgumentMatcher matcher) { if (matcher is IArgumentMatcher <T> typedMatcher) { return(typedMatcher.Matches(argumentValue)); } return(false); }
/// <summary> /// Pushes an argument matcher in the current <see cref="CallContext{Queue{IArgumentMatcher}}"/> /// and returns a default value for <typeparamref name="T"/>. /// </summary> public static T Push <T>(IArgumentMatcher matcher) { CallContext <Queue <IArgumentMatcher> > .GetData(() => new Queue <IArgumentMatcher>()) .Enqueue(matcher); // Pushing new matchers clears the last known setup. lastSetup.Value = null; return(default(T)); }
internal SetupMatcher(ISetupManager setupManager, ITargetMatcher targetMatcher, IArgumentMatcher argumentMatcher) { ArgumentChecker.NotNull(setupManager, () => setupManager); ArgumentChecker.NotNull(targetMatcher, () => targetMatcher); ArgumentChecker.NotNull(argumentMatcher, () => argumentMatcher); _setupManager = setupManager; _targetMatcher = targetMatcher; _argumentMatcher = argumentMatcher; }
private static bool Matches(Type parameterType, object argumentValue, IArgumentMatcher matcher) { // Calls Matches<arg.Type>(arg.Value, matcher) var genericMethod = typeof(MethodCallMatcher) .GetMethod(nameof(MatchesImpl), BindingFlags.NonPublic | BindingFlags.Static); var concreteMethod = genericMethod.MakeGenericMethod(parameterType); var args = new[] { argumentValue, matcher }; return((bool)concreteMethod.Invoke(null, args)); }
/// <summary> /// Enqueues a matcher for the method argument in current position and returns the value which should be /// passed back to the method you invoke. /// </summary> public static ref T?Enqueue <T>(IArgumentMatcher <T> argumentMatcher) { if (argumentMatcher == null) { throw new ArgumentNullException(nameof(argumentMatcher)); } IArgumentMatcher nonGenericMatcher = argumentMatcher switch { IDescribeNonMatches => new GenericToNonGenericMatcherProxyWithDescribe <T>(argumentMatcher), _ => new GenericToNonGenericMatcherProxy <T>(argumentMatcher) }; return(ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), nonGenericMatcher))); }
/// <summary> /// Enqueues a matcher for the method argument in current position and returns the value which should be /// passed back to the method you invoke. /// </summary> public static ref T Enqueue <T>(IArgumentMatcher <T> argumentMatcher) { if (argumentMatcher == null) { throw new ArgumentNullException(nameof(argumentMatcher)); } IArgumentMatcher nonGenericMatcher; if (argumentMatcher is IDescribeNonMatches) { nonGenericMatcher = new GenericToNonGenericMatcherProxyWithDescribe <T>(argumentMatcher); } else { nonGenericMatcher = new GenericToNonGenericMatcherProxy <T>(argumentMatcher); } return(ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), nonGenericMatcher))); }
public bool MatchesCall(IMethodCall call) { if (!Method.HasSameSignatureAs(call.CalledMethod)) { return(false); } Debug.Assert(ArgumentMatchers.Count == call.PassedArguments.Count); for (int i = 0; i < ArgumentMatchers.Count; i++) { IArgumentMatcher matcher = ArgumentMatchers[i]; Type parameterType = call.CalledMethod.ParameterTypes[i]; object argumentValue = call.PassedArguments[i]; if (!Matches(parameterType, argumentValue, matcher)) { return(false); } } return(true); }
internal static ref T?Enqueue <T>(IArgumentMatcher argumentMatcher, Action <object?> action) => ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), argumentMatcher, action));
public FluentArgumentSpecification(IArgumentSpecification innerSpecification, IArgumentMatcher matcher) { this.innerSpecification = innerSpecification; this.matcher = matcher; }
public ArgumentSpecification(Type forType, IArgumentMatcher matcher) : this(forType, matcher, NoOpAction) { }
private static void EnqueueMatcher <T>(IArgumentMatcher lambdaMatcher) { SubstitutionContext.Current.ThreadContext.EnqueueArgumentSpecification( new ArgumentSpecification(typeof(T), lambdaMatcher)); }
internal static ref T Enqueue <T>(IArgumentMatcher argumentMatcher, Action <object> action) { return(ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), argumentMatcher, action))); }
public WordMatchTaker(IArgumentMatcher matcher, bool required) { Matcher = matcher; Required = required; }
internal static ref T?Enqueue <T>(IArgumentMatcher argumentMatcher) => ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), argumentMatcher));
public void AddExplicitMatcher(IArgumentMatcher matcher) => _explicitMatchers.Add(matcher);
public GenericToNonGenericMatcherProxy(IArgumentMatcher <T> matcher) { _matcher = matcher; }
public override void Context() { base.Context(); _argumentMatcher = mock <IArgumentMatcher>(); }
/// <summary> /// Adds an argument matcher for the current mock invocation. /// </summary> public static void AddMatcher(IArgumentMatcher matcher) { argumentMatchers.Value.Push(matcher); lastPipeline.Value = null; }
public static void AddArgumentMatcher(IArgumentMatcher matcher) => _context.Value.AddArgumentMatcher(matcher);
public static void AddArgumentMatcher(IArgumentMatcher matcher) => Internal.ThreadLocalContext.AddArgumentMatcher(matcher);
/// <summary> /// Pushes an argument matcher in the current <see cref="CallContext{Queue{IArgumentMatcher}}"/>. /// </summary> public static void Push(IArgumentMatcher matcher) => Push <object>(matcher);
public static WordMatchTaker ToOptionalWordTaker(this IArgumentMatcher matcher) => new WordMatchTaker(matcher, required: false);
private static ref T EnqueueSpecFor <T>(IArgumentMatcher argumentMatcher) { var argumentSpecification = new ArgumentSpecification(typeof(T), argumentMatcher); return(ref EnqueueArgSpecification <T>(argumentSpecification)); }
public static WordMatchTaker ToRequiredWordTaker(this IArgumentMatcher matcher) => new WordMatchTaker(matcher, required: true);
public ArgumentSpecification(Type forType, IArgumentMatcher matcher, Action <object> action) { _forType = forType; _matcher = matcher; _action = action; }
public ArgumentSpecification(Type forType, IArgumentMatcher matcher, Action<object> action) { _forType = forType; _matcher = matcher; _action = action; }
/// <summary> /// Match the argument using the specified <paramref name="argumentMatcher"/>. /// </summary> public static ref T Matches <T>(IArgumentMatcher argumentMatcher) { return(ref EnqueueSpecFor <T>(argumentMatcher)); }