Example #1
0
        public void EqualIfTargetMethodAndArgumentsMatch()
        {
            var doThis  = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(Do)) !);
            var doThiss = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(Do)) !);

            Assert.Equal((object)doThis, doThiss);
            Assert.Equal(doThis, doThiss);
            Assert.Equal(doThis.GetHashCode(), doThiss.GetHashCode());
            Assert.True(doThis.Equals(doThiss));
            Assert.True(doThis.Equals((object)doThiss));

            var doOther = MethodInvocation.Create(new MethodInvocationTests(), typeof(MethodInvocationTests).GetMethod(nameof(Do)) !);

            Assert.NotEqual(doThis, doOther);

            var doInt5  = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithInt)) !, 5);
            var doInt5s = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithInt)) !, 5);

            Assert.NotEqual(doThis, doInt5);
            Assert.Equal(doInt5, doInt5s);
            Assert.Equal(doInt5.GetHashCode(), doInt5s.GetHashCode());

            var doInt6 = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithInt)) !, 6);

            Assert.NotEqual(doInt5, doInt6);

            var doIntNull  = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithNullableInt)) !, 5);
            var doIntNulls = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithNullableInt)) !, default(int?));

            Assert.NotEqual(doIntNull, doIntNulls);
            Assert.NotEqual(doIntNull.GetHashCode(), doIntNulls.GetHashCode());
        }
Example #2
0
        public void DoesNotFailWithNonMethodInfo()
        {
            var             ctor     = typeof(Foo).GetConstructors().First();
            IAvatarBehavior behavior = new DefaultValueBehavior();

            behavior.Execute(MethodInvocation.Create(new object(), ctor, PlatformID.Win32NT), null !);
        }
Example #3
0
        public void AppliesToGetHashCode()
        {
            var method   = typeof(Foo).GetMethod(nameof(object.GetHashCode)) !;
            var behavior = new DefaultEqualityBehavior();

            Assert.True(behavior.AppliesTo(MethodInvocation.Create(new Foo(), method)));
        }
Example #4
0
        public void CanExecutePipelineWithTarget()
        {
            var pipeline = new BehaviorPipeline();

            Action a = CanExecutePipelineWithTarget;

            pipeline.Execute(MethodInvocation.Create(this, a.GetMethodInfo(), (m, n) => m.CreateReturn()));
        }
Example #5
0
        public void TestDoWithNullableIntNull()
        {
            var invocation = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithNullableIntNull)) !, default(int?));

            var actual = invocation.ToString();

            Assert.Equal("void DoWithNullableIntNull(int? value: null)", actual);
        }
Example #6
0
        public void TestDoWithInt()
        {
            var invocation = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithInt)) !, 5);

            var actual = invocation.ToString();

            Assert.Equal("void DoWithInt(int value: 5)", actual);
        }
Example #7
0
        public void TestDoRef()
        {
            var invocation = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoRef)) !, 5);

            var actual = invocation.ToString();

            Assert.Equal("void DoRef(ref int i: 5)", actual);
        }
Example #8
0
        public void TestDoReturn()
        {
            var invocation = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoReturn)) !);

            var actual = invocation.ToString();

            Assert.Equal("bool DoReturn()", actual);
        }
Example #9
0
        public void TestDoWithString()
        {
            var invocation = MethodInvocation.Create(this, typeof(MethodInvocationTests).GetMethod(nameof(DoWithString)) !, "foo");

            var actual = invocation.ToString();

            Assert.Equal("void DoWithString(string value: \"foo\")", actual);
        }
Example #10
0
        public void SetsReturnEnum()
        {
            var             method   = typeof(IDefaultValues).GetMethod(nameof(IDefaultValues.ReturnEnum)) !;
            IAvatarBehavior behavior = new DefaultValueBehavior();

            var result = behavior.Execute(MethodInvocation.Create(new object(), method), null !);

            Assert.Equal(default(PlatformID), result.ReturnValue);
        }
Example #11
0
        public void WhenExecutingPipelineWithNoTarget_ThenThrowsIfNoBehaviorReturns()
        {
            var pipeline = new BehaviorPipeline();

            Action a = CanExecutePipelineNoTarget;

            Assert.Throws <NotImplementedException>(()
                                                    => pipeline.Execute(MethodInvocation.Create(this, a.GetMethodInfo())));
        }
Example #12
0
        public void WhenInvokingPipeline_ThenBehaviorsCanReturnException()
        {
            var pipeline = new BehaviorPipeline(
                new ExecuteHandler((m, n) => m.CreateExceptionReturn(new ArgumentException())));

            Action a = WhenInvokingPipeline_ThenBehaviorsCanReturnException;

            Assert.Throws <ArgumentException>(() => pipeline.Invoke(MethodInvocation.Create(this, a.GetMethodInfo()), true));
        }
