Example #1
0
        public void WhenPropertyIsSpecifiedReadOnly_ThenAdapterPropertySetterThrowsMissingMemberException(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(m => m.P1)
                             .Decoration
                             .SpecifyReadOnly());

            var model = new CustomizedPropertyShouldModel()
            {
                P1 = "test"
            };

            manager.GetAdapter(model);

            string value = manager.Value;

            value.ShouldBe("test");

            var ex = Assert.Throws <RuntimeBinderException>(() =>
            {
                manager.Value = "test2";
            });

            ex.Message.ShouldContain("cannot be");
        }
Example #2
0
        public void WhenPropertyGetterFilterIsAddedWithComponentAndSetter_ThenFilterIsApplied(PropertyBehaviorManager manager)
        {
            var list                = Expression.Parameter(typeof(List <string>), "list");
            var comp                = Expression.Parameter(typeof(CustomizedPropertyShouldModel), "comp");
            var setter              = Expression.Parameter(typeof(Action <List <string> >), "setter");
            var newList             = Expression.Parameter(typeof(List <string>), "newList");
            LambdaExpression lambda = Expression.Lambda(
                Expression.Condition(
                    Expression.Equal(list, Expression.Constant(null)),
                    Expression.Block(new [] { newList },
                                     Expression.Assign(newList, Expression.New(typeof(List <string>))),
                                     Expression.Invoke(setter, newList),
                                     newList), list)
                , list, comp, setter);

            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(m => m.P3)
                             .AddGetterFilter(lambda));

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            model.P3.ShouldBeNull();

            List <string> result = manager.Value;

            result.ShouldNotBeNull();
            result.ShouldBeSameAs(model.P3);
        }
Example #3
0
        public void WhenPropertyIsSpecifiedAsComponentPropertyInfo_ThenPropertySetterCallsComponentPropertySetter(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b.SpecifyBackingComponentProperty(typeof(CustomizedPropertyShouldModel).GetProperty(nameof(CustomizedPropertyShouldModel.P1))));

            var model   = new CustomizedPropertyShouldModel();
            var adapter = manager.GetAdapter(model);

            manager.Value = "test";
            model.P1.ShouldBe("test");
        }
Example #4
0
        public void WhenPropertyIsSpecifiedAsComponentProperty_ThenPropertyGetterReturnsComponentProperty(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b.SpecifyBackingComponentProperty(m => m.P1));

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            model.P1 = "test";
            string value = manager.Value;

            value.ShouldBe("test");
        }
Example #5
0
        public void WhenPropertySetterFilterIsAdded_ThenFilterIsApplied(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(m => m.P2)
                             .AddSetterFilter(i => i + 1)
                             .AddSetterFilter(i => i + 2));

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            manager.Value = 10;
            model.P2.ShouldBe(13);
        }
Example #6
0
        public void WhenPropertyIsSpecifiedPrivateSetter_ThenAdapterPropertySetterWorks(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(x => x.P1)
                             .Decoration
                             .SpecifyPrivateSetter());

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            manager.TypePropertyInfo.GetSetMethod(true).Invoke(manager.Adapter, new object[] { "test" });
            model.P1.ShouldBe("test");
        }
Example #7
0
        public void WhenPropertyNameIsChanged_ThenAdapterHasPropertyWithNewName(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b.SpecifyBackingComponentProperty(m => m.P1)
                             .Decoration
                             .PublicName = "FauxString");

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            model.P1 = "test";
            string result = manager.DynamicAdapter.FauxString;

            result.ShouldBe("test");
        }
Example #8
0
        public void WhenPropertySetterFilterIsAddedWithComponentAndGetter_ThenFilterIsApplied(PropertyBehaviorManager manager)
        {
            var list                = Expression.Parameter(typeof(List <string>), "value");
            var comp                = Expression.Parameter(typeof(CustomizedPropertyShouldModel), "comp");
            var getter              = Expression.Parameter(typeof(Func <List <string> >), "getter");
            var newList             = Expression.Parameter(typeof(List <string>), "newList");
            var existingList        = Expression.Parameter(typeof(List <string>), "existingList");
            LambdaExpression lambda = Expression.Lambda(
                Expression.Block(new[] { newList, existingList },
                                 Expression.Assign(existingList, Expression.Invoke(getter)),
                                 Expression.Condition(
                                     Expression.Equal(existingList, Expression.Constant(null)),
                                     list,
                                     Expression.Block(
                                         Expression.Call(
                                             existingList,
                                             typeof(List <string>).GetMethod("AddRange"),
                                             Expression.Condition(
                                                 Expression.Equal(list, Expression.Constant(null)),
                                                 Expression.New(typeof(List <string>)),
                                                 list)),
                                         existingList))
                                 ), list, comp, getter);

            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(m => m.P3)
                             .AddSetterFilter(lambda));

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            model.P3.ShouldBeNull();
            manager.Value = new List <string>()
            {
                "test1"
            };
            model.P3.ShouldHaveSingleItem()
            .ShouldBe("test1");

            manager.Value = new List <string>()
            {
                "test2"
            };
            model.P3[0].ShouldBe("test1");
            model.P3[1].ShouldBe("test2");
        }
Example #9
0
        public void WhenPropertyIsSpecifiedPrivateSetter_ThenAdapterPropertySetterIsPrivate(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .Decoration
                             .SpecifyPrivateSetter());

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            var ex = Assert.Throws <RuntimeBinderException>(() =>
            {
                manager.Value = "Test";
            });

            ex.Message.ShouldMatch("(protection level|inaccessible)");
        }
Example #10
0
        public void WhenPropertySetterFilterIsAddedWithComponent_ThenFilterIsApplied(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(m => m.P2)
                             .AddSetterFilter((i, c) => c.P1 == null ? i + 1 : i));

            var model = new CustomizedPropertyShouldModel();

            manager.GetAdapter(model);

            manager.Value = 5;
            // P1 is null, so P2 should be set to 5 + 1 = 6
            model.P2.ShouldBe(6);

            model.P1      = "test";
            manager.Value = 10;
            // P1 is not null, so P2 should bet set to 10
            model.P2.ShouldBe(10);
        }
Example #11
0
        public void WhenPropertyGetterFilterIsAddedWithComponent_ThenFilterIsApplied(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(m => m.P2)
                             .AddGetterFilter((i, c) => c.P1 == null ? i + 1 : i));

            var model = new CustomizedPropertyShouldModel()
            {
                P2 = 5
            };

            manager.GetAdapter(model);

            int result = manager.Value;

            // P1 is null, so P2 should return (actual P2 = 5) + 1 = 6
            result.ShouldBe(6);
            model.P1 = "test";
            // P1 is not null, so P2 should return actual P2 = 5;
            result = manager.Value;
            result.ShouldBe(5);
        }
Example #12
0
 protected virtual IAdapter <CustomizedPropertyShouldModel> Finish(CustomizedPropertyShouldModel model) => Spec.Finish().Create(model);
Example #13
0
 public IAdapter <CustomizedPropertyShouldModel> GetAdapter(CustomizedPropertyShouldModel model = null)
 {
     Adapter = Finish(model);
     return(Adapter);
 }
Example #14
0
 protected override IAdapter <CustomizedPropertyShouldModel> Finish(CustomizedPropertyShouldModel model)
 {
     return(_result.Create(model));
 }