Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for DataContractSerializer.
- Loading branch information
Jason Harmon
committed
May 17, 2018
1 parent
d10fa71
commit 3560ee3
Showing
7 changed files
with
356 additions
and
8 deletions.
There are no files selected for viewing
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
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
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,20 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace SimpleSOAPClient.Tests | ||
{ | ||
[DataContract(Name = "DataContractModel", Namespace ="urn:simplesoapclient:test")] | ||
public class DataContractModel | ||
{ | ||
[DataMember(Order = 0)] | ||
public string String { get; set; } | ||
|
||
[DataMember(Order = 1)] | ||
public int Int { get; set; } | ||
|
||
[DataMember(Order = 2)] | ||
public bool Bool { get; set; } | ||
|
||
[DataMember(Order = 3)] | ||
public string[] Array { get; set; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/SimpleSOAPClient.Tests/SimpleSOAPClient.Tests.csproj
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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<AssemblyOriginatorKeyFile>../../tools/SimpleSOAPClient.snk</AssemblyOriginatorKeyFile> | ||
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign> | ||
<SignAssembly>true</SignAssembly> | ||
|
||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" /> | ||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\SimpleSOAPClient\SimpleSOAPClient.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,87 @@ | ||
using SimpleSOAPClient.Helpers; | ||
using System.Collections.Generic; | ||
using System.Xml.Linq; | ||
using Xunit; | ||
|
||
namespace SimpleSOAPClient.Tests | ||
{ | ||
public class XmlHelpersTests | ||
{ | ||
const string SERIALIZED_XML = @"<XmlModel xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""urn:simplesoapclient:test""><String>foo</String><Int>42</Int><Bool>true</Bool><Array><string>One</string><string>Two</string><string>Three</string></Array></XmlModel>"; | ||
const string SERIALIZED_DATACONTRACT = @"<DataContractModel xmlns=""urn:simplesoapclient:test"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><String>foo</String><Int>42</Int><Bool>true</Bool><Array xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string>One</a:string><a:string>Two</a:string><a:string>Three</a:string></Array></DataContractModel>"; | ||
|
||
readonly XmlModel _xmlModel = new XmlModel | ||
{ | ||
String = "foo", | ||
Bool = true, | ||
Int = 42, | ||
Array = new[] { "One", "Two", "Three" } | ||
}; | ||
|
||
readonly DataContractModel _dataContractModel = new DataContractModel | ||
{ | ||
String = "foo", | ||
Bool = true, | ||
Int = 42, | ||
Array = new[] { "One", "Two", "Three" } | ||
}; | ||
|
||
[Fact] | ||
public void ToObject_XmlSerializer() | ||
{ | ||
var result = XmlHelpers.ToObject<XmlModel>(SERIALIZED_XML); | ||
Assert.Equal("foo", result.String); | ||
Assert.True(result.Bool); | ||
Assert.Equal(42, result.Int); | ||
Assert.Equal(3, result.Array.Length); | ||
} | ||
|
||
[Fact] | ||
public void ToObject_DataContractSerializer() | ||
{ | ||
var result = XmlHelpers.ToObject<DataContractModel>(SERIALIZED_DATACONTRACT); | ||
Assert.Equal("foo", result.String); | ||
Assert.True(result.Bool); | ||
Assert.Equal(42, result.Int); | ||
Assert.Equal(3, result.Array.Length); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ToXElement_XmlSerializer() | ||
{ | ||
var result = XmlHelpers.ToXElement(_xmlModel); | ||
var actualXml = result.ToString(); | ||
Assert.Equal(SERIALIZED_XML, actualXml, new XmlEqualityComparer()); | ||
} | ||
|
||
[Fact] | ||
public void ToXElement_DataContractSerializer() | ||
{ | ||
var result = XmlHelpers.ToXElement(_dataContractModel); | ||
var actualXml = result.ToString(); | ||
Assert.Equal(SERIALIZED_DATACONTRACT, actualXml, new XmlEqualityComparer()); | ||
} | ||
|
||
[Fact] | ||
public void ToXmlString_XmlSerializer() | ||
{ | ||
var result = XmlHelpers.ToXmlString(_xmlModel); | ||
Assert.Equal(SERIALIZED_XML, result, new XmlEqualityComparer()); | ||
} | ||
|
||
[Fact] | ||
public void ToXmlString_DataContractSerializer() | ||
{ | ||
var result = XmlHelpers.ToXmlString(_dataContractModel); | ||
Assert.Equal(SERIALIZED_DATACONTRACT, result, new XmlEqualityComparer()); | ||
} | ||
|
||
class XmlEqualityComparer : IEqualityComparer<string> | ||
{ | ||
public bool Equals(string x, string y) => XmlNormalizer.DeepEqualsWithNormalization(XDocument.Parse(x), XDocument.Parse(y), null); | ||
|
||
public int GetHashCode(string obj) => XmlNormalizer.Normalize(XDocument.Parse(obj), null).GetHashCode(); | ||
} | ||
} | ||
} |
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,20 @@ | ||
using System.Xml.Serialization; | ||
|
||
namespace SimpleSOAPClient.Tests | ||
{ | ||
[XmlRoot(Namespace = "urn:simplesoapclient:test")] | ||
public class XmlModel | ||
{ | ||
[XmlElement(Order = 0)] | ||
public string String { get; set; } | ||
|
||
[XmlElement(Order = 1)] | ||
public int Int { get; set; } | ||
|
||
[XmlElement(Order = 2)] | ||
public bool Bool { get; set; } | ||
|
||
[XmlArray(Order = 3)] | ||
public string[] Array { get; set; } | ||
} | ||
} |
Oops, something went wrong.