public void OpenXmlElementTraversingMethodsTest() { Paragraph para = new Paragraph(); Run r1 = para.AppendChild(new Run()); Bold b1 = r1.AppendChild(new RunProperties()).AppendChild(new Bold()); Text t1 = r1.AppendChild(new Text()); Run r2 = para.AppendChild(new Run()); r2.AppendChild(new Text()); Run r3 = para.AppendChild(new Run()); Assert.Equal(7, para.Descendants().Count()); Assert.Equal(3, r1.Descendants().Count()); Assert.Single(r2.Descendants()); Assert.Empty(r3.Descendants()); Assert.Same(r1, para.Descendants().First()); Assert.Same(b1, para.Descendants().ElementAt(2)); Assert.Same(t1, para.Descendants().ElementAt(3)); Assert.Same(r2, para.Descendants().ElementAt(4)); Assert.Same(r3, para.Descendants().Last()); // ancestors Assert.Empty(para.Ancestors()); Assert.Empty(para.Ancestors <Body>()); Assert.Single(r1.Ancestors()); Assert.Single(r1.Ancestors <Paragraph>()); Assert.Empty(r1.Ancestors <Body>()); Assert.Single(r3.Ancestors()); Assert.Single(r3.Ancestors <Paragraph>()); Assert.Empty(r3.Ancestors <Body>()); Assert.Equal(2, t1.Ancestors().Count()); Assert.Single(t1.Ancestors <Paragraph>()); Assert.Empty(t1.Ancestors <Body>()); Assert.Equal(3, b1.Ancestors().Count()); Assert.Single(b1.Ancestors <Paragraph>()); Assert.Single(b1.Ancestors <Run>()); Assert.Single(b1.Ancestors <RunProperties>()); Assert.Empty(b1.Ancestors <Body>()); Assert.Same(para, r1.Ancestors().First()); Assert.Same(para, r1.Ancestors <Paragraph>().First()); Assert.Same(r1, t1.Ancestors().First()); Assert.Same(para, t1.Ancestors().Last()); Assert.Same(r1, t1.Ancestors <Run>().First()); Assert.Same(para, t1.Ancestors <Paragraph>().First()); Assert.Same(para, b1.Ancestors().Last()); Assert.Same(r1, b1.Ancestors().ElementAt(1)); // IsBefore() / IsAfter() Assert.False(para.IsBefore(para)); Assert.False(para.IsAfter(para)); Assert.False(r2.IsBefore(r2)); Assert.False(r2.IsAfter(r2)); Assert.True(para.IsBefore(r1)); Assert.False(para.IsAfter(r1)); Assert.True(para.IsBefore(b1)); Assert.False(para.IsAfter(b1)); Assert.False(r1.IsBefore(para)); Assert.True(r1.IsAfter(para)); Assert.False(r1.IsBefore(para)); Assert.True(r1.IsAfter(para)); Assert.True(r1.IsBefore(r2)); Assert.False(r1.IsAfter(r2)); Assert.False(r3.IsBefore(r1)); Assert.True(r3.IsAfter(r1)); Assert.False(b1.IsAfter(r2)); Assert.True(b1.IsBefore(r2)); Assert.True(r1.IsBefore(b1)); Assert.False(r1.IsAfter(b1)); Assert.True(b1.IsAfter(r1)); Assert.False(b1.IsBefore(r1)); Paragraph p2 = new Paragraph(); Run p2r1 = p2.AppendChild(new Run()); Assert.False(p2.IsBefore(para)); Assert.False(p2.IsAfter(para)); Assert.False(p2r1.IsAfter(para)); Assert.False(p2r1.IsBefore(para)); // ElementsBefore / ElementsAfter Assert.False(para.ElementsBefore().Any()); Assert.False(para.ElementsAfter().Any()); Assert.True(r1.ElementsAfter().Any()); Assert.False(r1.ElementsBefore().Any()); Assert.False(r3.ElementsAfter().Any()); Assert.True(r3.ElementsBefore().Any()); Assert.True(r2.ElementsAfter().Any()); Assert.True(r2.ElementsBefore().Any()); Assert.Same(r1, r3.ElementsBefore().First()); Assert.Same(r2, r3.ElementsBefore().Last()); Assert.Same(r2, r1.ElementsAfter().First()); Assert.Same(r3, r1.ElementsAfter().Last()); Assert.False(b1.ElementsBefore().Any()); Assert.False(b1.ElementsAfter().Any()); }