public void TestConditionalMatchNodeRendering()
        {
            RegexNodeLiteral condition  = new RegexNodeLiteral(@"\w\s");
            RegexNodeLiteral trueMatch  = new RegexNodeLiteral(@"\w\s\d{5}-[^\u005c]+");
            RegexNodeLiteral falseMatch = new RegexNodeLiteral(@"\d{5,}\u005d");

            RegexNodeConditionalMatch conditionalMatch1 = new RegexNodeConditionalMatch(condition, trueMatch, falseMatch);

            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch1.ToRegexPattern());

            conditionalMatch1.Quantifier = RegexQuantifier.OneOrMore;
            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch1.ToRegexPattern());

            RegexNodeConditionalMatch conditionalMatch2 = new RegexNodeConditionalMatch("SomeGroup", trueMatch, falseMatch);

            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch2.ToRegexPattern());

            conditionalMatch2.Quantifier = RegexQuantifier.OneOrMore;
            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch2.ToRegexPattern());

            RegexNodeConditionalMatch conditionalMatch3 = RegexBuilder.ConditionalMatch(condition, trueMatch, falseMatch);

            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch3.ToRegexPattern());

            conditionalMatch3 = RegexBuilder.ConditionalMatch(condition, trueMatch, falseMatch, RegexQuantifier.OneOrMore);
            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch3.ToRegexPattern());

            RegexNodeConditionalMatch conditionalMatch4 = RegexBuilder.ConditionalMatch("SomeGroup", trueMatch, falseMatch);

            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch4.ToRegexPattern());

            conditionalMatch4 = RegexBuilder.ConditionalMatch("SomeGroup", trueMatch, falseMatch, RegexQuantifier.OneOrMore);
            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch4.ToRegexPattern());
        }
Exemplo n.º 2
0
        public void TestRegexNodeAdditionOperator6()
        {
            RegexNodeLiteral literal = new RegexNodeLiteral("\\w*");
            RegexNode        sum     = null + literal;

            Assert.IsNull(sum);
        }
        public void TestAlternationNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral(@"\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral(@"\d+");
            RegexNodeLiteral literal3 = new RegexNodeLiteral(@"\s?");

            RegexNodeAlternation alternation1 = new RegexNodeAlternation(literal1, literal2);
            Assert.AreEqual(@"(?:\w*|\d+)", alternation1.ToRegexPattern());

            RegexNodeAlternation alternation2 = new RegexNodeAlternation(literal1, literal2, literal3);
            Assert.AreEqual(@"(?:\w*|\d+|\s?)", alternation2.ToRegexPattern());

            RegexNodeAlternation alternation3 = RegexBuilder.Alternate(literal1, literal2);
            Assert.AreEqual(@"(?:\w*|\d+)", alternation3.ToRegexPattern());

            RegexNodeAlternation alternation4 = RegexBuilder.Alternate(new RegexNode[] { literal1, literal2, literal3 });
            Assert.AreEqual(@"(?:\w*|\d+|\s?)", alternation4.ToRegexPattern());

            RegexNodeAlternation alternation5 = new RegexNodeAlternation(literal1, literal2);
            alternation5.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w*|\d+)*", alternation5.ToRegexPattern());

            RegexNodeAlternation alternation6 = new RegexNodeAlternation(literal1, literal2, literal3);
            alternation6.Quantifier = RegexQuantifier.OneOrMore;
            Assert.AreEqual(@"(?:\w*|\d+|\s?)+", alternation6.ToRegexPattern());

            RegexNodeAlternation alternation7 = RegexBuilder.Alternate(literal1, literal2, RegexQuantifier.ZeroOrOne);
            Assert.AreEqual(@"(?:\w*|\d+)?", alternation7.ToRegexPattern());

            RegexNodeAlternation alternation8 = RegexBuilder.Alternate(new RegexNode[] { literal1, literal2, literal3 }, RegexQuantifier.AtLeast(5));
            Assert.AreEqual(@"(?:\w*|\d+|\s?){5,}", alternation8.ToRegexPattern());
        }
        public void TestLiteralNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral(@"\w\b.353\s");

            Assert.AreEqual(@"\w\b.353\s", literal1.ToRegexPattern());

            literal1.Quantifier = RegexQuantifier.ZeroOrOne;
            Assert.AreEqual(@"(?:\w\b.353\s)?", literal1.ToRegexPattern());

            RegexNodeLiteral literal2 = RegexBuilder.NonEscapedLiteral(@"\w\b.353\s");

            Assert.AreEqual(@"\w\b.353\s", literal2.ToRegexPattern());

            RegexNodeLiteral literal3 = RegexBuilder.NonEscapedLiteral(@"\w\b.353\s", RegexQuantifier.ZeroOrOne);

            Assert.AreEqual(@"(?:\w\b.353\s)?", literal3.ToRegexPattern());

            RegexNodeLiteral literal4 = RegexBuilder.NonEscapedLiteral(@"\w", RegexQuantifier.ZeroOrOne);

            Assert.AreEqual(@"\w?", literal4.ToRegexPattern());

            RegexNodeLiteral literal5 = RegexBuilder.NonEscapedLiteral(@"a", RegexQuantifier.ZeroOrMore);

            Assert.AreEqual(@"a*", literal5.ToRegexPattern());
        }
