public void mixed_activators_and_decorators_happy_path()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var decorator1 = new FuncInterceptor <ITarget>(t => new DecoratedTarget(t));
            var decorator2 = new FuncInterceptor <ITarget>(t => new BorderedTarget(t));
            var plan       = new InterceptionPlan(typeof(ITarget), inner, new Policies(), new IInterceptor[]
            {
                decorator1,
                decorator2,
                new ActivatorInterceptor <ITarget>(x => x.Activate()),
                new ActivatorInterceptor <Target>(x => x.TurnGreen())
            });

            var session = new StubBuildSession();

            plan.ToBuilder <ITarget>()(session, session)
            .ShouldBeOfType <BorderedTarget>()
            .Inner.ShouldBeOfType <DecoratedTarget>()
            .Inner.ShouldBeTheSameAs(target);

            target.Color.ShouldEqual("Green");
            target.HasBeenActivated.ShouldBeTrue();
        }
        public void accept_visitor_for_func_decorator()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var decorator = new FuncInterceptor <ITarget>((s, t) => new ContextKeepingTarget(s, t));
            var plan      = new InterceptionPlan(typeof(ITarget), inner, new Policies(), new IInterceptor[] { decorator });

            plan.AcceptVisitor(theVisitor);

            theVisitor.AssertWasCalled(x => x.Decorator(decorator));
        }
        public void accept_visitor_for_func_decorator()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var decorator = new FuncInterceptor<ITarget>((s, t) => new ContextKeepingTarget(s, t));
            var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { decorator });

            plan.AcceptVisitor(theVisitor);

            theVisitor.AssertWasCalled(x => x.Decorator(decorator));
        }
        public void single_decorator_happy_path_that_uses_the_plugin_type()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var decorator = new FuncInterceptor <ITarget>(t => new DecoratedTarget(t));
            var plan      = new InterceptionPlan(typeof(ITarget), inner, new Policies(), new IInterceptor[] { decorator });

            var session = new StubBuildSession();

            plan.ToBuilder <ITarget>()(session, session)
            .ShouldBeOfType <DecoratedTarget>()
            .Inner.ShouldBeTheSameAs(target);
        }
        public void single_decorator_happy_path_that_uses_the_plugin_type_and_session()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var decorator = new FuncInterceptor <ITarget>((s, t) => new ContextKeepingTarget(s, t));
            var plan      = new InterceptionPlan(typeof(ITarget), inner, new Policies(), new IInterceptor[] { decorator });

            var theSession = new StubBuildSession();
            var result     = plan.ToBuilder <ITarget>()(theSession, theSession)
                             .ShouldBeOfType <ContextKeepingTarget>();

            result.Inner.ShouldBeTheSameAs(target);
            result.Session.ShouldBeTheSameAs(theSession);
        }
        public void multiple_decorators_happy_path()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var decorator1 = new FuncInterceptor <ITarget>(t => new DecoratedTarget(t));
            var decorator2 = new FuncInterceptor <ITarget>(t => new BorderedTarget(t));
            var plan       = new InterceptionPlan(typeof(ITarget), inner, new Policies(), new IInterceptor[] { decorator1, decorator2 });

            var session = new StubBuildSession();

            plan.ToBuilder <ITarget>()(session, session)
            .ShouldBeOfType <BorderedTarget>()
            .Inner.ShouldBeOfType <DecoratedTarget>()
            .Inner.ShouldBeTheSameAs(target);
        }
        public void single_decorator_sad_path_that_uses_the_plugin_type_and_session()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var decorator = new FuncInterceptor <ITarget>((s, t) => new SadContextKeepingTarget(s, t));
            var plan      = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { decorator });

            var theSession = new StubBuildSession();

            var ex =
                Exception <StructureMapInterceptorException> .ShouldBeThrownBy(
                    () => { plan.ToBuilder <ITarget>()(theSession, theSession); });

            ex.Message.ShouldContain("new SadContextKeepingTarget(IContext, ITarget)");
        }
        public void single_decorator_sad_path_that_uses_the_plugin_type()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var decorator = new FuncInterceptor <ITarget>(t => new ThrowsDecoratedTarget(t));
            var plan      = new InterceptionPlan(typeof(ITarget), inner, new Policies(), new IInterceptor[] { decorator });

            var ex =
                Exception <StructureMapInterceptorException> .ShouldBeThrownBy(
                    () => {
                var session = new StubBuildSession();
                plan.ToBuilder <ITarget>()(session, session);
            });

            ex.Message.ShouldContain("new ThrowsDecoratedTarget(ITarget)");
        }
        public void compile_and_use_by_itself_using_IContext()
        {
            theInterceptor = new FuncInterceptor <ITarget>((c, t) => new ContextKeepingTarget(c, t));

            var variable = Expression.Variable(typeof(ITarget), "target");

            var expression = theInterceptor.ToExpression(Policies.Default(), Parameters.Context, variable);

            var lambdaType = typeof(Func <IContext, ITarget, ITarget>);
            var lambda     = Expression.Lambda(lambdaType, expression, Parameters.Context, variable);

            var func = lambda.Compile().As <Func <IContext, ITarget, ITarget> >();

            var target    = new Target();
            var session   = new FakeBuildSession();
            var decorated = func(session, target).ShouldBeOfType <ContextKeepingTarget>();

            decorated
            .Inner.ShouldBeTheSameAs(target);

            decorated.Session.ShouldBeTheSameAs(session);
        }
        public void FuncReferenceTypeWithoutParameters <T>(T?expectedResult)
            where T : class
        {
            // Given
            var proxyFactory = Context.ProxyFactory;
            var interceptor  = new FuncInterceptor <T>(expectedResult);

            // When
            var foo    = proxyFactory.CreateForInterface <IFooFuncReferenceTypeParameterless <T> >(interceptor);
            var result = foo.MethodWithoutParameter();

            // Then
            Assert.NotNull(foo);

            Assert.Single(interceptor.ForwardedInvocations);
            var invocation = interceptor.ForwardedInvocations.Single();

            invocation.ShouldInterceptMethodWithName(nameof(IFooFuncReferenceTypeParameterless <T> .MethodWithoutParameter));
            invocation.ShouldHaveNoParameterIn();
            invocation.ShouldHaveNoParameterRef();
            invocation.ShouldHaveNoParameterOut();
            Assert.Equal(expectedResult, result);
        }
