コード例 #1
0
ファイル: Fixture.cs プロジェクト: kmcginnes/fixie
 public Fixture(Type testClass, object instance, CaseBehavior caseExecutionBehavior, InvokeBehavior invokeBehavior, Case[] cases)
 {
     TestClass = testClass;
     Instance = instance;
     CaseExecutionBehavior = caseExecutionBehavior;
     InvokeBehavior = invokeBehavior;
     Cases = cases;
 }
コード例 #2
0
ファイル: MockExtensions.cs プロジェクト: wmadzha/moq
        /// <summary>
        /// Inserts a behavior into the mock behavior pipeline at the specified
        /// index for the current <see cref="MockContext.CurrentSetup"/> setup.
        /// </summary>
        public static IMock InsertBehavior(this IMock mock, int index, InvokeBehavior behavior, Lazy <string> displayName)
        {
            mock
            .BehaviorFor(MockContext.CurrentSetup ?? throw new InvalidOperationException(Strings.NoCurrentSetup))
            .Behaviors
            .Insert(index, new Behavior(behavior, displayName));

            return(mock);
        }
コード例 #3
0
 public void Execute(InvokeBehavior invokeBehavior, Case @case, object instance)
 {
     try
     {
         outer(@case, instance, () => inner.Execute(invokeBehavior, @case, instance));
     }
     catch (Exception exception)
     {
         @case.Fail(exception);
     }
 }
コード例 #4
0
ファイル: ProxyExtensions.cs プロジェクト: tralivali1234/moq
 /// <summary>
 /// Inserts a behavior into the proxy behasvior pipeline at the specified
 /// index.
 /// </summary>
 public static void InsertProxyBehavior(this object proxy, int index, InvokeBehavior behavior)
 {
     if (proxy is IProxy target)
     {
         target.Behaviors.Insert(index, ProxyBehavior.Create(behavior));
     }
     else
     {
         throw new ArgumentException(nameof(proxy));
     }
 }
コード例 #5
0
ファイル: ProxyExtensions.cs プロジェクト: tralivali1234/moq
 /// <summary>
 /// Adds a behavior to a proxy.
 /// </summary>
 public static void AddProxyBehavior(this object proxy, InvokeBehavior behavior)
 {
     if (proxy is IProxy target)
     {
         target.Behaviors.Add(ProxyBehavior.Create(behavior));
     }
     else
     {
         throw new ArgumentException(nameof(proxy));
     }
 }
コード例 #6
0
        /// <summary>
        /// Inserts a behavior into the stunt behavior pipeline at the specified
        /// index.
        /// </summary>
        public static TStunt InsertBehavior <TStunt>(this TStunt stunt, int index, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null)
        {
            if (stunt is IStunt target)
            {
                target.Behaviors.Insert(index, StuntBehavior.Create(behavior, appliesTo, name));
            }
            else
            {
                throw new ArgumentException(nameof(stunt));
            }

            return(stunt);
        }
コード例 #7
0
ファイル: MockExtensions.cs プロジェクト: ajaishankar/moq
        static TResult Returns <TResult>(Delegate value, InvokeBehavior behavior)
        {
            var invocation = CallContext <IMethodInvocation> .GetData();

            EnsureCompatible(invocation, value);

            var mock = ((IMocked)invocation.Target).Mock;

            mock.Invocations.Remove(invocation);
            mock.Behaviors.Add(new InvocationFilterBehavior(Matchers.AppliesTo(invocation), behavior, "Returns"));

            return(default(TResult));
        }
コード例 #8
0
ファイル: BehaviorPipeline.cs プロジェクト: moq/moq.proxy
		/// <summary>
		/// Execute the pipeline with the given input.
		/// </summary>
		/// <param name="input">Input to the method call.</param>
		/// <param name="target">The ultimate target of the call.</param>
		/// <returns>Return value from the pipeline.</returns>
		public IMethodReturn Invoke (IMethodInvocation input, InvokeBehavior target)
		{
			if (Behaviors.Count == 0)
				return target (input, null);

			var index = 0;
			return Behaviors[0].Invoke (input, () => {
				++index;
				return (index < Behaviors.Count) ?
					Behaviors[index].Invoke :
					target;
			});
		}
コード例 #9
0
        /// <summary>
        /// Inserts a behavior into the proxy behasvior pipeline at the specified
        /// index.
        /// </summary>
        public static TProxy InsertBehavior <TProxy>(this TProxy proxy, int index, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null)
        {
            if (proxy is IProxy target)
            {
                target.Behaviors.Insert(index, ProxyBehavior.Create(behavior, appliesTo, name));
            }
            else
            {
                throw new ArgumentException(nameof(proxy));
            }

            return(proxy);
        }