Exemplo n.º 5
0
        public void TestQuantifierSetterForNodesWithQuantifierAllowed()
        {
            RegexNode       node       = new RegexNodeLiteral("Value");
            RegexQuantifier quantifier = RegexQuantifier.OneOrMore;

            node.Quantifier = quantifier;
            Assert.AreEqual(quantifier, node.Quantifier);
        }
Exemplo n.º 6
0
        public void TestConditionalMatchNodeNullAssignment1()
        {
            RegexNodeLiteral          condition        = new RegexNodeLiteral("a");
            RegexNodeLiteral          trueMatch        = new RegexNodeLiteral("b");
            RegexNodeConditionalMatch conditionalMatch = new RegexNodeConditionalMatch(condition, trueMatch, null);

            Assert.IsNull(conditionalMatch);
        }
Exemplo n.º 7
0
        public void TestConditionalMatchNodeNullAssignment2()
        {
            RegexNodeLiteral          condition        = new RegexNodeLiteral("a");
            RegexNodeLiteral          falseMatch       = new RegexNodeLiteral("c");
            RegexNodeConditionalMatch conditionalMatch = new RegexNodeConditionalMatch(condition, null, falseMatch);

            Assert.IsNull(conditionalMatch);
        }
Exemplo n.º 8
0
        public void TestConditionalMatchNodeNullAssignment3()
        {
            RegexNodeLiteral          condition        = new RegexNodeLiteral("a");
            RegexNodeLiteral          trueMatch        = new RegexNodeLiteral("b");
            RegexNodeLiteral          falseMatch       = new RegexNodeLiteral("c");
            RegexNodeConditionalMatch conditionalMatch = new RegexNodeConditionalMatch(condition, trueMatch, falseMatch);

            conditionalMatch.TrueMatchExpression  = null;
            conditionalMatch.FalseMatchExpression = null;
            Assert.IsNull(conditionalMatch);
        }
Exemplo n.º 9
0
        public void TestRegexNodeAdditionOperator1()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("\\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral("\\d+");

            RegexNode sum = literal1 + literal2;

            Assert.IsInstanceOfType(sum, typeof(RegexNodeConcatenation));
            Assert.AreEqual(literal1, ((RegexNodeConcatenation)sum).ChildNodes[0]);
            Assert.AreEqual(literal2, ((RegexNodeConcatenation)sum).ChildNodes[1]);
        }
Exemplo n.º 10
0
        public void TestAlternationNodeConstruction()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("a");
            RegexNodeLiteral literal2 = new RegexNodeLiteral("b");
            RegexNodeAlternation alternation = new RegexNodeAlternation(literal1, literal2);

            List<RegexNode> expressions = new List<RegexNode>(alternation.Expressions);
            Assert.AreEqual(expressions.Count, 2);
            Assert.AreEqual(expressions[0], literal1);
            Assert.AreEqual(expressions[1], literal2);
        }
Exemplo n.º 11
0
        public void TestAlternationNodeConstruction()
        {
            RegexNodeLiteral     literal1    = new RegexNodeLiteral("a");
            RegexNodeLiteral     literal2    = new RegexNodeLiteral("b");
            RegexNodeAlternation alternation = new RegexNodeAlternation(literal1, literal2);

            List <RegexNode> expressions = new List <RegexNode>(alternation.Expressions);

            Assert.AreEqual(expressions.Count, 2);
            Assert.AreEqual(expressions[0], literal1);
            Assert.AreEqual(expressions[1], literal2);
        }
        public void TestLookAroundNodeRendering()
        {
            RegexNodeLiteral lookupExpression = new RegexNodeLiteral(@"a\bc");
            RegexNodeLiteral matchExpression  = new RegexNodeLiteral(@"\w+");

            RegexNodeLookAround positiveLookAhead = new RegexNodeLookAround(RegexLookAround.PositiveLookAhead, lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:\w+(?=a\bc))", positiveLookAhead.ToRegexPattern());
            positiveLookAhead.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w+(?=a\bc))*", positiveLookAhead.ToRegexPattern());

            RegexNodeLookAround positiveLookBehind = new RegexNodeLookAround(RegexLookAround.PositiveLookBehind, lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:(?<=a\bc)\w+)", positiveLookBehind.ToRegexPattern());
            positiveLookBehind.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:(?<=a\bc)\w+)*", positiveLookBehind.ToRegexPattern());

            RegexNodeLookAround negativeLookAhead = new RegexNodeLookAround(RegexLookAround.NegativeLookAhead, lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:\w+(?!a\bc))", negativeLookAhead.ToRegexPattern());
            negativeLookAhead.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w+(?!a\bc))*", negativeLookAhead.ToRegexPattern());

            RegexNodeLookAround negativeLookBehind = new RegexNodeLookAround(RegexLookAround.NegativeLookBehind, lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:(?<!a\bc)\w+)", negativeLookBehind.ToRegexPattern());
            negativeLookBehind.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:(?<!a\bc)\w+)*", negativeLookBehind.ToRegexPattern());

            RegexNodeLookAround positiveLookAhead2 = RegexBuilder.PositiveLookAhead(lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:\w+(?=a\bc))", positiveLookAhead2.ToRegexPattern());
            positiveLookAhead2 = RegexBuilder.PositiveLookAhead(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:\w+(?=a\bc))*", positiveLookAhead2.ToRegexPattern());

            RegexNodeLookAround positiveLookBehind2 = RegexBuilder.PositiveLookBehind(lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:(?<=a\bc)\w+)", positiveLookBehind2.ToRegexPattern());
            positiveLookBehind2 = RegexBuilder.PositiveLookBehind(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:(?<=a\bc)\w+)*", positiveLookBehind2.ToRegexPattern());

            RegexNodeLookAround negativeLookAhead2 = RegexBuilder.NegativeLookAhead(lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:\w+(?!a\bc))", negativeLookAhead2.ToRegexPattern());
            negativeLookAhead2 = RegexBuilder.NegativeLookAhead(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:\w+(?!a\bc))*", negativeLookAhead2.ToRegexPattern());

            RegexNodeLookAround negativeLookBehind2 = RegexBuilder.NegativeLookBehind(lookupExpression, matchExpression);

            Assert.AreEqual(@"(?:(?<!a\bc)\w+)", negativeLookBehind2.ToRegexPattern());
            negativeLookBehind2 = RegexBuilder.NegativeLookBehind(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:(?<!a\bc)\w+)*", negativeLookBehind2.ToRegexPattern());
        }
