Exemplo n.º 1
0
 public GenericToNonGenericMatcherProxyWithDescribe(IArgumentMatcher <T> matcher) : base(matcher)
 {
     if (matcher is not IDescribeNonMatches)
     {
         throw new SubstituteInternalException("Should implement IDescribeNonMatches type.");
     }
 }
Exemplo n.º 2
0
        /// <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));
        }
Exemplo n.º 3
0
 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);
        }
Exemplo n.º 6
0
        /// <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));
        }
Exemplo n.º 7
0
        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));
        }
Exemplo n.º 9
0
        /// <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)));
        }
Exemplo n.º 10
0
        /// <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);
        }
Exemplo n.º 12
0
 internal static ref T?Enqueue <T>(IArgumentMatcher argumentMatcher, Action <object?> action) =>
 ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), argumentMatcher, action));
Exemplo n.º 13
0
 public FluentArgumentSpecification(IArgumentSpecification innerSpecification, IArgumentMatcher matcher)
 {
     this.innerSpecification = innerSpecification;
     this.matcher            = matcher;
 }
Exemplo n.º 14
0
 public ArgumentSpecification(Type forType, IArgumentMatcher matcher)
     : this(forType, matcher, NoOpAction)
 {
 }
 public ArgumentSpecification(Type forType, IArgumentMatcher matcher) : this(forType, matcher, NoOpAction)
 {
 }
Exemplo n.º 16
0
 private static void EnqueueMatcher <T>(IArgumentMatcher lambdaMatcher)
 {
     SubstitutionContext.Current.ThreadContext.EnqueueArgumentSpecification(
         new ArgumentSpecification(typeof(T),
                                   lambdaMatcher));
 }
Exemplo n.º 17
0
 internal static ref T Enqueue <T>(IArgumentMatcher argumentMatcher, Action <object> action)
 {
     return(ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), argumentMatcher, action)));
 }
Exemplo n.º 18
0
 public WordMatchTaker(IArgumentMatcher matcher, bool required)
 {
     Matcher  = matcher;
     Required = required;
 }
Exemplo n.º 19
0
 internal static ref T?Enqueue <T>(IArgumentMatcher argumentMatcher) =>
 ref EnqueueArgSpecification <T>(new ArgumentSpecification(typeof(T), argumentMatcher));
 public void AddExplicitMatcher(IArgumentMatcher matcher)
 => _explicitMatchers.Add(matcher);
Exemplo n.º 21
0
 public GenericToNonGenericMatcherProxy(IArgumentMatcher <T> matcher)
 {
     _matcher = matcher;
 }
 public override void Context()
 {
     base.Context();
     _argumentMatcher = mock <IArgumentMatcher>();
 }
Exemplo n.º 23
0
 /// <summary>
 /// Adds an argument matcher for the current mock invocation.
 /// </summary>
 public static void AddMatcher(IArgumentMatcher matcher)
 {
     argumentMatchers.Value.Push(matcher);
     lastPipeline.Value = null;
 }
Exemplo n.º 24
0
 public static void AddArgumentMatcher(IArgumentMatcher matcher)
 => _context.Value.AddArgumentMatcher(matcher);
Exemplo n.º 25
0
 public static void AddArgumentMatcher(IArgumentMatcher matcher)
 => Internal.ThreadLocalContext.AddArgumentMatcher(matcher);
Exemplo n.º 26
0
 /// <summary>
 /// Pushes an argument matcher in the current <see cref="CallContext{Queue{IArgumentMatcher}}"/>.
 /// </summary>
 public static void Push(IArgumentMatcher matcher) => Push <object>(matcher);
Exemplo n.º 27
0
 public static WordMatchTaker ToOptionalWordTaker(this IArgumentMatcher matcher)
 => new WordMatchTaker(matcher, required: false);
Exemplo n.º 28
0
        private static ref T EnqueueSpecFor <T>(IArgumentMatcher argumentMatcher)
        {
            var argumentSpecification = new ArgumentSpecification(typeof(T), argumentMatcher);

            return(ref EnqueueArgSpecification <T>(argumentSpecification));
        }
Exemplo n.º 29
0
 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;
 }
Exemplo n.º 31
0
		/// <summary>
		/// Adds an argument matcher for the current mock invocation.
		/// </summary>
		public static void AddMatcher(IArgumentMatcher matcher)
		{
			argumentMatchers.Value.Push(matcher);
			lastPipeline.Value = null;
		}
Exemplo n.º 32
0
 public ArgumentSpecification(Type forType, IArgumentMatcher matcher, Action<object> action)
 {
     _forType = forType;
     _matcher = matcher;
     _action = action;
 }
Exemplo n.º 33
0
 /// <summary>
 /// Match the argument using the specified <paramref name="argumentMatcher"/>.
 /// </summary>
 public static ref T Matches <T>(IArgumentMatcher argumentMatcher)
 {
     return(ref EnqueueSpecFor <T>(argumentMatcher));
 }