Esempio n. 1
0
        public void RemoveCommentsAndText_LargeRandomTest()
        {
            var elements = new[] { "'", "\"", "[", "]", "]]", "--", "/*", "*/", "-", "/", "*", "a", "b", "c", "\r\n" };

            for (int seed = 0; seed < 20; seed++)
            {
                var    sb     = new StringBuilder();
                Random random = new Random(seed);
                for (int c = 0; c < 10000; c++)
                {
                    sb.Append(elements[random.Next(elements.Length)]);
                }
                string sql = sb.ToString();

                string result            = SqlAnalysis.RemoveCommentsAndText(sql);
                string alternativeResult = RemoveCommentsAndText_AlternativeImplementation(sql);
                Console.WriteLine(string.Format("seed, sql, result, alternativeResult: {0}, {1}, {2}, {3}.", seed, sql.Length, result.Length, alternativeResult.Length));
                Assert.AreEqual(alternativeResult, result);
            }
        }
Esempio n. 2
0
        public void RemoveCommentsAndText_LargeRandomTest()
        {
            var elements = new[] { "'", "\"", "[", "]", "]]", "--", "/*", "*/", "-", "/", "*", "a", "b", "c", "\r\n" };

            for (int seed = 0; seed < 20; seed++)
            {
                var sb     = new StringBuilder();
                var random = new Random(seed);
                for (int c = 0; c < 10000; c++)
#pragma warning disable CA5394 // Do not use insecure randomness. The Random class is used here with a 'seed' to make the unit test reproducible.
                {
                    sb.Append(elements[random.Next(elements.Length)]);
                }
#pragma warning restore CA5394 // Do not use insecure randomness
                string sql = sb.ToString();

                string result            = SqlAnalysis.RemoveCommentsAndText(sql);
                string alternativeResult = RemoveCommentsAndText_AlternativeImplementation(sql);
                Console.WriteLine(string.Format("seed, sql, result, alternativeResult: {0}, {1}, {2}, {3}.", seed, sql.Length, result.Length, alternativeResult.Length));
                Assert.AreEqual(alternativeResult, result);
            }
        }
Esempio n. 3
0
        public void RemoveCommentsAndTextTest()
        {
            var tests = new Dictionary <string, string>()
            {
                { "", null },
                { " ab ", null },
                { "/a-a-a*/a*/", null },

                { "--", "" },
                { "-", null },
                { "a-", null },
                { "a-b", null },
                { "-b", null },
                { "a--b", "a" },
                { "1--2\r3--4\n5--6\r\n", "1\r3\n5\r\n" },
                { "--\r", "\r" },
                { "--\n", "\n" },
                { "---\n", "\n" },

                { "/**/", "" },
                { "/*/", "" },
                { "/*/1", "" },
                { "/*/1*/2", "2" },
                { "/**", "" },
                { "/", "/" },
                { "*", "*" },
                { "1/*2/*3*/4*/5", "15" },
                { "1/*\r/*\n*/\r\n*/2", "12" },
                { "\r\n1\r\n/*2*/\r\n3\r\n", "\r\n1\r\n\r\n3\r\n" },
                { "/***/1", "1" },
                { "/*//**/1*/2", "2" },

                { "''", "" },
                { "1'23'4", "14" },
                { "1'2''''3'''4", "14" },

                { "1/*2", "1" },
                { "1'2", "1" },
                { "1[2", null },
                { "1\"2", null },

                { "1/*2--3*/4--5", "14" },
                { "1--2/*3\r\n4*/", "1\r\n4*/" },
                { "1'2--3\r\n4'5", "15" },
                { "1'2/*3\r\n4'5*/", "15*/" },
                { "1'2[3'4]", "14]" },

                { "[]", null },
                { "1[2'3]4''5", "1[2'3]45" },
                { "1[2--3]4", null },
                { "1[2/*\r\n*/\r\n3]4", null },
                { "1[2]]]]3--4]]]5--6", "1[2]]]]3--4]]]5" },

                { "\"\"", null },
                { "\"1", null },
                { "1\"", null },
                { "1\"23\"4", null },
                { "1\"2/*3\"4*/", null },
                { "1\"2--3\"4", null },
                { "1\"2'3\"4", null },
                { "1\"2]3\"4", null },
                { "1\"2[3\"4/*1", "1\"2[3\"4" },
            };

            foreach (var test in tests)
            {
                string result            = SqlAnalysis.RemoveCommentsAndText(test.Key);
                string alternativeResult = RemoveCommentsAndText_AlternativeImplementation(test.Key);
                Assert.AreEqual(test.Value ?? test.Key, result, "Input: " + test.Key);
                Assert.AreEqual(alternativeResult, result, "Alternative implementation.");
            }
        }