Exemplo n.º 13
0
        public void TestConcatenationNodeNullChildAssignment2()
        {
            RegexNodeLiteral       literal1      = new RegexNodeLiteral("a");
            RegexNodeLiteral       literal2      = new RegexNodeLiteral("b");
            RegexNodeConcatenation concatenation = new RegexNodeConcatenation(literal1, literal2);

            Assert.AreEqual(2, concatenation.ChildNodes.Count);

            // ReSharper disable RedundantAssignment
            // -- This line is expected to cause an exception.
            concatenation = new RegexNodeConcatenation(null);
            // ReSharper restore RedundantAssignment
        }
        public void TestConcatenationNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral(@"\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral(@"\d+");
            RegexNodeLiteral literal3 = new RegexNodeLiteral(@"\s?");
            RegexNodeLiteral literal4 = new RegexNodeLiteral(@"\t");

            RegexNodeConcatenation concatenation1 = new RegexNodeConcatenation(literal1, literal2);

            Assert.AreEqual(@"\w*\d+", concatenation1.ToRegexPattern());

            RegexNodeConcatenation concatenation2 = new RegexNodeConcatenation(new List <RegexNode>(new[] { literal1, literal2 }));

            Assert.AreEqual(@"\w*\d+", concatenation2.ToRegexPattern());

            RegexNodeConcatenation concatenation3 = new RegexNodeConcatenation(literal1, literal2);

            concatenation3.ChildNodes.Add(literal3);
            Assert.AreEqual(@"\w*\d+\s?", concatenation3.ToRegexPattern());

            concatenation3.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w*\d+\s?)*", concatenation3.ToRegexPattern());

            concatenation3            = new RegexNodeConcatenation(new List <RegexNode>(new[] { literal4, literal2, literal3, literal1 }));
            concatenation3.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\t\d+\s?\w*)*", concatenation3.ToRegexPattern());

            RegexNodeConcatenation concatenation4 = RegexBuilder.Concatenate(literal1, literal2);

            Assert.AreEqual(@"\w*\d+", concatenation4.ToRegexPattern());

            RegexNodeConcatenation concatenation5 = RegexBuilder.Concatenate(literal1, literal2, RegexQuantifier.ZeroOrOne);

            Assert.AreEqual(@"(?:\w*\d+)?", concatenation5.ToRegexPattern());

            RegexNodeConcatenation concatenation6 = RegexBuilder.Concatenate(literal1, literal2, literal3);

            Assert.AreEqual(@"\w*\d+\s?", concatenation6.ToRegexPattern());

            RegexNodeConcatenation concatenation7 = RegexBuilder.Concatenate(literal1, literal2, literal3, RegexQuantifier.ZeroOrMore);

            Assert.AreEqual(@"(?:\w*\d+\s?)*", concatenation7.ToRegexPattern());

            RegexNodeConcatenation concatenation8 = RegexBuilder.Concatenate(literal1, literal2, literal3, literal4);

            Assert.AreEqual(@"\w*\d+\s?\t", concatenation8.ToRegexPattern());

            RegexNodeConcatenation concatenation9 = RegexBuilder.Concatenate(literal1, literal2, literal3, literal4, RegexQuantifier.OneOrMore);

            Assert.AreEqual(@"(?:\w*\d+\s?\t)+", concatenation9.ToRegexPattern());
        }
