private void VerifyRemoveNodes(XDocument doc)
        {
            IEnumerable<XNode> nodesBefore = doc.Nodes().ToList();
            XDeclaration decl = doc.Declaration;

            doc.RemoveNodes();

            TestLog.Compare(doc.Nodes().IsEmpty(), "e.Nodes().IsEmpty()");
            TestLog.Compare(nodesBefore.Where(n => n.Parent != null).IsEmpty(), "nodesBefore.Where(n=>n.Parent!=null).IsEmpty()");
            TestLog.Compare(nodesBefore.Where(n => n.Document != null).IsEmpty(), "nodesBefore.Where(n=>n.Parent!=null).IsEmpty()");
            TestLog.Compare(doc.Declaration == decl, "doc.Declaration == decl");
        }
Esempio n. 2
0
                /// <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);
                    }
                }
        public void ValidAddIntoXDocument(TestedFunction testedFunction, CalculateExpectedValues calculateExpectedValues)
        {
            runWithEvents = (bool)Params[0];
            var isConnected = (bool)Variation.Params[0];
            var combCount = (int)Variation.Params[1];

            object[] nodes = { new XDocumentType("root", "", "", ""), new XDocumentType("roo2t", "", "", ""), new XProcessingInstruction("PI", "data"), new XComment("Comment"), new XElement("elem1"), new XElement("elem2", new XElement("C", "nodede")), new XText(""), new XText(" "), new XText("\t"), new XCData(""), new XCData("<A/>"), " ", "\t", "", null };

            object[] origNodes = { new XProcessingInstruction("OO", "oo"), new XComment("coco"), " ", new XDocumentType("root", null, null, null), new XElement("anUnexpectedlyLongNameForTheRootElement") };

            if (isConnected)
            {
                new XElement("foo", nodes.Where(n => n is XNode && !(n is XDocumentType)));
                foreach (XNode nn in nodes.Where(n => n is XDocumentType))
                {
                    new XDocument(nn);
                }
            }

            foreach (var origs in origNodes.NonRecursiveVariations(2))
            {
                if (origs.Select(o => new ExpectedValue(false, o)).IsXDocValid())
                {
                    continue;
                }
                foreach (var o in nodes.NonRecursiveVariations(combCount))
                {
                    var doc = new XDocument(origs);
                    XNode n = doc.FirstNode;

                    List<ExpectedValue> expNodes = Helpers.ProcessNodes(calculateExpectedValues(doc, 0, o)).ToList();
                    bool shouldFail = expNodes.IsXDocValid();

                    try
                    {
                        if (runWithEvents)
                        {
                            eHelper = new EventsHelper(doc);
                        }
                        testedFunction(n, o);
                        TestLog.Compare(!shouldFail, "should fail - exception expected here");
                        TestLog.Compare(expNodes.EqualAll(doc.Nodes(), XNode.EqualityComparer), "nodes does not pass");
                    }
                    catch (InvalidOperationException ex)
                    {
                        TestLog.Compare(shouldFail, "Unexpected exception : " + ex.Message);
                    }
                    catch (ArgumentException ex)
                    {
                        TestLog.Compare(shouldFail, "Unexpected exception : " + ex.Message);
                    }
                    finally
                    {
                        doc.RemoveNodes();
                    }
                }
            }
        }
Esempio n. 4
0
 public void ExecuteXDocumentVariation()
 {
     XNode[] content = Variation.Params as XNode[];
     XDocument xDoc = new XDocument(content);
     XDocument xDocOriginal = new XDocument(xDoc);
     using (UndoManager undo = new UndoManager(xDoc))
     {
         undo.Group();
         using (EventsHelper docHelper = new EventsHelper(xDoc))
         {
             xDoc.RemoveNodes();
             docHelper.Verify(XObjectChange.Remove, content);
         }
         undo.Undo();
         TestLog.Compare(XNode.DeepEquals(xDoc, xDocOriginal), "Undo did not work!");
     }
 }
Esempio n. 5
0
                /// <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);
                }
Esempio n. 6
0
                /// <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);
                }
Esempio n. 7
0
                /// <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();
                    }
                }
Esempio n. 8
0
        public void DocumentXmlDeclaration()
        {
            XDocument doc = new XDocument();
            Assert.Null(doc.Declaration);

            XDeclaration dec = new XDeclaration("1.0", "utf-16", "yes");
            XDocument doc2 = new XDocument(dec);
            Assert.Same(dec, doc2.Declaration);

            doc2.RemoveNodes();
            Assert.NotNull(doc2.Declaration);
        }
Esempio n. 9
0
        public void DocumentRoot()
        {
            XDocument doc = new XDocument();
            Assert.Null(doc.Root);

            XElement e = new XElement("element");
            doc.Add(e);
            Assert.Same(e, doc.Root);

            doc.RemoveNodes();
            doc.Add(new XComment("comment"));
            Assert.Null(doc.Root);
        }
