public void GetBestMatchingSetup_InstanceMethodWithArguments_ReturnsOnlyOnTargetAndArgumentsMatch(
            bool targetsMatch, bool argumentsMatch)
        {
            var subject = new SetupMatcher(_setupManagerMock.Object,
                                           _targetMatcherMock.Object, _argumentMatcherMock.Object);

            var method    = ReflectionUtility.GetMethod(() => "Test".IndexOf("c"));
            var arguments = new object[] { "Test", "c" }; // First argument is the target

            var setups = TestDataFactory.CreateSetups(() => "Test".IndexOf("c"));

            _setupManagerMock.Setup(manager => manager.GetSetupsForMethod(method))
            .Returns(setups);

            _targetMatcherMock
            .Setup(matcher => matcher.IsMatch(typeof(string), setups[0].MethodCall.Arguments[0], "Test"))
            .Returns(targetsMatch);

            _argumentMatcherMock
            .Setup(matcher => matcher.IsMatch(
                       It.Is <IEnumerable <Expression> >(enumerable => enumerable.SequenceEqual(setups[0].MethodCall.Arguments.Skip(1))),
                       It.Is <IEnumerable <object> >(enumerable => enumerable.SequenceEqual(arguments.Skip(1)))))
            .Returns(argumentsMatch);

            IInternalSetup result = subject.GetBestMatchingSetup(method, arguments);

            if (targetsMatch && argumentsMatch)
            {
                Assert.AreSame(setups[0], result);
            }
            else
            {
                Assert.IsNull(result);
            }
        }
        public void GetBestMatchingSetup_StaticMethodWithArguments_InvokesArgumentCheckerAndReturnsOnlyOnMatch(
            bool argumentsMatch)
        {
            var subject = new SetupMatcher(_setupManagerMock.Object,
                                           _targetMatcherMock.Object, _argumentMatcherMock.Object);

            var method    = ReflectionUtility.GetMethod(() => Console.WriteLine("Test"));
            var arguments = new object[] { "Test" };

            var setups = TestDataFactory.CreateSetups(() => Console.WriteLine("Test"));

            _setupManagerMock.Setup(manager => manager.GetSetupsForMethod(method))
            .Returns(setups);

            _argumentMatcherMock
            .Setup(matcher => matcher.IsMatch(setups[0].MethodCall.Arguments, arguments))
            .Returns(argumentsMatch);

            IInternalSetup result = subject.GetBestMatchingSetup(method, arguments);

            if (argumentsMatch)
            {
                Assert.AreSame(setups[0], result);
            }
            else
            {
                Assert.IsNull(result);
            }
        }
        public void GetBestMatchingSetup_InstanceMethodWithoutArguments_InvokesTargetCheckerAndReturnsOnlyOnMatch(
            bool targetsMatch)
        {
            var subject = new SetupMatcher(_setupManagerMock.Object,
                                           _targetMatcherMock.Object, _argumentMatcherMock.Object);

            var method    = ReflectionUtility.GetMethod(() => "Test".Trim());
            var arguments = new object[] { "Test" }; // First argument is the target

            var setups = TestDataFactory.CreateSetups(() => "Test".Trim());

            _setupManagerMock.Setup(manager => manager.GetSetupsForMethod(method))
            .Returns(setups);

            _targetMatcherMock
            .Setup(matcher => matcher.IsMatch(typeof(string), setups[0].MethodCall.Arguments[0], "Test"))
            .Returns(targetsMatch);

            IInternalSetup result = subject.GetBestMatchingSetup(method, arguments);

            if (targetsMatch)
            {
                Assert.AreSame(setups[0], result);
            }
            else
            {
                Assert.IsNull(result);
            }
        }
Exemple #4
0
        public void Track(MethodBase method, object[] arguments, IInternalSetup setup)
        {
            Invocation invocation = new Invocation(setup, arguments);

            _invocations.AddOrUpdate(
                method,
                new List<Invocation> { invocation },
                (key, existing) =>
                {
                    existing.Add(invocation);
                    return existing;
                });
        }
Exemple #5
0
        internal InterceptorResult InterceptVoidMethod(object[] arguments, MethodBase originalMethod)
        {
            IInternalSetup setup = _setupMatcher.GetBestMatchingSetup(originalMethod, arguments);

            _invocationTracker.Track(originalMethod, arguments, setup);

            if (setup == null)
            {
                return(new InterceptorResult(false));
            }

            HandleSetup(arguments, setup, originalMethod);

            return(new InterceptorResult(true));
        }
Exemple #6
0
        public void Track(MethodBase method, object[] arguments, IInternalSetup setup)
        {
            Invocation invocation = new Invocation(setup, arguments);

            _invocations.AddOrUpdate(
                method,
                new List <Invocation> {
                invocation
            },
                (key, existing) =>
            {
                existing.Add(invocation);
                return(existing);
            });
        }
Exemple #7
0
        private void AddSetup(IInternalSetup setup)
        {
            // Standard pattern for thread-safe adding to an immutable list:
            // Take the current list, create the new list with the added item,
            // then try to atomically swap the new list with the old list, checking
            // that it hasn't changed in the meantime. If we cannot swap because the list
            // has changed in the meantime, retry. Repeat infinitely.
            while (true)
            {
                ImmutableList <IInternalSetup> snapshot = _setups;
                var added = snapshot.Add(setup);

                if (Interlocked.CompareExchange(ref _setups, added, snapshot) == snapshot)
                {
                    break;
                }
            }
        }
        public void GetBestMatchingSetup_MultipleMatchingSetups_ReturnsLastSetup()
        {
            var subject = new SetupMatcher(_setupManagerMock.Object,
                                           _targetMatcherMock.Object, _argumentMatcherMock.Object);

            var method    = ReflectionUtility.GetMethod(() => Console.WriteLine(string.Empty));
            var arguments = new object[] { "Test" };

            var setups = TestDataFactory.CreateSetups(() => Console.WriteLine(string.Empty), () => Console.WriteLine(string.Empty));

            _setupManagerMock.Setup(manager => manager.GetSetupsForMethod(method))
            .Returns(setups);

            _argumentMatcherMock
            .Setup(matcher => matcher.IsMatch(It.IsAny <IEnumerable <Expression> >(), arguments))
            .Returns(true);

            IInternalSetup result = subject.GetBestMatchingSetup(method, arguments);

            Assert.AreSame(setups[1], result);
        }
Exemple #9
0
 public Invocation(IInternalSetup matchedSetup, object[] arguments)
 {
     MatchedSetup = matchedSetup;
     _arguments   = arguments;
 }
Exemple #10
0
 public Invocation(IInternalSetup matchedSetup, object[] arguments)
 {
     MatchedSetup = matchedSetup;
     Arguments = arguments;
 }