예제 #1
0
        public void GetLinesTestWithEmptyInput()
        {
            int inputLength = 3;
            var input = new string('\0', inputLength);
            var builder = new LineBuilder(input, null);

            List<string> lines;

            // a+b+ - should be abb, aab
            var aBlock = new TextBlock("a");
            var bBlock = new TextBlock("b");
            var plusBlock1 = new OneOrMoreBlock(aBlock);
            var plusBlock2 = new OneOrMoreBlock(bBlock);

            var groupBlock = new AndGroupBlock(new RegexBlock[] { plusBlock1, plusBlock2 });

            var regex1 = new RegularExpression(groupBlock);

            lines = builder.GetLines(regex1).ToList();
            string[] expectedLines = new string[] { "abb", "aab" };
            CollectionAssert.AreEquivalent(expectedLines, lines);

            // a*b* - should be bbb, abb, aab, aaa
            var starBlock1 = new ZeroOrMoreBlock(aBlock);
            var starBlock2 = new ZeroOrMoreBlock(bBlock);

            groupBlock = new AndGroupBlock(new RegexBlock[] { starBlock1, starBlock2 });

            var regex2 = new RegularExpression(groupBlock);

            lines = builder.GetLines(regex2).ToList();
            expectedLines = new string[] { "bbb", "abb", "aab", "aaa" };
            CollectionAssert.AreEquivalent(expectedLines, lines);
        }