Example #13
0
        public void WhenExecutingPipelineResultWithNoTarget_ThenThrowsIfNoResult()
        {
            var pipeline = new BehaviorPipeline();

            Func <object?> f = NonVoidMethod;

            Assert.Throws <NotImplementedException>(()
                                                    => pipeline.Execute <object>(MethodInvocation.Create(this, f.GetMethodInfo())));
        }
Example #14
0
        public void CanExecutePipelineResultNoTarget()
        {
            var value = new object();

            var pipeline = new BehaviorPipeline(new ExecuteHandler((m, n) => m.CreateValueReturn(value)));

            Func <object?> f = NonVoidMethod;

            Assert.Same(value, pipeline.Execute <object>(MethodInvocation.Create(this, f.GetMethodInfo())));
        }
Example #15
0
        public void NotEqualsDifferentInstance()
        {
            var method   = typeof(Foo).GetMethod(nameof(object.Equals)) !;
            var behavior = new DefaultEqualityBehavior();
            var target   = new Foo();

            var actual = (bool?)behavior.Execute(MethodInvocation.Create(target, method, new Foo()), null !).ReturnValue;

            Assert.False(actual);
        }
Example #16
0
        public void WhenExecutingPipelineResult_ThenBehaviorCanThrow()
        {
            var pipeline = new BehaviorPipeline(
                new ExecuteHandler((m, n) => m.CreateExceptionReturn(new ArgumentException())));

            Func <object?> f = NonVoidMethod;

            Assert.Throws <ArgumentException>(()
                                              => pipeline.Execute <object>(MethodInvocation.Create(this, f.GetMethodInfo())));
        }
Example #17
0
        public void SetsOutValue()
        {
            var             method   = typeof(IDefaultValues).GetMethod(nameof(IDefaultValues.VoidWithOut)) !;
            IAvatarBehavior behavior = new DefaultValueBehavior();

            var result = behavior.Execute(MethodInvocation.Create(new object(), method, Array.Empty <object>()), null !);

            Assert.Equal(1, result.Outputs.Count);
            Assert.NotNull(result.Outputs.GetValue(0));
            Assert.True(result.Outputs.GetValue(0) is object[]);
        }
Example #18
0
        public void GetsHashCode()
        {
            var method   = typeof(Foo).GetMethod(nameof(object.GetHashCode)) !;
            var behavior = new DefaultEqualityBehavior();
            var target   = new Foo();

            var expected = target.GetHashCode();
            var actual   = (int?)behavior.Execute(MethodInvocation.Create(target, method), null).ReturnValue;

            Assert.Equal(expected, actual);
        }
Example #19
0
        public void DoesNotSetRefValue()
        {
            var             method   = typeof(IDefaultValues).GetMethod(nameof(IDefaultValues.VoidWithRef)) !;
            IAvatarBehavior behavior = new DefaultValueBehavior();
            var             value    = new object[] { new object() };

            var result = behavior.Execute(MethodInvocation.Create(new object(), method, value), null !);

            Assert.Equal(1, result.Outputs.Count);
            Assert.NotNull(result.Outputs.GetValue(0));
            Assert.Same(result.Outputs.GetValue(0), value);
        }
Example #20
0
        public void DoesNotSetsRefEnumValue()
        {
            var             method   = typeof(IDefaultValues).GetMethod(nameof(IDefaultValues.VoidWithRefEnum)) !;
            IAvatarBehavior behavior = new DefaultValueBehavior();
            var             platform = PlatformID.Xbox;

            var result = behavior.Execute(MethodInvocation.Create(new object(), method, platform), null !);

            Assert.Equal(1, result.Outputs.Count);
            Assert.NotNull(result.Outputs.GetValue(0));
            Assert.Equal(platform, result.Outputs.GetValue(0));
        }
Example #21
0
        public void WhenSkippingBehavior_ThenBehaviorIsNotExecuted()
        {
            var pipeline = new BehaviorPipeline();

            pipeline.Behaviors.Add(new TestBehavior());

            var invocation = MethodInvocation.Create(new object(), typeof(object).GetMethod("ToString") !);

            invocation.SkipBehavior <TestBehavior>();

            Assert.Throws <NotImplementedException>(()
                                                    => pipeline.Execute <string>(invocation));
        }
