/// <summary> /// Tests the Parent property on Node. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeParent")] public void NodeParent() { // Only elements are returned as parents from the Parent property. // Documents are not returned. XDocument document = new XDocument(); XNode[] nodes = new XNode[] { new XComment("comment"), new XElement("element"), new XProcessingInstruction("target", "data"), new XDocumentType("name", "publicid", "systemid", "internalsubset") }; foreach (XNode node in nodes) { Validate.IsNull(node.Parent); document.Add(node); Validate.IsReferenceEqual(document, node.Document); // Parent element is null. Validate.IsNull(node.Parent); document.RemoveNodes(); } // Now test the cases where an element is the parent. nodes = new XNode[] { new XComment("abcd"), new XElement("nested"), new XProcessingInstruction("target2", "data2"), new XText("text") }; XElement root = new XElement("root"); document.ReplaceNodes(root); foreach (XNode node in nodes) { Validate.IsNull(node.Parent); root.AddFirst(node); Validate.IsReferenceEqual(node.Parent, root); root.RemoveNodes(); Validate.IsNull(node.Parent); } }
/// <summary> /// Validate behavior of the XDocument XmlDeclaration property. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "DocumentXmlDeclaration")] public void DocumentXmlDeclaration() { XDocument doc = new XDocument(); Validate.IsNull(doc.Declaration); XDeclaration dec = new XDeclaration("1.0", "utf-16", "yes"); XDocument doc2 = new XDocument(dec); XDeclaration dec2 = doc2.Declaration; Validate.IsReferenceEqual(dec2, dec); doc2.RemoveNodes(); Validate.IsNotNull(doc2.Declaration); }
/// <summary> /// Tests the operators on XName. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "NameOperators")] public void NameOperators() { // Implicit conversion from string. XName name = (XName)(string)null; Validate.IsNull(name); name = (XName)"foo"; Validate.String(name.Namespace.NamespaceName, ""); Validate.String(name.LocalName, "foo"); name = (XName)"{bar}foo"; Validate.String(name.Namespace.NamespaceName, "bar"); Validate.String(name.LocalName, "foo"); // Conversion to XmlQualifiedName XmlQualifiedName qname = GetQName(name); Validate.String(qname.Namespace, "bar"); Validate.String(qname.Name, "foo"); // Equality, which should be based on reference equality. XName ns1 = (XName)"foo"; XName ns2 = (XName)"foo"; XName ns3 = (XName)"bar"; XName ns4 = null; Validate.IsReferenceEqual(ns1, ns2); Validate.IsNotReferenceEqual(ns1, ns3); Validate.IsNotReferenceEqual(ns2, ns3); bool b1 = ns1 == ns2; // equal bool b2 = ns1 == ns3; // not equal bool b3 = ns1 == ns4; // not equal Validate.IsEqual(b1, true); Validate.IsEqual(b2, false); Validate.IsEqual(b3, false); b1 = ns1 != ns2; // false b2 = ns1 != ns3; // true b3 = ns1 != ns4; // true Validate.IsEqual(b1, false); Validate.IsEqual(b2, true); Validate.IsEqual(b3, true); }
/// <summary> /// Validate behavior of the XDocument Root property. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "DocumentRoot")] public void DocumentRoot() { XDocument doc = new XDocument(); Validate.IsNull(doc.Root); XElement e = new XElement("element"); doc.Add(e); XElement e2 = doc.Root; Validate.IsReferenceEqual(e2, e); doc.RemoveNodes(); doc.Add(new XComment("comment")); Validate.IsNull(doc.Root); }
/// <summary> /// Tests the Document property on Node. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeDocument")] public void NodeDocument() { XDocument document = new XDocument(); XNode[] topLevelNodes = new XNode[] { new XComment("comment"), new XElement("element"), new XProcessingInstruction("target", "data"), }; XNode[] nestedNodes = new XNode[] { new XText("abcd"), new XElement("nested"), new XProcessingInstruction("target2", "data2") }; // Test top-level cases. foreach (XNode node in topLevelNodes) { Validate.IsNull(node.Document); document.Add(node); Validate.IsReferenceEqual(document, node.Document); document.RemoveNodes(); } // Test nested cases. XElement root = new XElement("root"); document.Add(root); foreach (XNode node in nestedNodes) { Validate.IsNull(node.Document); root.Add(node); Validate.IsReferenceEqual(document, root.Document); root.RemoveNodes(); } }
/// <summary> /// Tests the operators on XNamespace. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "NamespaceOperators")] public void NamespaceOperators() { // Implicit conversion from string. XNamespace ns = (XNamespace)(string)null; Validate.IsNull(ns); ns = (XNamespace)"foo"; Validate.String(ns.NamespaceName, "foo"); // Operator + XName name; try { name = (XNamespace)null + "localname"; Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { name = ns + (string)null; Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } name = ns + "localname"; Validate.String(name.LocalName, "localname"); Validate.String(name.Namespace.NamespaceName, "foo"); // Equality, which should be based on reference equality. XNamespace ns1 = (XNamespace)"foo"; XNamespace ns2 = (XNamespace)"foo"; XNamespace ns3 = (XNamespace)"bar"; XNamespace ns4 = null; Validate.IsReferenceEqual(ns1, ns2); Validate.IsNotReferenceEqual(ns1, ns3); Validate.IsNotReferenceEqual(ns2, ns3); bool b1 = ns1 == ns2; // equal bool b2 = ns1 == ns3; // not equal bool b3 = ns1 == ns4; // not equal Validate.IsEqual(b1, true); Validate.IsEqual(b2, false); Validate.IsEqual(b3, false); b1 = ns1 != ns2; // false b2 = ns1 != ns3; // true b3 = ns1 != ns4; // true Validate.IsEqual(b1, false); Validate.IsEqual(b2, true); Validate.IsEqual(b3, true); }
/// <summary> /// Validate the behavior of annotations on Container. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "ContainerAnnotations")] public void ContainerAnnotations() { XElement element1 = new XElement("e1"); XElement element2 = new XElement("e2"); // Check argument null exception on add. try { element1.AddAnnotation(null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Before adding anything, should not be able to get any annotations. Validate.IsNull(element1.Annotation(typeof(object))); element1.RemoveAnnotations(typeof(object)); Validate.IsNull(element1.Annotation(typeof(object))); // First annotation: 2 cases, object[] and other. object obj1 = "hello"; element1.AddAnnotation(obj1); Validate.IsNull(element1.Annotation(typeof(byte))); Validate.IsReferenceEqual(element1.Annotation(typeof(string)), obj1); element1.RemoveAnnotations(typeof(string)); Validate.IsNull(element1.Annotation(typeof(string))); object[] obj2 = new object[] { 10, 20, 30 }; element2.AddAnnotation(obj2); Validate.IsReferenceEqual(element2.Annotation(typeof(object[])), obj2); Validate.Enumerator <object>((object[])element2.Annotation(typeof(object[])), new object[] { 10, 20, 30 }); element2.RemoveAnnotations(typeof(object[])); Validate.IsNull(element2.Annotation(typeof(object[]))); // Single annotation; add a second one. Check that duplicates are allowed. object obj3 = 10; element1.AddAnnotation(obj3); Validate.IsReferenceEqual(element1.Annotation(typeof(int)), obj3); element1.AddAnnotation(1000); element1.RemoveAnnotations(typeof(int[])); Validate.IsNull(element1.Annotation(typeof(object[]))); object obj4 = "world"; element1.AddAnnotation(obj4); Validate.IsReferenceEqual(element1.Annotation(typeof(int)), obj3); Validate.IsReferenceEqual(element1.Annotation(typeof(string)), obj4); // Multiple annotations already. Add one on the end. object obj5 = 20L; element1.AddAnnotation(obj5); Validate.IsReferenceEqual(element1.Annotation(typeof(int)), obj3); Validate.IsReferenceEqual(element1.Annotation(typeof(string)), obj4); Validate.IsReferenceEqual(element1.Annotation(typeof(long)), obj5); // Remove one from the middle and then add, which should use the // freed slot. element1.RemoveAnnotations(typeof(string)); Validate.IsNull(element1.Annotation(typeof(string))); object obj6 = 30m; element1.AddAnnotation(obj6); Validate.IsReferenceEqual(element1.Annotation(typeof(int)), obj3); Validate.IsReferenceEqual(element1.Annotation(typeof(long)), obj5); Validate.IsReferenceEqual(element1.Annotation(typeof(decimal)), obj6); // Ensure that duplicates are allowed. element1.AddAnnotation(40m); Validate.IsNull(element1.Annotation(typeof(sbyte))); // A couple of additional remove cases. element2.AddAnnotation(obj2); element2.AddAnnotation(obj3); element2.AddAnnotation(obj5); element2.AddAnnotation(obj6); element2.RemoveAnnotations(typeof(float)); Validate.IsNull(element2.Annotation(typeof(float))); }