public void GetValueInXmlForm_ShouldReturnXmlRepresentation() { var person = new Person {IsEnabled = true}; var mapping = new AttributeMapping<Person, bool>(Ns + "Person", x => x.IsEnabled); mapping.GetValueInXmlForm(person).ShouldBe("true"); }
public void SetValueFromXmlForm_ShouldSetPropertyValue() { var person = new Person(); var mapping = new AttributeMapping<Person, bool>(Ns + "Person", x => x.IsEnabled); mapping.SetValueFromXmlForm(person, "true"); person.IsEnabled.ShouldBe(true); }
public void CustomSerializer_ShouldSerializeCustomValue() { var person = new Person {Address = new Address {StreetName = "231 Queen Street", City = "Auckland"}}; var mapping = new AttributeMapping<Person, Address>(Ns + "Person", x => x.Address, UnpackAddressFromAttribute, PackAddressForAttribute); var actual = mapping.GetValueInXmlForm(person); actual.ShouldBe("231 Queen Street;Auckland"); }
public void SetOnContainer_ShouldWriteToContainer() { var person = new Person(); var mapping = new ChildElementMapping<Person, Address>(Ns + "Address", x => x.Address); var expected = new Address {StreetName = "231 Queen Street", City = "Auckland"}; mapping.SetOnContainer(person, expected); person.Address.ShouldBe(expected); }
public void CustomDeserializer_ShouldDeserializeCustomValue() { var person = new Person(); var mapping = new AttributeMapping<Person, Address>(Ns + "Person", x => x.Address, UnpackAddressFromAttribute, PackAddressForAttribute); mapping.SetValueFromXmlForm(person, "231 Queen Street;Auckland"); person.Address.StreetName.ShouldBe("231 Queen Street"); person.Address.City.ShouldBe("Auckland"); }
public void GetCollection_Property_ShouldReturnCollection() { var person = new Person(); var expected = new ContactMethod { Type = ContactMethodType.HomePhone, Value = "555-1234" }; var mapping = new CollectionChildElementMapping<Person, ContactMethod>(Ns + "ContactMethod", x => x.ContactMethods); mapping.GetCollection(person).ShouldBe(null); mapping.AddToCollection(person, expected); mapping.GetCollection(person).ShouldNotBe(null); mapping.GetCollection(person).ShouldBe(person.ContactMethods); }
public void GetFromContainer_ShouldReadFromContainer() { var person = new Person {Address = new Address {StreetName = "231 Queen Street", City = "Auckland"}}; var mapping = new ChildElementMapping<Person, Address>(Ns + "Address", x => x.Address); mapping.CreateInstance().ShouldBeTypeOf(typeof(Address)); mapping.NamespaceUri.ShouldBe(Ns.NamespaceName); mapping.LocalName.ShouldBe("Address"); var actual = mapping.GetFromContainer(person); actual.ShouldBe(person.Address); }
public void AddToCollection_Property_ShouldAddMemberAndInstantiateCollectionIfRequired() { var person = new Person(); var expected = new ContactMethod{Type = ContactMethodType.HomePhone, Value = "555-1234"}; var mapping = new CollectionChildElementMapping<Person, ContactMethod>(Ns + "ContactMethod", x => x.ContactMethods); mapping.CreateInstance().ShouldBeTypeOf(typeof(ContactMethod)); mapping.NamespaceUri.ShouldBe(Ns.NamespaceName); mapping.LocalName.ShouldBe("ContactMethod"); mapping.AddToCollection(person, expected); person.ContactMethods[0].ShouldBe(expected); }
public void AddToCollection_ContainingCollection_ShouldAddMemberToMethodArgument() { var person = new Person {ContactMethods = new List<ContactMethod>()}; var expected = new ContactMethod {Type = ContactMethodType.HomePhone, Value = "555-1234"}; var mapping = new CollectionChildElementMapping<Person, ContactMethod>(Ns + "ContactMethod", null); mapping.CreateInstance().ShouldBeTypeOf(typeof(ContactMethod)); mapping.NamespaceUri.ShouldBe(Ns.NamespaceName); mapping.LocalName.ShouldBe("ContactMethod"); mapping.AddToCollection(person.ContactMethods, expected); person.ContactMethods[0].ShouldBe(expected); }
public void Build_ShouldCreateValidMapping() { var person = new Person(); var method = new ContactMethod {Type = ContactMethodType.HomePhone, Value = "555-1234"}; var builder = new CollectionChildElementMappingBuilder<Person, ContactMethod, object>(null, Ns + "ContactMethod", x => x.ContactMethods); builder.Attribute(Ns + "Value", x => x.Value); builder.CollectionElement<string>(Ns + "Value1"); builder.CollectionElement(Ns + "Value2", x => x.AdditionalValues); var actual = (CollectionChildElementMapping<Person, ContactMethod>)builder.Build(); actual.AddToCollection(person, method); person.ContactMethods[0].ShouldBe(method); actual.Attributes.Length.ShouldBe(1); actual.ChildElements.Length.ShouldBe(2); }
public void Build_ShouldCreateValidMapping() { var person = new Person(); var address = new Address {StreetName = "231 Queen Street", City = "Auckland"}; var builder = new ChildElementMappingBuilder<Person, Address, object>(null, Ns + "Address", x => x.Address); builder.Attribute(Ns + "City", x => x.City, x => x, x => x); builder.Element(Ns + "PostCode", x => x.PostCode); builder.CollectionElement(Ns + "AreaCode", x => x.AreaCodes); var actual = (ChildElementMapping<Person, Address>)builder.Build(); actual.SetOnContainer(person, address); actual.GetFromContainer(person).ShouldBe(address); person.Address.ShouldBe(address); actual.Attributes.Length.ShouldBe(1); actual.ChildElements.Length.ShouldBe(2); actual.ChildElements[0].ShouldBeTypeOf(typeof(ChildElementMapping<Address, PostCode>)); actual.ChildElements[1].ShouldBeTypeOf(typeof(CollectionChildElementMapping<Address, AreaCode>)); }
public void CustomAnyElementSerializer_ShouldSerializeToCustomValue() { var description = new FluentSchemaDescription(); description.Element<Person>("Person") .AnyElement<int>(x => x.CustomIntegerElements, reader => reader.ReadElementContentAsInt(), (writer, value) => writer.WriteElementString("NewName", value.ToString())); var schema = description.Build(); var serializer = new Serializer(schema); var person = new Person {CustomIntegerElements = new List<int> {1, 2, 3}}; var stream = new MemoryStream(); serializer.Serialize(stream, person); XAssert.AreEqual(@"<Person> <NewName>1</NewName> <NewName>2</NewName> <NewName>3</NewName> </Person>", stream.ToXDocument().ToString()); }