public void GetProperties_IncorrectAttributeValuePropertyName_ThrowsArgumentException()
        {
            // Setup
            var attribute  = new KeyValueElementAttribute(nameof(TestObject.Name), "IDoNotExist");
            var attributes = new AttributeCollection(attribute);

            var mocks      = new MockRepository();
            var context    = mocks.Stub <ITypeDescriptorContext>();
            var descriptor = mocks.Stub <PropertyDescriptor>("name", new Attribute[0]);

            descriptor.Stub(d => d.Attributes).Return(attributes);
            context.Stub(c => c.PropertyDescriptor).Return(descriptor);
            mocks.ReplayAll();

            const int elementCount = 12;

            TestObject[] array = Enumerable.Repeat(new TestObject
            {
                Name = "some name"
            }, elementCount).ToArray();

            var converter = new KeyValueExpandableArrayConverter();

            // Call
            TestDelegate test = () => converter.GetProperties(context, array);

            // Assert
            Assert.Throws <ArgumentException>(test);
        }
        public void GetProperties_FromArray_SettingValuesShouldThrowNotSupportedException()
        {
            // Setup
            var attribute  = new KeyValueElementAttribute(nameof(TestObject.Name), nameof(TestObject.Value));
            var attributes = new AttributeCollection(attribute);

            var mocks      = new MockRepository();
            var context    = mocks.Stub <ITypeDescriptorContext>();
            var descriptor = mocks.Stub <PropertyDescriptor>("name", new Attribute[0]);

            descriptor.Stub(d => d.Attributes).Return(attributes);
            context.Stub(c => c.PropertyDescriptor).Return(descriptor);
            mocks.ReplayAll();

            const int elementCount = 12;

            TestObject[] array = Enumerable.Repeat(new TestObject
            {
                Name = "some name"
            }, elementCount).ToArray();

            var converter = new KeyValueExpandableArrayConverter();

            // Call
            PropertyDescriptorCollection propertyDescriptors = converter.GetProperties(context, array);

            // Assert
            for (var i = 0; i < elementCount; i++)
            {
                Assert.Throws <NotSupportedException>(() => propertyDescriptors[i].SetValue(array, i));
            }

            mocks.VerifyAll();
        }
        public void Constructor_WithParameters_CreatesNewInstance()
        {
            // Call
            var attribute = new KeyValueElementAttribute("name", "value");

            // Assert
            Assert.IsInstanceOf <Attribute>(attribute);
        }
        public void GetValue_WithObjectWithoutPropertyWithName_ThrowsArgumentException()
        {
            // Setup
            var attribute = new KeyValueElementAttribute(nameof(TestObject.Name), "IDoNotExist");

            // Call
            TestDelegate test = () => attribute.GetValue(new TestObject());

            // Assert
            const string expectedMessage = "Value property 'IDoNotExist' was not found on type TestObject.";

            TestHelper.AssertThrowsArgumentExceptionAndTestMessage <ArgumentException>(test, expectedMessage);
        }
        public void GetName_WithObjectWithNonStringProperty_ReturnsValueOfProperty()
        {
            // Setup
            int expectedName = new Random(21).Next(3, 50);

            var attribute = new KeyValueElementAttribute(nameof(TestObject.NonStringName), nameof(TestObject.NonStringValue));

            // Call
            string name = attribute.GetName(new TestObject
            {
                NonStringName = expectedName
            });

            // Assert
            Assert.AreEqual(Convert.ToString(expectedName), name);
        }
        public void GetName_WithObjectWithProperty_ReturnsValueOfProperty()
        {
            // Setup
            const string expectedName = "expectedName";

            var attribute = new KeyValueElementAttribute(nameof(TestObject.Name), nameof(TestObject.Value));

            // Call
            string name = attribute.GetName(new TestObject
            {
                Name = expectedName
            });

            // Assert
            Assert.AreEqual(expectedName, name);
        }
        public void GetProperties_FromArray_ReturnPropertyDescriptorsForEachElementWithNameToOneBasedIndex(int elementCount)
        {
            // Setup
            var attribute  = new KeyValueElementAttribute(nameof(TestObject.Name), nameof(TestObject.Value));
            var attributes = new AttributeCollection(attribute);

            var mocks      = new MockRepository();
            var context    = mocks.Stub <ITypeDescriptorContext>();
            var descriptor = mocks.Stub <PropertyDescriptor>("name", new Attribute[0]);

            descriptor.Stub(d => d.Attributes).Return(attributes);
            context.Stub(c => c.PropertyDescriptor).Return(descriptor);
            mocks.ReplayAll();

            const string name  = "name";
            const string value = "value";

            TestObject[] array = Enumerable.Repeat(new TestObject
            {
                Name  = name,
                Value = value
            }, elementCount).ToArray();

            var converter = new KeyValueExpandableArrayConverter();

            // Call
            PropertyDescriptorCollection propertyDescriptors = converter.GetProperties(context, array);

            // Assert
            Assert.IsNotNull(propertyDescriptors);
            Assert.AreEqual(elementCount, propertyDescriptors.Count);
            for (var i = 0; i < elementCount; i++)
            {
                Assert.AreEqual(typeof(TestObject), propertyDescriptors[i].ComponentType);
                Assert.AreEqual(name, propertyDescriptors[i].Name);
                Assert.AreEqual(name, propertyDescriptors[i].DisplayName);
                Assert.AreEqual(value.GetType(), propertyDescriptors[i].PropertyType);
                CollectionAssert.IsEmpty(propertyDescriptors[i].Attributes);

                var actualValue = propertyDescriptors[i].GetValue(array) as string;
                Assert.NotNull(actualValue);
                Assert.AreEqual(value, actualValue);
            }

            mocks.VerifyAll();
        }