Ejemplo n.º 1
0
        public void TestUnicodeCharacterRendering()
        {
            RegexNode node1 = RegexBuilder.UnicodeCharacter(0x1234);

            Assert.AreEqual(@"\u1234", node1.ToRegexPattern());

            RegexNode node2 = RegexBuilder.UnicodeCharacter(0x7F03, RegexQuantifier.Custom(1, 4, true));

            Assert.AreEqual(@"(?:\u7f03){1,4}?", node2.ToRegexPattern());

            RegexNode node3 = RegexBuilder.UnicodeCharacter(0x0BA5, RegexQuantifier.Exactly(5));

            Assert.AreEqual(@"(?:\u0ba5){5}", node3.ToRegexPattern());
        }
Ejemplo n.º 2
0
        public void TestMetaCharacterRendering()
        {
            RegexNode node1 = RegexBuilder.MetaCharacter(RegexMetaChars.NonWordBoundary);

            Assert.AreEqual(@"\B", node1.ToRegexPattern());

            RegexNode node2 = RegexBuilder.MetaCharacter(RegexMetaChars.Digit, RegexQuantifier.Custom(1, 4, true));

            Assert.AreEqual(@"\d{1,4}?", node2.ToRegexPattern());

            RegexNode node3 = RegexBuilder.MetaCharacter(RegexMetaChars.WhiteSpace, RegexQuantifier.Exactly(5));

            Assert.AreEqual(@"\s{5}", node3.ToRegexPattern());
        }
        public void TestExactlyRendering()
        {
            RegexQuantifier quantifier1 = RegexQuantifier.Exactly(5, false);

            Assert.AreEqual("{5}", quantifier1.ToRegexPattern());
            quantifier1.IsLazy = true;
            Assert.AreEqual("{5}?", quantifier1.ToRegexPattern());

            RegexQuantifier quantifier2 = RegexQuantifier.Custom(5, 5, false);

            Assert.AreEqual("{5}", quantifier2.ToRegexPattern());
            quantifier2.IsLazy = true;
            Assert.AreEqual("{5}?", quantifier2.ToRegexPattern());
        }
