示例#1
0
        public void ReadFooWithMissedPropertyThatRequiredTest()
        {
            var contract = new XmlObjectContractBuilder <Foo>()
                           .SetName("test")
                           .SetProperty(x => x.Id, p => p.SetName("id").SetRequired())
                           .SetProperty(x => x.Name, "name")
                           .Build();

            var converter = GetConverter();
            var value     = "<xml><name>test</name></xml>";

            Assert.Throws <XmlSerializationException>(() => converter.ParseXml <Foo>(value, contract: contract));
        }
示例#2
0
        public void PropertiesOrderWithInnerTestMappingTest()
        {
            var contract = new XmlObjectContractBuilder <Test>()
                           .SetProperty(x => x.Prop1, p => p.SetMappingType(XmlMappingType.Attribute))
                           .SetProperty(x => x.Prop2, p => p.SetMappingType(XmlMappingType.InnerText))
                           .SetProperty(x => x.Prop3, p => p.SetMappingType(XmlMappingType.Attribute))
                           .Build();

            var properties = contract.Properties;

            Assert.AreEqual("Prop1", properties[0].PropertyName);
            Assert.AreEqual("Prop3", properties[1].PropertyName);
            Assert.AreEqual("Prop2", properties[2].PropertyName);
        }
        public void ResolveCollectionContractWithoutSystemAttributesTest()
        {
            var resolver  = new XmlContractResolver(NamingConventions.CamelCase, ignoreSystemAttributes: true);
            var valueType = typeof(TestClassWithCollections);

            var actual = resolver.ResolveContract(valueType);

            var expected = new XmlObjectContractBuilder <TestClassWithCollections>()
                           .SetName("testClassWithCollections")
                           .SetProperty(x => x.Array, p => p.SetName("array"))
                           .SetProperty(x => x.GenericList, p => p.SetName("genericList"))
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ResolveObjectContractWithFlattenCollectionTest()
        {
            var actual = new XmlContractResolver(NamingConventions.CamelCase)
                         .ResolveContract <TestClassWithFlattenCollection>();

            var expected = new XmlObjectContractBuilder <TestClassWithFlattenCollection>()
                           .SetName("testClassWithFlattenCollection")
                           .SetProperty(x => x.Items, p => p
                                        .SetName("number")
                                        .SetCollection()
                                        .SetItem("number"))
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ResolveObjectContractWithCollectionsTest()
        {
            var actual = new XmlContractResolver(NamingConventions.CamelCase)
                         .ResolveContract <TestClassWithCollections>();

            var expected = new XmlObjectContractBuilder <TestClassWithCollections>()
                           .SetName("testClassWithCollections")
                           .SetProperty(x => x.Array, p => p.SetName("array"))
                           .SetProperty(x => x.GenericList, p => p
                                        .SetName("Array2")
                                        .SetItem(new XmlName("Item", "http://example.org")))
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
示例#6
0
        public void PropertiesOrderTest()
        {
            var contract = new XmlObjectContractBuilder <Test>()
                           .SetName("test")
                           .SetProperty(x => x.Prop1, p => p.SetOrder(10))
                           .SetProperty(x => x.Prop2, p => p.SetOrder(3))
                           .SetProperty(x => x.Prop3)
                           .Build();

            var properties = contract.Properties;

            Assert.AreEqual("Prop3", properties[0].PropertyName);
            Assert.AreEqual("Prop2", properties[1].PropertyName);
            Assert.AreEqual("Prop1", properties[2].PropertyName);
        }
        public void ResolveGenericCollectionContractTest()
        {
            var resolver  = new XmlContractResolver(NamingConventions.CamelCase, ignoreSystemAttributes: true);
            var valueType = typeof(List <TestClass>);

            var actual = resolver.ResolveContract(valueType);

            var expected = new XmlObjectContractBuilder <List <TestClass> >()
                           .SetName("list")
                           .SetProperty(x => x.Capacity, "capacity")
                           .SetProperty(x => x.Count, "count")
                           .SetItem("testClass")
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ResolveObjectContractWithoutSystemAttributesTest()
        {
            var resolver  = new XmlContractResolver(NamingConventions.CamelCase, ignoreSystemAttributes: true);
            var valueType = typeof(TestClass);

            var actual = resolver.ResolveContract(valueType);

            var expected = new XmlObjectContractBuilder <TestClass>()
                           .SetName("testClass")
                           .SetProperty(x => x.Id, "id")
                           .SetProperty(x => x.Value, "value")
                           .SetProperty(x => x.Comment, "comment")
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ResolveObjectContractWithElementKnownTypesTest()
        {
            var resolver = new XmlContractResolver(NamingConventions.CamelCase);

            var actual = resolver.ResolveContract <TestClassWithElementKnownTypes>();

            var expected = new XmlObjectContractBuilder <TestClassWithElementKnownTypes>()
                           .SetName("testClassWithElementKnownTypes")
                           .SetProperty(x => x.Value, p => p
                                        .SetName("object")
                                        .SetKnownType <int>("number")
                                        .SetKnownType <string>("string"))
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ResolveObjectContractWithInnerTextTest()
        {
            var resolver = new XmlContractResolver(NamingConventions.Ignore);

            var valueType = typeof(TestClassWithInnerText);

            var actual = resolver.ResolveContract(valueType);

            var expected = new XmlObjectContractBuilder <TestClassWithInnerText>()
                           .SetName("TestClassWithInnerText")
                           .SetProperty(x => x.Comment, p => p
                                        .SetMappingType(XmlMappingType.InnerText))
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ResolveObjectContractWithCollectionItemKnownTypesTest()
        {
            var actual = new XmlContractResolver(NamingConventions.CamelCase)
                         .ResolveContract <TestClassWithCollectionItemKnownTypes>();

            var expected = new XmlObjectContractBuilder <TestClassWithCollectionItemKnownTypes>()
                           .SetName("testClassWithCollectionItemKnownTypes")
                           .SetProperty(x => x.Items, p => p
                                        .SetName("items")
                                        .SetItem(i => i
                                                 .SetName("object")
                                                 .SetKnownType <int>("number")
                                                 .SetKnownType <string>("string")))
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ResolveObjectContractTest()
        {
            var resolver  = new XmlContractResolver(NamingConventions.CamelCase);
            var valueType = typeof(TestClass);

            var actual = resolver.ResolveContract(valueType);

            var expected = new XmlObjectContractBuilder <TestClass>()
                           .SetName("test")
                           .SetProperty(x => x.Id, p => p
                                        .SetName(new XmlName("ID", "http://example.org"))
                                        .SetMappingType(XmlMappingType.Attribute))
                           .SetProperty(x => x.Value, p => p
                                        .SetName("VALUE")
                                        .SetNullValueHandling(XmlNullValueHandling.Include).SetOrder(10))
                           .SetProperty(x => x.Comment, "comment")
                           .Build();

            XmlContractAssert.AreEqual(expected, actual);
        }
        public void ReadCollectionWithKnownItemTypeTest()
        {
            var collectionType = GetCollectionType <Foo>();
            var xml            = @"<xml><foo><id>1</id><name>foo</name></foo><fooBar><id>2</id><name>foo-bar</name></fooBar></xml>";

            var contract = new XmlObjectContractBuilder(collectionType)
                           .SetItem(typeof(Foo), x => x.SetName("foo").SetKnownType <FooBar>("fooBar"))
                           .Build();

            var actual = CreateConverter().ParseXml(collectionType, xml, contract: contract);

            var expected = new List <Foo>
            {
                new Foo {
                    Id = 1, Name = "foo"
                },
                new FooBar {
                    Id = 2, Name = "foo-bar"
                }
            };

            Assert.IsInstanceOf(collectionType, actual);
            Assert.That(actual, Is.EqualTo(expected));
        }
        public void WriteCollectionWithKnownItemTypeTest()
        {
            var value = GetCollection(new List <Foo>
            {
                new Foo {
                    Id = 1, Name = "foo"
                },
                new FooBar {
                    Id = 2, Name = "foo-bar"
                }
            });

            var collectionType = value.GetType();
            var itemType       = typeof(Foo);

            var contract = new XmlObjectContractBuilder(collectionType)
                           .SetItem(itemType, x => x.SetName("foo").SetKnownType <FooBar>("fooBar"))
                           .Build();

            var actual   = CreateConverter().ToXml(collectionType, value, contract: contract);
            var expected = @"<xml><foo><id>1</id><name>foo</name></foo><fooBar><id>2</id><name>foo-bar</name></fooBar></xml>";

            Assert.That(actual, IsXml.Equals(expected));
        }