Ejemplo n.º 1
0
        public void NodeWithNoParent()
        {
            _runWithEvents = (bool)Params[0];
            XNode[] nodes = { new XElement("a"), new XElement("b", new XAttribute("id", "a0")), new XElement("c", new XAttribute("id", "a0"), new XElement("cc")), new XComment("comm"), new XProcessingInstruction("PI", ""), new XText(""), new XText("  "), new XText("normal"), new XCData("cdata"), new XDocument(), new XDocument(new XDeclaration("1.0", "UTF8", "true"), new XElement("c", new XAttribute("id", "a0"), new XElement("cc"))) };

            foreach (XNode n in nodes)
            {
                try
                {
                    if (_runWithEvents)
                    {
                        _eHelper = new EventsHelper(n);
                    }
                    n.Remove();
                    if (_runWithEvents)
                    {
                        _eHelper.Verify(XObjectChange.Remove, n);
                    }
                    TestLog.Compare(false, "Exception expected [" + n.NodeType + "] " + n);
                }
                catch (InvalidOperationException)
                {
                }
            }
        }
        /// <summary>
        /// XElement:
        ///  - with content, empty
        ///  - adding valid nodes
        ///  - adding invalid nodes
        ///  - adding text, text concatenation, node identity
        ///  - adding nulls
        ///  - adding IEnumerable (order)
        /// </summary>
        public void SingeNodeAddIntoElement()
        {
            EventsHelper eHelper;
            bool runWithEvents;

            runWithEvents = (bool)Params[0];
            var xml = Variation.Params[0] as string;
            var variationLength = (int)Variation.Params[1];
            var isConnected = (bool)Variation.Params[2];
            string stringOnlyContent = Variation.Params.Length > 3 ? Variation.Params[3] as string : null;

            object[] nodes = { new XElement("B"), new XElement(XNamespace.Get("ns1") + "B"), new XElement("B", new XElement("C"), new XAttribute("a", "aa")), new XProcessingInstruction("PI", "data"), new XComment("comment"), "text plain", " ", "", null, new XText("xtext"), new XText(""), new XCData("xcdata") };

            if (isConnected)
            {
                new XElement("dummy", nodes);
            } // connect the nodes to force cloning

            foreach (var toInsert in nodes.NonRecursiveVariations(variationLength))
            {
                XElement e = XElement.Parse(xml);
                string stringOnlyContentCopy = stringOnlyContent == null ? null : new string(stringOnlyContent.ToCharArray());

                List<ExpectedValue> expectedNodes = CalculateExpectedValuesAddFirst(e, toInsert, stringOnlyContentCopy).ProcessNodes().ToList();
                if (runWithEvents)
                {
                    eHelper = new EventsHelper(e);
                }
                e.AddFirst(toInsert);
                TestLog.Compare(expectedNodes.EqualAll(e.Nodes(), XNode.EqualityComparer), "AddFirst");
                e.RemoveAll();
            }
        }
