Exemple #1
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);
                }
Exemple #2
0
                /// <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");
                }
Exemple #3
0
                /// <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);
                }
Exemple #4
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));
                    }
                }
Exemple #5
0
                /// <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);
                }