public void WriteXmlWithNoProperties(ComponentPropertyBag propertyBag, XmlWriter xmlWriter, StringWriter stringWriter, Exception e)
        {
            "Given an empty component property bag"
            .x(() =>
            {
                propertyBag = new ComponentPropertyBag();
            });

            "And a writer for the xml"
            .x(() =>
            {
                stringWriter = new StringWriter();

                xmlWriter = XmlWriter.Create(stringWriter);
            });

            "When writing the xml"
            .x(() => e = Record.Exception(() => propertyBag.WriteXml(xmlWriter)));

            "Then the code should not throw an exception"
            .x(() => e.Should().BeNull());

            "And the xml contains no properties"
            .x(() =>
            {
                xmlWriter.Flush();

                stringWriter.ToString().Should().NotBeEmpty();

                var element = XElement.Parse(stringWriter.ToString());
                element.Should().NotBeNull();
                element.Name.LocalName.Should().Be("Properties");
                element.Elements().Should().BeNullOrEmpty();
            });
        }
        public void ReadXmlWithNoProperties(ComponentPropertyBag propertyBag, string xml, XmlReader reader, Exception e)
        {
            "Given a component property bag"
            .x(() =>
            {
                propertyBag = new ComponentPropertyBag();
            });

            "And xml containing no properties"
            .x(() =>
            {
                xml = "<Properties/>";
            });

            "And a reader for the xml"
            .x(() =>
            {
                reader = XmlReader.Create(new StringReader(xml));
                reader.MoveToContent();
            });

            "When reading the xml"
            .x(() => e = Record.Exception(() => propertyBag.ReadXml(reader)));

            "Then the code should not throw an exception"
            .x(() => e.Should().BeNull());

            "And property bag is empty"
            .x(() =>
            {
                propertyBag.Should().HaveCount(0);
            });
        }
        public void WriteXmlWithOneProperty(ComponentPropertyBag propertyBag, XmlWriter xmlWriter, StringWriter stringWriter, Exception e)
        {
            "Given a component property bag"
            .x(() =>
            {
                propertyBag = new ComponentPropertyBag();
            });

            "And a writer for the xml"
            .x(() =>
            {
                stringWriter = new StringWriter();

                xmlWriter = XmlWriter.Create(stringWriter);
            });

            "And one property in the property bag"
            .x(() =>
            {
                propertyBag.Add(
                    new ComponentProperty {
                    Name = "TestName", Value = "TestValue", ValueType = "TestValueType"
                }
                    );
            });

            "When writing the xml"
            .x(() => e = Record.Exception(() => propertyBag.WriteXml(xmlWriter)));

            "Then the code should not throw an exception"
            .x(() => e.Should().BeNull());

            "And the xml contains one property"
            .x(() =>
            {
                xmlWriter.Flush();

                stringWriter.ToString().Should().NotBeEmpty();

                var element = XElement.Parse(stringWriter.ToString());
                element.Should().NotBeNull();
                element.Name.LocalName.Should().Be("Properties");
                element.Elements().Should().HaveCount(1);

                element.Elements().SingleOrDefault().Name.LocalName.Should().Be(propertyBag[0].Name);
                element.Elements().SingleOrDefault().Value.Should().Be(propertyBag[0].Value);
                element.Elements().SingleOrDefault().Attributes().SingleOrDefault(a => a.Name.LocalName == "vt").Should().NotBeNull().And.HaveValue(propertyBag[0].ValueType);
            });
        }
        public void ReadXmlWithOneProperty(ComponentPropertyBag propertyBag, ComponentProperty property, string xml, XmlReader reader, Exception e)
        {
            "Given a component property bag"
            .x(() =>
            {
                propertyBag = new ComponentPropertyBag();
            });

            "And xml containing one property"
            .x(() =>
            {
                property = new ComponentProperty {
                    Name = "NameOfTheProperty", Value = "ValueOfTheProperty", ValueType = "ValueTypeOfTheProperty"
                };

                var properties = new List <ComponentProperty> {
                    property
                };

                xml = BuildXml(properties);
            });

            "And a reader for the xml"
            .x(() =>
            {
                reader = XmlReader.Create(new StringReader(xml));
                reader.MoveToContent();
            });

            "When reading the xml"
            .x(() => e = Record.Exception(() => propertyBag.ReadXml(reader)));

            "Then the code should not throw an exception"
            .x(() => e.Should().BeNull());

            "And the property bag contains one property"
            .x(() =>
            {
                propertyBag.FirstOrDefault().Should().NotBeNull();
            });

            "And the properties should be as expected"
            .x(() =>
            {
                propertyBag[0].Name.Should().Be(property.Name);
                propertyBag[0].ValueType.Should().Be(property.ValueType);
                propertyBag[0].Value.Should().Be(property.Value);
            });
        }
        public void GetSchemaReturnsNull(ComponentPropertyBag propertyBag, XmlSchema schema, Exception e)
        {
            "Given a component property bag"
            .x(() =>
            {
                propertyBag = new ComponentPropertyBag();
            });

            "When getting the schema"
            .x(() => e = Record.Exception(() => schema = propertyBag.GetSchema()));

            "Then the code should not throw an exception"
            .x(() => e.Should().BeNull());

            "And the schema should be null"
            .x(() =>
            {
                schema.Should().BeNull();
            });
        }
        public void WriteXmlWithMultipleProperties(ComponentPropertyBag propertyBag, XmlWriter xmlWriter, StringWriter stringWriter, Exception e)
        {
            "Given a component property bag"
            .x(() =>
            {
                propertyBag = new ComponentPropertyBag();
            });

            "And a writer for the xml"
            .x(() =>
            {
                stringWriter = new StringWriter();

                xmlWriter = XmlWriter.Create(stringWriter);
            });

            "And one property in the property bag"
            .x(() =>
            {
                propertyBag.AddRange(
                    new List <ComponentProperty>()
                {
                    new ComponentProperty {
                        Name = "TestNameOne", Value = "TestValueOne", ValueType = "TestValueTypeOne"
                    },
                    new ComponentProperty {
                        Name = "TestNameTwo", Value = "TestValueTwo", ValueType = "TestValueTypeTwo"
                    },
                    new ComponentProperty {
                        Name = "TestNameThree", Value = "TestValueThree", ValueType = "TestValueTypeThree"
                    }
                });
            });

            "When writing the xml"
            .x(() => e = Record.Exception(() => propertyBag.WriteXml(xmlWriter)));

            "Then the code should not throw an exception"
            .x(() => e.Should().BeNull());

            "And the xml contains one property"
            .x(() =>
            {
                xmlWriter.Flush();

                stringWriter.ToString().Should().NotBeEmpty();

                var element = XElement.Parse(stringWriter.ToString());
                element.Should().NotBeNull();
                element.Name.LocalName.Should().Be("Properties");
                element.Elements().Should().HaveCount(propertyBag.Count);

                foreach (var property in propertyBag)
                {
                    var propertyElement = element.Elements().SingleOrDefault(e => e.Name.LocalName == property.Name);
                    propertyElement.Should().NotBeNull();
                    propertyElement.Value.Should().Be(property.Value);
                    propertyElement.Attributes().SingleOrDefault(a => a.Name.LocalName == "vt").Should().NotBeNull().And.HaveValue(property.ValueType);
                }
            });
        }
        public void ReadXmlWithMultipleProperties(ComponentPropertyBag propertyBag, List <ComponentProperty> properties, string xml, XmlReader reader, Exception e)
        {
            "Given a component property bag"
            .x(() =>
            {
                propertyBag = new ComponentPropertyBag();
            });

            "And xml containing multiple properties"
            .x(() =>
            {
                properties = new List <ComponentProperty>
                {
                    new ComponentProperty {
                        Name = "NameOfThePropertyOne", Value = "ValueOfThePropertyOne", ValueType = "ValueTypeOfThePropertyOne"
                    },
                    new ComponentProperty {
                        Name = "NameOfThePropertyTwo", Value = "ValueOfThePropertyTwo", ValueType = "ValueTypeOfThePropertyTwo"
                    },
                    new ComponentProperty {
                        Name = "NameOfThePropertyThree", Value = "ValueOfThePropertyThree", ValueType = "ValueTypeOfThePropertyThree"
                    }
                };

                xml = BuildXml(properties);
            });

            "And a reader for the xml"
            .x(() =>
            {
                reader = XmlReader.Create(new StringReader(xml));
                reader.MoveToContent();
            });

            "When reading the xml"
            .x(() => e = Record.Exception(() => propertyBag.ReadXml(reader)));

            "Then the code should not throw an exception"
            .x(() => e.Should().BeNull());

            "And the property bag contains multiple properties"
            .x(() =>
            {
                propertyBag.Should().HaveCount(properties.Count);
            });

            "And the properties should be as expected"
            .x(() =>
            {
                foreach (var property in propertyBag)
                {
                    // Find the property.
                    var testProperty = propertyBag.SingleOrDefault(p => p.Name == property.Name);
                    testProperty.Should().NotBeNull();

                    // Match the attributes.
                    property.Name.Should().Be(testProperty.Name);
                    property.ValueType.Should().Be(testProperty.ValueType);
                    property.Value.Should().Be(testProperty.Value);
                }
            });
        }