Example #1
0
        public void PokeChildren()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            XmlPoke p = new XmlPoke();

            p.BuildEngine  = engine;
            p.XmlInputPath = new TaskItem(xmlInputPath);
            p.Query        = "//class/.";
            p.Value        = new TaskItem("<Test>Testing</Test>");
            Assert.IsTrue(p.Execute(), engine.Log);

            string result;

            using (StreamReader sr = new StreamReader(xmlInputPath))
            {
                result = sr.ReadToEnd();

                Regex           r  = new Regex("<Test>Testing</Test>");
                MatchCollection mc = r.Matches(result);

                Assert.IsTrue(mc.Count == 1, "Should match once");
            }
        }
Example #2
0
        /// <summary>
        /// Executes an <see cref="XmlPoke"/> task with the specified arguments.
        /// </summary>
        /// <param name="query">The query to use.</param>
        /// <param name="useNamespace"><code>true</code> to use namespaces, otherwise <code>false</code> (Default).</param>
        /// <param name="value">The value to use.</param>
        /// <returns>An <see cref="XmlDocument"/> containing the resulting XML after the XmlPoke task has executed.</returns>
        private XmlDocument ExecuteXmlPoke(string query, bool useNamespace = false, string value = null)
        {
            MockEngine engine = new MockEngine(true);

            string xmlInputPath;

            Prepare(useNamespace ? _xmlFileWithNs : _xmlFileNoNs, out xmlInputPath);

            XmlPoke p = new XmlPoke
            {
                BuildEngine  = engine,
                XmlInputPath = new TaskItem(xmlInputPath),
                Query        = query,
                Namespaces   = useNamespace ? $"<Namespace Prefix=\"s\" Uri=\"{XmlNamespaceUsedByTests}\" />" : null,
                Value        = value == null ? null : new TaskItem(value)
            };

            Assert.True(p.Execute(), engine.Log);

            string result = File.ReadAllText(xmlInputPath);

            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.LoadXml(result);

            return(xmlDocument);
        }
Example #3
0
        public void PokeNoNamespace()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            XmlPoke p = new XmlPoke();

            p.BuildEngine  = engine;
            p.XmlInputPath = new TaskItem(xmlInputPath);
            p.Query        = "//variable/@Name";
            p.Value        = new TaskItem("Mert");
            p.Execute();

            List <int> positions = new List <int>();

            positions.AddRange(new int[] { 117, 172, 227 });

            string result;

            using (StreamReader sr = new StreamReader(xmlInputPath))
            {
                result = sr.ReadToEnd();
                Regex           r  = new Regex("Mert");
                MatchCollection mc = r.Matches(result);

                foreach (Match m in mc)
                {
                    Assert.IsTrue(positions.Contains(m.Index), "This test should effect 3 positions. There should be 3 occurances of 'Mert'\n" + result);
                }
            }
        }
Example #4
0
        public void PokeAttribute()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            XmlPoke p = new XmlPoke();

            p.BuildEngine  = engine;
            p.XmlInputPath = new TaskItem(xmlInputPath);
            p.Query        = "//class[1]/@AccessModifier";
            p.Value        = new TaskItem("<Test>Testing</Test>");
            p.Execute();
            string result;

            using (StreamReader sr = new StreamReader(xmlInputPath))
            {
                result = sr.ReadToEnd();
                Regex           r  = new Regex("AccessModifier=\"&lt;Test&gt;Testing&lt;/Test&gt;\"");
                MatchCollection mc = r.Matches(result);

                Assert.Equal(1, mc.Count); // "Should match once"
            }
        }
Example #5
0
        public void PokeMissingParams()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            for (int i = 0; i < 8; i++)
            {
                XmlPoke p = new XmlPoke();
                p.BuildEngine = engine;

                if ((i & 1) == 1)
                {
                    p.XmlInputPath = new TaskItem(xmlInputPath);
                }

                if ((i & 2) == 2)
                {
                    p.Query = "//variable/@Name";
                }

                if ((i & 4) == 4)
                {
                    p.Value = new TaskItem("Mert");
                }

                // "Expecting argumentnullexception for the first 7 tests"
                if (i < 7)
                {
                    Should.Throw <ArgumentNullException>(() => p.Execute());
                }
                else
                {
                    Should.NotThrow(() => p.Execute());
                }
            }
        }
