예제 #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
        protected virtual object VisitAndGroupBlock(AndGroupBlock block)
        {
            foreach (RegexBlock innerBlock in block.InnerBlocks)
            {
                Visit(innerBlock);
            }

            return block;
        }
예제 #3
0
        public IEnumerable<int> GetPositionsFromAndGroupBlock(int position, AndGroupBlock andGroupbBlock)
        {
            int groupId = _nextGroupId++;
            foreach (var nextPosition in GetPositionsFromBlocks(position, andGroupbBlock.InnerBlocks))
            {
                SetGroupValue(groupId, _input.Substring(position, nextPosition - position));
                yield return nextPosition;
            }

            _nextGroupId--;
        }
예제 #4
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");
        }
예제 #5
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");
        }
예제 #6
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());
        }
예제 #7
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 plusBlock1 = new OneOrMoreBlock(aBlock);
            var plusBlock2 = new OneOrMoreBlock(bBlock);

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

            var expected = new RegularExpression(groupBlock);

            var actual = parser.Parse(pattern);
            Assert.IsTrue(expected.Equals(actual), "Pattern was parsed incorrectly");
        }
예제 #8
0
        public void GetPositionsFromAndGroupBlockTest()
        {
            int patternLength = 4;
            var pattern = new string('\0', patternLength);
            var builder = new Checker(pattern);

            // (a|aa)(b|bb)
            var aBlock = new TextBlock("a");
            var aaBlock = new TextBlock("aa");
            var bBlock = new TextBlock("b");
            var bbBlock = new TextBlock("bb");
            var orGroupBlock1 = new OrGroupBlock(new RegexBlock[] { aBlock, aaBlock });
            var orGroupBlock2 = new OrGroupBlock(new RegexBlock [] { bBlock, bbBlock });
            var andGroupBlock = new AndGroupBlock(new[] { orGroupBlock1, orGroupBlock2 });

            var positions = builder.GetPositionsFromAndGroupBlock(0, andGroupBlock).ToList();

            CollectionAssert.AreEquivalent(new[] { 2, 3, 3, 4 }, positions);
        }
예제 #9
0
        public void GetLinesFromAndGroupBlockTest()
        {
            int patternLength = 4;
            var pattern = new string('\0', patternLength);
            LineBuilder builder = new LineBuilder(pattern, null);

            // (a|aa)(b|bb)

            TextBlock aBlock = new TextBlock("a");
            TextBlock aaBlock = new TextBlock("aa");
            TextBlock bBlock = new TextBlock("b");
            TextBlock bbBlock = new TextBlock("bb");
            OrGroupBlock orGroupBlock1 = new OrGroupBlock(new[] { aBlock, aaBlock });
            OrGroupBlock orGroupBlock2 = new OrGroupBlock(new[] { bBlock, bbBlock });
            AndGroupBlock andGroupBlock = new AndGroupBlock(new[] { orGroupBlock1, orGroupBlock2 });
            List<string> lines = builder.GetLinesFromAndGroupBlock(0, andGroupBlock).ToList();

            string[] expectedLines = new string[] { "ab", "abb", "aab", "aabb" };
            CollectionAssert.AreEquivalent(expectedLines, lines);
        }
예제 #10
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");
        }
예제 #11
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());
        }
예제 #12
0
        public void TestRealWorldExpression2()
        {
            int inputLength = 12;
            var input = new string('\0', inputLength);
            var builder = new LineBuilder(input, null);

            List<string> lines;

            var block1 = new TextBlock("c");
            var block2 = new TextBlock("mc");
            var block3 = new TextBlock("ccc");
            var block4 = new TextBlock("mm");

            var starBlock1 = new ZeroOrMoreBlock(block1);

            var orGroupBlock = new OrGroupBlock(new RegexBlock[] { block3, block4 });
            var starBlock2 = new ZeroOrMoreBlock(orGroupBlock);

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

            var regex = new RegularExpression(andGroupBlock);

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

            // Check against regular expression
            string regexPattern = "c*mc(ccc|mm)*";
            CheckGeneratedLines(lines, regexPattern);
        }
예제 #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 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());
        }
예제 #15
0
        public void ParseTest5()
        {
            var parser = new RegexParser();
            string pattern = "a+b";

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

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

            var expected = new RegularExpression(andGroupBlock);

            var actual = parser.Parse(pattern);
            actual.ShouldBeEquivalentTo(expected, options => options.IncludingAllRuntimeProperties());
        }
예제 #16
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);
            Assert.IsTrue(expected.Equals(actual), "Pattern was parsed incorrectly");
        }
예제 #17
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());
        }
예제 #18
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());
        }
예제 #19
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);
            Assert.IsTrue(expected.Equals(actual), "Pattern was parsed incorrectly");
        }