Esempio n. 10
0
        private void TestReplacement(XDocument e, object[] nodes, int numOfNodes)
        {
            for (int i = 0; i < e.Nodes().Count(); i++)
            {
                object[] allowedNodes = e.Nodes().ElementAt(i) is XElement ? nodes.Where(o => !(o is XElement)).ToArray() : nodes;
                foreach (var replacement in nodes.NonRecursiveVariations(numOfNodes))
                {
                    bool shouldFail = false;

                    var doc = new XDocument(e);
                    XNode toReplace = doc.Nodes().ElementAt(i);
                    XNode prev = toReplace.PreviousNode;
                    XNode next = toReplace.NextNode;
                    bool isReplacementOriginal = replacement.Where(o => o is XNode).Where(o => (o as XNode).Document != null).IsEmpty();

                    IEnumerable<ExpectedValue> expValues = ReplaceWithExpValues(doc, toReplace, replacement).ProcessNodes().ToList();

                    // detect invalid states
                    shouldFail = expValues.IsXDocValid();

                    try
                    {
                        if (_runWithEvents)
                        {
                            _eHelper = new EventsHelper(doc);
                        }
                        toReplace.ReplaceWith(replacement);

                        TestLog.Compare(!shouldFail, "Should fail ... ");
                        TestLog.Compare(toReplace.Parent == null, "toReplace.Parent == null");
                        TestLog.Compare(toReplace.Document == null, "toReplace.Document == null");
                        TestLog.Compare(expValues.EqualAll(doc.Nodes(), XNode.EqualityComparer), "expected values");
                        if (isReplacementOriginal)
                        {
                            TestLog.Compare(replacement.Where(o => o is XNode).Where(o => (o as XNode).Document != doc.Document).IsEmpty(), "replacement.Where(o=>o is XNode).Where(o=>(o as XNode).Document!=doc.Document).IsEmpty()");
                        }
                    }
                    catch (InvalidOperationException)
                    {
                        TestLog.Compare(shouldFail, "Exception not expected here");
                    }
                    catch (ArgumentException)
                    {
                        TestLog.Compare(shouldFail, "Exception not expected here");
                    }
                    finally
                    {
                        doc.RemoveNodes();
                    }
                }
            }
        }
Esempio n. 11
0
 /// <summary>
 /// Runs test for valid cases
 /// </summary>
 /// <param name="nodeType">XElement/XAttribute</param>
 /// <param name="name">name to be tested</param>
 public void RunValidTests(string nodeType, string name)
 {
     XDocument xDocument = new XDocument();
     XElement element = null;
     try
     {
         switch (nodeType)
         {
             case "XElement":
                 element = new XElement(name, name);
                 xDocument.Add(element);
                 IEnumerable<XNode> nodeList = xDocument.Nodes();
                 TestLog.Compare(nodeList.Count(), 1, "Failed to create element { " + name + " }");
                 xDocument.RemoveNodes();
                 break;
             case "XAttribute":
                 element = new XElement(name, name);
                 XAttribute attribute = new XAttribute(name, name);
                 element.Add(attribute);
                 xDocument.Add(element);
                 XAttribute x = element.Attribute(name);
                 TestLog.Compare(x.Name.LocalName, name, "Failed to get the added attribute");
                 xDocument.RemoveNodes();
                 break;
             case "XName":
                 XName xName = XName.Get(name, name);
                 TestLog.Compare(xName.LocalName.Equals(name), "Invalid LocalName");
                 TestLog.Compare(xName.NamespaceName.Equals(name), "Invalid Namespace Name");
                 TestLog.Compare(xName.Namespace.NamespaceName.Equals(name), "Invalid Namespace Name");
                 break;
             default:
                 break;
         }
     }
     catch (Exception e)
     {
         TestLog.WriteLine(nodeType + "failed to create with a valid name { " + name + " }");
         throw new TestFailedException(e.ToString());
     }
 }
Esempio n. 12
0
                //[Variation(Priority = 0, Desc = "XDocument : dynamic")]
                public void Document1()
                {
                    object[] content = new object[] {
                            new object [] {new string [] {" ", null, " "}, "  "},
                            new object [] {new string [] {" "," \t"}, new XText("  \t")},
                            new object [] {new XText [] {new XText(" "), new XText("\t")}, new XText(" \t")},
                            new XDocumentType ("root", "", "", ""),
                            new XProcessingInstruction ("PI1",""),
                            new XText("\n"),
                            new XText("\t"),
                            new XText("       "),
                            new XProcessingInstruction ("PI1",""),
                            new XElement ("myroot"),
                            new XProcessingInstruction ("PI2","click"),
                            new object [] {new XElement ("X", new XAttribute ("id","a1"), new XText ("hula")), new XElement ("X", new XText ("hula"), new XAttribute ("id","a1"))},
                            new XComment (""),
                            new XComment ("comment"),
                        };

                    foreach (object[] objs in content.NonRecursiveVariations(4))
                    {
                        XDocument doc1 = null;
                        XDocument doc2 = null;
                        try
                        {
                            object[] o1 = ExpandAndProtectTextNodes(objs, 0).ToArray();
                            object[] o2 = ExpandAndProtectTextNodes(objs, 1).ToArray();
                            if (o1.Select(x => new ExpectedValue(false, x)).IsXDocValid() || o2.Select(x => new ExpectedValue(false, x)).IsXDocValid()) continue;
                            doc1 = new XDocument(o1);
                            doc2 = new XDocument(o2);
                            VerifyComparison(true, doc1, doc2);
                        }
                        catch (InvalidOperationException)
                        {
                            // some combination produced from the array are invalid
                            continue;
                        }
                        finally
                        {
                            if (doc1 != null) doc1.RemoveNodes();
                            if (doc2 != null) doc2.RemoveNodes();
                        }
                    }
                }