Esempio n. 1
0
        public void CanExecutePipelineNoTarget()
        {
            var pipeline = new BehaviorPipeline(new ExecuteDelegate((m, n) => m.CreateValueReturn(null)));

            Action a = CanExecutePipelineNoTarget;

            pipeline.Execute(new MethodInvocation(this, a.GetMethodInfo()));
        }
Esempio n. 2
0
        public void CanExecutePipelineWithTarget()
        {
            var pipeline = new BehaviorPipeline();

            Action a = CanExecutePipelineWithTarget;

            pipeline.Execute(MethodInvocation.Create(this, a.GetMethodInfo(), (m, n) => m.CreateReturn()));
        }
Esempio n. 3
0
        public void WhenExecutingPipelineWithNoTarget_ThenThrowsIfNoBehaviorReturns()
        {
            var pipeline = new BehaviorPipeline();

            Action a = CanExecutePipelineNoTarget;

            Assert.Throws <NotImplementedException>(() => pipeline.Execute(new MethodInvocation(this, a.GetMethodInfo())));
        }
Esempio n. 4
0
        public void WhenInvokingPipelineWithNoBehaviors_ThenTargetCannotInvokeNext()
        {
            var pipeline = new BehaviorPipeline();

            Action a = WhenInvokingPipelineWithNoBehaviors_ThenInvokesTarget;

            Assert.Throws <NotSupportedException>(() => pipeline.Invoke(
                                                      new MethodInvocation(this, a.GetMethodInfo(), (m, n) => n.Invoke(m, n))));
        }
Esempio n. 5
0
        public void WhenExecutingPipelineResultWithNoTarget_ThenThrowsIfNoResult()
        {
            var pipeline = new BehaviorPipeline();

            Func <object> f = NonVoidMethod;

            Assert.Throws <NotImplementedException>(()
                                                    => pipeline.Execute <object>(new MethodInvocation(this, f.GetMethodInfo())));
        }
Esempio n. 6
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));
        }
Esempio n. 7
0
        public void CanExecutePipelineResultNoTarget()
        {
            var value = new object();

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

            Func <object> f = NonVoidMethod;

            Assert.Same(value, pipeline.Execute <object>(new MethodInvocation(this, f.GetMethodInfo())));
        }
Esempio n. 8
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())));
        }
Esempio n. 9
0
        public void WhenExecutingPipeline_ThenBehaviorCanThrow()
        {
            var pipeline = new BehaviorPipeline(
                new ExecuteDelegate((m, n) => m.CreateExceptionReturn(new ArgumentException())));

            Action a = WhenInvokingPipeline_ThenBehaviorsCanReturnException;

            Assert.Throws <ArgumentException>(()
                                              => pipeline.Execute(new MethodInvocation(this, a.GetMethodInfo()),
                                                                  new ExecuteDelegate((m, n) => throw new NotImplementedException())));
        }
Esempio n. 10
0
        public void WhenExecutingPipelineResult_ThenBehaviorCanThrow()
        {
            var pipeline = new BehaviorPipeline(
                new ExecuteDelegate((m, n) => m.CreateExceptionReturn(new ArgumentException())));

            Func <object> f = NonVoidMethod;

            Assert.Throws <ArgumentException>(()
                                              => pipeline.Execute <object>(new MethodInvocation(this, f.GetMethodInfo()),
                                                                           new ExecuteDelegate((m, n) => throw new NotImplementedException())));
        }
Esempio n. 11
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));
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        public void WhenInvokingPipelineWithNoBehaviors_ThenInvokesTarget()
        {
            var targetCalled = false;

            var pipeline = new BehaviorPipeline();

            Action a = WhenInvokingPipelineWithNoBehaviors_ThenInvokesTarget;

            pipeline.Invoke(new MethodInvocation(this, a.GetMethodInfo()),
                            new ExecuteDelegate((m, n) => { targetCalled = true; return(m.CreateValueReturn(null)); }));

            Assert.True(targetCalled);
        }
Esempio n. 14
0
        public void WhenInvokingPipeline_ThenBehaviorsCanReturnValueWithArg()
        {
            var expected = new object();

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

            Func <object, object> a = NonVoidMethodWithArg;

            var result = pipeline.Invoke(new MethodInvocation(this, a.GetMethodInfo(), expected),
                                         new ExecuteDelegate((m, n) => throw new NotImplementedException()));

            Assert.Equal(expected, result.ReturnValue);
        }
Esempio n. 15
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));
        }
