public void select_interceptors() { var activator1 = new ActivatorInterceptor <ITarget>(x => x.Activate()); var activator2 = new ActivatorInterceptor <Target>(x => x.UseSession(null)); var activator3 = new ActivatorInterceptor <Target>(x => x.ThrowUp()); var activator4 = new ActivatorInterceptor <ITarget>(x => x.Debug()); var activator5 = new ActivatorInterceptor <IGateway>(x => x.DoSomething()); var policies = Policies.Default(); policies.Add(activator1.ToPolicy()); policies.Add(activator2.ToPolicy()); policies.Add(activator3.ToPolicy()); policies.Add(activator4.ToPolicy()); policies.Add(activator5.ToPolicy()); var instance1 = new SmartInstance <Target>(); policies.Apply(typeof(ITarget), instance1); instance1.Interceptors .ShouldHaveTheSameElementsAs(activator1, activator2, activator3, activator4); var instance2 = new SmartInstance <ATarget>(); policies.Apply(typeof(ITarget), instance2); instance2.Interceptors .ShouldHaveTheSameElementsAs(activator1, activator4); var instance3 = new SmartInstance <StubbedGateway>(); policies.Apply(typeof(ITarget), instance3); instance3.Interceptors .ShouldHaveTheSameElementsAs(activator5); }
public void see_the_description_of_class_with__only_ctors() { var build = ConcreteType.BuildSource(typeof(GuyWithOnlyCtor), null, new DependencyCollection(), Policies.Default()); Debug.WriteLine(build.Description); }
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 create_the_expression_when_the_variable_is_the_right_type() { var variable = Expression.Variable(typeof(ITarget), "target"); var expression = theActivator.ToExpression(Policies.Default(), Parameters.Session, variable); expression.ToString().ShouldBe("target.Activate()"); }
public static TPluginType Build <TPluginType>(this Instance instance, IBuildSession session = null) where TPluginType : class { var plan = instance.ResolveBuildPlan(typeof(TPluginType), Policies.Default()); var buildSession = session ?? new FakeBuildSession(); return(plan.Build(buildSession, buildSession.As <IContext>()).As <TPluginType>()); }
public void throw_a_description_exception_when_no_suitable_ctor_can_be_found() { var ex = Exception <StructureMapConfigurationException> .ShouldBeThrownBy(() => { ConcreteType.BuildConstructorStep(typeof(GuyWithNoSuitableCtor), null, new DependencyCollection(), Policies.Default()); }); ex.Message.ShouldContain("No public constructor could be selected for concrete type " + typeof(GuyWithNoSuitableCtor).GetFullName()); }
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.Received().Decorator(decorator); }
public void accept_visitor_for_DependencyInterceptor() { var target = new Target(); var inner = Constant.For(target); var decorator = new DecoratorInterceptor(typeof(ITarget), typeof(DecoratedTarget)); var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { decorator }); plan.AcceptVisitor(theVisitor); theVisitor.Received().Decorator(decorator); }
public void is_valid_sad_path_with_ctor_checks() { // This class needs all three of these things var dependencies = new DependencyCollection(); dependencies.Add("name", "Jeremy"); dependencies.Add("age", 40); //dependencies.Add("isAwake", true); ConcreteType.BuildSource(typeof(GuyWithPrimitives), null, dependencies, Policies.Default()) .IsValid().ShouldBeFalse(); }
public void see_the_description_of_class_with_only_ctor_with_inline_depencencies() { var dependencies = new DependencyCollection(); dependencies.Add(typeof(Rule), new ColorRule("Red")); dependencies.Add(typeof(IWidget), new AWidget()); var build = ConcreteType.BuildSource(typeof(GuyWithOnlyCtor), null, dependencies, Policies.Default()); Debug.WriteLine(build.Description); }
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 will_not_accept_decorator_that_does_not_match_the_plugin_type() { var target = new Target(); var inner = Constant.For(target); Exception <StructureMapBuildPlanException> .ShouldBeThrownBy(() => { new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { new DecoratorInterceptor(typeof(ATarget), typeof(ATarget)) }); }); }
public void intercept_happy_path_with_a_single_activation_that_uses_session() { var target = new Target(); var inner = Constant.For(target); var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { new ActivatorInterceptor <Target>((session, x) => x.UseSession(session)) }); var theSession = new StubBuildSession(); plan.ToBuilder <ITarget>()(theSession, theSession) .ShouldBeTheSameAs(target); target.Session.ShouldBeTheSameAs(theSession); }
public BuildPlanTester() { theTarget = new BuildTarget(); theInstance = new ObjectInstance(theTarget); theInterceptors = new IInterceptor[0]; theInner = Constant.For(theTarget); plan = new Lazy <BuildPlan>( () => new BuildPlan(typeof(IBuildTarget), theInstance, theInner, Policies.Default(), theInterceptors)); theSession = new FakeBuildSession(); }
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 intercept_sad_path_with_a_single_activation_that_uses_session() { var target = new Target(); var inner = Constant.For(target); var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { new ActivatorInterceptor <Target>((session, x) => x.BlowUpOnSession(session)) }); var theSession = new StubBuildSession(); var ex = Exception <StructureMapInterceptorException> .ShouldBeThrownBy( () => { plan.ToBuilder <ITarget>()(theSession, theSession); }); ex.Message.ShouldContain("Target.BlowUpOnSession(IContext)"); }
public void try_the_exception_message() { var instance = new ConstructorInstance(typeof(GuyWithPrimitives)); instance.Dependencies.Add("name", "Jeremy"); var ex = Exception <StructureMapBuildPlanException> .ShouldBeThrownBy( () => { instance.ToBuilder(typeof(GuyWithPrimitives), Policies.Default()); }); ex.ToString() .ShouldContain( "Unable to create a build plan for concrete type StructureMap.Testing.Building.GuyWithPrimitives"); Debug.WriteLine(ex); }
public void accept_visitor_for_activator() { var target = new Target(); var inner = Constant.For(target); var interceptor = new ActivatorInterceptor <Target>(x => x.ThrowUp()); var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { interceptor }); plan.AcceptVisitor(theVisitor); theVisitor.Received().Activator(interceptor); }
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 compile_and_use_by_itself_not_using_IBuildSession() { var variable = Expression.Variable(typeof(ITarget), "target"); var expression = theInterceptor.ToExpression(Policies.Default(), Parameters.Session, variable); var lambdaType = typeof(Func <ITarget, ITarget>); var lambda = Expression.Lambda(lambdaType, expression, variable); var func = lambda.Compile().As <Func <ITarget, ITarget> >(); var target = new Target(); var decorated = func(target); decorated.ShouldBeOfType <DecoratedTarget>() .Inner.ShouldBeTheSameAs(target); }
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 void do_not_duplicate_interceptor_policies() { var theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate()); var policy1 = new InterceptorPolicy <ITarget>(theActivator); var policy2 = new InterceptorPolicy <ITarget>(theActivator); policy1.ShouldBe(policy2); var policies = Policies.Default(); policies.Add(policy1); policies.Add(policy2); policies.Add(policy1); policies.Add(policy2); policies.Interception().Single().ShouldBeTheSameAs(policy1); }
public void compile_and_use_by_itself() { var variable = Expression.Variable(typeof(ITarget), "target"); var expression = theActivator.ToExpression(Policies.Default(), Parameters.Context, variable); var lambdaType = typeof(Action <ITarget>); var lambda = Expression.Lambda(lambdaType, expression, variable); var action = lambda.Compile().As <Action <ITarget> >(); var target = new Target(); action(target); target.HasBeenActivated.ShouldBeTrue(); }
public void intercept_happy_path_with_a_single_activation() { var target = new Target(); var inner = Constant.For(target); var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { new ActivatorInterceptor <ITarget>(x => x.Activate()) }); var session = new StubBuildSession(); plan.ToBuilder <ITarget>()(session, session) .ShouldBeTheSameAs(target); target.HasBeenActivated.ShouldBeTrue(); }
public void compile_and_use_by_itself_with_session() { var activator = new ActivatorInterceptor <Target>((s, t) => t.UseSession(s)); var variable = Expression.Variable(typeof(Target), "target"); var expression = activator.ToExpression(Policies.Default(), Parameters.Context, variable); var lambdaType = typeof(Action <IContext, Target>); var lambda = Expression.Lambda(lambdaType, expression, Parameters.Context, variable); var action = lambda.Compile().As <Action <IContext, Target> >(); var target = new Target(); var session = new FakeBuildSession(); action(session, target); target.Session.ShouldBeTheSameAs(session); }
public void multiple_activators_taking_different_accept_types() { var target = new Target(); var inner = Constant.For(target); var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { new ActivatorInterceptor <ITarget>(x => x.Activate()), new ActivatorInterceptor <Target>(x => x.TurnGreen()) }); var session = new StubBuildSession(); plan.ToBuilder <ITarget>()(session, session) .ShouldBeTheSameAs(target); target.HasBeenActivated.ShouldBeTrue(); target.Color.ShouldBe("Green"); }
public void sends_the_constructor_args_and_parameters_to_the_visitor() { var dependencies = new DependencyCollection(); dependencies.Add(typeof(Rule), new ColorRule("Red")); dependencies.Add(typeof(IWidget), new AWidget()); var build = ConcreteType.BuildSource(typeof(GuyWithCtorAndArgs), null, dependencies, Policies.Default()); var visitor = new StubVisitor(); build.AcceptVisitor(visitor); visitor.Items.ShouldHaveTheSameElementsAs( "Constructor: Void .ctor(StructureMap.Testing.Widget3.IGateway, StructureMap.Testing.Widget.Rule)", "Set IWidget Widget = Value: StructureMap.Testing.Widget.AWidget", "Set IService Service = *Default of IService*" ); }
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 activator_that_fails_gets_wrapped_in_descriptive_text() { var target = new Target(); var inner = Constant.For(target); var interceptor = new ActivatorInterceptor <Target>(x => x.ThrowUp()); var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(), new IInterceptor[] { interceptor }); var ex = Exception <StructureMapInterceptorException> .ShouldBeThrownBy( () => { var session = new StubBuildSession(); plan.ToBuilder <ITarget>()(session, session); }); ex.Title.Trim().ShouldBe( "Activator interceptor failed during object creation. See the inner exception for details."); ex.Message.ShouldContain(interceptor.Description); }
public void CanBeAutoFilledIsFalse() { Policies.Default().CanBeAutoFilled(typeof(ClassWithPrimitiveConstructorArguments)) .ShouldBeFalse(); }