/// <summary> /// Tests the ProcessingInstruction constructor that takes a value. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "CreateProcessingInstructionSimple")] public void CreateProcessingInstructionSimple() { try { new XProcessingInstruction(null, "abcd"); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { new XProcessingInstruction("abcd", null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } XProcessingInstruction c = new XProcessingInstruction("foo", "bar"); Validate.IsEqual(c.Target, "foo"); Validate.IsEqual(c.Data, "bar"); Validate.IsNull(c.Parent); }
/// <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> /// Validate behavior of the default XDocument constructor. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "CreateEmptyDocument")] public void CreateEmptyDocument() { XDocument doc = new XDocument(); Validate.IsNull(doc.Parent); Validate.IsNull(doc.Root); Validate.IsNull(doc.Declaration); Validate.Enumerator(doc.Nodes(), new XNode[0]); }
/// <summary> /// Validates the explicit conversion operators on XAttribute /// for nullable value types. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToNullables")] public void AttributeExplicitToNullables() { string guid = "cd8d69ed-fef9-4283-aaf4-216463e4496f"; bool? b = (bool?)new XAttribute("x", true); int? i = (int?)new XAttribute("x", 5); uint? u = (uint?)new XAttribute("x", 5); long? l = (long?)new XAttribute("x", 5); ulong? ul = (ulong?)new XAttribute("x", 5); float? f = (float?)new XAttribute("x", 5); double? n = (double?)new XAttribute("x", 5); decimal? d = (decimal?)new XAttribute("x", 5); DateTime?dt = (DateTime?)new XAttribute("x", "1968-01-07"); TimeSpan?ts = (TimeSpan?)new XAttribute("x", "PT1H2M3S"); Guid? g = (Guid?)new XAttribute("x", guid); Validate.IsEqual(b.Value, true); Validate.IsEqual(i.Value, 5); Validate.IsEqual(u.Value, 5u); Validate.IsEqual(l.Value, 5L); Validate.IsEqual(ul.Value, 5uL); Validate.IsEqual(f.Value, 5.0f); Validate.IsEqual(n.Value, 5.0); Validate.IsEqual(d.Value, 5.0m); Validate.IsEqual(dt.Value, new DateTime(1968, 1, 7)); Validate.IsEqual(ts.Value, new TimeSpan(1, 2, 3)); Validate.IsEqual(g.Value, new Guid(guid)); b = (bool?)((XAttribute)null); i = (int?)((XAttribute)null); u = (uint?)((XAttribute)null); l = (long?)((XAttribute)null); ul = (ulong?)((XAttribute)null); f = (float?)((XAttribute)null); n = (double?)((XAttribute)null); d = (decimal?)((XAttribute)null); dt = (DateTime?)((XAttribute)null); ts = (TimeSpan?)((XAttribute)null); g = (Guid?)((XAttribute)null); Validate.IsNull(b); Validate.IsNull(i); Validate.IsNull(u); Validate.IsNull(l); Validate.IsNull(ul); Validate.IsNull(f); Validate.IsNull(n); Validate.IsNull(d); Validate.IsNull(dt); Validate.IsNull(ts); Validate.IsNull(g); }
/// <summary> /// Validates the explicit string conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToString")] public void AttributeExplicitToString() { XAttribute e2 = new XAttribute("x", string.Empty); XAttribute e3 = new XAttribute("x", 10.0); string s0 = (string)((XAttribute)null); string s2 = (string)e2; string s3 = (string)e3; Validate.IsNull(s0); Validate.String(s2, string.Empty); Validate.String(s3, "10"); }
/// <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> /// Tests Parent on XAttribute. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "AttributeParent")] public void AttributeParent() { XAttribute a = new XAttribute("att-name", "value"); XElement e = new XElement("elem-name"); Validate.IsNull(a.Parent); e.Add(a); Validate.IsEqual(a.Parent, e); e.RemoveAttributes(); Validate.IsNull(a.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> /// Tests the XCData constructor that takes a value. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "CreateTextSimple")] public void CreateTextSimple() { try { new XCData((string)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } XCData c = new XCData("foo"); Validate.IsEqual(c.Value, "foo"); Validate.IsNull(c.Parent); }
/// <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))); }