Example #22
0
        public void WhenInvokingPipelineWithNoBehaviors_ThenInvokesTarget()
        {
            var targetCalled = false;

            var pipeline = new BehaviorPipeline();

            Action a = WhenInvokingPipelineWithNoBehaviors_ThenInvokesTarget;

            pipeline.Invoke(MethodInvocation.Create(this, a.GetMethodInfo(),
                                                    (m, n) => { targetCalled = true; return(m.CreateReturn()); }));

            Assert.True(targetCalled);
        }
Example #23
0
        public void WhenInvokingPipeline_ThenBehaviorsCanReturnValueWithArg()
        {
            var expected = new object();

            var pipeline = new BehaviorPipeline(
                new ExecuteHandler((m, n) => m.CreateValueReturn(expected, m.Arguments)));

            Func <object, object?> a = NonVoidMethodWithArg;

            var result = pipeline.Invoke(MethodInvocation.Create(this, a.GetMethodInfo(), expected));

            Assert.Equal(expected, result.ReturnValue);
        }
Example #24
0
        public void WhenInvokingPipeline_ThenBehaviorsCanReturnValueWithOut()
        {
            var expected = new object();
            var output   = new object();

            var pipeline = new BehaviorPipeline(
                new ExecuteHandler((m, n) => m.CreateValueReturn(expected, new object(), output)));

            NonVoidMethodWithArgOutDelegate a = NonVoidMethodWithArgOut;

            var result = pipeline.Invoke(MethodInvocation.Create(this, a.GetMethodInfo(), expected, output));

            Assert.Equal(expected, result.ReturnValue);
            Assert.Equal(output, result.Outputs.GetValue(0));
        }
Example #25
0
        public void InvokeNextIfNotEqualsOrGetHashCode()
        {
            var method     = typeof(Foo).GetMethod(nameof(Foo.ToString)) !;
            var behavior   = new DefaultEqualityBehavior();
            var target     = new Foo();
            var nextCalled = false;

            behavior.Execute(
                MethodInvocation.Create(target, method),
                (m, n) =>
            {
                nextCalled = true;
                return(m.CreateReturn());
            });

            Assert.True(nextCalled);
        }
Example #26
0
        public void WhenInvokingPipeline_ThenBehaviorsCanPassDataWithContext()
        {
            var expected = Guid.NewGuid();
            var actual   = Guid.Empty;

            var pipeline = new BehaviorPipeline(
                new ExecuteHandler((m, n) => { m.Context["guid"] = expected; return(n.Invoke(m, n)); }),
                new ExecuteHandler((m, n) => { actual = (Guid)m.Context["guid"]; return(n.Invoke(m, n)); }));

            Action a = WhenInvokingPipeline_ThenBehaviorsCanPassDataWithContext;

            var result = pipeline.Invoke(MethodInvocation.Create(this, a.GetMethodInfo(), (m, n) => m.CreateReturn()));

            Assert.Equal(expected, actual);
            Assert.True(result.Context.ContainsKey("guid"));
            Assert.Equal(expected, result.Context["guid"]);
        }
Example #27
0
        public void WhenInvokingPipeline_ThenSkipsNonApplicableBehaviors()
        {
            var firstCalled  = false;
            var secondCalled = false;
            var targetCalled = false;

            var pipeline = new BehaviorPipeline(
                new AnonymousBehavior((m, n) => { firstCalled = true; return(n.Invoke(m, n)); }),
                new AnonymousBehavior((m, n) => { secondCalled = true; return(n.Invoke(m, n)); }, m => false));

            Action a = WhenInvokingPipeline_ThenInvokesAllBehaviorsAndTarget;

            pipeline.Invoke(MethodInvocation.Create(this, a.GetMethodInfo(),
                                                    (m, n) => { targetCalled = true; return(m.CreateReturn()); }));

            Assert.True(firstCalled);
            Assert.False(secondCalled);
            Assert.True(targetCalled);
        }
Example #28
0
        public void WhenInvokingPipeline_ThenInvokesAllBehaviorsAndTarget()
        {
            var firstCalled  = false;
            var secondCalled = false;
            var targetCalled = false;

            var pipeline = new BehaviorPipeline(
                new ExecuteHandler((m, n) => { firstCalled = true; return(n.Invoke(m, n)); }),
                new ExecuteHandler((m, n) => { secondCalled = true; return(n.Invoke(m, n)); }));

            Action a = WhenInvokingPipeline_ThenInvokesAllBehaviorsAndTarget;

            pipeline.Invoke(MethodInvocation.Create(this, a.GetMethodInfo(),
                                                    (m, n) => { targetCalled = true; return(m.CreateReturn()); }));

            Assert.True(firstCalled);
            Assert.True(secondCalled);
            Assert.True(targetCalled);
        }