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); }
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()); }
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"); }
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); }
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 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"); }
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); }
protected virtual object VisitZeroOrMoreBlock(ZeroOrMoreBlock block) { return Visit(block.InnerBlock); }
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()); }
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()); }
public IEnumerable<int> GetPositionsFromZeroOrMoreBlock(int position, ZeroOrMoreBlock block) { yield return position; foreach (var nextPosition in GetPositionsFromQuanitifierBlock(position, block)) { yield return nextPosition; } }
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"); }
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"); }
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); }