Beispiel #11
0
        public void FunctValueTypeWithSingleParameterRef <T>(T expectedResult, T expectedValue)
            where T : struct
        {
            // Given
            var proxyFactory = Context.ProxyFactory;
            var interceptor  = new FuncInterceptor <T>(expectedResult);

            // When
            var foo    = proxyFactory.CreateForInterface <IFooFuncValueTypeParameterRef <T> >(interceptor);
            var result = foo.MethodWithOneParameter(ref expectedValue);

            // Then
            Assert.NotNull(foo);

            Assert.Single(interceptor.ForwardedInvocations);
            var invocation = interceptor.ForwardedInvocations.Single();

            invocation.ShouldInterceptMethodWithName(nameof(IFooFuncValueTypeParameterRef <T> .MethodWithOneParameter));
            invocation.ShouldHaveNoParameterIn();
            invocation.ShouldHaveParameterRefCountOf(1);
            invocation.ShouldHaveParameterRef("first", typeof(T), expectedValue);
            invocation.ShouldHaveNoParameterOut();
            Assert.Equal(expectedResult, result);
        }
        public void single_decorator_happy_path_that_uses_the_plugin_type()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var decorator = new FuncInterceptor<ITarget>(t => new DecoratedTarget(t));
            var plan = new InterceptionPlan(typeof (ITarget), inner, Policies.Default(), new IInterceptor[] {decorator});

            var session = new StubBuildSession();
            plan.ToBuilder<ITarget>()(session, session)
                .ShouldBeOfType<DecoratedTarget>()
                .Inner.ShouldBeTheSameAs(target);
        }
        public void explicit_description_with_icontext()
        {
            theInterceptor = new FuncInterceptor <ITarget>((c, t) => new ContextKeepingTarget(c, t), "context keeping");

            theInterceptor.Description.ShouldContain("context keeping");
        }
        public void description_when_uses_IContext_too()
        {
            theInterceptor = new FuncInterceptor <ITarget>((c, t) => new ContextKeepingTarget(c, t));

            theInterceptor.Description.ShouldContain("new ContextKeepingTarget(IContext, ITarget)");
        }
        public void explicit_description()
        {
            theInterceptor = new FuncInterceptor <ITarget>(x => new DecoratedTarget(x), "decorating the target");

            theInterceptor.Description.ShouldContain("decorating the target");
        }
        public void compile_and_use_by_itself_using_IContext()
        {
            theInterceptor = new FuncInterceptor<ITarget>((c, t) => new ContextKeepingTarget(c, t));

            var variable = Expression.Variable(typeof (ITarget), "target");

            var expression = theInterceptor.ToExpression(Policies.Default(), Parameters.Context, variable);

            var lambdaType = typeof (Func<IContext, ITarget, ITarget>);
            var lambda = Expression.Lambda(lambdaType, expression, Parameters.Context, variable);

            var func = lambda.Compile().As<Func<IContext, ITarget, ITarget>>();

            var target = new Target();
            var session = new FakeBuildSession();
            var decorated = func(session, target).ShouldBeOfType<ContextKeepingTarget>();

            decorated
                .Inner.ShouldBeTheSameAs(target);

            decorated.Session.ShouldBeTheSameAs(session);
        }
