示例#1
0
                /// <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));
                            }
                        }
                    }
                }
示例#2
0
                /// <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);
                }