From b4e9b3ed5aff73e075bf7dce9db6b1e8d31540b7 Mon Sep 17 00:00:00 2001 From: JohanLarsson Date: Mon, 10 Sep 2018 10:36:26 +0200 Subject: [PATCH] WIP #20. --- ...sts.SerializeWithDataContractAttributes.cs | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs diff --git a/Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs b/Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs new file mode 100644 index 0000000..d8c7c90 --- /dev/null +++ b/Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs @@ -0,0 +1,114 @@ +namespace Gu.Xml.Tests +{ + using System; + using System.Diagnostics; + using System.Runtime.Serialization; + using NUnit.Framework; + + public partial class XmlTests + { + public class SerializeWithDataContractAttributes + { + private static readonly TestCaseData[] Values = + { + new TestCaseData(new WithDataContractAttribute { Value = 1 }), + new TestCaseData(new WithDataContractAttributeExplicitName { Value = 1 }), + new TestCaseData(new PropertyWithIgnoreDataMemberAttribute { Value = 1 }), + new TestCaseData(new PropertyWithDataMemberAttribute { Value = 1 }), + new TestCaseData(new PropertyWithDataMemberAttributeExplicitName { Value = 1 }), + new TestCaseData(new ExplicitInterfaceWithDataMemberAttribute()), + new TestCaseData(new FieldWithIgnoreDataMemberAttribute { Value = 1 }), + new TestCaseData(new FieldWithDataMemberAttribute { Value = 1 }), + new TestCaseData(new FieldWithDataMemberAttributeExplicitName { Value = 1 }), + }; + + [TestCaseSource(nameof(Values))] + public void Serialize(object value) + { + var expected = Reference.DataContractSerializer(value); + var actual = Xml.Serialize(value); + if (actual == expected) + { + if (Debugger.IsAttached) + { + Console.WriteLine(expected); + } + + return; + } + + Console.WriteLine("Expected:"); + Console.Write(expected); + Console.WriteLine(); + Console.WriteLine(); + + Console.WriteLine("Actual:"); + Console.Write(actual); + Console.WriteLine(); + Console.WriteLine(); + + Assert.AreEqual(expected, actual); + } + + [DataContract] + public class WithDataContractAttribute + { + public int Value { get; set; } = 1; + } + + [DataContract(Name = "Name")] + public class WithDataContractAttributeExplicitName + { + public int Value { get; set; } = 1; + } + + public class PropertyWithDataMemberAttribute + { + [DataMember] + public int Value { get; set; } + } + + public class PropertyWithDataMemberAttributeExplicitName + { + [DataMember(Name = "Name")] + public int Value { get; set; } + } + + public class FieldWithDataMemberAttribute + { + [DataMember] + public int Value = 1; + } + + public class FieldWithDataMemberAttributeExplicitName + { + [DataMember(Name = "Name")] + public int Value = 1; + } + + public interface IValue + { + // ReSharper disable once UnusedMember.Global + int Value { get; set; } + } + + public class ExplicitInterfaceWithDataMemberAttribute : IValue + { + [DataMember] + int IValue.Value { get; set; } + } + + public class PropertyWithIgnoreDataMemberAttribute + { + [IgnoreDataMember] + public int Value { get; set; } + } + + public class FieldWithIgnoreDataMemberAttribute + { + [IgnoreDataMember] + public int Value { get; set; } + } + } + } +} \ No newline at end of file