Beispiel #17
0
 public void SetUp()
 {
     theInterceptor = new FuncInterceptor <ITarget>(x => new DecoratedTarget(x));
 }
 public void SetUp()
 {
     theInterceptor = new FuncInterceptor<ITarget>(x => new DecoratedTarget(x));
 }
        public void single_decorator_happy_path_that_uses_the_plugin_type_and_session()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var decorator = new FuncInterceptor<ITarget>((s, t) => new ContextKeepingTarget(s, t));
            var plan = new InterceptionPlan(typeof (ITarget), inner, Policies.Default(), new IInterceptor[] {decorator});

            var theSession = new StubBuildSession();
            var result = plan.ToBuilder<ITarget>()(theSession, theSession)
                .ShouldBeOfType<ContextKeepingTarget>();

            result.Inner.ShouldBeTheSameAs(target);
            result.Session.ShouldBeTheSameAs(theSession);
        }
        public void explicit_description()
        {
            theInterceptor = new FuncInterceptor<ITarget>(x => new DecoratedTarget(x), "decorating the target");

            theInterceptor.Description.ShouldContain("decorating the target");
        }
        public void description_when_uses_IContext_too()
        {
            theInterceptor = new FuncInterceptor<ITarget>((c, t) => new ContextKeepingTarget(c, t));

            theInterceptor.Description.ShouldContain("new ContextKeepingTarget(IContext, ITarget)");
        }
        public void single_decorator_sad_path_that_uses_the_plugin_type()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var decorator = new FuncInterceptor<ITarget>(t => new ThrowsDecoratedTarget(t));
            var plan = new InterceptionPlan(typeof (ITarget), inner, new Policies(), new IInterceptor[] {decorator});

            var ex =
                Exception<StructureMapInterceptorException>.ShouldBeThrownBy(
                    () =>
                    {
                        var session = new StubBuildSession();
                        plan.ToBuilder<ITarget>()(session, session);
                    });

            ex.Message.ShouldContain("new ThrowsDecoratedTarget(ITarget)");
        }
        public void multiple_decorators_happy_path()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var decorator1 = new FuncInterceptor<ITarget>(t => new DecoratedTarget(t));
            var decorator2 = new FuncInterceptor<ITarget>(t => new BorderedTarget(t));
            var plan = new InterceptionPlan(typeof (ITarget), inner, Policies.Default(),
                new IInterceptor[] {decorator1, decorator2});

            var session = new StubBuildSession();
            plan.ToBuilder<ITarget>()(session, session)
                .ShouldBeOfType<BorderedTarget>()
                .Inner.ShouldBeOfType<DecoratedTarget>()
                .Inner.ShouldBeTheSameAs(target);
        }
 public FuncInterceptorTester()
 {
     theInterceptor = new FuncInterceptor <ITarget>(x => new DecoratedTarget(x));
 }
        public void mixed_activators_and_decorators_happy_path()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var decorator1 = new FuncInterceptor<ITarget>(t => new DecoratedTarget(t));
            var decorator2 = new FuncInterceptor<ITarget>(t => new BorderedTarget(t));
            var plan = new InterceptionPlan(typeof (ITarget), inner, Policies.Default(), new IInterceptor[]
            {
                decorator1,
                decorator2,
                new ActivatorInterceptor<ITarget>(x => x.Activate()),
                new ActivatorInterceptor<Target>(x => x.TurnGreen())
            });

            var session = new StubBuildSession();
            plan.ToBuilder<ITarget>()(session, session)
                .ShouldBeOfType<BorderedTarget>()
                .Inner.ShouldBeOfType<DecoratedTarget>()
                .Inner.ShouldBeTheSameAs(target);

            target.Color.ShouldBe("Green");
            target.HasBeenActivated.ShouldBeTrue();
        }
        public void explicit_description_with_icontext()
        {
            theInterceptor = new FuncInterceptor<ITarget>((c, t) => new ContextKeepingTarget(c, t), "context keeping");

            theInterceptor.Description.ShouldContain("context keeping");
        }
        public void single_decorator_sad_path_that_uses_the_plugin_type_and_session()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var decorator = new FuncInterceptor<ITarget>((s, t) => new SadContextKeepingTarget(s, t));
            var plan = new InterceptionPlan(typeof (ITarget), inner, Policies.Default(), new IInterceptor[] {decorator});

            var theSession = new StubBuildSession();


            var ex =
                Exception<StructureMapInterceptorException>.ShouldBeThrownBy(
                    () => { plan.ToBuilder<ITarget>()(theSession, theSession); });

            ex.Message.ShouldContain("new SadContextKeepingTarget(IContext, ITarget)");
        }
 public FuncInterceptorTester()
 {
     theInterceptor = new FuncInterceptor<ITarget>(x => new DecoratedTarget(x));
 }