-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlSerializerTests.cs
192 lines (175 loc) · 5.75 KB
/
XmlSerializerTests.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
namespace Demo.Serialization
{
using BlueDotBrigade.DatenLokator.TestTools;
using Demo.Droids;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// The intent of this class is to demonstrate how to retrieve input data using the DatenLokator library.
/// </summary>
[TestClass]
public class XmlSerializerTests
{
/// <summary>
/// An example of a typical unit test that includes an embedded string value for comparison.
/// </summary>
/// <remarks>
/// Notice...
/// <list type="number">
/// <item>the visual noise introduced by the multiline string value</item>
/// <item>how the escape codes make the multiline string value harder to read</item>
/// </list>
/// </remarks>
[TestMethod]
public void ToXml_AstromechDroid_StringsMatch()
{
// Arrange
var droid = new AstromechDroid
{
SerialNo = "IA-16CFR2D2",
Manufacturer = "Industrial Automaton",
ProductLine = "R-Series",
Peripherals = new Peripheral[]
{
new Peripheral { Type = "Mark IV Locomotion System", SerialNumber = "IA-1A963F95"},
new Peripheral { Type = "SCOMP Link", SerialNumber = "IA-80C56868"},
new Peripheral { Type = "Fusioncutter", SerialNumber = "IA-6D9D071C"},
new Peripheral { Type = "Holoprojector", SerialNumber = "IA-12735813"},
}
};
// Act
var message = new XmlSerializer().ToXml(droid);
// Assert
var expectedMessage =
@"<?xml version=""1.0"" encoding=""utf-8""?>" + "\r\n" +
@"<AstromechDroid xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/Demo.Droids"">" + "\r\n" +
@" <Manufacturer>Industrial Automaton</Manufacturer>" + "\r\n" +
@" <SerialNo>IA-16CFR2D2</SerialNo>" + "\r\n" +
@" <Peripherals>" + "\r\n" +
@" <Peripheral>" + "\r\n" +
@" <SerialNumber>IA-1A963F95</SerialNumber>" + "\r\n" +
@" <Type>Mark IV Locomotion System</Type>" + "\r\n" +
@" </Peripheral>" + "\r\n" +
@" <Peripheral>" + "\r\n" +
@" <SerialNumber>IA-80C56868</SerialNumber>" + "\r\n" +
@" <Type>SCOMP Link</Type>" + "\r\n" +
@" </Peripheral>" + "\r\n" +
@" <Peripheral>" + "\r\n" +
@" <SerialNumber>IA-6D9D071C</SerialNumber>" + "\r\n" +
@" <Type>Fusioncutter</Type>" + "\r\n" +
@" </Peripheral>" + "\r\n" +
@" <Peripheral>" + "\r\n" +
@" <SerialNumber>IA-12735813</SerialNumber>" + "\r\n" +
@" <Type>Holoprojector</Type>" + "\r\n" +
@" </Peripheral>" + "\r\n" +
@" </Peripherals>" + "\r\n" +
@" <ProductLine>R-Series</ProductLine>" + "\r\n" +
@"</AstromechDroid>";
// Not only does the preceding line make the test visually noisy,
// ... but the string is also very error prone.
Assert.AreEqual(expectedMessage, message);
}
/// <summary>
/// An example of DatenLokator automatically retrieving the correct input data for the test case.
/// </summary>
/// <remarks>
/// By default, DatenLokator expects the input data to be stored in a directory structure
/// that mirrors the namespace. For example, in this case:
///
/// \.Daten\Serialization\XmlSerializerTests\ProtocolDroid.xml
///
/// Where:
/// <list type="bullet">
/// <item>Serialization : represents the namespace</item>
/// <item>XmlSerializerTests : represents the MS Test class name</item>
/// <item>ProtocolDroid.xml : represents the test case</item>
/// </list>
/// </remarks>
[TestMethod]
public void ToXml_ProtocolDroid_StringsMatch()
{
// Arrange
var droid = new ProtocolDroid
{
SerialNo = "CG-C3P09D41C",
Manufacturer = "Cybot Galactica",
SupportedLanguages = 6_000_000
};
// Act
var message = new XmlSerializer().ToXml(droid);
// Assert
Assert.AreEqual(new Daten().AsString(), message);
}
/// <summary>
/// An example of DatenLokator retrieving input data using a specific file name.
/// </summary>
[TestMethod]
public void ToXml_RudeProtocolDroid_StringsMatch()
{
// Arrange
var droid = new ProtocolDroid
{
SerialNo = "CG-E3PO9D41C",
Manufacturer = "Cybot Galactica",
SupportedLanguages = 1_235_813
};
// Act
var message = new XmlSerializer().ToXml(droid);
// Assert
Assert.AreEqual(new Daten().AsString("Bespin.xml"), message);
}
/// <summary>
/// An example of DatenLokator automatically retrieving shared data
/// from the `\.Daten\.Global` cache.
/// </summary>
/// <remarks>
/// This approach is useful when many automated tests rely on
/// the same input data.
/// </remarks>
[TestMethod]
public void ToXml_PitDroid_StringsMatch()
{
// Arrange
var droid = new PitDroid
{
SerialNo = "SD-922WAC471",
Manufacturer = "Serv-O-Droid",
Series = "DUM",
};
// Act
var message = new XmlSerializer().ToXml(droid);
// Assert
Assert.AreEqual(new Daten().AsString(), message);
}
/// <summary>
/// An example of DatenLokator automatically retrieving compressed input data
/// for a specific test case.
/// </summary>
/// <remarks>
/// DatenLokator expects the zip file to be stored within the directory that
/// matches the namespace. In this case:
///
/// \.Daten\Serialization\XmlSerializerTests\
/// \.Daten\Serialization\XmlSerializerTests.zip
/// </remarks>
[TestMethod]
public void ToXml_SuperBattleDroid_StringsMatch()
{
// Arrange
var droid = new SuperBattleDroid()
{
SerialNo = "BA-4248964B3",
Manufacturer = "Baktoid Automata",
// Imagine a large data set where it would be useful
// to compress the input data.
WeaponsActivationKey = new byte[8192],
};
// Act
var message = new XmlSerializer().ToXml(droid);
// Assert
Assert.AreEqual(
expected: new Daten().AsString(),
actual: message,
message:"Input data was not automatically decompressed.");
}
}
}