/// <summary> /// Tests Remove on Node. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeRemove")] public void NodeRemove() { XElement parent = new XElement("parent"); XComment child1 = new XComment("child1"); XText child2 = new XText("child2"); XElement child3 = new XElement("child3"); parent.Add(child1, child2, child3); // Sanity check Validate.EnumeratorDeepEquals(parent.Nodes(), new XNode[] { child1, child2, child3 }); // Remove the text. child1.NextNode.Remove(); Validate.EnumeratorDeepEquals(parent.Nodes(), new XNode[] { child1, child3 }); // Remove the XComment. child1.Remove(); Validate.EnumeratorDeepEquals(parent.Nodes(), new XNode[] { child3 }); // Remove the XElement. child3.Remove(); Validate.EnumeratorDeepEquals(parent.Nodes(), new XNode[] { }); }
/// <summary> /// Validate enumeration of container elements. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "ContainerElements")] public void ContainerElements() { XElement level1_1 = new XElement("level1"); XElement level1_2 = new XElement("level1", new XElement("level1"), new XElement("level2")); XElement element = new XElement("level0", new XComment("my comment"), level1_1, level1_2 ); XElement empty = new XElement("empty"); // Can't find anything in an empty element Validate.IsNull(empty.Element("foo")); // Can't find element with no name or bogus name. Validate.IsNull(element.Element(null)); Validate.IsNull(element.Element("foo")); // Check element by name Validate.IsEqual(element.Element("level1"), level1_1); // Check element sequence -- should not include nested elements. Validate.EnumeratorDeepEquals(element.Elements(), new XElement[] { level1_1, level1_2 }); // Check element sequence by name. Validate.EnumeratorDeepEquals(element.Elements(null), new XElement[0]); Validate.EnumeratorDeepEquals(element.Elements("level1"), new XElement[] { level1_1, level1_2 }); }
/// <summary> /// Tests the Add methods on Container. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "ContainerAdd")] public void ContainerAdd() { XElement element = new XElement("foo"); // Adding null does nothing. element.Add(null); Validate.Count(element.Nodes(), 0); // Add node, attrbute, string, some other value, and an IEnumerable. XComment comment = new XComment("this is a comment"); XComment comment2 = new XComment("this is a comment 2"); XComment comment3 = new XComment("this is a comment 3"); XAttribute attribute = new XAttribute("att", "att-value"); string str = "this is a string"; int other = 7; element.Add(comment); element.Add(attribute); element.Add(str); element.Add(other); element.Add(new XComment[] { comment2, comment3 }); Validate.EnumeratorDeepEquals( element.Nodes(), new XNode[] { comment, new XText(str + other), comment2, comment3 }); Validate.EnumeratorAttributes(element.Attributes(), new XAttribute[] { attribute }); element.RemoveAll(); Validate.Count(element.Nodes(), 0); // Now test params overload. element.Add(comment, attribute, str, other); Validate.EnumeratorDeepEquals( element.Nodes(), new XNode[] { comment, new XText(str + other) }); Validate.EnumeratorAttributes(element.Attributes(), new XAttribute[] { attribute }); // Not allowed to add a document as a child. XDocument document = new XDocument(); try { element.Add(document); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } }
/// <summary> /// Tests the AddFirst methods on Container. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "ContainerAddFirst")] public void ContainerAddFirst() { XElement element = new XElement("foo"); // Adding null does nothing. element.AddFirst(null); Validate.Count(element.Nodes(), 0); // Add a sentinal value. XText text = new XText("abcd"); element.AddFirst(text); // Add node and string. XComment comment = new XComment("this is a comment"); string str = "this is a string"; element.AddFirst(comment); element.AddFirst(str); Validate.EnumeratorDeepEquals(element.Nodes(), new XNode[] { new XText(str), comment, text }); element.RemoveAll(); Validate.Count(element.Nodes(), 0); // Now test params overload. element.AddFirst(text); element.AddFirst(comment, str); Validate.EnumeratorDeepEquals(element.Nodes(), new XNode[] { comment, new XText(str), text }); // Can't use to add attributes. XAttribute a = new XAttribute("foo", "bar"); try { element.AddFirst(a); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } }
/// <summary> /// Validate enumeration of container descendents. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "ContainerDescendents")] public void ContainerDescendents() { XComment comment = new XComment("comment"); XElement level3 = new XElement("Level3"); XElement level2 = new XElement("Level2", level3); XElement level1 = new XElement("Level1", level2, comment); XElement level0 = new XElement("Level1", level1); Validate.EnumeratorDeepEquals( level1.Descendants(), new XElement[] { level2, level3 }); Validate.EnumeratorDeepEquals( level0.DescendantNodes(), new XNode[] { level1, level2, level3, comment }); Validate.EnumeratorDeepEquals(level0.Descendants(null), new XElement[0]); Validate.EnumeratorDeepEquals(level0.Descendants("Level1"), new XElement[] { level1 }); }
/// <summary> /// Tests AddBeforeSelf on Node. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeAddBeforeSelf")] public void NodeAddBeforeSelf() { XElement parent = new XElement("parent"); XElement child = new XElement("child"); parent.Add(child); XElement sibling1 = new XElement("sibling1"); XComment sibling2 = new XComment("sibling2"); XText sibling3 = new XText("sibling3"); child.AddBeforeSelf(sibling1); Validate.EnumeratorDeepEquals(parent.Nodes(), new XNode[] { sibling1, child }); child.AddBeforeSelf(sibling2, sibling3); Validate.EnumeratorDeepEquals( parent.Nodes(), new XNode[] { sibling1, sibling2, sibling3, child }); }
/// <summary> /// Tests removing text content from a container. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "ContainerRemoveTextual")] public void ContainerRemoveTextual() { XElement e1 = XElement.Parse("<a>abcd</a>"); XElement e2 = new XElement(e1); XElement eb = new XElement("b"); e2.Add(eb); eb.Remove(); Validate.IsEqual(XNode.EqualityComparer.Equals(e1, e2), true); // Removing non-text between some text should NOT collapse the text. e1.Add(eb); e1.Add("efgh"); Validate.EnumeratorDeepEquals(e1.Nodes(), new XNode[] { new XText("abcd"), eb, new XText("efgh") }); eb.Remove(); Validate.EnumeratorDeepEquals(e1.Nodes(), new XNode[] { new XText("abcd"), new XText("efgh") }); }
/// <summary> /// Validate ReplaceNodes on container. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "ContainerReplaceNodes")] public void ContainerReplaceNodes() { XElement element = new XElement("foo", new XAttribute("att", "bar"), "abc", new XElement("nested", new XText("abcd"))); // Replace with a node, attribute, string, some other value, and an IEnumerable. // ReplaceNodes does not remove attributes. XComment comment = new XComment("this is a comment"); XComment comment2 = new XComment("this is a comment 2"); XComment comment3 = new XComment("this is a comment 3"); XAttribute attribute = new XAttribute("att2", "att-value"); string str = "this is a string"; TimeSpan other1 = new TimeSpan(1, 2, 3); element.ReplaceNodes( comment, attribute, str, other1, new XComment[] { comment2, comment3 }); Validate.EnumeratorDeepEquals( element.Nodes(), new XNode[] { comment, new XText(str + XmlConvert.ToString(other1)), comment2, comment3 }); Validate.Count(element.Attributes(), 2); Validate.AttributeNameAndValue(element.Attribute("att"), "att", "bar"); Validate.AttributeNameAndValue(element.Attribute("att2"), "att2", "att-value"); }