Ejemplo n.º 4
0
        public void TestAsciiCharacterRendering()
        {
            RegexNode node1 = RegexBuilder.AsciiCharacter(0x30);

            Assert.AreEqual(@"\x30", node1.ToRegexPattern());

            RegexNode node2 = RegexBuilder.AsciiCharacter(0x7F, RegexQuantifier.Custom(1, 4, true));

            Assert.AreEqual(@"(?:\x7f){1,4}?", node2.ToRegexPattern());

            RegexNode node3 = RegexBuilder.AsciiCharacter(0x0B, RegexQuantifier.Exactly(5));

            Assert.AreEqual(@"(?:\x0b){5}", node3.ToRegexPattern());
        }
        public void TestAtLeastRendering()
        {
            RegexQuantifier quantifier1 = RegexQuantifier.AtLeast(5);

            Assert.AreEqual("{5,}", quantifier1.ToRegexPattern());
            quantifier1.IsLazy = true;
            Assert.AreEqual("{5,}?", quantifier1.ToRegexPattern());

            RegexQuantifier quantifier2 = RegexQuantifier.Custom(5, null, false);

            Assert.AreEqual("{5,}", quantifier2.ToRegexPattern());
            quantifier2.IsLazy = true;
            Assert.AreEqual("{5,}?", quantifier2.ToRegexPattern());
        }
        public void TestZeroOrOneRendering()
        {
            RegexQuantifier quantifier1 = RegexQuantifier.ZeroOrOne;

            Assert.AreEqual("?", quantifier1.ToRegexPattern());
            quantifier1.IsLazy = true;
            Assert.AreEqual("??", quantifier1.ToRegexPattern());

            RegexQuantifier quantifier2 = RegexQuantifier.Custom(0, 1, false);

            Assert.AreEqual("?", quantifier2.ToRegexPattern());
            quantifier2.IsLazy = true;
            Assert.AreEqual("??", quantifier2.ToRegexPattern());
        }
        public void TestCustomRendering()
        {
            RegexQuantifier quantifier1 = RegexQuantifier.Custom(1, 2, false);

            Assert.AreEqual("{1,2}", quantifier1.ToRegexPattern());
            quantifier1.IsLazy = true;
            Assert.AreEqual("{1,2}?", quantifier1.ToRegexPattern());

            RegexQuantifier quantifier2 = RegexQuantifier.Custom(101, 152, false);

            Assert.AreEqual("{101,152}", quantifier2.ToRegexPattern());
            quantifier2.IsLazy = true;
            Assert.AreEqual("{101,152}?", quantifier2.ToRegexPattern());
        }
        public void TestZeroOrMoreRendering()
        {
            RegexQuantifier quantifier1 = RegexQuantifier.ZeroOrMore;

            Assert.AreEqual("*", quantifier1.ToRegexPattern());
            quantifier1.IsLazy = true;
            Assert.AreEqual("*?", quantifier1.ToRegexPattern());

            RegexQuantifier quantifier2 = RegexQuantifier.AtLeast(0);

            Assert.AreEqual("*", quantifier2.ToRegexPattern());
            quantifier2.IsLazy = true;
            Assert.AreEqual("*?", quantifier2.ToRegexPattern());

            RegexQuantifier quantifier3 = RegexQuantifier.Custom(0, null, false);

            Assert.AreEqual("*", quantifier3.ToRegexPattern());
            quantifier3.IsLazy = true;
            Assert.AreEqual("*?", quantifier3.ToRegexPattern());
        }
        public void TestOneOrMoreRendering()
        {
            RegexQuantifier quantifier1 = RegexQuantifier.OneOrMore;

            Assert.AreEqual("+", quantifier1.ToRegexPattern());
            quantifier1.IsLazy = true;
            Assert.AreEqual("+?", quantifier1.ToRegexPattern());

            RegexQuantifier quantifier2 = RegexQuantifier.AtLeast(1);

            Assert.AreEqual("+", quantifier2.ToRegexPattern());
            quantifier2.IsLazy = true;
            Assert.AreEqual("+?", quantifier2.ToRegexPattern());

            RegexQuantifier quantifier3 = RegexQuantifier.Custom(1, null, false);

            Assert.AreEqual("+", quantifier3.ToRegexPattern());
            quantifier3.IsLazy = true;
            Assert.AreEqual("+?", quantifier3.ToRegexPattern());
        }
Ejemplo n.º 10
0
        public void TestCustomMethodShouldReturnProperObject()
        {
            RegexQuantifier greedyQuantifier = RegexQuantifier.Custom(1, 9, false);

            Assert.AreEqual(1, greedyQuantifier.MinOccurrenceCount);
            Assert.AreEqual(9, greedyQuantifier.MaxOccurrenceCount);
            Assert.IsFalse(greedyQuantifier.IsLazy);

            RegexQuantifier lazyQuantifier1 = RegexQuantifier.Custom(4, 3, true);

            Assert.AreEqual(4, lazyQuantifier1.MinOccurrenceCount);
            Assert.AreEqual(3, lazyQuantifier1.MaxOccurrenceCount);
            Assert.IsTrue(lazyQuantifier1.IsLazy);

            RegexQuantifier lazyQuantifier2 = RegexQuantifier.Custom(2, null, true);

            Assert.AreEqual(2, lazyQuantifier2.MinOccurrenceCount);
            Assert.AreEqual(null, lazyQuantifier2.MaxOccurrenceCount);
            Assert.IsTrue(lazyQuantifier2.IsLazy);

            Assert.AreNotSame(greedyQuantifier, lazyQuantifier1);
            Assert.AreNotSame(greedyQuantifier, lazyQuantifier2);
            Assert.AreNotSame(lazyQuantifier1, lazyQuantifier2);
        }
Ejemplo n.º 11
0
        public void TestMinMaxOccurrenceCountPropertiesShouldNotAcceptNegativeInts4()
        {
            RegexQuantifier quantifier = RegexQuantifier.Custom(1, 2, true);

            quantifier.MaxOccurrenceCount = -1;
        }
Ejemplo n.º 12
0
 public void TestMinMaxOccurrenceCountPropertiesShouldNotAcceptNegativeInts2()
 {
     RegexQuantifier.Custom(0, -1, true);
 }
