/// <summary> /// Tests the WriteTo method on XTest. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "TextWriteTo")] public void TextWriteTo() { XCData c = new XCData("abcd"); // Null writer not allowed. try { c.WriteTo(null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test. StringBuilder stringBuilder = new StringBuilder(); XmlWriter xmlWriter = XmlWriter.Create(stringBuilder); xmlWriter.WriteStartElement("x"); c.WriteTo(xmlWriter); xmlWriter.WriteEndElement(); xmlWriter.Flush(); Validate.IsEqual( stringBuilder.ToString(), "<?xml version=\"1.0\" encoding=\"utf-16\"?><x><![CDATA[abcd]]></x>"); }
/// <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); }
public void AttributeConstructor() { string value = "bar"; // Name/value constructor. try { XAttribute a = new XAttribute(null, value); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { XAttribute a = new XAttribute("foo", null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Codepaths for special-casing xmlns namespace XName name = XName.Get("xmlns", string.Empty); XAttribute att1 = new XAttribute(name, value); Validate.AttributeNameAndValue(att1, "xmlns", value); name = XName.Get("xmlns", "namespacename"); att1 = new XAttribute(name, value); Validate.AttributeNameAndValue(att1, "{namespacename}xmlns", value); name = XName.Get("foo", string.Empty); att1 = new XAttribute(name, value); Validate.AttributeNameAndValue(att1, "foo", value); name = XName.Get("foo", "namespacename"); att1 = new XAttribute(name, value); Validate.AttributeNameAndValue(att1, "{namespacename}foo", value); // Copy constructor. try { XAttribute a = new XAttribute((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } XAttribute att2 = new XAttribute(att1); Validate.AttributeNameAndValue(att2, att1.Name.ToString(), att1.Value); }
/// <summary> /// Tests the WriteTo method on XComment. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "ProcessingInstructionWriteTo")] public void ProcessingInstructionWriteTo() { XProcessingInstruction c = new XProcessingInstruction("target", "data"); // Null writer not allowed. try { c.WriteTo(null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test. StringBuilder stringBuilder = new StringBuilder(); XmlWriter xmlWriter = XmlWriter.Create(stringBuilder); xmlWriter.WriteStartElement("x"); c.WriteTo(xmlWriter); xmlWriter.WriteEndElement(); xmlWriter.Flush(); Validate.IsEqual( stringBuilder.ToString(), "<?xml version=\"1.0\" encoding=\"utf-16\"?><x><?target data?></x>"); }
/// <summary> /// Test loading an element from an XmlReader. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "ElementLoadFromXmlReader")] public void ElementLoadFromXmlReader() { // Null reader not allowed. try { XElement.Load((XmlReader)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Extra stuff in xml after the element is not allowed StringReader reader = new StringReader("<abc><def/></abc>"); XmlReader xmlreader = XmlReader.Create(reader); xmlreader.Read(); xmlreader.Read(); // position on <def> try { XElement.Load(xmlreader); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } reader.Dispose(); xmlreader.Dispose(); }
/// <summary> /// Tests the ReadFrom static method on Node. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeReadFrom")] public void NodeReadFrom() { // Null reader not allowed. try { XNode.ReadFrom(null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Valid cases: cdata, comment, element string[] rawXml = new string[] { "text", "<![CDATA[abcd]]>", "<!-- comment -->", "<y>y</y>" }; Type[] types = new Type[] { typeof(XText), typeof(XCData), typeof(XComment), typeof(XElement) }; int count = rawXml.Length; for (int i = 0; i < count; i++) { using (StringReader stringReader = new StringReader("<x>" + rawXml[i] + "</x>")) { using (XmlReader reader = XmlReader.Create(stringReader)) { reader.Read(); // skip to <x> reader.Read(); // skip over <x> to the meat XNode node = XNode.ReadFrom(reader); // Ensure that the right kind of node got created. Validate.Type(node, types[i]); // Ensure that the value is right. Validate.IsEqual(rawXml[i], node.ToString(SaveOptions.DisableFormatting)); } } } // Also test a case that is not allowed. using (StringReader stringReader = new StringReader("<x y='abcd'/>")) { using (XmlReader reader = XmlReader.Create(stringReader)) { reader.Read(); reader.MoveToFirstAttribute(); try { XNode node = XNode.ReadFrom(reader); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } } } }
/// <summary> /// Tests the AddAfterSelf/AddBeforeSelf/Remove method on Node, /// when there's no parent. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeNoParentAddRemove")] public void NodeNoParentAddRemove() { // Not allowed if parent is null. int i = 0; while (true) { XNode node = null; switch (i++) { case 0: node = new XElement("x"); break; case 1: node = new XComment("c"); break; case 2: node = new XText("abc"); break; case 3: node = new XProcessingInstruction("target", "data"); break; default: i = -1; break; } if (i < 0) { break; } try { node.AddBeforeSelf("foo"); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } try { node.AddAfterSelf("foo"); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } try { node.Remove(); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } } }
/// <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 trying to use an invalid name with XNamespace.Get. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "NamespaceGetNull")] public void NamespaceGetNull() { try { XNamespace.Get(null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } }
/// <summary> /// Validates the explicit int32 conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToInt32")] public void AttributeExplicitToInt32() { // Calling explicit operator with null should result in exception. try { int i = (int)((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test various values. XAttribute e1 = new XAttribute("x", string.Empty); XAttribute e2 = new XAttribute("x", "bogus"); XAttribute e3 = new XAttribute("x", "2147483648"); XAttribute e4 = new XAttribute("x", "5"); try { int i1 = (int)e1; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { int i2 = (int)e2; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { int i3 = (int)e3; Validate.ExpectedThrow(typeof(OverflowException)); } catch (Exception ex) { Validate.Catch(ex, typeof(OverflowException)); } Validate.IsEqual((int)e4, 5); }
/// <summary> /// Validates the explicit decimal conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToDecimal")] public void AttributeExplicitToDecimal() { // Calling explicit operator with null should result in exception. try { decimal d = (decimal)((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test various values. XAttribute e1 = new XAttribute("x", string.Empty); XAttribute e2 = new XAttribute("x", "bogus"); XAttribute e3 = new XAttribute("x", "111111111111111111111111111111111111111111111111"); XAttribute e4 = new XAttribute("x", "5.0"); try { decimal d1 = (decimal)e1; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { decimal d2 = (decimal)e2; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { decimal d3 = (decimal)e3; Validate.ExpectedThrow(typeof(OverflowException)); } catch (Exception ex) { Validate.Catch(ex, typeof(OverflowException)); } Validate.IsEqual((decimal)e4, 5m); }
/// <summary> /// Validates the explicit uint64 conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToUInt64")] public void AttributeExplicitToUInt64() { // Calling explicit operator with null should result in exception. try { ulong i = (ulong)((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test various values. XAttribute e1 = new XAttribute("x", string.Empty); XAttribute e2 = new XAttribute("x", "bogus"); XAttribute e3 = new XAttribute("x", "18446744073709551616"); XAttribute e4 = new XAttribute("x", "5"); try { ulong i1 = (ulong)e1; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { ulong i2 = (ulong)e2; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { ulong i3 = (ulong)e3; Validate.ExpectedThrow(typeof(OverflowException)); } catch (Exception ex) { Validate.Catch(ex, typeof(OverflowException)); } Validate.IsEqual((ulong)e4, 5UL); }
/// <summary> /// Tests the AddAttributes method on Container. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "ContainerAddAttributes")] public void ContainerAddAttributes() { // Not allowed to add attributes in the general case. // The only general case of a container is a document. XDocument document = new XDocument(); try { document.Add(new XAttribute("foo", "bar")); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } // Can add to elements, but no duplicates allowed. XElement e = new XElement("element"); XAttribute a1 = new XAttribute("foo", "bar1"); XAttribute a2 = new XAttribute("foo", "bar2"); e.Add(a1); try { e.Add(a2); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } // Can add the same attribute to different parent elements; // it gets copied. XElement e2 = new XElement("element2"); e2.Add(a1); if (!object.ReferenceEquals(a1, e.Attribute("foo"))) { throw new TestFailedException( "Attribute added to an element was unexpectedly copied"); } if (object.ReferenceEquals(a1, e2.Attribute("foo"))) { throw new TestFailedException( "Attribute added to a second element was not copied"); } }
/// <summary> /// Tests the Save overloads on element, that write to an XmlWriter. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "ElementSaveToXmlWriter")] public void ElementSaveToXmlWriter() { XElement ee = new XElement("x"); try { ee.Save((XmlWriter)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } }
/// <summary> /// Tests WriteTo on document. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "DocumentWriteTo")] public void DocumentWriteTo() { XDocument ee = new XDocument(); try { ee.WriteTo(null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } }
/// <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> /// Validates the explicit boolean conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToBoolean")] public void AttributeExplicitToBoolean() { // Calling explicit operator with null should result in exception. try { bool b = (bool)((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test various values. XAttribute e1 = new XAttribute("x", string.Empty); XAttribute e2 = new XAttribute("x", "bogus"); XAttribute e3 = new XAttribute("x", "true"); XAttribute e4 = new XAttribute("x", "false"); XAttribute e5 = new XAttribute("x", "0"); XAttribute e6 = new XAttribute("x", "1"); try { bool b1 = (bool)e1; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { bool b2 = (bool)e2; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } Validate.IsEqual((bool)e3, true); Validate.IsEqual((bool)e4, false); Validate.IsEqual((bool)e5, false); Validate.IsEqual((bool)e6, true); }
/// <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 behavior of the Value property on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeValue")] public void AttributeValue() { XAttribute a = new XAttribute("foo", 10m); Validate.IsEqual(a.Value, "10"); try { a.Value = null; Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } a.Value = "100"; Validate.IsEqual(a.Value, "100"); }
/// <summary> /// Validates the explicit guid conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToGuId")] public void AttributeExplicitToGuid() { // Calling explicit operator with null should result in exception. try { Guid g = (Guid)((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } string guid = "2b67e9fb-97ad-4258-8590-8bc8c2d32df5"; // Test various values. XAttribute e1 = new XAttribute("x", string.Empty); XAttribute e2 = new XAttribute("x", "bogus"); XAttribute e3 = new XAttribute("x", guid); try { Guid g1 = (Guid)e1; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { Guid g2 = (Guid)e2; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } Validate.IsEqual((Guid)e3, new Guid(guid)); }
/// <summary> /// Validates the behavior of the Value property on XText. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "TextValue")] public void TextValue() { XCData c = new XCData("xxx"); Validate.IsEqual(c.Value, "xxx"); // Null value not allowed. try { c.Value = null; Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Try setting a value. c.Value = "abcd"; Validate.IsEqual(c.Value, "abcd"); }
/// <summary> /// Validates the explicit DateTime conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToDateTime")] public void AttributeExplicitToDateTime() { // Calling explicit operator with null should result in exception. try { DateTime d = (DateTime)((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test various values. XAttribute e1 = new XAttribute("x", string.Empty); XAttribute e2 = new XAttribute("x", "bogus"); XAttribute e3 = new XAttribute("x", "1968-01-07"); try { DateTime d1 = (DateTime)e1; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { DateTime d2 = (DateTime)e2; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } Validate.IsEqual((DateTime)e3, new DateTime(1968, 1, 7)); }
/// <summary> /// Validates the explicit TimeSpan conversion operator on XAttribute. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "AttributeExplicitToTimeSpan")] public void AttributeExplicitToTimeSpan() { // Calling explicit operator with null should result in exception. try { TimeSpan d = (TimeSpan)((XAttribute)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Test various values. XAttribute e1 = new XAttribute("x", string.Empty); XAttribute e2 = new XAttribute("x", "bogus"); XAttribute e3 = new XAttribute("x", "PT1H2M3S"); try { TimeSpan d1 = (TimeSpan)e1; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } try { TimeSpan d2 = (TimeSpan)e2; Validate.ExpectedThrow(typeof(FormatException)); } catch (Exception ex) { Validate.Catch(ex, typeof(FormatException)); } Validate.IsEqual((TimeSpan)e3, new TimeSpan(1, 2, 3)); }
/// <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); }
/// <summary> /// Validate behavior of adding string content to a document. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "DocumentAddString")] public void DocumentAddString() { XDocument doc = new XDocument(); try { doc.Add(""); Validate.String(doc.ToString(SaveOptions.DisableFormatting), ""); doc.Add(" \t" + Environment.NewLine); Validate.String(doc.ToString(SaveOptions.DisableFormatting), " \t" + Environment.NewLine); } catch (Exception ex) { throw new TestException(TestResult.Failed, ex.ToString()); } try { doc.Add("a"); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } try { doc.Add("\tab"); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } }
/// <summary> /// Validates the behavior of the Target and Data properties on XProcessingInstruction. /// </summary> /// <returns>true if pass, false if fail</returns> //[Variation(Desc = "ProcessingInstructionValues")] public void ProcessingInstructionValues() { XProcessingInstruction c = new XProcessingInstruction("xxx", "yyy"); Validate.IsEqual(c.Target, "xxx"); Validate.IsEqual(c.Data, "yyy"); // Null values not allowed. try { c.Target = null; Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { c.Data = null; Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Try setting values. c.Target = "abcd"; Validate.IsEqual(c.Target, "abcd"); c.Data = "efgh"; Validate.IsEqual(c.Data, "efgh"); Validate.IsEqual(c.Target, "abcd"); }
/// <summary> /// Tests trying to use an invalid name with XName.Get. /// </summary> /// <param name="contextValue"></param> /// <returns></returns> //[Variation(Desc = "NameGetInvalId")] public void NameGetInvalid() { try { XName.Get(null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { XName.Get(null, "foo"); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { XName.Get(string.Empty, "foo"); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } try { XName.Get(string.Empty); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } try { XName.Get("{}"); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } try { XName.Get("{foo}"); Validate.ExpectedThrow(typeof(ArgumentException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentException)); } }
/// <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))); }