Esempio n. 16
0
        public void WhenInvokingPipeline_ThenBehaviorsCanReturnValueWithOut()
        {
            var expected = new object();
            var output   = new object();

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

            NonVoidMethodWithArgOutDelegate a = NonVoidMethodWithArgOut;

            var result = pipeline.Invoke(new MethodInvocation(this, a.GetMethodInfo(), expected, output),
                                         new InvokeBehavior((m, n) => throw new NotImplementedException()));

            Assert.Equal(expected, result.ReturnValue);
            Assert.Equal(output, result.Outputs[0]);
        }
Esempio n. 17
0
        public void WhenCloningBehaviors_ThenCanManipulateCopyWithoutAffectingOriginal()
        {
            var pipeline = new BehaviorPipeline();

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

            var clone = (IList <IAvatarBehavior>)((ICloneable)pipeline.Behaviors).Clone();

            Assert.True(pipeline.Behaviors.SequenceEqual(clone));

            clone.Clear();

            Assert.NotEmpty(pipeline.Behaviors);
            Assert.Empty(clone);
        }
Esempio n. 18
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"]);
        }
Esempio n. 19
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);
        }
Esempio n. 20
0
        public void WhenInvokingPipeline_ThenBehaviorCanShortcircuitInvocation()
        {
            var firstCalled  = false;
            var secondCalled = false;
            var targetCalled = false;

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

            Action a = WhenInvokingPipeline_ThenBehaviorCanShortcircuitInvocation;

            pipeline.Invoke(new MethodInvocation(this, a.GetMethodInfo()),
                            new ExecuteDelegate((m, n) => { targetCalled = true; return(m.CreateValueReturn(null)); }));

            Assert.True(firstCalled);
            Assert.False(secondCalled);
            Assert.False(targetCalled);
        }
Esempio n. 21
0
        public void WhenInvokingPipeline_ThenSkipsNonApplicableBehaviors()
        {
            var firstCalled  = false;
            var secondCalled = false;
            var targetCalled = false;

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

            Action a = WhenInvokingPipeline_ThenInvokesAllBehaviorsAndTarget;

            pipeline.Invoke(new MethodInvocation(this, a.GetMethodInfo()),
                            new ExecuteDelegate((m, n) => { targetCalled = true; return(m.CreateValueReturn(null)); }));

            Assert.True(firstCalled);
            Assert.False(secondCalled);
            Assert.True(targetCalled);
        }
Esempio n. 22
0
        public void WhenInvokingPipeline_ThenInvokesAllBehaviorsAndTarget()
        {
            var firstCalled  = false;
            var secondCalled = false;
            var targetCalled = false;

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

            Action a = WhenInvokingPipeline_ThenInvokesAllBehaviorsAndTarget;

            pipeline.Invoke(new MethodInvocation(this, a.GetMethodInfo()),
                            new InvokeBehavior((m, n) => { targetCalled = true; return(m.CreateValueReturn(null)); }));

            Assert.True(firstCalled);
            Assert.True(secondCalled);
            Assert.True(targetCalled);
        }
Esempio n. 23
0
        public void WhenAddingMultipleBehaviors_ThenCanOptimizeCollectionChanges()
        {
            var changes = new List <NotifyCollectionChangedAction>();

            var pipeline = new BehaviorPipeline();

            ((INotifyCollectionChanged)pipeline.Behaviors).CollectionChanged += (sender, args) => changes.Add(args.Action);

            (pipeline.Behaviors as ISupportInitialize)?.BeginInit();

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

            Assert.Empty(changes);

            (pipeline.Behaviors as ISupportInitialize)?.EndInit();

            Assert.Single(changes);
            Assert.Equal(NotifyCollectionChangedAction.Reset, changes[0]);
        }
Esempio n. 24
0
 internal DynamicStuntInterceptor(bool notImplemented)
 {
     this.notImplemented = notImplemented;
     pipeline            = new BehaviorPipeline();
 }
Esempio n. 25
0
		internal Interceptor ()
		{
			pipeline = new BehaviorPipeline ();
		}
Esempio n. 26
0
 public CalculatorInterfaceProxy()
 {
     pipeline = new BehaviorPipeline();
 }
Esempio n. 27
0
		public CalculatorClassProxy ()
		{
			pipeline = new BehaviorPipeline ();
		}
Esempio n. 28
0
 internal Interceptor()
 {
     pipeline = new BehaviorPipeline();
 }
Esempio n. 29
0
 public CalculatorClassProxy()
 {
     pipeline = new BehaviorPipeline();
 }
Esempio n. 30
0
		public CalculatorInterfaceProxy ()
		{
			pipeline = new BehaviorPipeline ();
		}