コード例 #10
0
        /// <summary>
        /// Adds a behavior to a proxy.
        /// </summary>
        public static TProxy AddBehavior <TProxy>(this TProxy proxy, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null)
        {
            // We can't just add a constraint to the method signature, because
            // proxies are typically geneated and don't expose the IProxy interface directly.
            if (proxy is IProxy target)
            {
                target.Behaviors.Add(ProxyBehavior.Create(behavior, appliesTo, name));
            }
            else
            {
                throw new ArgumentException(nameof(proxy));
            }

            return(proxy);
        }
コード例 #11
0
        /// <summary>
        /// Execute the pipeline with the given input.
        /// </summary>
        /// <param name="input">Input to the method call.</param>
        /// <param name="target">The ultimate target of the call.</param>
        /// <returns>Return value from the pipeline.</returns>
        public IMethodReturn Invoke(IMethodInvocation input, InvokeBehavior target)
        {
            if (Behaviors.Count == 0)
            {
                return(target(input, null));
            }

            var index = 0;

            return(Behaviors[0].Invoke(input, () =>
            {
                ++index;
                return (index < Behaviors.Count) ?
                Behaviors[index].Invoke :
                target;
            }));
        }
コード例 #12
0
ファイル: MockExtensions.cs プロジェクト: zhangwenquan/moq
        static TResult Returns <TResult>(Delegate value, InvokeBehavior behavior)
        {
            var setup = MockSetup.Current;

            if (setup != null)
            {
                EnsureCompatible(setup.Invocation, value);
                var mock = ((IMocked)setup.Invocation.Target).Mock;

                mock.Invocations.Remove(setup.Invocation);
                var mockBehavior = mock.BehaviorFor(setup);

                mockBehavior.Behaviors.Add(new InvocationBehavior(behavior, "Returns"));
            }

            return(default(TResult));
        }
コード例 #13
0
        /// <summary>
        /// Invoke the pipeline with the given input.
        /// </summary>
        /// <param name="invocation">Input to the method call.</param>
        /// <param name="target">The ultimate target of the call.</param>
        /// <param name="throwOnException">Whether to throw the <see cref="IMethodReturn.Exception"/> if it has a value after running
        /// the beaviors.</param>
        /// <returns>Return value from the pipeline.</returns>
        public IMethodReturn Invoke(IMethodInvocation invocation, InvokeBehavior target, bool throwOnException = false)
        {
            if (Behaviors.Count == 0)
            {
                return(target(invocation, null));
            }

            var index = -1;

            for (var i = 0; i < Behaviors.Count; i++)
            {
                if (Behaviors[i].AppliesTo(invocation))
                {
                    index = i;
                    break;
                }
            }

            if (index == -1)
            {
                return(target(invocation, null));
            }

            var result = Behaviors[index].Invoke(invocation, () =>
            {
                for (index++; index < Behaviors.Count; index++)
                {
                    if (Behaviors[index].AppliesTo(invocation))
                    {
                        break;
                    }
                }

                return((index < Behaviors.Count) ?
                       Behaviors[index].Invoke :
                       target);
            });

            if (throwOnException && result.Exception != null)
            {
                throw result.Exception;
            }

            return(result);
        }
コード例 #14
0
ファイル: ReturnsExtension.cs プロジェクト: wmadzha/moq
        static TResult Returns <TResult>(Delegate value, InvokeBehavior behavior)
        {
            var setup = MockContext.CurrentSetup;

            if (setup != null)
            {
                // TODO: Is this even necessary given that IntelliSense gives us
                // the right compiler safety already?
                setup.Invocation.EnsureCompatible(value);

                var mock = setup.Invocation.Target.GetMock();
                mock.Invocations.Remove(setup.Invocation);
                var mockBehavior = mock.BehaviorFor(setup);

                mockBehavior.Behaviors.Add(new Behavior(behavior, "Returns(() => ...)"));
            }

            return(default(TResult));
        }
