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);
        }
Beispiel #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());
        }
Beispiel #3
0
        public void ParseTest1()
        {
            var parser = new RegexParser();
            string pattern = "a+b+";

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

            var groupBlock = new AndGroupBlock(new RegexBlock[] { new OneOrMoreBlock(aBlock), new OneOrMoreBlock(bBlock) });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected,  options => options.IncludingAllRuntimeProperties(), "Pattern has been parsed incorrectly");
        }
Beispiel #4
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());
        }
        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);
        }
Beispiel #6
0
 protected object Visit(RegularExpression expression)
 {
     return Visit(expression.InnerBlock);
 }
Beispiel #7
0
        public void ParseTest9()
        {
            var parser = new RegexParser();
            string pattern = "[^abc]";

            // Expected regular expression
            var setBlock = new ExclusiveSetBlock("abc");
            var groupBlock = new AndGroupBlock(new RegexBlock[] { setBlock });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
Beispiel #8
0
        public void ParseTest6()
        {
            var parser = new RegexParser();
            string pattern = "(a|b)";

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

            var orGroupBlock = new OrGroupBlock(new RegexBlock[] { aBlock, bBlock });

            var expected = new RegularExpression(orGroupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
Beispiel #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());
        }
Beispiel #10
0
        public void ParseTest3()
        {
            var parser = new RegexParser();
            string pattern = "(ab|bc)+";

            // Expected regular expression
            var abBlock = new TextBlock("ab");
            var bcBlock = new TextBlock("bc");

            var orGroupBlock = new OrGroupBlock(new RegexBlock[] { abBlock, bcBlock });
            var plusBlock = new OneOrMoreBlock(orGroupBlock);
            var andGroupBlock = new AndGroupBlock(new[] { plusBlock });
            var expected = new RegularExpression(andGroupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
Beispiel #11
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());
        }
Beispiel #12
0
        public void ParseTest14()
        {
            var parser = new RegexParser();
            string pattern = @"[^c]*[^r]*iii.*";

            // Expected regular expression
            var textBlock = new TextBlock("iii");

            var groupBlock =
                new AndGroupBlock(
                    new RegexBlock[]
                        {
                            new ZeroOrMoreBlock(new ExclusiveSetBlock("c")), new ZeroOrMoreBlock(new ExclusiveSetBlock("r")), textBlock,
                            new ZeroOrMoreBlock(new AnyCharacterBlock())
                        });

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
Beispiel #13
0
 public bool Check(RegularExpression expression)
 {
     return GetPositions(0, expression.InnerBlock).Any(position => position == _input.Length);
 }
 public CrosswordQuestion(RegularExpression expression, string pattern)
 {
     Expression = expression;
     _pattern = pattern;
 }