Ejemplo n.º 3
0
        public void OnDocument()
        {
            _runWithEvents = (bool)Params[0];
            var itemCount = (int)Variation.Params[0];
            var addDecl = (bool)Variation.Params[1];

            object[] data = { new XDocumentType("root", null, null, null), new XElement("A"), new XElement("B", new XElement("x"), "string", new XAttribute("at", "home")), new XProcessingInstruction("PI1", ""), new XProcessingInstruction("PI2", ""), new XText(" "), new XText(" "), new XText(" "), new XComment("comment1"), new XComment("comment2") };

            foreach (var nodes in data.NonRecursiveVariations(itemCount))
            {
                if (nodes.Count(x => x is XElement) > 1 || nodes.CheckDTDAfterElement())
                {
                    continue; // double root elem check and dtd after elem check
                }

                int length = (new XDocument(nodes)).Nodes().Count();
                for (int i = 0; i < length; i++)
                {
                    XDocument doc = addDecl ? new XDocument(new XDeclaration("1.0", "UTF8", "true"), nodes) : new XDocument(nodes);
                    XNode o = doc.Nodes().ElementAt(i);

                    if (_runWithEvents)
                    {
                        _eHelper = new EventsHelper(doc);
                    }

                    DoRemoveTest(doc, i);

                    if (_runWithEvents)
                    {
                        _eHelper.Verify(XObjectChange.Remove, o);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        //[Variation(Priority = 1, Desc = "Add attribute (empty elem I., no attrs)", Params = new object[] { "<A></A>", "a1", "a1" })]
        //[Variation(Priority = 0, Desc = "Add attribute (empty elem II., no attrs)", Params = new object[] { "<A/>", "a1", "a1" })]
        //[Variation(Priority = 0, Desc = "Add attribute (empty elem)", Params = new object[] { "<A a='a' b='b'/>", "a1", "a1" })]
        //[Variation(Priority = 0, Desc = "Add attribute (empty elem, namespaces)", Params = new object[] { "<A a1='a1' b='b'/>", "{ns}a1", "ns_a1" })]
        //[Variation(Priority = 1, Desc = "Add attribute", Params = new object[] { "<A a1='a1' b='b'>text<B/></A>", "{ns}a1", "ns_a1" })]
        //[Variation(Priority = 2, Desc = "Add attribute (content text only)", Params = new object[] { "<A a1='a1' b='b'>text</A>", "{ns}a1", "ns_a1" })]

        //[Variation(Priority = 0, Desc = "Replace attribute (empty elem, only one attr.)", Params = new object[] { "<A a1='original' />", "a1", "a1" })]
        //[Variation(Priority = 0, Desc = "Replace attribute (empty elem, multiple attr., namespaces)", Params = new object[] { "<A p:a1='x' a1='original' xmlns:p='ns'/>", "{ns}a1", "ns_a1" })]
        //[Variation(Priority = 1, Desc = "Replace attribute (multiple attr. (first))", Params = new object[] { "<A a1='original' x='x' y='y'>text</A>", "a1", "a1" })]
        //[Variation(Priority = 1, Desc = "Replace attribute (multiple attr. (mIddle))", Params = new object[] { "<A x='x' a1='original' y='y'>text</A>", "a1", "a1" })]
        //[Variation(Priority = 1, Desc = "Replace attribute (multiple attr. (last))", Params = new object[] { "<A x='x' y='y' a1='original'>text</A>", "a1", "a1" })]

        //[Variation(Priority = 0, Desc = "Remove attribute (only attribute)", Params = new object[] { "<A a1='original'></A>", "a1", null })]
        //[Variation(Priority = 0, Desc = "Remove attribute (from multiple attribs)", Params = new object[] { "<A y='y' a1='original' x='x'></A>", "a1", null })]
        //[Variation(Priority = 1, Desc = "Remove attribute (from multiple attribs, namespace)", Params = new object[] { "<A a1='original' p:a1='o' xmlns:p='A' ></A>", "{A}a1", null })]
        //[Variation(Priority = 1, Desc = "Remove attribute (from multiple attribs, content)", Params = new object[] { "<A x='t' a1='original' y='r'>trt</A>", "a1", null })]
        //[Variation(Priority = 1, Desc = "Remove attribute (nonexisting)", Params = new object[] { "<A x='t' a1='original' y='r'>trt</A>", "nonex", null })]

        public void OnXElement()
        {
            _runWithEvents = (bool)Params[0];
            var xml = Variation.Params[0] as string;
            var newName = Variation.Params[1] as string;
            var newValue = Variation.Params[2] as string;

            XElement e = XElement.Parse(xml);

            XAttribute origAttrib = e.Attribute(newName);
            IEnumerable<XAttribute> origAttrs = e.Attributes().ToList();
            IEnumerable<ExpectedValue> refComparison = getExpectedAttributes(e, newName, newValue, true).ToList();
            IEnumerable<ExpectedValue> valueComparison = getExpectedAttributes(e, newName, newValue, false).ToList();
            IEnumerable<XNode> nodes = e.Nodes().ToList();

            if (_runWithEvents)
            {
                _eHelper = new EventsHelper(e);
            }
            e.SetAttributeValue(newName, newValue);
            // Not sure how to verify this yet( what possible events and in what order)
            if (_runWithEvents)
            {
                if (newValue == null)
                {
                    if (origAttrib != null)
                    {
                        _eHelper.Verify(XObjectChange.Remove);
                    }
                }
                else
                {
                    if (origAttrib != null)
                    {
                        _eHelper.Verify(XObjectChange.Value);
                    }
                    else
                    {
                        _eHelper.Verify(XObjectChange.Add);
                    }
                }
            }

            if (newValue != null)
            {
                TestLog.Compare(origAttrib == null || ReferenceEquals(origAttrib, e.Attribute(newName)), "origAttrib == null || Object.ReferenceEquals (origAttrib, e.Attribute(newName))");
                TestLog.Compare(e.Attribute(newName).Value, newValue, "e.Attribute(newName), newValue");
            }
            else
            {
                TestLog.Compare(origAttrib == null || (origAttrib.Parent == null && origAttrib.Document == null), "origAttrib == null || (origAttrib.Parent==null && origAttrib.Document==null)");
                TestLog.Compare(e.Attribute(newName), null, "e.Attribute(newName), null");
            }

            TestLog.Compare(refComparison.EqualAllAttributes(e.Attributes(), Helpers.MyAttributeComparer), "refComparison.EqualAllAttributes(e.Attributes(), Helpers.MyAttributeComparer))");
            TestLog.Compare(valueComparison.EqualAllAttributes(e.Attributes(), Helpers.MyAttributeComparer), "valueComparison.EqualAllAttributes(e.Attributes(), Helpers.MyAttributeComparer))");
            TestLog.Compare(nodes.SequenceEqual(e.Nodes()), "nodes.EqualAll(e.Nodes())");

            e.Verify();
        }
Ejemplo n.º 5
0
 //[Variation(Priority = 0, Desc = "XProcessingInstruction - Valid Name")]
 public void ValidPIVariation()
 {
     _runWithEvents = (bool)Params[0];
     XProcessingInstruction toChange = new XProcessingInstruction("target", "data");
     if (_runWithEvents) _eHelper = new EventsHelper(toChange);
     toChange.Target = "newTarget";
     if (_runWithEvents) _eHelper.Verify(XObjectChange.Name);
     TestLog.Compare(toChange.Target.Equals("newTarget"), "Name did not change");
 }
Ejemplo n.º 6
0
 //[Variation(Priority = 0, Desc = "XDocumentType - Name")]
 public void DocTypeVariation()
 {
     XDocumentType toChange = new XDocumentType("root", "", "", "");
     XDocumentType original = new XDocumentType(toChange);
     using (EventsHelper eHelper = new EventsHelper(toChange))
     {
         toChange.Name = "newName";
         TestLog.Compare(toChange.Name.Equals("newName"), "Name did not change");
         eHelper.Verify(XObjectChange.Name, toChange);
     }
 }
Ejemplo n.º 7
0
 //[Variation(Priority = 0, Desc = "XElement - same name", Params = new object[] { "<element>value</element>", "element" })]
 //[Variation(Priority = 0, Desc = "XElement - different name", Params = new object[] { "<element>value</element>", "newElement" })]
 //[Variation(Priority = 0, Desc = "XElement - name with namespace", Params = new object[] { "<element>value</element>", "{a}newElement" })]
 //[Variation(Priority = 0, Desc = "XElement - name with xml namespace", Params = new object[] { "<element>value</element>", "{http://www.w3.org/XML/1998/namespace}newElement" })]
 //[Variation(Priority = 0, Desc = "XElement - element with namespace", Params = new object[] { "<p:element xmlns:p='mynamespace'><p:child>value</p:child></p:element>", "{a}newElement" })]
 public void ValidVariation()
 {
     _runWithEvents = (bool)Params[0];
     string xml = Variation.Params[0] as string;
     XElement toChange = XElement.Parse(xml);
     XName newName = Variation.Params[1] as string;
     if (_runWithEvents) _eHelper = new EventsHelper(toChange);
     toChange.Name = newName;
     if (_runWithEvents) _eHelper.Verify(XObjectChange.Name);
     TestLog.Compare(newName.Namespace == toChange.Name.Namespace, "Namespace did not change");
     TestLog.Compare(newName.LocalName == toChange.Name.LocalName, "LocalName did not change");
 }
Ejemplo n.º 8
0
 //[Variation(Priority = 0, Desc = "XProcessingInstruction - Name")]
 public void PIVariation()
 {
     XProcessingInstruction toChange = new XProcessingInstruction("target", "data");
     XProcessingInstruction original = new XProcessingInstruction(toChange);
     using (UndoManager undo = new UndoManager(toChange))
     {
         undo.Group();
         using (EventsHelper eHelper = new EventsHelper(toChange))
         {
             toChange.Target = "newTarget";
             TestLog.Compare(toChange.Target.Equals("newTarget"), "Name did not change");
             eHelper.Verify(XObjectChange.Name, toChange);
         }
         undo.Undo();
         TestLog.Compare(XNode.DeepEquals(toChange, original), "Undo did not work");
     }
 }
 /// <summary>
 /// On XDocument
 ///  ~ Empty
 ///  ~ Not empty
 ///      With XDecl
 ///      With DTD
 ///      Just root elem
 ///      Root elem + PI + whitespace + comment
 /// </summary>
 public void OnXDocument1()
 {
     int count = 0;
     _runWithEvents = (bool)Params[0];
     var xml = Variation.Param as string;
     XDocument e = xml == "" ? new XDocument() : XDocument.Parse(xml);
     if (_runWithEvents)
     {
         _eHelper = new EventsHelper(e);
         count = e.Nodes().Count();
     }
     VerifyRemoveNodes(e);
     if (_runWithEvents)
     {
         _eHelper.Verify(XObjectChange.Remove, count);
     }
 }
 /// <summary>
 /// On XElement
 ///  ~ With and Without attributes
 ///  ~ Empty
 ///  ~ Not Empty
 ///      text node only
 ///      mixed content
 ///      children but not mixed content
 /// </summary>
 public void OnXElement1()
 {
     int count = 0;
     _runWithEvents = (bool)Params[0];
     var xml = Variation.Param as string;
     XElement e = XElement.Parse(xml);
     if (_runWithEvents)
     {
         _eHelper = new EventsHelper(e);
         count = e.Nodes().Count();
     }
     VerifyRemoveNodes(e);
     if (_runWithEvents && !xml.Equals(@"<A></A>"))
     {
         _eHelper.Verify(XObjectChange.Remove, count);
     }
 }
Ejemplo n.º 11
0
        public void AddingMultipleNodesIntoElement(TestedFunction testedFunction, CalculateExpectedValues calculateExpectedValues)
        {
            runWithEvents = (bool)Params[0];
            var isConnected = (bool)Variation.Params[0];
            var lengthOfVariations = (int)Variation.Params[1];

            object[] toAdd = { new XElement("ToAddEmpty"), new XElement("ToAddWithAttr", new XAttribute("id", "a1")), new XElement("ToAddWithContent", new XAttribute("id", "a1"), new XElement("inner", "innerContent"), "content"), new XProcessingInstruction("PiWithData", "data"), "", new XProcessingInstruction("PiNOData", ""), new XComment("comment"), new XCData("xtextCdata"), new XText("xtext"), "plaintext1", "plaintext2", null };

            if (isConnected)
            {
                var dummy = new XElement("dummy", toAdd);
            }

            var referenceElement = new XElement("testElement", "text0", new XElement("tin"), new XElement("tin2", new XAttribute("id", "a2")), //
                "text1", new XAttribute("hu", "ha"), new XProcessingInstruction("PI", "data"), new XText("heleho"), new XComment("M&M"), "textEnd");

            for (int startPos = 0; startPos < referenceElement.Nodes().Count(); startPos++)
            {
                // iterate over all nodes in the original element
                foreach (var newNodes in toAdd.NonRecursiveVariations(lengthOfVariations))
                {
                    var orig = new XElement(referenceElement);

                    IEnumerable<ExpectedValue> expectedNodes = Helpers.ProcessNodes(calculateExpectedValues(orig, startPos, newNodes)).ToList();

                    // Add node on the expected place
                    XNode n = orig.FirstNode;
                    for (int position = 0; position < startPos; position++)
                    {
                        n = n.NextNode;
                    }

                    if (runWithEvents)
                    {
                        eHelper = new EventsHelper(orig);
                    }
                    testedFunction(n, newNodes);

                    // Node Equals check
                    TestLog.Compare(expectedNodes.EqualAll(orig.Nodes(), XNode.EqualityComparer), "constructed != added :: nodes Deep equals");

                    // release nodes
                    orig.RemoveAll();
                }
            }
        }
Ejemplo n.º 12
0
 //[Variation(Priority = 0, Desc = "XElement - space character name", Params = new object[] { "<element>value</element>", " " })]
 //[Variation(Priority = 0, Desc = "XElement - empty string name", Params = new object[] { "<element>value</element>", "" })]
 //[Variation(Priority = 0, Desc = "XElement - null name", Params = new object[] { "<element>value</element>", null })]
 public void InValidVariation()
 {
     _runWithEvents = (bool)Params[0];
     string xml = Variation.Params[0] as string;
     XElement toChange = XElement.Parse(xml);
     try
     {
         if (_runWithEvents) _eHelper = new EventsHelper(toChange);
         toChange.Name = Variation.Params[1] as string;
     }
     catch (Exception)
     {
         if (_runWithEvents) _eHelper.Verify(0);
         return;
     }
     throw new TestException(TestResult.Failed, "");
 }
 public void OnXElement2()
 {
     int count = 0;
     _runWithEvents = (bool)Params[0];
     var e = new XElement("A", new XText(""));
     TestLog.Compare(e.Nodes().Any(), "Test failed:: e.Nodes().Any()");
     if (_runWithEvents)
     {
         _eHelper = new EventsHelper(e);
         count = e.Nodes().Count();
     }
     VerifyRemoveNodes(e);
     if (_runWithEvents)
     {
         _eHelper.Verify(XObjectChange.Remove, count);
     }
 }
Ejemplo n.º 14
0
 public void ExecuteXDocumentVariation()
 {
     XNode toReplace = Variation.Params[0] as XNode;
     XNode newValue = Variation.Params[1] as XNode;
     XDocument xDoc = new XDocument(toReplace);
     XDocument xDocOriginal = new XDocument(xDoc);
     using (UndoManager undo = new UndoManager(xDoc))
     {
         undo.Group();
         using (EventsHelper docHelper = new EventsHelper(xDoc))
         {
             toReplace.ReplaceWith(newValue);
             docHelper.Verify(new XObjectChange[] { XObjectChange.Remove, XObjectChange.Add }, new XObject[] { toReplace, newValue });
         }
         undo.Undo();
         TestLog.Compare(XNode.DeepEquals(xDoc, xDocOriginal), "Undo did not work!");
     }
 }
Ejemplo n.º 15
0
 public void ExecuteXDocumentVariation()
 {
     XNode[] content = Variation.Params[0] as XNode[];
     int index = (int)Variation.Params[1];
     XDocument xDoc = new XDocument(content);
     XDocument xDocOriginal = new XDocument(xDoc);
     XNode toRemove = xDoc.Nodes().ElementAt(index);
     using (UndoManager undo = new UndoManager(xDoc))
     {
         undo.Group();
         using (EventsHelper docHelper = new EventsHelper(xDoc))
         {
             toRemove.Remove();
             docHelper.Verify(XObjectChange.Remove, toRemove);
         }
         undo.Undo();
         TestLog.Compare(XNode.DeepEquals(xDoc, xDocOriginal), "Undo did not work!");
     }
 }
Ejemplo n.º 16
0
 public void ExecuteXElementVariation()
 {
     XElement toChange = Variation.Params[0] as XElement;
     XName newName = (XName)Variation.Params[1];
     XElement original = new XElement(toChange);
     using (UndoManager undo = new UndoManager(toChange))
     {
         undo.Group();
         using (EventsHelper eHelper = new EventsHelper(toChange))
         {
             toChange.Name = newName;
             TestLog.Compare(newName.Namespace == toChange.Name.Namespace, "Namespace did not change");
             TestLog.Compare(newName.LocalName == toChange.Name.LocalName, "LocalName did not change");
             eHelper.Verify(XObjectChange.Name, toChange);
         }
         undo.Undo();
         TestLog.Compare(XNode.DeepEquals(toChange, original), "Undo did not work");
     }
 }
Ejemplo n.º 17
0
        // Remove standalone attribute
        // Remove the only attribute
        // Remove from multiple attributes - first, middle, last
        // Remove namespace decl {http://www.w3.org/2000/xmlns/}

        //[Variation(Priority = 1, Desc = "The only attribute, in Document", Params = new object[] { @"<A attr='1'/>", "attr", true })]
        //[Variation(Priority = 2, Desc = "The only attribute", Params = new object[] { @"<A attr='1'/>", "attr", false })]
        //[Variation(Priority = 0, Desc = "First attribute, in Document", Params = new object[] { @"<A attr='1' a2='a2' a3='a3'/>", "attr", true })]
        //[Variation(Priority = 2, Desc = "First attribute", Params = new object[] { @"<A attr='1' a2='a2' a3='a3'/>", "attr", false })]
        //[Variation(Priority = 0, Desc = "MIddle attribute, in Document", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns:p='ns' p:a3='pa3'/>", "a2", true })]
        //[Variation(Priority = 2, Desc = "MIddle attribute", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns:p='ns' p:a3='pa3'/>", "a2", false })]
        //[Variation(Priority = 1, Desc = "Last attribute, in Document", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns:p='ns' p:a3='pa3'/>", "{ns}a3", true })]
        //[Variation(Priority = 2, Desc = "Last attribute", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns:p='ns' p:a3='pa3'/>", "{ns}a3", false })]
        //[Variation(Priority = 1, Desc = "Remove namespace, in Document", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns:p='ns' p:a3='pa3'/>", "{http://www.w3.org/2000/xmlns/}p", true })]
        //[Variation(Priority = 2, Desc = "Remove namespace", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns:p='ns' p:a3='pa3'/>", "{http://www.w3.org/2000/xmlns/}p", false })]
        //[Variation(Priority = 1, Desc = "Remove default namespace, in Document", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns='def' xmlns:p='ns' p:a3='pa3'/>", "xmlns", true })]
        //[Variation(Priority = 2, Desc = "Remove default namespace", Params = new object[] { @"<A attr='1' a2='a2' a3='a3' xmlns:p='ns' xmlns='def' p:a3='pa3'/>", "xmlns", false })]
        public void RemoveAttribute()
        {
            _runWithEvents = (bool)Params[0];
            var xml = (string)Variation.Params[0];
            var attrName = (string)Variation.Params[1];
            var useDoc = (bool)Variation.Params[2];

            XElement elem = useDoc ? XDocument.Parse(xml).Root : XElement.Parse(xml);
            XAttribute a = elem.Attribute(attrName);
            a.Verify();

            List<ExpectedValue> expValues = GetExpectedResults(elem, a).ToList();

            if (_runWithEvents)
            {
                _eHelper = new EventsHelper(elem);
            }
            a.Remove();
            if (_runWithEvents)
            {
                _eHelper.Verify(XObjectChange.Remove, a);
            }
            a.Verify();

            TestLog.Compare(a.Parent, null, "Parent after");
            TestLog.Compare(a.NextAttribute, null, "NextAttribute after");
            TestLog.Compare(a.PreviousAttribute, null, "PrevAttribute after");

            try
            {
                a.Remove();
                TestLog.Compare(false, "Exception was expected here");
            }
            catch (InvalidOperationException)
            {
                // Expected exception
            }

            TestLog.Compare(expValues.EqualAllAttributes(elem.Attributes(), Helpers.MyAttributeComparer), "The rest of the attributes");

            elem.Verify();
        }
Ejemplo n.º 18
0
 public void ExecuteXElementVariation()
 {
     XNode toReplace = Variation.Params[0] as XNode;
     XNode newValue = Variation.Params[1] as XNode;
     XElement xElem = new XElement("root", toReplace);
     XElement xElemOriginal = new XElement(xElem);
     using (UndoManager undo = new UndoManager(xElem))
     {
         undo.Group();
         using (EventsHelper eHelper = new EventsHelper(xElem))
         {
             toReplace.ReplaceWith(newValue);
             xElem.Verify();
             eHelper.Verify(new XObjectChange[] { XObjectChange.Remove, XObjectChange.Add }, new XObject[] { toReplace, newValue });
         }
         undo.Undo();
         TestLog.Compare(xElem.Nodes().SequenceEqual(xElemOriginal.Nodes(), XNode.EqualityComparer), "Undo did not work!");
         TestLog.Compare(xElem.Attributes().EqualsAllAttributes(xElemOriginal.Attributes(), Helpers.MyAttributeComparer), "Undo did not work!");
     }
 }
Ejemplo n.º 19
0
        // no attributes
        // single attribute/namespace
        // multiple attributes/namespaces
        // default attributes ... @"<!DOCTYPE square [<!ELEMENT square EMPTY><!ATTLIST square width CDATA "10">]><square/>"

        //[Variation(Priority = 1, Desc = "No attributes")]

        //[Variation(Priority = 2, Desc = "Default attribute from DTD", Params = new object[] { @"<!DOCTYPE square [<!ELEMENT square (B*)><!ATTLIST square wIdth CDATA '10'>]><square>text<B/><!--commnet--></square>", new string[] { "wIdth" } })]
        //[Variation(Priority = 2, Desc = "Single attribute", Params = new object[] { @"<square wIdth='1'>text<B/><!--commnet--></square>", new string[] { "wIdth" } })]
        //[Variation(Priority = 2, Desc = "Single namespace (default)", Params = new object[] { @"<square xmlns='x'>text<B/><!--commnet--></square>", new string[] { "xmlns" } })]
        //[Variation(Priority = 2, Desc = "Single namespace", Params = new object[] { @"<p:square xmlns:p='x'>text<B/><!--commnet--></p:square>", new string[] { "{http://www.w3.org/2000/xmlns/}p" } })]
        //[Variation(Priority = 2, Desc = "Multiple attributes", Params = new object[] { @"<square wIdth='1' depth='11'><sub1/>text</square>", new string[] { "wIdth", "depth" } })]
        //[Variation(Priority = 2, Desc = "Multiple namespaces", Params = new object[] { @"<p:square xmlns:p='x' xmlns='x1'><p:A/><B/></p:square>", new string[] { "{http://www.w3.org/2000/xmlns/}p", "{http://www.w3.org/2000/xmlns/}p" } })]
        //[Variation(Priority = 2, Desc = "Multiple namespaces + attributes", Params = new object[] { @"<p:square xmlns:p='x' xmlns='x1' a1='xxx' p:a1='yyy'><p:A/><B p:x='ayay'/></p:square>", new string[] { "{http://www.w3.org/2000/xmlns/}p", "{http://www.w3.org/2000/xmlns/}p", "a1", "{x}a1" } })]

        public void DeleteAttributes()
        {
            int count = 0;
            _runWithEvents = (bool)Params[0];
            var xml = Variation.Params[0] as string;
            var attNames = Variation.Params[1] as string[];

            XDocument doc = XDocument.Parse(xml);

            TestLog.Compare(doc.Root.HasAttributes, "HasAttributes");
            TestLog.Compare(doc.Root.Attributes().Count(), attNames.Count(), "default attribute - Attributes()");
            foreach (string name in attNames)
            {
                TestLog.Compare(doc.Root.Attribute(name) != null, "Attribute (XName) before");
            }
            IEnumerable<XNode> origNodes = doc.Root.Nodes().ToList();
            IEnumerable<XAttribute> origAttributes = doc.Root.Attributes().ToList();

            if (_runWithEvents)
            {
                _eHelper = new EventsHelper(doc.Root);
                count = origAttributes.IsEmpty() ? 0 : origAttributes.Count();
            }
            doc.Root.RemoveAttributes();
            if (_runWithEvents)
            {
                _eHelper.Verify(XObjectChange.Remove, count);
            }

            TestLog.Compare(!doc.Root.HasAttributes, "default attribute not deleted");
            TestLog.Compare(doc.Root.Attributes().IsEmpty(), "default attribute - Attributes()");
            TestLog.Compare(doc.Root.FirstAttribute == null, "FirstAttribute");
            TestLog.Compare(doc.Root.LastAttribute == null, "LastAttribute");
            TestLog.Compare(doc.Root.Nodes().SequenceEqual(origNodes), "Content");
            TestLog.Compare(origAttributes.Where(a => a.Parent != null).IsEmpty(), "Deleted attributes property");

            foreach (string name in attNames)
            {
                TestLog.Compare(doc.Root.Attribute(name) == null, "Attribute (XName) after: " + name);
            }
        }
        //[Variation(Priority = 0, Desc = "Add Element (empty)", Params = new object[] { "<A/>", "X", "text" })]
        //[Variation(Priority = 0, Desc = "Add Element (text only)", Params = new object[] { "<A>t1</A>", "X", "text" })]
        //[Variation(Priority = 0, Desc = "Add Element (Empty, full)", Params = new object[] { "<A></A>", "X", "text" })]
        //[Variation(Priority = 0, Desc = "Add Element (mixed content)", Params = new object[] { "<A><B/>t2<!--comm-->t3</A>", "X", "text" })]
        //[Variation(Priority = 0, Desc = "Replace Element (the only child)", Params = new object[] { "<A><X xmlns='' Id='Id'/></A>", "X", "text" })]
        //[Variation(Priority = 1, Desc = "Replace Element (mixed content, empty)", Params = new object[] { "<A><X/>tn<X xmlns='a'/></A>", "{a}X", "text" })]
        //[Variation(Priority = 0, Desc = "Replace Element (mixed content)", Params = new object[] { "<A><X/>tn<X xmlns='a'><Y/>hum<?PI?></X></A>", "{a}X", "text" })]
        //[Variation(Priority = 1, Desc = "Delete Element (only element, empty)", Params = new object[] { "<A><X xmlns='a'>tralala</X></A>", "{a}X", null })]
        //[Variation(Priority = 0, Desc = "Delete Element (only element)", Params = new object[] { "<A><X xmlns='a'>tralala</X></A>", "{a}X", null })]
        //[Variation(Priority = 0, Desc = "Delete Element (mixed content, empty)", Params = new object[] { "<A>t1<X xmlns='a'/>t2</A>", "{a}X", null })]
        //[Variation(Priority = 1, Desc = "Delete Element (mixed content)", Params = new object[] { "<A>t1<X xmlns='a'><m/></X>t2</A>", "{a}X", null })]
        //[Variation(Priority = 1, Desc = "Delete Element (non existing)", Params = new object[] { "<A>t1<X xmlns='a'><m/></X>t2</A>", "X", null })]
        //[Variation(Priority = 1, Desc = "Delete Element (non existing, text only)", Params = new object[] { "<A>t1</A>", "X", null })]
        //[Variation(Priority = 1, Desc = "Delete Element (non existing, text only, full)", Params = new object[] { "<A></A>", "X", null })]

        public void OnXElement()
        {
            _runWithEvents = (bool)Params[0];
            var xml = Variation.Params[0] as string;
            var newName = Variation.Params[1] as string;
            var newValue = Variation.Params[2] as string;

            XElement e = XElement.Parse(xml);

            XElement changed = e.Element(newName);

            IEnumerable<ExpectedValue> nodeIdentityExpValues = getExpectedValues(e, newName, newValue, true).ProcessNodes().ToList();
            IEnumerable<ExpectedValue> valueExpectedValues = getExpectedValues(e, newName, newValue, false).ProcessNodes().ToList();
            IEnumerable<XAttribute> attr = changed == null ? null : changed.Attributes().ToArray();

            if (_runWithEvents)
            {
                _eHelper = new EventsHelper(e);
            }
            e.SetElementValue(newName, newValue);

            if (newValue == null)
            {
                TestLog.Compare(changed == null || changed.Parent == null, "delete: changed.Parent == null");
                TestLog.Compare(e.Element(newName) == null, "e.Element(newName)==null");
            }
            else
            {
                TestLog.Compare(changed == null || ReferenceEquals(changed, e.Element(newName)), "changed==null || Object.ReferenceEquals(changed, e.Element(newName))");
                TestLog.Compare(e.Element(newName).Value, newValue, "e.Element(newName).Value, newValue");
                TestLog.Compare(!e.Element(newName).HasElements, "!e.Element(newName).HasElements");
                TestLog.Compare(attr == null || attr.SequenceEqual(changed.Attributes()), "attr.EqualAll(changed.Attributes())");
            }

            TestLog.Compare(nodeIdentityExpValues.EqualAll(e.Nodes(), XNode.EqualityComparer), "nodeidentityExpValues.EqualAll (e.Nodes(), XNode.EqualityComparer)");
            TestLog.Compare(valueExpectedValues.EqualAll(e.Nodes(), XNode.EqualityComparer), "valueExpectedValues.EqualAll(e.Nodes(), XNode.EqualityComparer)");

            e.Verify();
        }
 public void InvalidAddIntoXDocument3()
 {
     _runWithEvents = (bool)Params[0];
     try
     {
         var doc = new XDocument(new XProcessingInstruction("pi", "halala"), new XElement("A"));
         var o = new XElement("C");
         if (_runWithEvents)
         {
             _eHelper = new EventsHelper(doc);
         }
         doc.AddFirst(o);
         if (_runWithEvents)
         {
             _eHelper.Verify(XObjectChange.Add, o);
         }
         TestLog.Compare(false, "Exception expected");
     }
     catch (InvalidOperationException)
     {
     }
 }
 public void InvalidAddIntoXDocument1()
 {
     _runWithEvents = (bool)Params[0];
     try
     {
         var doc = new XDocument(new XDocumentType("root", null, null, null), new XElement("A"));
         var o = new XDocumentType("D", null, null, null);
         if (_runWithEvents)
         {
             _eHelper = new EventsHelper(doc);
         }
         doc.AddFirst(o);
         if (_runWithEvents)
         {
             _eHelper.Verify(XObjectChange.Add, o);
         }
         TestLog.Compare(false, "Exception expected");
     }
     catch (InvalidOperationException)
     {
     }
 }
Ejemplo n.º 23
0
                //[Variation(Priority = 0, Desc = "XProcessingInstruction - Invalid Name")]
                public void InvalidPIVariation()
                {
                    _runWithEvents = (bool)Params[0];
                    XProcessingInstruction toChange = new XProcessingInstruction("target", "data");
                    if (_runWithEvents) _eHelper = new EventsHelper(toChange);

                    try
                    {
                        toChange.Target = null;
                    }
                    catch (Exception)
                    {
                        if (_runWithEvents) _eHelper.Verify(0);
                        return;
                    }

                    try
                    {
                        toChange.Target = " ";
                    }
                    catch (Exception)
                    {
                        if (_runWithEvents) _eHelper.Verify(0);

                        return;
                    }

                    try
                    {
                        toChange.Target = "";
                    }
                    catch (Exception)
                    {
                        if (_runWithEvents) _eHelper.Verify(0);
                        return;
                    }

                    throw new TestException(TestResult.Failed, "");
                }
Ejemplo n.º 24
0
 public void ExecuteXDocumentVariation()
 {
     XNode[] toAdd = Variation.Params[0] as XNode[];
     XNode contextNode = Variation.Params[1] as XNode;
     IEnumerable<XNode> toAddList = toAdd.OfType<XNode>();
     XDocument xDoc = new XDocument(contextNode);
     XDocument xDocOriginal = new XDocument(xDoc);
     using (UndoManager undo = new UndoManager(xDoc))
     {
         undo.Group();
         using (EventsHelper docHelper = new EventsHelper(xDoc))
         {
             using (EventsHelper nodeHelper = new EventsHelper(contextNode))
             {
                 contextNode.AddBeforeSelf(toAdd);
                 TestLog.Compare(toAddList.SequenceEqual(contextNode.NodesBeforeSelf(), XNode.EqualityComparer), "Nodes not added correctly!");
                 nodeHelper.Verify(0);
             }
             docHelper.Verify(XObjectChange.Add, toAdd);
         }
         undo.Undo();
         TestLog.Compare(XNode.DeepEquals(xDoc, xDocOriginal), "Undo did not work!");
     }
 }
Ejemplo n.º 25
0
        // From the same element 
        //   - all attributes
        //   - some attributes
        // From different elements - the same document
        // From different documents
        // Enumerable + nulls

        //[Variation(Priority = 1, Desc = "attributes from multiple elements")]

        //[Variation(Priority = 3, Desc = "Duplicate attribute in sequence")]

        public void DuplicateAttributeInside()
        {
            int count = 0;
            _runWithEvents = (bool)Params[0];
            XDocument doc = XDocument.Parse(@"<A id='a' xmlns:p1='nsp1'><B id='b' xmlns='nbs' xmlns:p='nsp' p:x='xx'>text</B><C/><p1:D id='x' datrt='dat'/></A>");
            IEnumerable<XAttribute> allAttributes = doc.Root.Attributes().Concat(doc.Root.Attributes());
            try
            {
                if (_runWithEvents)
                {
                    _eHelper = new EventsHelper(doc);
                    count = allAttributes.IsEmpty() ? 0 : allAttributes.Count();
                }
                allAttributes.Remove(); // should throw because of snapshot logic
                if (_runWithEvents)
                {
                    _eHelper.Verify(XObjectChange.Remove, count);
                }
                TestLog.Compare(false, "exception expected here");
            }
            catch (InvalidOperationException)
            {
            }
        }
Ejemplo n.º 26
0
 //[Variation(Priority = 0, Desc = "XComment - change value")]
 public void XCommentValue()
 {
     XComment toChange = new XComment("Original Value");
     String newValue = "New Value";
     XElement xElem = new XElement("root", toChange);
     XElement xElemOriginal = new XElement(xElem);
     using (UndoManager undo = new UndoManager(xElem))
     {
         undo.Group();
         using (EventsHelper eHelper = new EventsHelper(xElem))
         {
             using (EventsHelper comHelper = new EventsHelper(toChange))
             {
                 toChange.Value = newValue;
                 TestLog.Compare(toChange.Value.Equals(newValue), "Value did not change");
                 xElem.Verify();
                 comHelper.Verify(XObjectChange.Value, toChange);
             }
             eHelper.Verify(XObjectChange.Value, toChange);
         }
         undo.Undo();
         TestLog.Compare(XNode.DeepEquals(xElem, xElemOriginal), "Undo did not work!");
     }
 }
 public void InvalidAddIntoXDocument5()
 {
     _runWithEvents = (bool)Params[0];
     foreach (object o in new object[] { new XCData("CD"), new XAttribute("a1", "avalue"), "text1", new XText("text2"), new XDocument() })
     {
         try
         {
             var doc = new XDocument(new XElement("A"));
             if (_runWithEvents)
             {
                 _eHelper = new EventsHelper(doc);
             }
             doc.AddFirst(o);
             if (_runWithEvents)
             {
                 _eHelper.Verify(XObjectChange.Add, o);
             }
             TestLog.Compare(false, "Exception expected");
         }
         catch (ArgumentException)
         {
         }
     }
 }
Ejemplo n.º 28
0
 public void ExecuteXElementVariation()
 {
     XNode[] content = Variation.Params[0] as XNode[];
     int index = (int)Variation.Params[1];
     XElement xElem = new XElement("root", content);
     XNode toRemove = xElem.Nodes().ElementAt(index);
     XElement xElemOriginal = new XElement(xElem);
     using (UndoManager undo = new UndoManager(xElem))
     {
         undo.Group();
         using (EventsHelper elemHelper = new EventsHelper(xElem))
         {
             toRemove.Remove();
             xElem.Verify();
             elemHelper.Verify(XObjectChange.Remove, toRemove);
         }
         undo.Undo();
         TestLog.Compare(xElem.Nodes().SequenceEqual(xElemOriginal.Nodes(), XNode.EqualityComparer), "Undo did not work!");
         TestLog.Compare(xElem.Attributes().EqualsAllAttributes(xElemOriginal.Attributes(), Helpers.MyAttributeComparer), "Undo did not work!");
     }
 }
Ejemplo n.º 29
0
        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();
                    }
                }
            }
        }
        /// <summary>
        /// XElement:
        ///  - with content, empty
        ///  - adding valid nodes
        ///  - adding text, text concatenation, node identity
        ///  - adding nulls
        ///  - adding IEnumerable (order)
        /// </summary>
        public void AddIntoElement()
        {
            _runWithEvents = (bool)Params[0];
            var xml = Variation.Params[0] as string;
            var variationLength = (int)Variation.Params[1];
            var isConnected = (bool)Variation.Params[2];
            string stringOnlyContent = Variation.Params.Length > 3 ? Variation.Params[3] as string : null;

            object[] nodes = { new XElement("B"), new XElement(XNamespace.Get("ns1") + "B"), new XElement("B", new XElement("C"), new XAttribute("a", "aa")), new XProcessingInstruction("PI", "data"), new XComment("comment"), new XAttribute("xxx", "yyy"), new XAttribute("{a}xxx", "a_yyy"), new XAttribute("{b}xxx", "b_yyy"), "text plain", " ", "", null, new XText("xtext"), new XText(""), new XCData("xcdata") };

            if (isConnected)
            {
                foreach (XNode n in nodes.OfType<XNode>())
                {
                    new XDocument(new XElement("dumma", n));
                }
            }

            foreach (var toInsert in nodes.NonRecursiveVariations(variationLength))
            {
                XElement e = XElement.Parse(xml);
                string stringOnlyContentCopy = stringOnlyContent == null ? null : new string(stringOnlyContent.ToCharArray());

                List<ExpectedValue> expectedNodes = CalculateExpectedContent(e, toInsert, stringOnlyContentCopy).ProcessNodes().ToList();
                List<ExpectedValue> expectedAttributes = CalculateExpectedAttributes(e, toInsert, stringOnlyContentCopy).ToList();

                if (_runWithEvents)
                {
                    _eHelper = new EventsHelper(e);
                }
                e.Add(toInsert);

                TestLog.Compare(expectedNodes.EqualAll(e.Nodes(), XNode.EqualityComparer), "Add - content");
                TestLog.Compare(expectedAttributes.EqualAllAttributes(e.Attributes(), Helpers.MyAttributeComparer), "expectedAttributes.EqualAllAttributes(e.Attributes(), Helpers.MyAttributeComparer)");
                e.RemoveAll();
            }
        }