1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System.Collections;
6 using System.Collections.Generic;
7 using System.Linq;
8 using Xunit;
9 
10 namespace System.Xml.Linq.Tests
11 {
12     public static class AxisOrderValidation
13     {
14         [Fact]
NodesAfterSelfBeforeAndAfter()15         public static void NodesAfterSelfBeforeAndAfter()
16         {
17             XText aText = new XText("a"), bText = new XText("b");
18             XElement a = new XElement("A", aText, bText);
19             IEnumerable<XNode> nodes = aText.NodesAfterSelf();
20             Assert.Equal(1, nodes.Count());
21             bText.Remove();
22             Assert.Equal(0, nodes.Count());
23         }
24 
25         [Fact]
NodesBeforeSelfBeforeAndAfter()26         public static void NodesBeforeSelfBeforeAndAfter()
27         {
28             XText aText = new XText("a"), bText = new XText("b");
29             XElement a = new XElement("A", aText, bText);
30             IEnumerable<XNode> nodes = bText.NodesBeforeSelf();
31             Assert.Equal(1, nodes.Count());
32             aText.Remove();
33             Assert.Equal(0, nodes.Count());
34         }
35 
36         [Fact]
AncestorsBeforeAndAfter()37         public static void AncestorsBeforeAndAfter()
38         {
39             XText aText = new XText("a"), bText = new XText("b");
40             XElement a = new XElement("A", aText), b = new XElement("B", bText);
41             a.Add(b);
42             IEnumerable<XElement> nodes = bText.Ancestors();
43             Assert.Equal(2, nodes.Count());
44             bText.Remove();
45             a.Add(bText);
46             Assert.Equal(1, nodes.Count());
47         }
48 
49         [Fact]
AncestorsWithXNameBeforeAndAfter()50         public static void AncestorsWithXNameBeforeAndAfter()
51         {
52             XText aText = new XText("a"), bText = new XText("b");
53             XElement a = new XElement("A", aText), b = new XElement("B", bText);
54             a.Add(b);
55             IEnumerable<XElement> nodes = bText.Ancestors("B");
56             Assert.Equal(1, nodes.Count());
57             bText.Remove(); a.Add(bText);
58             Assert.Equal(0, nodes.Count());
59         }
60 
61         [Fact]
ElementsAfterSelfBeforeAndAfter()62         public static void ElementsAfterSelfBeforeAndAfter()
63         {
64             XText aText = new XText("a"), bText = new XText("b");
65             XElement a = new XElement("A", aText), b = new XElement("B", bText);
66             a.Add(b);
67             IEnumerable<XElement> nodes = aText.ElementsAfterSelf();
68             Assert.Equal(1, nodes.Count());
69             b.Remove();
70             Assert.Equal(0, nodes.Count());
71         }
72 
73         [Fact]
ElementsAfterSelfWithXNameBeforeAndAfter()74         public static void ElementsAfterSelfWithXNameBeforeAndAfter()
75         {
76             XText aText = new XText("a"), bText = new XText("b");
77             XElement a = new XElement("A", aText), b = new XElement("B", bText);
78             a.Add(b);
79             IEnumerable<XElement> nodes = aText.ElementsAfterSelf("B");
80             Assert.Equal(1, nodes.Count());
81             b.ReplaceWith(a);
82             Assert.Equal(0, nodes.Count());
83         }
84 
85         [Fact]
ElementsBeforeSelfBeforeAndAfter()86         public static void ElementsBeforeSelfBeforeAndAfter()
87         {
88             XText aText = new XText("a"), bText = new XText("b");
89             XElement a = new XElement("A", aText), b = new XElement("B", bText);
90             aText.AddBeforeSelf(b);
91             IEnumerable<XElement> nodes = aText.ElementsBeforeSelf();
92             Assert.Equal(1, nodes.Count());
93             b.Remove();
94             Assert.Equal(0, nodes.Count());
95         }
96 
97         [Fact]
ElementsBeforeSelfWithXNameBeforeAndAfter()98         public static void ElementsBeforeSelfWithXNameBeforeAndAfter()
99         {
100             XText aText = new XText("a"), bText = new XText("b");
101             XElement a = new XElement("A", aText), b = new XElement("B", bText);
102             aText.AddBeforeSelf(b);
103             IEnumerable<XElement> nodes = aText.ElementsBeforeSelf("B");
104             Assert.Equal(1, nodes.Count());
105             b.Remove();
106             Assert.Equal(0, nodes.Count());
107         }
108 
109         [Fact]
NodesOnXDocBeforeAndAfter()110         public static void NodesOnXDocBeforeAndAfter()
111         {
112             XText aText = new XText("a"), bText = new XText("b");
113             XElement a = new XElement("A", aText, bText);
114             XDocument xDoc = new XDocument(a);
115             IEnumerable<XNode> nodes = xDoc.Nodes();
116             Assert.Equal(1, nodes.Count());
117             a.Remove();
118             Assert.Equal(0, nodes.Count());
119         }
120 
121         [Fact]
DescendantNodesOnXDocBeforeAndAfter()122         public static void DescendantNodesOnXDocBeforeAndAfter()
123         {
124             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
125             a.Add(b);
126             XDocument xDoc = new XDocument(a);
127             IEnumerable<XNode> nodes = xDoc.DescendantNodes();
128             Assert.Equal(4, nodes.Count());
129             a.Remove();
130             Assert.Equal(0, nodes.Count());
131         }
132 
133         [Fact]
ElementsOnXDocBeforeAndAfter()134         public static void ElementsOnXDocBeforeAndAfter()
135         {
136             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
137             a.Add(b);
138             XDocument xDoc = new XDocument(a);
139             IEnumerable<XElement> nodes = xDoc.Elements();
140             Assert.Equal(1, nodes.Count());
141             a.Remove();
142             Assert.Equal(0, nodes.Count());
143         }
144 
145         [Fact]
ElementsWithXNameOnXDocBeforeAndAfter()146         public static void ElementsWithXNameOnXDocBeforeAndAfter()
147         {
148             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
149             a.Add(b);
150             XDocument xDoc = new XDocument(a);
151             IEnumerable<XElement> nodes = xDoc.Elements("A");
152             Assert.Equal(1, nodes.Count());
153             a.Remove();
154             Assert.Equal(0, nodes.Count());
155         }
156 
157         [Fact]
DescendantsOnXDocBeforeAndAfter()158         public static void DescendantsOnXDocBeforeAndAfter()
159         {
160             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
161             a.Add(b);
162             XDocument xDoc = new XDocument(a);
163             IEnumerable<XElement> nodes = xDoc.Descendants("B");
164             Assert.Equal(1, nodes.Count());
165             b.Remove();
166             Assert.Equal(0, nodes.Count());
167         }
168 
169         [Fact]
DescendantsWithXNameOnXDocBeforeAndAfter()170         public static void DescendantsWithXNameOnXDocBeforeAndAfter()
171         {
172             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
173             b.Add(b, b); a.Add(b);
174             XDocument xDoc = new XDocument(a);
175             IEnumerable<XElement> nodes = xDoc.Descendants("B");
176             Assert.Equal(4, nodes.Count());
177             b.Remove();
178             Assert.Equal(0, nodes.Count());
179         }
180 
181         [Fact]
NodesOnXElementBeforeAndAfter()182         public static void NodesOnXElementBeforeAndAfter()
183         {
184             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
185             a.Add(b);
186             IEnumerable<XNode> nodes = a.Nodes();
187             Assert.Equal(2, nodes.Count());
188             b.Remove();
189             Assert.Equal(1, nodes.Count());
190         }
191 
192         [Fact]
DescendantNodesOnXElementBeforeAndAfter()193         public static void DescendantNodesOnXElementBeforeAndAfter()
194         {
195             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
196             a.Add(b);
197             IEnumerable<XNode> nodes = a.DescendantNodes();
198             Assert.Equal(3, nodes.Count());
199             a.Add("New Text Node");
200             Assert.Equal(4, nodes.Count());
201         }
202 
203         [Fact]
ElementsOnXElementBeforeAndAfter()204         public static void ElementsOnXElementBeforeAndAfter()
205         {
206             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
207             IEnumerable<XElement> nodes = a.Elements();
208             Assert.Equal(0, nodes.Count());
209             a.Add(b, b, b, b);
210             Assert.Equal(4, nodes.Count());
211         }
212 
213         [Fact]
ElementsWithXNameOnXElementBeforeAndAfter()214         public static void ElementsWithXNameOnXElementBeforeAndAfter()
215         {
216             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
217             IEnumerable<XElement> nodes = a.Elements("B");
218             Assert.Equal(0, nodes.Count());
219             a.Add(b, b, b, b);
220             Assert.Equal(4, nodes.Count());
221         }
222 
223         [Fact]
DescendantsOnXElementBeforeAndAfter()224         public static void DescendantsOnXElementBeforeAndAfter()
225         {
226             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
227             a.Add(b);
228             IEnumerable<XElement> nodes = a.Descendants();
229             Assert.Equal(1, nodes.Count());
230             b.Remove();
231             Assert.Equal(0, nodes.Count());
232         }
233 
234         [Fact]
DescendantsWithXNameOnXElementBeforeAndAfter()235         public static void DescendantsWithXNameOnXElementBeforeAndAfter()
236         {
237             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
238             a.Add(b);
239             IEnumerable<XElement> nodes = a.Descendants("B");
240             Assert.Equal(1, nodes.Count());
241             b.Remove();
242             Assert.Equal(0, nodes.Count());
243         }
244 
245         [Fact]
DescendantNodesAndSelfBeforeAndAfter()246         public static void DescendantNodesAndSelfBeforeAndAfter()
247         {
248             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
249             a.Add(b);
250             IEnumerable<XNode> nodes = a.DescendantNodesAndSelf();
251             Assert.Equal(4, nodes.Count());
252             a.Add("New Text Node");
253             Assert.Equal(5, nodes.Count());
254         }
255 
256         [Fact]
DescendantsAndSelfBeforeAndAfter()257         public static void DescendantsAndSelfBeforeAndAfter()
258         {
259             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
260             a.Add(b);
261             IEnumerable<XElement> nodes = a.DescendantsAndSelf();
262             Assert.Equal(2, nodes.Count());
263             b.Add(a);
264             Assert.Equal(4, nodes.Count());
265         }
266 
267         [Fact]
DescendantsAndSelfWithXNameBeforeAndAfter()268         public static void DescendantsAndSelfWithXNameBeforeAndAfter()
269         {
270             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
271             a.Add(b);
272             IEnumerable<XElement> nodes = a.DescendantsAndSelf("A");
273             Assert.Equal(1, nodes.Count());
274             b.ReplaceWith(a);
275             Assert.Equal(2, nodes.Count());
276         }
277 
278         [Fact]
AncestorsAndSelfBeforeAndAfter()279         public static void AncestorsAndSelfBeforeAndAfter()
280         {
281             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
282             a.Add(b);
283             IEnumerable<XElement> nodes = b.AncestorsAndSelf();
284             Assert.Equal(2, nodes.Count());
285             XElement c = new XElement("C", "c", a);
286             Assert.Equal(3, nodes.Count());
287         }
288 
289         [Fact]
AncestorsAndSelfWithXNameBeforeAndAfter()290         public static void AncestorsAndSelfWithXNameBeforeAndAfter()
291         {
292             XElement a = new XElement("A", "a"), b = new XElement("B", "b");
293             a.Add(b);
294             IEnumerable<XElement> nodes = b.AncestorsAndSelf("A");
295             Assert.Equal(1, nodes.Count());
296             XElement c = new XElement("A", "a", a);
297             Assert.Equal(2, nodes.Count());
298         }
299 
300         [Fact]
AttributesBeforeAndAfter()301         public static void AttributesBeforeAndAfter()
302         {
303             XElement a = new XElement("A", "a");
304             IEnumerable<XAttribute> nodes = a.Attributes();
305             Assert.Equal(0, nodes.Count());
306             a.Add(new XAttribute("name", "a"), new XAttribute("type", "alphabet"));
307             Assert.Equal(2, nodes.Count());
308         }
309 
310         [Fact]
AttributeWithXNameBeforeAndAfter()311         public static void AttributeWithXNameBeforeAndAfter()
312         {
313             XElement a = new XElement("A", "a");
314             IEnumerable<XAttribute> nodes = a.Attributes("name");
315             Assert.Equal(0, nodes.Count());
316             a.Add(new XAttribute("name", "a"), new XAttribute("type", "alphabet"));
317             Assert.Equal(1, nodes.Count());
318         }
319 
320         [Fact]
IEnumerableInDocumentOrderVariationOne()321         public static void IEnumerableInDocumentOrderVariationOne()
322         {
323             XDocument xDoc = TestData.GetDocumentWithContacts();
324             IEnumerable<XNode> xNodes = xDoc.Root.DescendantNodesAndSelf().Reverse();
325             Assert.True(xNodes.InDocumentOrder().EqualsAll(xDoc.Root.DescendantNodesAndSelf(), XNode.DeepEquals));
326         }
327 
328         [Fact]
IEnumerableInDocumentOrderVariationTwo()329         public static void IEnumerableInDocumentOrderVariationTwo()
330         {
331             XDocument xDoc = TestData.GetDocumentWithContacts();
332             IEnumerable<XElement> xElement = xDoc.Root.DescendantsAndSelf().Reverse();
333             Assert.True(xElement.InDocumentOrder().EqualsAll(xDoc.Root.DescendantsAndSelf(), XNode.DeepEquals));
334         }
335 
336         [Fact]
ReorderToDocumentOrder()337         public static void ReorderToDocumentOrder()
338         {
339             XDocument xDoc = TestData.GetDocumentWithContacts();
340             Random rnd = new Random();
341             var randomOrderedElements = xDoc.Root.DescendantNodesAndSelf().OrderBy(n => rnd.Next(0, int.MaxValue));
342             using (var en = randomOrderedElements.InDocumentOrder().GetEnumerator())
343             {
344                 en.MoveNext();
345                 var nodeFirst = en.Current;
346                 while (en.MoveNext())
347                 {
348                     var nodeSecond = en.Current;
349                     Assert.True(XNode.DocumentOrderComparer.Compare(nodeFirst, nodeSecond) < 0);
350                     nodeFirst = nodeSecond;
351                 }
352             }
353         }
354     }
355 }
356