Exemplo n.º 15
0
        public void TestRegexNodeAdditionOperator3()
        {
            RegexNodeLiteral       literal1      = new RegexNodeLiteral("\\w*");
            RegexNodeLiteral       literal2      = new RegexNodeLiteral("\\d+");
            RegexNodeConcatenation concatenation = new RegexNodeConcatenation(literal1, literal2);

            RegexNodeLiteral literal3 = new RegexNodeLiteral("\\s?");
            RegexNode        sum      = literal3 + concatenation;

            Assert.IsInstanceOfType(sum, typeof(RegexNodeConcatenation));
            Assert.AreNotSame(concatenation, sum);
            Assert.AreEqual(literal3, ((RegexNodeConcatenation)sum).ChildNodes[0]);
            Assert.AreEqual(literal1, ((RegexNodeConcatenation)sum).ChildNodes[1]);
            Assert.AreEqual(literal2, ((RegexNodeConcatenation)sum).ChildNodes[2]);
        }
        public void TestBacktrackingSuppressionNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("\\w*");

            RegexNodeBacktrackingSuppression suppression1 = new RegexNodeBacktrackingSuppression(literal1);
            Assert.AreEqual("(?>\\w*)", suppression1.ToRegexPattern());

            RegexNodeBacktrackingSuppression suppression2 = new RegexNodeBacktrackingSuppression(literal1);
            suppression2.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?>\\w*)*", suppression2.ToRegexPattern());

            RegexNodeBacktrackingSuppression suppression3 = RegexBuilder.BacktrackingSuppression(literal1);
            Assert.AreEqual("(?>\\w*)", suppression3.ToRegexPattern());

            RegexNodeBacktrackingSuppression suppression4 = RegexBuilder.BacktrackingSuppression(literal1, RegexQuantifier.ZeroOrMore);
            suppression4.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?>\\w*)*", suppression2.ToRegexPattern());
        }
        public void TestInlineOptionNodeRendering()
        {
            RegexNodeLiteral      literal = new RegexNodeLiteral(@"ab\wc{0}");
            RegexNodeInlineOption option1 = new RegexNodeInlineOption(RegexOptions.IgnoreCase, literal);

            Assert.AreEqual(@"(?i:ab\wc{0})", option1.ToRegexPattern());

            RegexNodeInlineOption option2 = new RegexNodeInlineOption(RegexOptions.Singleline | RegexOptions.IgnoreCase, literal);

            Assert.AreEqual(@"(?is:ab\wc{0})", option2.ToRegexPattern());

            RegexNodeInlineOption option3 = new RegexNodeInlineOption(RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace, literal);

            Assert.AreEqual(@"(?mx:ab\wc{0})", option3.ToRegexPattern());

            RegexNodeInlineOption option4 = new RegexNodeInlineOption(RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace, literal);

            Assert.AreEqual(@"(?nx:ab\wc{0})", option4.ToRegexPattern());
        }