Ejemplo n.º 13
0
        public void TestMinOccurrenceCountPropertyShouldNotAcceptNulls2()
        {
            RegexQuantifier quantifier = RegexQuantifier.Custom(1, 2, true);

            quantifier.MinOccurrenceCount = null;
        }
Ejemplo n.º 14
0
 public void TestMinOccurrenceCountPropertyShouldNotAcceptNulls1()
 {
     RegexQuantifier.Custom(null, 2, true);
 }
Ejemplo n.º 15
0
        public void TestEmailValidator()
        {
            const string quotationMark          = "\"";
            const string alphaCharacters        = "a-zA-Z";
            const string alphaNumericCharacters = "0-9a-zA-Z";
            const string allowedLoginSymbols    = "-!#$%&'*+=?^`{}|~";

            // Regex value taken from MSDN: http://msdn.microsoft.com/en-us/library/01escwtf.aspx
            Regex emailRegex = RegexBuilder.Build
                               (
                RegexBuilder.MetaCharacter(RegexMetaChars.LineStart),
                // Match everything before @
                RegexBuilder.ConditionalMatch
                (
                    // If the string starts with a quotation mark...
                    RegexBuilder.Literal(quotationMark),
                    // ...then match the quoted text and the @ character...
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.Literal(quotationMark),
                        RegexBuilder.MetaCharacter(RegexMetaChars.AnyCharacter, RegexQuantifier.OneOrMoreLazy),
                        RegexBuilder.Literal(quotationMark),
                        RegexBuilder.Literal("@")
                    ),
                    // ...otherwise, match a sequence of alphanumeric characters and symbols, followed by @
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                        RegexBuilder.Alternate
                        (
                            // The sequence can either contain only one dot...
                            RegexBuilder.NegativeLookAhead
                            (
                                RegexBuilder.Literal("."),
                                RegexBuilder.Literal(".")
                            ),
                            // ...or contain some special symbols (-!#$%&'*+=?^`{}|~)
                            RegexBuilder.CharacterSet(allowedLoginSymbols + RegexMetaChars.WordCharacter, RegexQuantifier.ZeroOrMore),

                            RegexQuantifier.ZeroOrMore
                        ),
                        // Before matching the @ character, make sure that it is preceded by alphanumeric characters.
                        RegexBuilder.PositiveLookBehind
                        (
                            RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                            RegexBuilder.Literal("@")
                        )
                    )
                ),
                // Match everything after @
                RegexBuilder.ConditionalMatch
                (
                    // Domain can be either an IP address enclosed in square brackets...
                    RegexBuilder.Literal("["),
                    // ...(IP address should look like [aaa.bbb.ccc.ddd])...
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.Literal("["),
                        RegexBuilder.Concatenate
                        (
                            RegexBuilder.MetaCharacter(RegexMetaChars.Digit, RegexQuantifier.Custom(1, 3, false)),
                            RegexBuilder.Literal("."),
                            RegexQuantifier.Exactly(3)
                        ),
                        RegexBuilder.MetaCharacter(RegexMetaChars.Digit, RegexQuantifier.Custom(1, 3, false)),
                        RegexBuilder.Literal("]")
                    ),
                    // ...or it can be a hostname.
                    RegexBuilder.Concatenate
                    (
                        RegexBuilder.Concatenate
                        (
                            RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                            RegexBuilder.CharacterSet("-" + RegexMetaChars.WordCharacter, RegexQuantifier.ZeroOrMore),
                            RegexBuilder.CharacterSet(alphaNumericCharacters, RegexQuantifier.None),
                            RegexBuilder.Literal("."),
                            RegexQuantifier.OneOrMore
                        ),
                        RegexBuilder.CharacterSet(alphaCharacters, RegexQuantifier.Custom(2, 6, false))
                    )
                ),
                RegexBuilder.MetaCharacter(RegexMetaChars.LineEnd)
                               );

            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("[email protected]"));
            Assert.IsTrue(emailRegex.IsMatch("js#[email protected]"));
            Assert.IsTrue(emailRegex.IsMatch("j_9@[129.126.118.1]"));
            Assert.IsFalse(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("js*@proseware.com"));
            Assert.IsFalse(emailRegex.IsMatch("*****@*****.**"));
            Assert.IsFalse(emailRegex.IsMatch("[email protected]"));
            Assert.IsTrue(emailRegex.IsMatch("*****@*****.**"));
        }