コード例 #15
0
ファイル: Invoke.cs プロジェクト: kmcginnes/fixie
        public void Execute(InvokeBehavior invokeBehavior, Case @case, object instance)
        {
            try
            {
                var method = @case.Method;

                bool isDeclaredAsync = method.IsAsync();

                if (isDeclaredAsync && method.IsVoid())
                    ThrowForUnsupportedAsyncVoid();

                object result;
                try
                {
                    result = invokeBehavior.Execute(method, instance);
                }
                catch (TargetInvocationException exception)
                {
                    throw new PreservedException(exception.InnerException);
                }

                if (isDeclaredAsync)
                {
                    var task = (Task)result;
                    try
                    {
                        task.Wait();
                    }
                    catch (AggregateException exception)
                    {
                        throw new PreservedException(exception.InnerExceptions.First());
                    }
                }
            }
            catch (Exception exception)
            {
                @case.Fail(exception);
            }
        }
コード例 #16
0
ファイル: BehaviorPipeline.cs プロジェクト: ajaishankar/moq
        /// <summary>
        /// Invoke the pipeline with the given input.
        /// </summary>
        /// <param name="input">Input to the method call.</param>
        /// <param name="target">The ultimate target of the call.</param>
        /// <param name="throwOnException">Whether to throw the <see cref="IMethodReturn.Exception"/> if it has a value after running
        /// the beaviors.</param>
        /// <returns>Return value from the pipeline.</returns>
        public IMethodReturn Invoke(IMethodInvocation input, InvokeBehavior target, bool throwOnException = false)
        {
            if (Behaviors.Count == 0)
            {
                return(target(input, null));
            }

            var index  = 0;
            var result = Behaviors[0].Invoke(input, () =>
            {
                ++index;
                return((index < Behaviors.Count) ?
                       Behaviors[index].Invoke :
                       target);
            });

            if (throwOnException && result.Exception != null)
            {
                throw result.Exception;
            }

            return(result);
        }
コード例 #17
0
ファイル: MockBehavior.cs プロジェクト: ajaishankar/moq
 public AnonymousMockBehavior(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name)
 {
     this.appliesTo = appliesTo;
     this.behavior  = behavior;
     this.name      = name;
 }
コード例 #18
0
ファイル: MockBehavior.cs プロジェクト: ajaishankar/moq
 /// <summary>
 /// Creates an <see cref="IMockBehavior"/> from an anonymous delegate/lambda.
 /// </summary>
 /// <param name="appliesTo">The condition for the <see cref="IMockBehavior.AppliesTo(IMethodInvocation)"/> method.</param>
 /// <param name="behavior">The behavior to invoke for <see cref="IProxyBehavior.Invoke(IMethodInvocation, GetNextBehavior)"/>
 /// whenever the <paramref name="appliesTo"/> condition is satisfied by the current method invocation.</param>
 /// <param name="name">Optional name of the anonymous behavior to add.</param>
 public static IMockBehavior Create(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null) =>
 new AnonymousMockBehavior(appliesTo, behavior, name);
コード例 #19
0
 public InvocationBehavior(InvokeBehavior invoke, string name = null)
 {
     Invoke = invoke;
     Name   = name;
 }
コード例 #20
0
ファイル: MockExtensions.cs プロジェクト: ajaishankar/moq
        /// <summary>
        /// Inserts a behavior into the mock behasvior pipeline at the specified
        /// index.
        /// </summary>
        public static TMock InsertBehavior <TMock>(this TMock mock, int index, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null)
        {
            if (mock is IMocked mocked)
            {
                mocked.Mock.Behaviors.Insert(index, MockBehavior.Create(appliesTo, behavior, name));
            }
            else
            {
                throw new ArgumentException(nameof(mock));
            }

            return(mock);
        }
コード例 #21
0
ファイル: Behavior.cs プロジェクト: wmadzha/moq
 /// <summary>
 /// Creates an instance of the invokable behavior with the given
 /// delegate and friendly display name.
 /// </summary>
 public Behavior(InvokeBehavior invoke, string displayName)
     : this(invoke, new Lazy <string>(() => displayName))
 {
 }
コード例 #22
0
 /// <summary>
 /// Inserts a behavior into the proxy behavior pipeline at the specified
 /// index.
 /// </summary>
 public static IProxy InsertBehavior(this IProxy proxy, int index, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null)
 {
     proxy.Behaviors.Insert(index, ProxyBehavior.Create(behavior, appliesTo, name));
     return(proxy);
 }
コード例 #23
0
ファイル: MockExtensions.cs プロジェクト: ajaishankar/moq
 /// <summary>
 /// Adds a behavior to a mock.
 /// </summary>
 public static IMock AddBehavior(this IMock mock, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null)
 {
     mock.Behaviors.Add(MockBehavior.Create(appliesTo, behavior, name));
     return(mock);
 }