Exemplo n.º 18
0
        public void TestRegexNodeAdditionOperator4()
        {
            RegexNodeLiteral       literal1       = new RegexNodeLiteral("\\w*");
            RegexNodeLiteral       literal2       = new RegexNodeLiteral("\\d+");
            RegexNodeConcatenation concatenation1 = new RegexNodeConcatenation(literal1, literal2);

            RegexNodeLiteral       literal3       = new RegexNodeLiteral("\\W*");
            RegexNodeLiteral       literal4       = new RegexNodeLiteral("\\t+");
            RegexNodeConcatenation concatenation2 = new RegexNodeConcatenation(literal3, literal4);

            RegexNode sum = concatenation1 + concatenation2;

            Assert.IsInstanceOfType(sum, typeof(RegexNodeConcatenation));
            Assert.AreNotSame(concatenation1, sum);
            Assert.AreNotSame(concatenation2, sum);
            Assert.AreEqual(literal1, ((RegexNodeConcatenation)sum).ChildNodes[0]);
            Assert.AreEqual(literal2, ((RegexNodeConcatenation)sum).ChildNodes[1]);
            Assert.AreEqual(literal3, ((RegexNodeConcatenation)sum).ChildNodes[2]);
            Assert.AreEqual(literal4, ((RegexNodeConcatenation)sum).ChildNodes[3]);
        }
        public void TestAlternationNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral(@"\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral(@"\d+");
            RegexNodeLiteral literal3 = new RegexNodeLiteral(@"\s?");

            RegexNodeAlternation alternation1 = new RegexNodeAlternation(literal1, literal2);

            Assert.AreEqual(@"(?:\w*|\d+)", alternation1.ToRegexPattern());

            RegexNodeAlternation alternation2 = new RegexNodeAlternation(literal1, literal2, literal3);

            Assert.AreEqual(@"(?:\w*|\d+|\s?)", alternation2.ToRegexPattern());

            RegexNodeAlternation alternation3 = RegexBuilder.Alternate(literal1, literal2);

            Assert.AreEqual(@"(?:\w*|\d+)", alternation3.ToRegexPattern());

            RegexNodeAlternation alternation4 = RegexBuilder.Alternate(new RegexNode[] { literal1, literal2, literal3 });

            Assert.AreEqual(@"(?:\w*|\d+|\s?)", alternation4.ToRegexPattern());

            RegexNodeAlternation alternation5 = new RegexNodeAlternation(literal1, literal2);

            alternation5.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w*|\d+)*", alternation5.ToRegexPattern());

            RegexNodeAlternation alternation6 = new RegexNodeAlternation(literal1, literal2, literal3);

            alternation6.Quantifier = RegexQuantifier.OneOrMore;
            Assert.AreEqual(@"(?:\w*|\d+|\s?)+", alternation6.ToRegexPattern());

            RegexNodeAlternation alternation7 = RegexBuilder.Alternate(literal1, literal2, RegexQuantifier.ZeroOrOne);

            Assert.AreEqual(@"(?:\w*|\d+)?", alternation7.ToRegexPattern());

            RegexNodeAlternation alternation8 = RegexBuilder.Alternate(new RegexNode[] { literal1, literal2, literal3 }, RegexQuantifier.AtLeast(5));

            Assert.AreEqual(@"(?:\w*|\d+|\s?){5,}", alternation8.ToRegexPattern());
        }
        public void TestGroupNodeRendering()
        {
            RegexNodeLiteral literal = new RegexNodeLiteral("abc");

            RegexNodeGroup group1 = new RegexNodeGroup(literal);

            Assert.AreEqual("(abc)", group1.ToRegexPattern());
            group1.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(abc)*", group1.ToRegexPattern());

            RegexNodeGroup group2 = new RegexNodeGroup(literal, false);

            Assert.AreEqual("(?:abc)", group2.ToRegexPattern());
            group2.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?:abc)*", group2.ToRegexPattern());

            RegexNodeGroup group3 = new RegexNodeGroup(literal, "SomeGroup");

            Assert.AreEqual("(?<SomeGroup>abc)", group3.ToRegexPattern());
            group3.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?<SomeGroup>abc)*", group3.ToRegexPattern());

            RegexNodeGroup group4 = RegexBuilder.Group(literal);

            Assert.AreEqual("(abc)", group4.ToRegexPattern());
            group4 = RegexBuilder.Group(literal, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual("(abc)*", group4.ToRegexPattern());

            RegexNodeGroup group5 = RegexBuilder.NonCapturingGroup(literal);

            Assert.AreEqual("(?:abc)", group5.ToRegexPattern());
            group5 = RegexBuilder.NonCapturingGroup(literal, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual("(?:abc)*", group5.ToRegexPattern());

            RegexNodeGroup group6 = RegexBuilder.Group("SomeGroup", literal);

            Assert.AreEqual("(?<SomeGroup>abc)", group6.ToRegexPattern());
            group6 = RegexBuilder.Group("SomeGroup", literal, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual("(?<SomeGroup>abc)*", group6.ToRegexPattern());
        }
        public void TestBacktrackingSuppressionNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("\\w*");

            RegexNodeBacktrackingSuppression suppression1 = new RegexNodeBacktrackingSuppression(literal1);

            Assert.AreEqual("(?>\\w*)", suppression1.ToRegexPattern());

            RegexNodeBacktrackingSuppression suppression2 = new RegexNodeBacktrackingSuppression(literal1);

            suppression2.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?>\\w*)*", suppression2.ToRegexPattern());

            RegexNodeBacktrackingSuppression suppression3 = RegexBuilder.BacktrackingSuppression(literal1);

            Assert.AreEqual("(?>\\w*)", suppression3.ToRegexPattern());

            RegexNodeBacktrackingSuppression suppression4 = RegexBuilder.BacktrackingSuppression(literal1, RegexQuantifier.ZeroOrMore);

            suppression4.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?>\\w*)*", suppression2.ToRegexPattern());
        }
Exemplo n.º 22
0
 public void TestRegexNodeAdditionOperator6()
 {
     RegexNodeLiteral literal = new RegexNodeLiteral("\\w*");
     RegexNode sum = null + literal;
     Assert.IsNull(sum);
 }
Exemplo n.º 23
0
        public void TestRegexNodeAdditionOperator4()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("\\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral("\\d+");
            RegexNodeConcatenation concatenation1 = new RegexNodeConcatenation(literal1, literal2);

            RegexNodeLiteral literal3 = new RegexNodeLiteral("\\W*");
            RegexNodeLiteral literal4 = new RegexNodeLiteral("\\t+");
            RegexNodeConcatenation concatenation2 = new RegexNodeConcatenation(literal3, literal4);

            RegexNode sum = concatenation1 + concatenation2;

            Assert.IsInstanceOfType(sum, typeof(RegexNodeConcatenation));
            Assert.AreNotSame(concatenation1, sum);
            Assert.AreNotSame(concatenation2, sum);
            Assert.AreEqual(literal1, ((RegexNodeConcatenation)sum).ChildNodes[0]);
            Assert.AreEqual(literal2, ((RegexNodeConcatenation)sum).ChildNodes[1]);
            Assert.AreEqual(literal3, ((RegexNodeConcatenation)sum).ChildNodes[2]);
            Assert.AreEqual(literal4, ((RegexNodeConcatenation)sum).ChildNodes[3]);
        }
