Ejemplo n.º 1
0
        static void AssertHtmlAttributeEncode(string text, string expected)
        {
            string encoded;

            encoded = HtmlUtils.HtmlAttributeEncode(text);
            Assert.AreEqual(expected, encoded, "HtmlAttributeEncode(string)");

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlAttributeEncode(writer, text);
                encoded = writer.ToString();
                Assert.AreEqual(expected, encoded, "HtmlAttributeEncode(TextWriter,string)");
            }

            encoded = HtmlUtils.HtmlAttributeEncode(text, 0, text.Length);
            Assert.AreEqual(expected, encoded, "HtmlAttributeEncode(string,int,int)");

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlAttributeEncode(writer, text, 0, text.Length);
                encoded = writer.ToString();
                Assert.AreEqual(expected, encoded, "HtmlAttributeEncode(TextWriter,string,int,int)");
            }

            encoded = HtmlUtils.HtmlAttributeEncode(text.ToCharArray(), 0, text.Length);
            Assert.AreEqual(expected, encoded, "HtmlAttributeEncode(char[],int,int)");

            using (var writer = new StringWriter()) {
                HtmlUtils.HtmlAttributeEncode(writer, text.ToCharArray(), 0, text.Length);
                encoded = writer.ToString();
                Assert.AreEqual(expected, encoded, "HtmlAttributeEncode(TextWriter,char[],int,int)");
            }
        }
Ejemplo n.º 2
0
        public void TestArgumentExceptions()
        {
            var          writer = new StringWriter();
            const string text   = "text";

            // HtmlAttributeEncode
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(text, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null, text));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(writer, null));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode((string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text, 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(text, 0, text.Length, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode((char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(text.ToCharArray(), 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(text.ToCharArray(), 0, text.Length, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null, text, 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(writer, (string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(writer, text, 0, text.Length, 'x'));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(null, text.ToCharArray(), 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlAttributeEncode(writer, (char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlAttributeEncode(writer, text.ToCharArray(), 0, text.Length + 1));
            Assert.Throws <ArgumentException> (() => HtmlUtils.HtmlAttributeEncode(writer, text.ToCharArray(), 0, text.Length, 'x'));

            // HtmlEncode
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null, text));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(writer, null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode((string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text, 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode((char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(text.ToCharArray(), 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null, text, 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(writer, (string)null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text, 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(null, text.ToCharArray(), 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlEncode(writer, (char[])null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text.ToCharArray(), -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlEncode(writer, text.ToCharArray(), 0, text.Length + 1));

            // HtmlDecode
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null, text));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(writer, null));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(text, 0, text.Length + 1));

            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(null, text, 0, text.Length));
            Assert.Throws <ArgumentNullException> (() => HtmlUtils.HtmlDecode(writer, null, 0, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(writer, text, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => HtmlUtils.HtmlDecode(writer, text, 0, text.Length + 1));
        }