forked from fluentassertions/fluentassertions
/
XElementAssertions.cs
375 lines (337 loc) · 18.6 KB
/
XElementAssertions.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using FluentAssertions.Common;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;
using FluentAssertions.Xml.Equivalency;
namespace FluentAssertions.Xml
{
/// <summary>
/// Contains a number of methods to assert that an <see cref="XElement"/> is in the expected state.
/// </summary>
[DebuggerNonUserCode]
public class XElementAssertions : ReferenceTypeAssertions<XElement, XElementAssertions>
{
/// <summary>
/// Initializes a new instance of the <see cref="XElementAssertions" /> class.
/// </summary>
public XElementAssertions(XElement xElement)
: base(xElement)
{
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> equals the
/// <paramref name="expected"/> element, by using
/// <see cref="XNode.DeepEquals(XNode, XNode)"/>
/// </summary>
/// <param name="expected">The expected element</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<XElementAssertions> Be(XElement expected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition(XNode.DeepEquals(Subject, expected))
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:subject} to be {0}{reason}, but found {1}.", expected, Subject);
return new AndConstraint<XElementAssertions>(this);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> does not equal the
/// <paramref name="unexpected"/> element, using
/// <see cref="XNode.DeepEquals(XNode, XNode)" />.
/// </summary>
/// <param name="unexpected">The unexpected element</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<XElementAssertions> NotBe(XElement unexpected, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.ForCondition((Subject is null && unexpected is not null) || !XNode.DeepEquals(Subject, unexpected))
.BecauseOf(because, becauseArgs)
.FailWith("Did not expect {context:subject} to be {0}{reason}.", unexpected);
return new AndConstraint<XElementAssertions>(this);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> is equivalent to the
/// <paramref name="expected"/> element, using a semantic equivalency
/// comparison.
/// </summary>
/// <param name="expected">The expected element</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<XElementAssertions> BeEquivalentTo(XElement expected, string because = "",
params object[] becauseArgs)
{
using (XmlReader subjectReader = Subject?.CreateReader())
using (XmlReader expectedReader = expected?.CreateReader())
{
var xmlReaderValidator = new XmlReaderValidator(subjectReader, expectedReader, because, becauseArgs);
xmlReaderValidator.Validate(shouldBeEquivalent: true);
}
return new AndConstraint<XElementAssertions>(this);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> is not equivalent to
/// the <paramref name="unexpected"/> element, using a semantic
/// equivalency comparison.
/// </summary>
/// <param name="unexpected">The unexpected element</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<XElementAssertions> NotBeEquivalentTo(XElement unexpected, string because = "",
params object[] becauseArgs)
{
using (XmlReader subjectReader = Subject?.CreateReader())
using (XmlReader otherReader = unexpected?.CreateReader())
{
var xmlReaderValidator = new XmlReaderValidator(subjectReader, otherReader, because, becauseArgs);
xmlReaderValidator.Validate(shouldBeEquivalent: false);
}
return new AndConstraint<XElementAssertions>(this);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> has the specified <paramref name="expected"/> value.
/// </summary>
/// <param name="expected">The expected value</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndConstraint<XElementAssertions> HaveValue(string expected, string because = "", params object[] becauseArgs)
{
bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith("Expected the element to have value {0}{reason}, but {context:member} is <null>.", expected);
if (success)
{
Execute.Assertion
.ForCondition(Subject.Value == expected)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:subject} '{0}' to have value {1}{reason}, but found {2}.",
Subject.Name, expected, Subject.Value);
}
return new AndConstraint<XElementAssertions>(this);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> has an attribute with the specified <paramref name="expectedName"/>
/// and <paramref name="expectedValue"/>.
/// </summary>
/// <param name="expectedName">The name of the expected attribute</param>
/// <param name="expectedValue">The value of the expected attribute</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expectedName"/> is <c>null</c> or empty.</exception>
public AndConstraint<XElementAssertions> HaveAttribute(string expectedName, string expectedValue, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNullOrEmpty(expectedName, nameof(expectedName));
return HaveAttribute(XNamespace.None + expectedName, expectedValue, because, becauseArgs);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> has an attribute with the specified <paramref name="expectedName"/>
/// and <paramref name="expectedValue"/>.
/// </summary>
/// <param name="expectedName">The name <see cref="XName"/> of the expected attribute</param>
/// <param name="expectedValue">The value of the expected attribute</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expectedName"/> is <c>null</c>.</exception>
public AndConstraint<XElementAssertions> HaveAttribute(XName expectedName, string expectedValue, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expectedName, nameof(expectedName));
string expectedText = expectedName.ToString();
bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith(
"Expected attribute {0} in element to have value {1}{reason}, but {context:member} is <null>.",
expectedText, expectedValue);
if (success)
{
XAttribute attribute = Subject.Attribute(expectedName);
Execute.Assertion
.ForCondition(attribute is not null)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:subject} to have attribute {0} with value {1}{reason},"
+ " but found no such attribute in {2}",
expectedText, expectedValue, Subject);
Execute.Assertion
.ForCondition(attribute.Value == expectedValue)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected attribute {0} in {context:subject} to have value {1}{reason}, but found {2}.",
expectedText, expectedValue, attribute.Value);
}
return new AndConstraint<XElementAssertions>(this);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> has a direct child element with the specified
/// <paramref name="expected"/> name.
/// </summary>
/// <param name="expected">The name <see cref="XName"/> of the expected child element</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <c>null</c> or empty.</exception>
public AndWhichConstraint<XElementAssertions, XElement> HaveElement(string expected, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNullOrEmpty(expected, nameof(expected));
return HaveElement(XNamespace.None + expected, because, becauseArgs);
}
/// <summary>
/// Asserts that the current <see cref="XElement"/> has a direct child element with the specified
/// <paramref name="expected"/> name.
/// </summary>
/// <param name="expected">The name <see cref="XName"/> of the expected child element</param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="expected"/> is <c>null</c>.</exception>
public AndWhichConstraint<XElementAssertions, XElement> HaveElement(XName expected, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected));
bool success = Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(Subject is not null)
.FailWith(
"Expected the element to have child element {0}{reason}, but {context:member} is <null>.",
expected.ToString().EscapePlaceholders());
XElement xElement = null;
if (success)
{
xElement = Subject.Element(expected);
Execute.Assertion
.ForCondition(xElement is not null)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:subject} to have child element {0}{reason}, but no such child element was found.",
expected.ToString().EscapePlaceholders());
}
return new AndWhichConstraint<XElementAssertions, XElement>(this, xElement);
}
/// <summary>
/// Asserts that the <see cref="XElement"/> of the current <see cref="XElement"/> has the specified occurrence of
/// child elements with the specified <paramref name="expected"/> name.
/// </summary>
/// <param name="expected">
/// The full name <see cref="XName"/> of the expected child element of the current element's <see cref="XElement"/>.
/// </param>
/// <param name="occurrenceConstraint">
/// A constraint specifying the number of times the specified elements should appear.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndWhichConstraint<XElementAssertions, IEnumerable<XElement>> HaveElement(XName expected,
OccurrenceConstraint occurrenceConstraint, string because = "",
params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected),
"Cannot assert the element has an element count if the element name is <null>.");
bool success = Execute.Assertion
.ForCondition(Subject is not null)
.BecauseOf(because, becauseArgs)
.FailWith(
"Expected {context:subject} to have an element with count of {0}{reason}, but the element itself is <null>.",
expected.ToString());
IEnumerable<XElement> xElements = Enumerable.Empty<XElement>();
if (success)
{
xElements = Subject.Elements(expected);
int actualCount = xElements.Count();
Execute.Assertion
.ForConstraint(occurrenceConstraint, actualCount)
.BecauseOf(because, becauseArgs)
.FailWith(
$"Expected {{context:subject}} to have {occurrenceConstraint.Mode} {{0}} child element(s) {{1}}{{reason}}, but found {{2}}.",
occurrenceConstraint.ExpectedCount,
expected.ToString(),
actualCount);
}
return new AndWhichConstraint<XElementAssertions, IEnumerable<XElement>>(this, xElements);
}
/// <summary>
/// Asserts that the <see cref="XElement"/> of the current <see cref="XElement"/> has a direct
/// child element with the specified <paramref name="expected"/> name.
/// </summary>
/// <param name="expected">
/// The name of the expected child element of the current element's <see cref="XElement"/>.
/// </param>
/// <param name="occurrenceConstraint">
/// A constraint specifying the number of times the specified elements should appear.
/// </param>
/// <param name="because">
/// A formatted phrase as is supported by <see cref="string.Format(string,object[])" /> explaining why the assertion
/// is needed. If the phrase does not start with the word <i>because</i>, it is prepended automatically.
/// </param>
/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because" />.
/// </param>
public AndWhichConstraint<XElementAssertions, IEnumerable<XElement>> HaveElement(string expected,
OccurrenceConstraint occurrenceConstraint, string because = "", params object[] becauseArgs)
{
Guard.ThrowIfArgumentIsNull(expected, nameof(expected),
"Cannot assert the element has an element if the expected name is <null>.");
return HaveElement(XNamespace.None + expected, occurrenceConstraint, because, becauseArgs);
}
/// <summary>
/// Returns the type of the subject the assertion applies on.
/// </summary>
protected override string Identifier => "XML element";
}
}