Exemplo n.º 24
0
        public void TestRegexNodeAdditionOperator3()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("\\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral("\\d+");
            RegexNodeConcatenation concatenation = new RegexNodeConcatenation(literal1, literal2);

            RegexNodeLiteral literal3 = new RegexNodeLiteral("\\s?");
            RegexNode sum = literal3 + concatenation;

            Assert.IsInstanceOfType(sum, typeof(RegexNodeConcatenation));
            Assert.AreNotSame(concatenation, sum);
            Assert.AreEqual(literal3, ((RegexNodeConcatenation)sum).ChildNodes[0]);
            Assert.AreEqual(literal1, ((RegexNodeConcatenation)sum).ChildNodes[1]);
            Assert.AreEqual(literal2, ((RegexNodeConcatenation)sum).ChildNodes[2]);
        }
Exemplo n.º 25
0
        public void TestRegexNodeAdditionOperator1()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("\\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral("\\d+");

            RegexNode sum = literal1 + literal2;
            Assert.IsInstanceOfType(sum, typeof(RegexNodeConcatenation));
            Assert.AreEqual(literal1, ((RegexNodeConcatenation)sum).ChildNodes[0]);
            Assert.AreEqual(literal2, ((RegexNodeConcatenation)sum).ChildNodes[1]);
        }
Exemplo n.º 26
0
 public void TestQuantifierSetterForNodesWithQuantifierAllowed()
 {
     RegexNode node = new RegexNodeLiteral("Value");
     RegexQuantifier quantifier = RegexQuantifier.OneOrMore;
     node.Quantifier = quantifier;
     Assert.AreEqual(quantifier, node.Quantifier);
 }
Exemplo n.º 27
0
 public void TestConditionalMatchNodeNullAssignment3()
 {
     RegexNodeLiteral condition = new RegexNodeLiteral("a");
     RegexNodeLiteral trueMatch = new RegexNodeLiteral("b");
     RegexNodeLiteral falseMatch = new RegexNodeLiteral("c");
     RegexNodeConditionalMatch conditionalMatch = new RegexNodeConditionalMatch(condition, trueMatch, falseMatch);
     conditionalMatch.TrueMatchExpression = null;
     conditionalMatch.FalseMatchExpression = null;
     Assert.IsNull(conditionalMatch);
 }
Exemplo n.º 28
0
 public void TestConditionalMatchNodeNullAssignment2()
 {
     RegexNodeLiteral condition = new RegexNodeLiteral("a");
     RegexNodeLiteral falseMatch = new RegexNodeLiteral("c");
     RegexNodeConditionalMatch conditionalMatch = new RegexNodeConditionalMatch(condition, null, falseMatch);
     Assert.IsNull(conditionalMatch);
 }
        public void TestConditionalMatchNodeRendering()
        {
            RegexNodeLiteral condition = new RegexNodeLiteral(@"\w\s");
            RegexNodeLiteral trueMatch = new RegexNodeLiteral(@"\w\s\d{5}-[^\u005c]+");
            RegexNodeLiteral falseMatch = new RegexNodeLiteral(@"\d{5,}\u005d");

            RegexNodeConditionalMatch conditionalMatch1 = new RegexNodeConditionalMatch(condition, trueMatch, falseMatch);
            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch1.ToRegexPattern());

            conditionalMatch1.Quantifier = RegexQuantifier.OneOrMore;
            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch1.ToRegexPattern());

            RegexNodeConditionalMatch conditionalMatch2 = new RegexNodeConditionalMatch("SomeGroup", trueMatch, falseMatch);
            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch2.ToRegexPattern());

            conditionalMatch2.Quantifier = RegexQuantifier.OneOrMore;
            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch2.ToRegexPattern());

            RegexNodeConditionalMatch conditionalMatch3 = RegexBuilder.ConditionalMatch(condition, trueMatch, falseMatch);
            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch3.ToRegexPattern());

            conditionalMatch3 = RegexBuilder.ConditionalMatch(condition, trueMatch, falseMatch, RegexQuantifier.OneOrMore);
            Assert.AreEqual(@"(?(\w\s)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch3.ToRegexPattern());

            RegexNodeConditionalMatch conditionalMatch4 = RegexBuilder.ConditionalMatch("SomeGroup", trueMatch, falseMatch);
            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)", conditionalMatch4.ToRegexPattern());

            conditionalMatch4 = RegexBuilder.ConditionalMatch("SomeGroup", trueMatch, falseMatch, RegexQuantifier.OneOrMore);
            Assert.AreEqual(@"(?(SomeGroup)\w\s\d{5}-[^\u005c]+|\d{5,}\u005d)+", conditionalMatch4.ToRegexPattern());
        }
Exemplo n.º 30
0
 public void TestConditionalMatchNodeNullAssignment1()
 {
     RegexNodeLiteral condition = new RegexNodeLiteral("a");
     RegexNodeLiteral trueMatch = new RegexNodeLiteral("b");
     RegexNodeConditionalMatch conditionalMatch = new RegexNodeConditionalMatch(condition, trueMatch, null);
     Assert.IsNull(conditionalMatch);
 }
