/// <summary> /// Validate behavior of the XDocument copy/clone constructor. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "CreateDocumentCopy")] public void CreateDocumentCopy() { try { new XDocument((XDocument)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } XDeclaration declaration = new XDeclaration("1.0", "utf-8", "yes"); XComment comment = new XComment("This is a document"); XProcessingInstruction instruction = new XProcessingInstruction("doc-target", "doc-data"); XElement element = new XElement("RootElement"); XDocument doc = new XDocument(declaration, comment, instruction, element); XDocument doc2 = new XDocument(doc); IEnumerator e = doc2.Nodes().GetEnumerator(); // First node: declaration Validate.IsEqual(doc.Declaration.ToString(), doc2.Declaration.ToString()); // Next node: comment Validate.IsEqual(e.MoveNext(), true); Validate.Type(e.Current, typeof(XComment)); Validate.IsNotReferenceEqual(e.Current, comment); XComment comment2 = (XComment)e.Current; Validate.IsEqual(comment2.Value, comment.Value); // Next node: processing instruction Validate.IsEqual(e.MoveNext(), true); Validate.Type(e.Current, typeof(XProcessingInstruction)); Validate.IsNotReferenceEqual(e.Current, instruction); XProcessingInstruction instruction2 = (XProcessingInstruction)e.Current; Validate.String(instruction2.Target, instruction.Target); Validate.String(instruction2.Data, instruction.Data); // Next node: element. Validate.IsEqual(e.MoveNext(), true); Validate.Type(e.Current, typeof(XElement)); Validate.IsNotReferenceEqual(e.Current, element); XElement element2 = (XElement)e.Current; Validate.ElementName(element2, element.Name.ToString()); Validate.Count(element2.Nodes(), 0); // Should be end. Validate.IsEqual(e.MoveNext(), false); }
/// <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 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"); }
/// <summary> /// Validates the behavior of the Remove method on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeRemove")] public void AttributeRemove() { XElement e = new XElement("element"); XAttribute a = new XAttribute("attribute", "value"); // Can't remove when no parent. try { a.Remove(); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } e.Add(a); Validate.Count(e.Attributes(), 1); a.Remove(); Validate.Count(e.Attributes(), 0); }