Exemplo n.º 1
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);
        }
Exemplo n.º 2
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");
        }
Exemplo n.º 3
0
        public void WhenPropertyIsHidden_ThenAdapterDoesNotHaveProperty(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b.Decoration.IsHidden = true);

            manager.GetAdapter();

            manager.TypePropertyInfo.ShouldBeNull();
        }
Exemplo n.º 4
0
 public void WhenPropertyIsSpecifiedWithBothDelegatesNull_ThenSpecificationThrows(PropertyBehaviorManager manager)
 {
     Should.Throw <InvalidPropertySpecificationException>(() =>
     {
         manager.Behavior(b => b.SpecifyDelegates <string>(null, null));
         manager.GetAdapter();
     });
 }
Exemplo n.º 5
0
        public void WhenPropertyIsSpecifiedVirtual_ThenAdapterPropertySetterIsVirtual(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .Decoration
                             .IsVirtual = true);

            manager.GetAdapter();

            manager.TypePropertyInfo.GetSetMethod().IsVirtual.ShouldBeTrue();
        }
Exemplo n.º 6
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");
        }
Exemplo n.º 7
0
        public void WhenPropertyAttributeIsHidden_ThenAdapterPropertyDoesNotHaveAttribute(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .Decoration
                             .Attributes
                             .HideAttributesOfType <CustomizedPropertyShould1Attribute>());

            manager.GetAdapter();

            manager.TypePropertyInfo.GetCustomAttributes(true).ShouldBeEmpty();
        }
Exemplo n.º 8
0
        public void WhenPropertyIsSpecifiedAsAutoImplemented_ThenPropertyBehavesAsAutoImplemented(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b.SpecifyAutoImplemented <string>());

            var adapter = manager.GetAdapter();

            manager.Value = "42";
            string d = manager.Value;

            d.ShouldBe("42");
        }
Exemplo n.º 9
0
 public void WhenPropertyIsSpecifiedWithNullSetter_ThenMakePublicSetterThrows(PropertyBehaviorManager manager)
 {
     Should.Throw <InvalidPropertySpecificationException>(() =>
     {
         manager.Behavior(b => b.SpecifyDelegates(
                              component => component.P1,
                              null)
                          .Decoration
                          .SpecifyPublicSetter());
         manager.GetAdapter();
     });
 }
Exemplo n.º 10
0
 public void WhenPropertyIsSpecifiedWithNullGetter_ThenMakePublicGetterThrows(PropertyBehaviorManager manager)
 {
     Should.Throw <InvalidPropertySpecificationException>(() =>
     {
         string backingField = null;
         manager.Behavior(b => b.SpecifyDelegates <string>(
                              null,
                              (component, value) => backingField = value)
                          .Decoration
                          .SpecifyReadWrite());
         manager.GetAdapter();
     });
 }
Exemplo n.º 11
0
        public void WhenPropertyAttributeIsConverted_ThenAdapterPropertyDoesNotHaveOriginalAttribute(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .Decoration
                             .Attributes
                             .RegisterAttributeConversion <CustomizedPropertyShould1Attribute>(a => new CustomizedPropertyShould2Attribute()));

            manager.GetAdapter();

            manager.TypePropertyInfo.GetCustomAttributes(true)
            .OfType <CustomizedPropertyShould1Attribute>()
            .ShouldBeEmpty();
        }
Exemplo n.º 12
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");
        }
Exemplo n.º 13
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");
        }
Exemplo n.º 14
0
        public void WhenPropertyAttributeIsConverted_ThenAdapterPropertyHasConvertedAttribute(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .Decoration
                             .Attributes
                             .RegisterAttributeConversion <CustomizedPropertyShould1Attribute>(a => new CustomizedPropertyShould2Attribute()));

            manager.GetAdapter();

            manager.TypePropertyInfo.GetCustomAttributes(true)
            .Cast <CustomizedPropertyShould2Attribute>()
            .ShouldHaveSingleItem()
            .ShouldNotBeNull();
        }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
        public void WhenPropertyAttributeIsAdded_ThenAdapterPropertyHasAttribute(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .Decoration
                             .Attributes
                             .AddAttribute(() => new CustomizedPropertyShould2Attribute()));

            manager.GetAdapter();

            manager.TypePropertyInfo.GetCustomAttributes(true)
            .OfType <CustomizedPropertyShould2Attribute>()
            .ShouldHaveSingleItem()
            .ShouldNotBeNull();
        }
Exemplo n.º 17
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");
        }
Exemplo n.º 18
0
        public void WhenPropertyGetterFilterIsAdded_ThenFilterIsApplied(PropertyBehaviorManager manager)
        {
            manager.Behavior(b => b
                             .SpecifyBackingComponentProperty(m => m.P2)
                             .AddGetterFilter(i => i + 1)
                             .AddGetterFilter(i => i + 2));

            manager.GetAdapter(new CustomizedPropertyShouldModel()
            {
                P2 = 10
            });

            int value = manager.Value;

            value.ShouldBe(13);
        }
Exemplo n.º 19
0
        public void WhenPropertyIsSpecifiedAsDelegates_ThenSetterCallsSetterFunc(PropertyBehaviorManager manager)
        {
            string backingField = null;

            manager.Behavior(b => b.SpecifyDelegates <string>(
                                 null,
                                 (component, value) => backingField = value));

            manager.GetAdapter(new CustomizedPropertyShouldModel()
            {
                P1 = "test0"
            });

            manager.Value = "test2";
            backingField.ShouldBe("test2");
        }
Exemplo n.º 20
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");
        }
Exemplo n.º 21
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)");
        }
Exemplo n.º 22
0
        public void WhenPropertyIsSpecifiedAsDelegates_ThenGetterCallsGetterFunc(PropertyBehaviorManager manager)
        {
            string backingField = null;

            manager.Behavior(b => b.SpecifyDelegates(
                                 component => $"{component.P1}.{backingField}",
                                 null));

            manager.GetAdapter(new CustomizedPropertyShouldModel()
            {
                P1 = "test0"
            });

            backingField = "test1";
            string value = manager.Value;

            value.ShouldBe("test0.test1");
        }
Exemplo n.º 23
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);
        }
Exemplo n.º 24
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);
        }