public void RepeatAtLeastAtMostTimesStructuralChecksTest2() { dynamic se = new SimpleExpression(); SimpleExpression result = se .StartsWith .Group .Text("Repeat").Anything .Either .Text("AtLeast").Anything .Maybe("AtMost").Anything .Or .Text("Exactly").Anything .Then .Text("Times") .Together.As("Whole") .EndOfLine .Generate(); var regex = new Regex(result.Expression, RegexOptions.ExplicitCapture); Assert.IsTrue(regex.IsMatch("Repeat.http.AtLeast(2).Times"), "AtLeast"); Assert.IsTrue(regex.IsMatch("Repeat.http.AtLeast(2).AtMost(5).Times"), "AtLeast.AtMost"); Assert.IsTrue(regex.IsMatch("Repeat.http.Exactly(2).Times"), "Exactly"); //Wrong, the produced regex has too many wildcards that make the matching succeed in those cases Assert.IsFalse(regex.IsMatch("Repeat.http.AtMost(2).AtLeast(2).Times"), "AtMost.AtLeast"); Assert.IsFalse(regex.IsMatch("Repeat.http.Exactly(2).AtLeast(5).Times"), "Exactly.AtLeast"); Assert.IsFalse(regex.IsMatch("Repeat.http.Exactly(2).AtMost(5).Times"), "Exactly.AtMost"); }
public void RepeatAtLeastAtMostTimesStructuralChecksTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .StartsWith .Either .Group .Text("AtLeast").One("\\(").Numbers.AtLeast(1).One("\\)") .Maybe("\\.AtMost").One("\\(").Numbers.AtLeast(1).One("\\)") .Together .Or .Group .Text("Exactly").One("\\(").Numbers.AtLeast(1).One("\\)") .Together .Then .Text("\\.Times") .Generate(); const string desiredPattern = @"^((AtLeast\([0-9]{1,}\)(\.AtMost\([0-9]{1,}\))?)|(Exactly\([0-9]{1,}\))).Times"; Assert.AreEqual(desiredPattern, result.Expression); var regex = new Regex(result.Expression, RegexOptions.ExplicitCapture); //Positive tests Assert.IsTrue(regex.IsMatch("AtLeast(2).Times"), "AtLeast"); Assert.IsTrue(regex.IsMatch("AtLeast(2).AtMost(5).Times"), "AtLeast.AtMost"); Assert.IsTrue(regex.IsMatch("Exactly(2).Times"), "Exactly"); //Negative tests Assert.IsFalse(regex.IsMatch("AtMost(2).AtLeast(2).Times"), "AtMost.AtLeast"); Assert.IsFalse(regex.IsMatch("Exactly(2).AtLeast(5).Times"), "Exactly.AtLeast"); Assert.IsFalse(regex.IsMatch("Exactly(2).AtMost(5).Times"), "Exactly.AtMost"); }
public void CharacterDotRegex() { dynamic se = new SimpleExpression(); SimpleExpression result = se .One('.') .Generate(); Assert.AreEqual(@"\.", result.Expression); }
public void CharactersRangeRegex() { dynamic se = new SimpleExpression(); SimpleExpression result = se .OneOf("a|z") .Generate(); Assert.AreEqual(@"(a|z)", result.Expression); }
public void SimpleRangeTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .NumberInRange("1-5") .Generate(); Assert.AreEqual(@"([1-4]|5)", result.Expression); }
public void SimpleLetterRangeTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .LetterInRange("a-d") .Generate(); Assert.AreEqual(@"[a-dA-D]", result.Expression); }
public void StringsEither() { dynamic se = new SimpleExpression(); SimpleExpression result = se .EitherOf("vowels|consons") .Generate(); Assert.AreEqual(@"(vowels|consons)", result.Expression); }
public void MultipleLettersRangeTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .LetterInRange("a-d").AtLeast(3).AtMost(10) .Generate(); Assert.AreEqual(@"([a-d]{3,10})", result.Expression); }
public void MultiWildcardsRegex() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Letters .Numbers .Generate(); Assert.AreEqual(@"[a-zA-Z]*[0-9]*", result.Expression); }
public void OneConson() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Letters .Except("aeiou") .Generate(); Assert.AreEqual(@"[a-zA-Z-[aeiouAEIOU]]*", result.Expression); }
public void CardinalityMultipleBoundTests() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Letters .AtLeast(5) .Generate(); Assert.AreEqual(@"[a-zA-Z]{5,}", result.Expression); }
public void SimpleLettersAndMinusRegex() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Letters .And("_") .Generate(); Assert.AreEqual(@"[a-zA-Z_]*", result.Expression); }
public void SimpleOr() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Text("http") .Or .Text("ftp") .Generate(); Assert.AreEqual("(http|ftp)", result.Expression); }
public void ExactMatchRegex() { dynamic se = new SimpleExpression(); SimpleExpression result = se .One('a') .Text("ei") .Text("ou") .Generate(); Assert.AreEqual(@"aeiou", result.Expression); }
public void SimpleWordTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Text("ponys") .Alphanumerics.And(" ") .Text("rainbows") .Generate(); Assert.AreEqual(@"ponys[a-zA-Z0-9 ]*rainbows", result.Expression); }
public void SimpleMaybeTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Text("http") .Maybe("s") .Text("://") .Generate(); Assert.AreEqual(@"http(s)?://", result.Expression); }
public void CharsOnlyEither() { dynamic se = new SimpleExpression(); SimpleExpression result = se .EitherOf("a|e|i|o|u") .Generate(); Assert.AreEqual(@"(a|e|i|o|u)", result.Expression); Assert.IsTrue(result.IsMatch("a")); Assert.IsFalse(result.IsMatch("k")); }
public void SequenceRepeatTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Repeat .Text("http") .Exactly(3) .Times .Generate(); Assert.AreEqual(@"(http){3}", result.Expression); }
public void FixedRepetitionTests() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Repeat .Text("42") .Exactly(3) .Times .Generate(); Assert.AreEqual(@"(42){3}", result.Expression); }
public void GroupRepetition() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Group.AtLeast(3) .Text("houuu") .Together .As("ghost") .Generate(); Assert.AreEqual(@"(?<ghost>houuu){3,}", result.Expression); }
public void BlockRepetition() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Repeat .Text("aei") .AtLeast(3) .Times //AtLeast3Times? .Generate(); Assert.AreEqual(@"(aei){3,}", result.Expression); }
public void SimpleGroup() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Group .Text("aei") .Text("ou") .Together .Generate(); Assert.AreEqual(@"(aeiou)", result.Expression); }
public void CharsAndStringsEither() { dynamic se = new SimpleExpression(); SimpleExpression result = se .EitherOf("ab|cd|e") .Generate(); Assert.AreEqual(@"(ab|cd|e)", result.Expression); Assert.IsTrue(result.IsMatch("cd")); Assert.IsFalse(result.IsMatch("bd")); }
public void SimpleDate() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Numbers.AtLeast(1).AtMost(4) .One('/') .Numbers.AtLeast(1).AtMost(2) .One('/') .Numbers.AtLeast(1).AtMost(2) .Generate(); Assert.AreEqual(@"[0-9]{1,4}/[0-9]{1,2}/[0-9]{1,2}", result.Expression); }
public void EmailRegex() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Alphanumerics.AtLeast(1) .One("@") .Alphanumerics.AtLeast(1) .One(".") .Alphanumerics.AtLeast(2).AtMost(5) .Generate(); Assert.AreEqual(@"[a-zA-Z0-9]{1,}@[a-zA-Z0-9]{1,}\.[a-zA-Z0-9]{2,5}", result.Expression); }
public void BountRepetitionTests() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Repeat .Text("42") .AtLeast(2) .AtMost(4) .Times .Generate(); Assert.AreEqual(@"(42){2,4}", result.Expression); }
public void GroupTogetherAsStructuralChecksTest() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Text("Group").Anything .Text("Together").Maybe(".As") .Generate(); Assert.AreEqual(@"Group.*Together(.As)?", result.Expression); var regex = new Regex(result.Expression); Assert.IsTrue(regex.IsMatch("Group.http.Together.As(\"Something\")")); }
public void SimpleDateWithRanges() { dynamic se = new SimpleExpression(); SimpleExpression result = se .NumberInRange("1-9999") .One('/') .NumberInRange("1-12") .One('/') .NumberInRange("1-31") .Generate(); Assert.AreEqual(@"([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-8][0-9][0-9][0-9]|9[0-8][0-9][0-9]|99[0-8][0-9]|999[0-9])/([1-9]|1[0-2])/([1-9]|[1-2][0-9]|3[0-1])", result.Expression); }
public void SimpleEitherOr() { dynamic se = new SimpleExpression(); var result = se .Numbers.AtLeast(1) .Maybe(" ") .One("€") .Or .Text("EURO") //.Then //Can be omited .Generate(); Assert.AreEqual("[0-9]{1,}( )?(€|EURO)", result.Expression); }
public void SimpleNamedGroup() { dynamic se = new SimpleExpression(); SimpleExpression result = se .Group .Text("aei") .Text("ou") .Together.As("vowels") .Generate(); var pattern = (result as SimpleExpression).Expression; Assert.AreEqual(@"(?<vowels>aeiou)", pattern); Assert.IsTrue(result.IsMatch("aeiou")); var names = result.GetGroupNames(); Assert.AreEqual("vowels", names[1]); }