Exemplo n.º 31
0
        public void TestConcatenationNodeNullChildAssignment2()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral("a");
            RegexNodeLiteral literal2 = new RegexNodeLiteral("b");
            RegexNodeConcatenation concatenation = new RegexNodeConcatenation(literal1, literal2);
            Assert.AreEqual(2, concatenation.ChildNodes.Count);

            // ReSharper disable RedundantAssignment
            // -- This line is expected to cause an exception.
            concatenation = new RegexNodeConcatenation(null);
            // ReSharper restore RedundantAssignment
        }
        public void TestConcatenationNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral(@"\w*");
            RegexNodeLiteral literal2 = new RegexNodeLiteral(@"\d+");
            RegexNodeLiteral literal3 = new RegexNodeLiteral(@"\s?");
            RegexNodeLiteral literal4 = new RegexNodeLiteral(@"\t");

            RegexNodeConcatenation concatenation1 = new RegexNodeConcatenation(literal1, literal2);
            Assert.AreEqual(@"\w*\d+", concatenation1.ToRegexPattern());

            RegexNodeConcatenation concatenation2 = new RegexNodeConcatenation(new List<RegexNode>(new[] { literal1, literal2 }));
            Assert.AreEqual(@"\w*\d+", concatenation2.ToRegexPattern());

            RegexNodeConcatenation concatenation3 = new RegexNodeConcatenation(literal1, literal2);
            concatenation3.ChildNodes.Add(literal3);
            Assert.AreEqual(@"\w*\d+\s?", concatenation3.ToRegexPattern());

            concatenation3.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w*\d+\s?)*", concatenation3.ToRegexPattern());

            concatenation3 = new RegexNodeConcatenation(new List<RegexNode>(new[] { literal4, literal2, literal3, literal1 }));
            concatenation3.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\t\d+\s?\w*)*", concatenation3.ToRegexPattern());

            RegexNodeConcatenation concatenation4 = RegexBuilder.Concatenate(literal1, literal2);
            Assert.AreEqual(@"\w*\d+", concatenation4.ToRegexPattern());

            RegexNodeConcatenation concatenation5 = RegexBuilder.Concatenate(literal1, literal2, RegexQuantifier.ZeroOrOne);
            Assert.AreEqual(@"(?:\w*\d+)?", concatenation5.ToRegexPattern());

            RegexNodeConcatenation concatenation6 = RegexBuilder.Concatenate(literal1, literal2, literal3);
            Assert.AreEqual(@"\w*\d+\s?", concatenation6.ToRegexPattern());

            RegexNodeConcatenation concatenation7 = RegexBuilder.Concatenate(literal1, literal2, literal3, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:\w*\d+\s?)*", concatenation7.ToRegexPattern());

            RegexNodeConcatenation concatenation8 = RegexBuilder.Concatenate(literal1, literal2, literal3, literal4);
            Assert.AreEqual(@"\w*\d+\s?\t", concatenation8.ToRegexPattern());

            RegexNodeConcatenation concatenation9 = RegexBuilder.Concatenate(literal1, literal2, literal3, literal4, RegexQuantifier.OneOrMore);
            Assert.AreEqual(@"(?:\w*\d+\s?\t)+", concatenation9.ToRegexPattern());
        }
        public void TestGroupNodeRendering()
        {
            RegexNodeLiteral literal = new RegexNodeLiteral("abc");

            RegexNodeGroup group1 = new RegexNodeGroup(literal);
            Assert.AreEqual("(abc)", group1.ToRegexPattern());
            group1.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(abc)*", group1.ToRegexPattern());

            RegexNodeGroup group2 = new RegexNodeGroup(literal, false);
            Assert.AreEqual("(?:abc)", group2.ToRegexPattern());
            group2.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?:abc)*", group2.ToRegexPattern());

            RegexNodeGroup group3 = new RegexNodeGroup(literal, "SomeGroup");
            Assert.AreEqual("(?<SomeGroup>abc)", group3.ToRegexPattern());
            group3.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual("(?<SomeGroup>abc)*", group3.ToRegexPattern());

            RegexNodeGroup group4 = RegexBuilder.Group(literal);
            Assert.AreEqual("(abc)", group4.ToRegexPattern());
            group4 = RegexBuilder.Group(literal, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual("(abc)*", group4.ToRegexPattern());

            RegexNodeGroup group5 = RegexBuilder.NonCapturingGroup(literal);
            Assert.AreEqual("(?:abc)", group5.ToRegexPattern());
            group5 = RegexBuilder.NonCapturingGroup(literal, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual("(?:abc)*", group5.ToRegexPattern());

            RegexNodeGroup group6 = RegexBuilder.Group("SomeGroup", literal);
            Assert.AreEqual("(?<SomeGroup>abc)", group6.ToRegexPattern());
            group6 = RegexBuilder.Group("SomeGroup", literal, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual("(?<SomeGroup>abc)*", group6.ToRegexPattern());
        }
        public void TestInlineOptionNodeRendering()
        {
            RegexNodeLiteral literal = new RegexNodeLiteral(@"ab\wc{0}");
            RegexNodeInlineOption option1 = new RegexNodeInlineOption(RegexOptions.IgnoreCase, literal);
            Assert.AreEqual(@"(?i:ab\wc{0})", option1.ToRegexPattern());

            RegexNodeInlineOption option2 = new RegexNodeInlineOption(RegexOptions.Singleline | RegexOptions.IgnoreCase, literal);
            Assert.AreEqual(@"(?is:ab\wc{0})", option2.ToRegexPattern());

            RegexNodeInlineOption option3 = new RegexNodeInlineOption(RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace, literal);
            Assert.AreEqual(@"(?mx:ab\wc{0})", option3.ToRegexPattern());

            RegexNodeInlineOption option4 = new RegexNodeInlineOption(RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace, literal);
            Assert.AreEqual(@"(?nx:ab\wc{0})", option4.ToRegexPattern());
        }
        public void TestLiteralNodeRendering()
        {
            RegexNodeLiteral literal1 = new RegexNodeLiteral(@"\w\b.353\s");
            Assert.AreEqual(@"\w\b.353\s", literal1.ToRegexPattern());

            literal1.Quantifier = RegexQuantifier.ZeroOrOne;
            Assert.AreEqual(@"(?:\w\b.353\s)?", literal1.ToRegexPattern());

            RegexNodeLiteral literal2 = RegexBuilder.NonEscapedLiteral(@"\w\b.353\s");
            Assert.AreEqual(@"\w\b.353\s", literal2.ToRegexPattern());

            RegexNodeLiteral literal3 = RegexBuilder.NonEscapedLiteral(@"\w\b.353\s", RegexQuantifier.ZeroOrOne);
            Assert.AreEqual(@"(?:\w\b.353\s)?", literal3.ToRegexPattern());

            RegexNodeLiteral literal4 = RegexBuilder.NonEscapedLiteral(@"\w", RegexQuantifier.ZeroOrOne);
            Assert.AreEqual(@"\w?", literal4.ToRegexPattern());

            RegexNodeLiteral literal5 = RegexBuilder.NonEscapedLiteral(@"a", RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"a*", literal5.ToRegexPattern());
        }
        public void TestLookAroundNodeRendering()
        {
            RegexNodeLiteral lookupExpression = new RegexNodeLiteral(@"a\bc");
            RegexNodeLiteral matchExpression = new RegexNodeLiteral(@"\w+");

            RegexNodeLookAround positiveLookAhead = new RegexNodeLookAround(RegexLookAround.PositiveLookAhead, lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:\w+(?=a\bc))", positiveLookAhead.ToRegexPattern());
            positiveLookAhead.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w+(?=a\bc))*", positiveLookAhead.ToRegexPattern());

            RegexNodeLookAround positiveLookBehind = new RegexNodeLookAround(RegexLookAround.PositiveLookBehind, lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:(?<=a\bc)\w+)", positiveLookBehind.ToRegexPattern());
            positiveLookBehind.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:(?<=a\bc)\w+)*", positiveLookBehind.ToRegexPattern());

            RegexNodeLookAround negativeLookAhead = new RegexNodeLookAround(RegexLookAround.NegativeLookAhead, lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:\w+(?!a\bc))", negativeLookAhead.ToRegexPattern());
            negativeLookAhead.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:\w+(?!a\bc))*", negativeLookAhead.ToRegexPattern());

            RegexNodeLookAround negativeLookBehind = new RegexNodeLookAround(RegexLookAround.NegativeLookBehind, lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:(?<!a\bc)\w+)", negativeLookBehind.ToRegexPattern());
            negativeLookBehind.Quantifier = RegexQuantifier.ZeroOrMore;
            Assert.AreEqual(@"(?:(?<!a\bc)\w+)*", negativeLookBehind.ToRegexPattern());

            RegexNodeLookAround positiveLookAhead2 = RegexBuilder.PositiveLookAhead(lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:\w+(?=a\bc))", positiveLookAhead2.ToRegexPattern());
            positiveLookAhead2 = RegexBuilder.PositiveLookAhead(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:\w+(?=a\bc))*", positiveLookAhead2.ToRegexPattern());

            RegexNodeLookAround positiveLookBehind2 = RegexBuilder.PositiveLookBehind(lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:(?<=a\bc)\w+)", positiveLookBehind2.ToRegexPattern());
            positiveLookBehind2 = RegexBuilder.PositiveLookBehind(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:(?<=a\bc)\w+)*", positiveLookBehind2.ToRegexPattern());

            RegexNodeLookAround negativeLookAhead2 = RegexBuilder.NegativeLookAhead(lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:\w+(?!a\bc))", negativeLookAhead2.ToRegexPattern());
            negativeLookAhead2 = RegexBuilder.NegativeLookAhead(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:\w+(?!a\bc))*", negativeLookAhead2.ToRegexPattern());

            RegexNodeLookAround negativeLookBehind2 = RegexBuilder.NegativeLookBehind(lookupExpression, matchExpression);
            Assert.AreEqual(@"(?:(?<!a\bc)\w+)", negativeLookBehind2.ToRegexPattern());
            negativeLookBehind2 = RegexBuilder.NegativeLookBehind(lookupExpression, matchExpression, RegexQuantifier.ZeroOrMore);
            Assert.AreEqual(@"(?:(?<!a\bc)\w+)*", negativeLookBehind2.ToRegexPattern());
        }