/// <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 the ElementsBeforeSelf methods on Node. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "NodeElementsBeforeSelf")] public void NodeElementsBeforeSelf() { XElement parent = new XElement("parent"); XElement child1a = new XElement("child1", new XElement("nested")); XElement child1b = new XElement("child1", new XElement("nested")); XElement child2a = new XElement("child2", new XElement("nested")); XElement child2b = new XElement("child2", new XElement("nested")); XComment comment = new XComment("this is a comment"); // If no parent, should not be any elements before it. Validate.Enumerator(comment.ElementsBeforeSelf(), new XElement[0]); parent.Add(child1a); parent.Add(child1b); parent.Add(child2a); parent.Add(comment); parent.Add(child2b); Validate.Enumerator( comment.ElementsBeforeSelf(), new XElement[] { child1a, child1b, child2a }); Validate.Enumerator( comment.ElementsBeforeSelf("child1"), new XElement[] { child1a, child1b }); Validate.Enumerator( child2b.ElementsBeforeSelf(), new XElement[] { child1a, child1b, child2a }); Validate.Enumerator( child2b.ElementsBeforeSelf("child2"), new XElement[] { child2a }); }
/// <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))); }
/// <summary> /// Test loading a document from an XmlReader. /// </summary> /// <param name="context"></param> /// <returns></returns> //[Variation(Desc = "DocumentLoadFromXmlReader")] public void DocumentLoadFromXmlReader() { // Null reader not allowed. try { XDocument.Load((XmlReader)null); Validate.ExpectedThrow(typeof(ArgumentNullException)); } catch (Exception ex) { Validate.Catch(ex, typeof(ArgumentNullException)); } // Extra content at end of reader. StringReader stringReader = new StringReader("<x/><y/>"); using (XmlReader xmlReader = XmlReader.Create(stringReader)) { try { XDocument.Load(xmlReader); Validate.ExpectedThrow(typeof(XmlException)); } catch (Exception ex) { Validate.Catch(ex, typeof(XmlException)); } } // Empty content. stringReader = new StringReader(""); using (XmlReader xmlReader = XmlReader.Create(stringReader)) { try { XDocument.Load(xmlReader); Validate.ExpectedThrow(typeof(XmlException)); } catch (Exception ex) { Validate.Catch(ex, typeof(XmlException)); } } // No root element. stringReader = new StringReader("<!-- comment -->"); using (XmlReader xmlReader = XmlReader.Create(stringReader)) { try { XDocument.Load(xmlReader); Validate.ExpectedThrow(typeof(XmlException)); } catch (Exception ex) { Validate.Catch(ex, typeof(XmlException)); } } // Reader mispositioned, so not at eof when done stringReader = new StringReader("<x></x>"); using (XmlReader xmlReader = XmlReader.Create(stringReader)) { // Position the reader on the end element. xmlReader.Read(); xmlReader.Read(); try { XDocument.Load(xmlReader); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } } // Reader mispositioned, so empty root when done stringReader = new StringReader("<x></x>"); using (XmlReader xmlReader = XmlReader.Create(stringReader)) { // Position the reader at eof. xmlReader.Read(); xmlReader.Read(); xmlReader.Read(); try { XDocument.Load(xmlReader); Validate.ExpectedThrow(typeof(InvalidOperationException)); } catch (Exception ex) { Validate.Catch(ex, typeof(InvalidOperationException)); } } }