コード例 #24
0
 /// <summary>
 /// Adds a behavior to a mock.
 /// </summary>
 public static IMock AddBehavior(this IMock mock, InvokeBehavior behavior, string name = null)
 {
     mock.BehaviorFor(MockSetup.Current).Behaviors.Add(new InvocationBehavior(behavior, name));
     return(mock);
 }
コード例 #25
0
 /// <summary>
 /// Inserts a behavior into the mock behavior pipeline at the specified
 /// index.
 /// </summary>
 public static IMock InsertBehavior(this IMock mock, int index, InvokeBehavior behavior, string name = null)
 {
     mock.BehaviorFor(MockSetup.Current).Behaviors.Insert(index, new InvocationBehavior(behavior, name));
     return(mock);
 }
コード例 #26
0
ファイル: MockBehavior.cs プロジェクト: tralivali1234/moq
 public AnonymousMockBehavior(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior)
 {
     this.appliesTo = appliesTo;
     this.behavior  = behavior;
 }
コード例 #27
0
 /// <summary>
 /// Creates an <see cref="AnonymousProxyBehavior"/> for the given
 /// <see cref="InvokeBehavior"/> delegate.
 /// </summary>
 /// <param name="behavior">The actual behavior that will be invoked as part of the stunt.</param>
 /// <param name="appliesTo">Whether the <paramref name="behavior"/> applies to a particular <see cref="IMethodInvocation"/>. Defaults to 'always applies'.</param>
 /// <param name="name">Optional friendly name for the behavior for diagnostics.</param>
 public static IStuntBehavior Create(InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null)
 => new AnonymousProxyBehavior(behavior, appliesTo, name);
コード例 #28
0
 public AnonymousProxyBehavior(InvokeBehavior behavior, AppliesTo appliesTo, string name)
 {
     this.behavior  = behavior;
     this.appliesTo = appliesTo ?? new AppliesTo(invocation => true);
     this.name      = name;
 }
コード例 #29
0
 /// <summary>
 /// Adds a behavior to a proxy.
 /// </summary>
 public static IProxy AddBehavior(this IProxy proxy, InvokeBehavior behavior, AppliesTo appliesTo = null, string name = null)
 {
     proxy.Behaviors.Add(ProxyBehavior.Create(behavior, appliesTo, name));
     return(proxy);
 }
コード例 #30
0
ファイル: MockExtensions.cs プロジェクト: ajaishankar/moq
 /// <summary>
 /// Inserts a behavior into the mock behasvior pipeline at the specified
 /// index.
 /// </summary>
 public static IMock InsertBehavior(this IMock mock, int index, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null)
 {
     mock.Behaviors.Insert(index, MockBehavior.Create(appliesTo, behavior, name));
     return(mock);
 }
コード例 #31
0
ファイル: ProxyExtensions.cs プロジェクト: tralivali1234/moq
 /// <summary>
 /// Inserts a behavior into the proxy behavior pipeline at the specified
 /// index.
 /// </summary>
 public static void InsertProxyBehavior(this IProxy proxy, int index, InvokeBehavior behavior)
 {
     proxy.Behaviors.Insert(index, ProxyBehavior.Create(behavior));
 }
コード例 #32
0
ファイル: MockExtensions.cs プロジェクト: ajaishankar/moq
        /// <summary>
        /// Adds a behavior to a mock.
        /// </summary>
        public static TMock AddBehavior <TMock>(this TMock mock, Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior, string name = null)
        {
            // We can't just add a constraint to the method signature, because this is
            // implemented internally for Moq.Sdk to consume.
            if (mock is IMocked mocked)
            {
                mocked.Mock.Behaviors.Add(MockBehavior.Create(appliesTo, behavior, name));
            }
            else
            {
                throw new ArgumentException(nameof(mock));
            }

            return(mock);
        }
コード例 #33
0
ファイル: MockBehavior.cs プロジェクト: tralivali1234/moq
 public static IMockBehavior Create(Func <IMethodInvocation, bool> appliesTo, InvokeBehavior behavior) =>
 new AnonymousMockBehavior(appliesTo, behavior);
コード例 #34
0
ファイル: Behavior.cs プロジェクト: wmadzha/moq
 /// <summary>
 /// Creates an instance of the invokable behavior with the given
 /// delegate and friendly display name.
 /// </summary>
 /// <remarks>
 /// Use this constructor overload whenever constructing the display
 /// name is somewhat expensive.
 /// </remarks>
 public Behavior(InvokeBehavior invoke, Lazy <string> displayName)
 {
     Invoke           = invoke;
     this.displayName = displayName;
 }