Example #6
0
        public void PokeMissingParams()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            for (int i = 0; i < 8; i++)
            {
                XmlPoke p = new XmlPoke();
                p.BuildEngine = engine;

                if ((i & 1) == 1)
                {
                    p.XmlInputPath = new TaskItem(xmlInputPath);
                }

                if ((i & 2) == 2)
                {
                    p.Query = "//variable/@Name";
                }

                if ((i & 4) == 4)
                {
                    p.Value = new TaskItem("Mert");
                }

                bool exceptionThrown = false;
                try
                {
                    p.Execute();
                }
                catch (ArgumentNullException)
                {
                    exceptionThrown = true;
                }

                if (i < 7)
                {
                    Assert.True(exceptionThrown); // "Expecting argumentnullexception for the first 7 tests"
                }
                else
                {
                    Assert.False(exceptionThrown); // "Expecting argumentnullexception for the first 7 tests"
                }
            }
        }
Example #7
0
        public void PokeNoNSWPrefixedQueryError()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            XmlPoke p = new XmlPoke();

            p.BuildEngine = engine;

            p.XmlInputPath = new TaskItem(xmlInputPath);
            p.Query        = "//s:variable/@Name";
            p.Value        = new TaskItem("Nur");
            Assert.False(p.Execute()); // "Test should've failed"
            Assert.True(engine.Log.Contains("MSB3732"), "Engine log should contain error code MSB3732 " + engine.Log);
        }
Example #8
0
        public void PokeNoNSWPrefixedQueryError()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            XmlPoke p = new XmlPoke();

            p.BuildEngine = engine;

            p.XmlInputPath = new TaskItem(xmlInputPath);
            p.Query        = "//s:variable/@Name";
            p.Value        = new TaskItem("Nur");
            p.Execute().ShouldBeFalse(); // "Test should've failed"
            engine.AssertLogContains("MSB3732");
        }
Example #9
0
        public void ErrorInNamespaceDecl()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileWithNs, out xmlInputPath);

            XmlPoke p = new XmlPoke();

            p.BuildEngine  = engine;
            p.XmlInputPath = new TaskItem(xmlInputPath);
            p.Query        = "//s:variable/@Name";
            p.Namespaces   = "<!THIS IS ERROR Namespace Prefix=\"s\" Uri=\"http://nsurl\" />";
            p.Namespaces.ShouldBe("<!THIS IS ERROR Namespace Prefix=\"s\" Uri=\"http://nsurl\" />");
            p.Value = new TaskItem("Nur");

            p.Execute().ShouldBeFalse(); // "Execution should've failed"
            engine.AssertLogContains("MSB3731");
        }
Example #10
0
        public void PokeElement()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileNoNs, out xmlInputPath);

            XmlPoke p = new XmlPoke();

            p.BuildEngine  = engine;
            p.XmlInputPath = new TaskItem(xmlInputPath);
            Assert.IsTrue(p.XmlInputPath.ItemSpec.Equals(xmlInputPath));
            p.Query = "//variable/.";
            Assert.IsTrue(p.Query.Equals("//variable/."));
            string valueString = "<testing the=\"element\">With<somewhat complex=\"value\" /></testing>";

            p.Value = new TaskItem(valueString);
            Assert.IsTrue(p.Value.ItemSpec.Equals(valueString));

            Assert.IsTrue(p.Execute());

            List <int> positions = new List <int>();

            positions.AddRange(new int[] { 126, 249, 372 });

            string result;

            using (StreamReader sr = new StreamReader(xmlInputPath))
            {
                result = sr.ReadToEnd();

                Regex           r  = new Regex("<testing the=\"element\">With<somewhat complex=\"value\" /></testing>");
                MatchCollection mc = r.Matches(result);

                foreach (Match m in mc)
                {
                    Assert.IsTrue(positions.Contains(m.Index), "This test should effect 3 positions. There should be 3 occurances of 'Mert'\n" + result);
                }
            }
        }
Example #11
0
        public void MissingNamespaceParameters()
        {
            MockEngine engine = new MockEngine(true);
            string     xmlInputPath;

            Prepare(_xmlFileWithNs, out xmlInputPath);

            string[] attrs = new string[] { "Prefix=\"s\"", "Uri=\"http://nsurl\"" };
            for (int i = 0; i < Math.Pow(2, attrs.Length); i++)
            {
                string res = "";
                for (int k = 0; k < attrs.Length; k++)
                {
                    if ((i & (int)Math.Pow(2, k)) != 0)
                    {
                        res += attrs[k] + " ";
                    }
                }
                XmlPoke p = new XmlPoke();
                p.BuildEngine  = engine;
                p.XmlInputPath = new TaskItem(xmlInputPath);
                p.Query        = "//s:variable/@Name";
                p.Namespaces   = "<Namespace " + res + " />";
                p.Value        = new TaskItem("Nur");

                bool result = p.Execute();

                if (i == 3)
                {
                    Assert.True(result); // "Only 3rd value should pass."
                }
                else
                {
                    Assert.False(result); // "Only 3rd value should pass."
                }
            }
        }