예제 #2
0
        public void ParseTest10()
        {
            var parser = new RegexParser();
            string pattern = ".*a.*";

            // Expected regular expression
            var anyCharacterBlock = new AnyCharacterBlock();
            var zeroOrOneBlock = new ZeroOrMoreBlock(anyCharacterBlock);
            var textBlock = new TextBlock("a");
            var groupBlock = new AndGroupBlock(new RegexBlock[] { zeroOrOneBlock, textBlock, zeroOrOneBlock });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
예제 #3
0
        public void ParseTest10()
        {
            var parser = new RegexParser();
            string pattern = ".*a.*";

            // Expected regular expression
            var anyCharacterBlock = new AnyCharacterBlock();
            var zeroOrOneBlock = new ZeroOrMoreBlock(anyCharacterBlock);
            var textBlock = new TextBlock("a");
            var groupBlock = new AndGroupBlock(new RegexBlock[] { zeroOrOneBlock, textBlock, zeroOrOneBlock });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            Assert.IsTrue(expected.Equals(actual), "Pattern was parsed incorrectly");
        }
예제 #4
0
        public void GePostionsFromZeroOrMoreBlockTest()
        {
            var pattern = "a\0\0\0";
            var builder = new Checker(pattern);
            var textBlock = new TextBlock("a");
            var zeroOrMoreBlock = new ZeroOrMoreBlock(textBlock);

            var positions = builder.GetPositionsFromZeroOrMoreBlock(0, zeroOrMoreBlock).ToList();
            CollectionAssert.AreEquivalent(Enumerable.Range(0, 5).ToArray(), positions);

            var aBlock = new TextBlock("a");
            var bBlock = new TextBlock("b");
            var orGroupBlock = new OrGroupBlock(new[] { aBlock, bBlock });
            zeroOrMoreBlock = new ZeroOrMoreBlock(orGroupBlock);
            positions = builder.GetPositionsFromZeroOrMoreBlock(0, zeroOrMoreBlock).ToList();
            Assert.AreEqual(16, positions.Count);
        }
예제 #5
0
        public void ParseTest11()
        {
            var parser = new RegexParser();
            string pattern = @".+(.)(.)(.)(.)\4\3\2\1.*";

            // Expected regular expression
            var anyCharacterBlock = new AnyCharacterBlock();
            var oneOrMoreBlock = new OneOrMoreBlock(anyCharacterBlock);
            var zeroOrOneBlock = new ZeroOrMoreBlock(anyCharacterBlock);
            var andGroupBlock = new AndGroupBlock(new [] { anyCharacterBlock });
            var b4 = new BackreferenceBlock(4);
            var b3 = new BackreferenceBlock(3);
            var b2 = new BackreferenceBlock(2);
            var b1 = new BackreferenceBlock(1);
            var groupBlock = new AndGroupBlock(new RegexBlock[] { oneOrMoreBlock, andGroupBlock, andGroupBlock, andGroupBlock, andGroupBlock, b4, b3, b2, b1, zeroOrOneBlock });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
예제 #6
0
        public void ParseTest11()
        {
            var parser = new RegexParser();
            string pattern = @".+(.)(.)(.)(.)\4\3\2\1.*";

            // Expected regular expression
            var anyCharacterBlock = new AnyCharacterBlock();
            var oneOrMoreBlock = new OneOrMoreBlock(anyCharacterBlock);
            var zeroOrOneBlock = new ZeroOrMoreBlock(anyCharacterBlock);
            var andGroupBlock = new AndGroupBlock(new [] { anyCharacterBlock });
            var b4 = new BackreferenceBlock(4);
            var b3 = new BackreferenceBlock(3);
            var b2 = new BackreferenceBlock(2);
            var b1 = new BackreferenceBlock(1);
            var groupBlock = new AndGroupBlock(new RegexBlock[] { oneOrMoreBlock, andGroupBlock, andGroupBlock, andGroupBlock, andGroupBlock, b4, b3, b2, b1, zeroOrOneBlock });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            Assert.IsTrue(expected.Equals(actual), "Pattern was parsed incorrectly");
        }
예제 #7
0
        public void TestRealWorldExpression1()
        {
            int inputLength = 10;
            var input = new string('\0', inputLength);
            var builder = new LineBuilder(input, null);

            List<string> lines;

            var block1 = new TextBlock("o");
            var block2 = new TextBlock("rhh");
            var block3 = new TextBlock("mm");

            var orGroupBlock = new OrGroupBlock(new RegexBlock[] { block1, block2, block3 });
            var starBlock = new ZeroOrMoreBlock(orGroupBlock);
            var andGroupBlock = new OrGroupBlock(new RegexBlock[] { starBlock });
            var regex = new RegularExpression(andGroupBlock);

            lines = builder.GetLines(regex).ToList();

            // Check against regular expression
            string regexPattern = "(o|rhh|mm)*";
            CheckGeneratedLines(lines, regexPattern);
        }
예제 #8
0
 protected virtual object VisitZeroOrMoreBlock(ZeroOrMoreBlock block)
 {
     return Visit(block.InnerBlock);
 }
예제 #9
0
        public void ParseTest4()
        {
            var parser = new RegexParser();
            string pattern = "(a+)*";

            // Expected regular expression
            var aBlock = new TextBlock("a");

            var plusBlock = new OneOrMoreBlock(aBlock);
            var andGroupBlock = new AndGroupBlock(new[] { plusBlock });
            var starBlock = new ZeroOrMoreBlock(andGroupBlock);
            var andGroupBlock2 = new AndGroupBlock(new[] { starBlock });
            var expected = new RegularExpression(andGroupBlock2);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
예제 #10
0
        public void ParseTest2()
        {
            var parser = new RegexParser();
            string pattern = "a*b*";

            // Expected regular expression
            var aBlock = new TextBlock("a");
            var bBlock = new TextBlock("b");

            var starBlock1 = new ZeroOrMoreBlock(aBlock);
            var starBlock2 = new ZeroOrMoreBlock(bBlock);

            var groupBlock = new AndGroupBlock(new RegexBlock[] { starBlock1, starBlock2 });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
예제 #11
0
 public IEnumerable<int> GetPositionsFromZeroOrMoreBlock(int position, ZeroOrMoreBlock block)
 {
     yield return position;
     foreach (var nextPosition in GetPositionsFromQuanitifierBlock(position, block))
     {
         yield return nextPosition;
     }
 }
예제 #12
0
        public void ParseTest2()
        {
            var parser = new RegexParser();
            string pattern = "a*b*";

            // Expected regular expression
            var aBlock = new TextBlock("a");
            var bBlock = new TextBlock("b");

            var starBlock1 = new ZeroOrMoreBlock(aBlock);
            var starBlock2 = new ZeroOrMoreBlock(bBlock);

            var groupBlock = new AndGroupBlock(new RegexBlock[] { starBlock1, starBlock2 });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            Assert.IsTrue(expected.Equals(actual), "Pattern was parsed incorrectly");
        }
예제 #13
0
        public void ParseTest4()
        {
            var parser = new RegexParser();
            string pattern = "(a+)*";

            // Expected regular expression
            var aBlock = new TextBlock("a");

            var plusBlock = new OneOrMoreBlock(aBlock);
            var andGroupBlock = new AndGroupBlock(new[] { plusBlock });
            var starBlock = new ZeroOrMoreBlock(andGroupBlock);
            var andGroupBlock2 = new AndGroupBlock(new[] { starBlock });
            var expected = new RegularExpression(andGroupBlock2);

            var actual = parser.Parse(pattern);
            Assert.IsTrue(expected.Equals(actual), "Pattern was parsed incorrectly");
        }
예제 #14
0
        public void GetLinesFromZeroOrMoreBlockTest()
        {
            int patternLength = 4;
            var pattern = new string('\0', patternLength);
            LineBuilder builder = new LineBuilder(pattern, null);
            string text = "a";
            var textBlock = new TextBlock(text);
            var zeroOrMoreBlock = new ZeroOrMoreBlock(textBlock);

            List<string> lines = builder.GetLinesFromZeroOrMoreBlock(0, zeroOrMoreBlock).ToList();

            Assert.AreEqual(patternLength + 1, lines.Count);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < patternLength + 1; i++)
            {
                Assert.AreEqual(sb.ToString(), lines[i]);
                sb.Append(text);
            }

            TextBlock aBlock = new TextBlock("a");
            TextBlock bBlock = new TextBlock("b");
            OrGroupBlock orGroupBlock = new OrGroupBlock(new[] { aBlock, bBlock });
            zeroOrMoreBlock = new ZeroOrMoreBlock(orGroupBlock);
            lines = builder.GetLinesFromZeroOrMoreBlock(0, zeroOrMoreBlock).ToList();
            Assert.AreEqual(31, lines.Count);
        }