-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathDataContractSerializerTests.cs
104 lines (83 loc) · 4.08 KB
/
DataContractSerializerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.IO;
using System.Runtime.Serialization;
using UnitsNet.Units;
using Xunit;
namespace UnitsNet.Tests.Serialization.Xml
{
/// <summary>
/// These tests demonstrate the behavior of the DataContractSerializer (the default WCF serializer) when dealing with
/// quantities
/// </summary>
public class DataContractSerializerTests : SerializationTestsBase<string>
{
private const string XmlSchema = "xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"";
private const string Namespace = "xmlns=\"http://schemas.datacontract.org/2004/07/UnitsNet\"";
protected override string SerializeObject(object obj)
{
var serializer = new DataContractSerializer(obj.GetType());
using var stream = new MemoryStream();
serializer.WriteObject(stream, obj);
stream.Position = 0;
using var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
protected override T DeserializeObject<T>(string xml)
{
var serializer = new DataContractSerializer(typeof(T));
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
return (T)serializer.ReadObject(stream);
}
[Fact]
public void DoubleQuantity_SerializedWithValueAndMemberName()
{
var quantity = new Mass(1.20, MassUnit.Milligram);
var expectedXml = $"<Mass {Namespace} {XmlSchema}><Value>1.2</Value><Unit>Milligram</Unit></Mass>";
var xml = SerializeObject(quantity);
Assert.Equal(expectedXml, xml);
}
[Fact]
public void DecimalQuantity_SerializedWithValueAndMemberName()
{
var quantity = new Information(1.20m, InformationUnit.Exabyte);
var expectedXml = $"<Information {Namespace} {XmlSchema}><Value>1.20</Value><Unit>Exabyte</Unit></Information>";
var xml = SerializeObject(quantity);
Assert.Equal(expectedXml, xml);
}
[Fact]
public void DoubleQuantity_InScientificNotation_SerializedWithExpandedValueAndMemberName()
{
var quantity = new Mass(1E+9, MassUnit.Milligram);
var expectedXml = $"<Mass {Namespace} {XmlSchema}><Value>1000000000</Value><Unit>Milligram</Unit></Mass>";
var xml = SerializeObject(quantity);
Assert.Equal(expectedXml, xml);
}
[Fact]
public void DecimalQuantity_InScientificNotation_SerializedWithExpandedValueAndMemberName()
{
var quantity = new Information(1E+9m, InformationUnit.Exabyte);
var expectedXml = $"<Information {Namespace} {XmlSchema}><Value>1000000000</Value><Unit>Exabyte</Unit></Information>";
var xml = SerializeObject(quantity);
Assert.Equal(expectedXml, xml);
}
[Fact]
public void InterfaceObject_IncludesTypeInformation()
{
var testObject = new TestInterfaceObject { Quantity = new Information(1.20m, InformationUnit.Exabyte) };
var quantityNamespace = "xmlns:a=\"http://schemas.datacontract.org/2004/07/UnitsNet\""; // there is an extra 'a' compared to Namespace
var expectedQuantityXml = $"<Quantity i:type=\"a:Information\" {quantityNamespace}><a:Value>1.20</a:Value><a:Unit>Exabyte</a:Unit></Quantity>";
var expectedXml = $"<TestInterfaceObject xmlns=\"http://schemas.datacontract.org/2004/07/UnitsNet.Tests.Serialization\" {XmlSchema}>{expectedQuantityXml}</TestInterfaceObject>";
var xml = SerializeObject(testObject);
Assert.Equal(expectedXml, xml);
}
[Fact]
public void InterfaceObject_WithMissingKnownTypeInformation_ThrowsSerializationException()
{
var testObject = new TestInterfaceObject { Quantity = new Volume(1.2, VolumeUnit.Microliter) };
Assert.Throws<SerializationException>(() => SerializeObject(testObject));
}
}
}