-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b8c330
commit b4e9b3e
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
Gu.Xml.Tests/XmlTests.SerializeWithDataContractAttributes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} | ||
} | ||
} |