Пример #1
0
        public void AddProperty_TypeHasAlreadyBeenBuilt_ThrowsInvalidOperationException()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");

            builder.Build();

            Assert.That(() => builder.AddProperty("name", typeof(string)),
                Throws.TypeOf<InvalidOperationException>()
                      .And.Message.Contains("Unable to add properties once a type has already been built."));
        }
Пример #2
0
        public void CreatedType_ImplementsIPropertyBag()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");
            Type type = builder.Build();

            Assert.That(typeof(IPropertyBag).IsAssignableFrom(type), Is.True);
        }
Пример #3
0
        public void CreatedType_SetPropertyToIncompatibleType_ThrowsArgumentException()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");
            builder.AddProperty("Property", typeof(string));

            Type type = builder.Build();
            PropertyInfo property = type.GetProperty("Property");
            object instance = Activator.CreateInstance(type);

            string message = "Object of type 'System.Object' cannot be converted to type 'System.String'.";
            Assert.That(() => property.SetValue(instance, new object(), null),
                Throws.TypeOf<ArgumentException>()
                      .And.Message.Contains(message));
        }
Пример #4
0
        public void CreatedType_ContainsExpectedProperties()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");
            builder.AddProperty("Property1", typeof(string));
            builder.AddProperty("Property2", typeof(int));

            Type type = builder.Build();

            Assert.That(type.GetProperty("Property1"), Is.Not.Null);
            Assert.That(type.GetProperty("Property1").PropertyType, Is.EqualTo(typeof(string)));
            Assert.That(type.GetProperty("Property2"), Is.Not.Null);
            Assert.That(type.GetProperty("Property2").PropertyType, Is.EqualTo(typeof(int)));
        }
Пример #5
0
        public void CreatedType_CanGetAndSetPropertyAsExpected()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");
            builder.AddProperty("Property", typeof(string));

            Type type = builder.Build();
            PropertyInfo property = type.GetProperty("Property");
            object instance = Activator.CreateInstance(type);

            property.SetValue(instance, "Value", null);
            object value = property.GetValue(instance, null);
            Assert.That(value, Is.EqualTo("Value"));
        }
Пример #6
0
        public void CreatedType_AsIPropertyBag_SetValue_SetsPropertyValue()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");
            builder.AddProperty("Property", typeof(string));

            Type type = builder.Build();
            IPropertyBag bag = Activator.CreateInstance(type) as IPropertyBag;
            bag.SetValue("Property", "expected_value");

            PropertyInfo propertyInfo = type.GetProperty("Property");
            Assert.That(propertyInfo.GetValue(bag, null), Is.EqualTo("expected_value"));
        }
Пример #7
0
        public void CreatedType_AsIPropertyBag_SetValue_PropertyNameIsNotValid_ThrowsArgumentException()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");
            builder.AddProperty("Property", typeof(string));

            Type type = builder.Build();
            IPropertyBag bag = Activator.CreateInstance(type) as IPropertyBag;

            string message = "propertyName must refer to a parameter that exists on this type.";
            Assert.That(() => bag.SetValue("Name", new object()),
                Throws.TypeOf<ArgumentException>()
                      .And.Message.Contains(message));
        }
Пример #8
0
        public void CreatedType_AsIPropertyBag_SetValue_ObjectIsOfInvalidType_ThrowsInvalidCastException()
        {
            PropertyBagBuilder builder = new PropertyBagBuilder("name");
            builder.AddProperty("Property", typeof(string));

            Type type = builder.Build();
            IPropertyBag bag = Activator.CreateInstance(type) as IPropertyBag;

            string message = "Unable to cast object of type 'System.Object' to type 'System.String'.";
            Assert.That(() => bag.SetValue("Property", new object()),
                Throws.TypeOf<InvalidCastException>()
                      .And.Message.Contains(message));
        }