public void Add_AddDotCom_DoesNotMatchGoogleComWithoutDot()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add(".com");

            var isMatch = verbEx.IsMatch("http://www.googlecom/");
            Assert.IsFalse(isMatch, "Should not match 'ecom'");
        }
        public void SimpleRouting()
        {
            var path = "/Controller/{?id}/Parameter";
            var matchRegex = new VerbalExpressions().Then("{").Maybe("?").Word().WithAnyCase().Then("}").ToRegex();
            var matches = matchRegex.Matches(path);

            Assert.AreEqual(matches[0].Value, "{id}");
        }
        public void Add_WhenNullStringPassedAsParameter_ShouldThrowNullArgumentException()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string value = null;

            //Act
            //Assert
            verbEx.Add(value);
        }
        public void AnyOf_WhenValueParameterIsNullOrEmpty_ShouldThrowArgumentException()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string value = null;

            //Act
            //Assert
            verbEx.AnyOf(value);
        }
        public void GetRoutingValues()
        {
            var path = "/Controller/5/Parameter";
            var matchRegex = new VerbalExpressions().Then("/Controller/").BeginCapture("id").Word().WithAnyCase().EndCapture().Then("/Parameter").ToRegex();
            var matches = matchRegex.Matches(path);

            foreach (Match match in matches)
            {
                Assert.AreEqual(match.Groups["id"].Value, "5");
            }
        }
        static void Main(string[] args)
        {
            var urlExp = new VerbalExpressions()
                                .StartOfLine()
                                .Then("http")
                                .Maybe("s")
                                .Then("://")
                                .AnythingBut(" ")
                                .EndOfLine();

            var emailExp = new VerbalExpressions()
                                .StartOfLine()
                                .Anything()
                                .Then("@")
                                .AnythingBut(" ")
                                .Then(".")
                                .AnythingBut(" ")
                                .EndOfLine();

            var phoneExp = new VerbalExpressions()
                                .StartOfLine()
                                .Maybe("(")
                                .Range('0', '9')
                                .RepeatPrevious(3)
                                .Maybe(")")
                                .Maybe(" ")
                                .Range('0', '9')
                                .RepeatPrevious(3)
                                .Maybe("-")
                                .Range('0', '9')
                                .RepeatPrevious(4)
                                .EndOfLine();

            var url = "http://www.exceptionnotfound.net";
            var email = "*****@*****.**";
            var invalidEmail = "test@example";
            var phone = "(123) 456-7890";

            Assert.IsTrue(urlExp.Test(url), "The URL is not valid!");
            Assert.IsTrue(emailExp.Test(email), "The email is not valid!");
            Assert.IsTrue(phoneExp.Test(phone), "The phone number is invalid.");
            Assert.IsTrue(emailExp.Test(invalidEmail), "The email is not valid!");
        }
        private void BuildRouting()
        {
            var matchRegex = new VerbalExpressions().Then("{").Maybe("?").Word().WithAnyCase().Then("}").ToRegex();
            var matches = matchRegex.Matches(Routing);

            var routingRegex = Routing;

            routingRegex = routingRegex.Replace("{*}", @"(\w+)?");

            foreach (Match match in matches)
            {
                var name = match.Value.Replace("{", string.Empty).Replace("}", string.Empty).Replace("?", string.Empty);
                this.Parameters.Add(new RoutingParameter
                {
                    Name = name,
                    IsOptional = match.Value.StartsWith("?")
                });
                routingRegex = routingRegex.Replace(match.Value, $"(?<{name}>(.*))");
            }

            this.regex = new Regex(routingRegex);
        }
        public void TestRoutingEngine()
        {
            var path = "/Controller/5/Parameter/";
            var routingDeclaration = "/Controller/{id}/Parameter/{subId}";
            
            var matchRegex = new VerbalExpressions().Then("{").Word().WithAnyCase().Then("}").ToRegex();
            var matches = matchRegex.Matches(routingDeclaration);

            var parameterNames = new List<string>();

            foreach (Match match in matches)
            {
                var name = match.Value.Replace("{", string.Empty).Replace("}", string.Empty);
                parameterNames.Add(name);
                routingDeclaration = routingDeclaration.Replace(match.Value, $"(?<{name}>(.*))");
            }

            var regex = new Regex(routingDeclaration);
            var parameter = regex.Match(path);
            foreach (var groupName in parameterNames)
            {
                Debug.WriteLine("{0}: {1}", groupName, parameter.Groups[groupName].Value);
            }
        }
        public void Then_VerbalExpressionsEmail_DoesNotMatchUrl()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.StartOfLine().Then(CommonRegex.Email);

            var isMatch = verbEx.IsMatch("http://www.google.com");
            Assert.IsFalse(isMatch, "Should not match url address");
        }
        public void Replace_WhenCalledImmediatelyAfteInitialize_ShouldNotThrowNullReferenceException()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string value = "value";
            bool hasThrownNullReferenceEx = false;

            //Act
            try
            {
                verbEx.Replace(value);
            }
            catch (NullReferenceException)
            {
                hasThrownNullReferenceEx = true;
            }

            //Assert
            Assert.IsFalse(hasThrownNullReferenceEx);
        }
 public void Initialize()
 {
     verbEx = VerbalExpressions.NewExpression;
 }
        public void Sanitize_AddCharactersThatShouldBeEscaped_ReturnsEscapedString()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string value = "*+?";
            string result = string.Empty;
            string expected = @"\*\+\?";

            //Act
            result = verbEx.Sanitize(value);

            //Assert
            Assert.AreEqual(expected, result);
        }
        public void Anything_StartOfLineAnythingEndOfline_DoesMatchAnyThing()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx
                .StartOfLine()
                .Anything()
                .EndOfLine();

            var isMatch = verbEx.IsMatch("'!@#$%¨&*()__+{}'");
            Assert.IsTrue(isMatch, "Ooops, should match anything");
        }
        public void Word_WhenCalled_ReturnsExpectedNumberOfWords()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string text = "three words here";
            int expectedCount = 3;

            //Act
            verbEx.Word();
            Regex currentExpression = verbEx.ToRegex();
            int result = currentExpression.Matches(text).Count;

            //Assert
            Assert.AreEqual(expectedCount, result);
        }
        public void Sanitize_Handles_Null_String()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string value = null;

            //Act
            //Assert
            value = verbEx.Sanitize(value);
        }
        public void RemoveModifier_RemoveModifierM_RemovesMulitilineAsDefault()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            var regex = verbEx.ToRegex();
            Assert.IsTrue(regex.Options.HasFlag(RegexOptions.Multiline), "RegexOptions should have MultiLine as default");

            verbEx.RemoveModifier('m');
            regex = verbEx.ToRegex();

            Assert.IsFalse(regex.Options.HasFlag(RegexOptions.Multiline), "RegexOptions should now have been removed");
        }
        public void WithAnyCase_SetsCorrectIgnoreCaseRegexOptionAndHasMultiLineRegexOptionAsDefault()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.WithAnyCase();

            var regex = verbEx.ToRegex();
            Assert.IsTrue(regex.Options.HasFlag(RegexOptions.IgnoreCase), "RegexOptions should have ignoreCase");
            Assert.IsTrue(regex.Options.HasFlag(RegexOptions.Multiline), "RegexOptions should have MultiLine as default");
        }
 public void StartOfLine_CreatesCorrectRegex()
 {
     verbEx = VerbalExpressions.DefaultExpression;
     verbEx.StartOfLine();
     Assert.AreEqual("^", verbEx.ToString(), "missing start of line regex");
 }
 public void TearDown()
 {
     verbEx = null;
 }
        public void StartOfLine_ThenHttpMaybeWww_DoesMatchHttpInStart()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.StartOfLine()
                .Then("http")
                .Maybe("www");

            var isMatch = Regex.IsMatch("http", verbEx.ToString());
            Assert.IsTrue(isMatch, "Should match http in start");
        }
        public void StartOfLine_ThenHttpMaybeWww_DoesNotMatchWwwInStart()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.StartOfLine()
                .Then("http")
                .Maybe("www");

            var isMatch = verbEx.IsMatch("www");
            Assert.IsFalse(isMatch, "Should not match www in start");
        }
        public void Then_VerbalExpressionsEmail_DoesMatchEmail()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.StartOfLine().Then(CommonRegex.Email);

            var isMatch = verbEx.IsMatch("*****@*****.**");
            Assert.IsTrue(isMatch, "Should match email address");
        }
        public void Tab_WhenCalled_ReturnsExpectedExpression()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string text = string.Format("text that contains {0} a tab",@"\t");

            //Act
            verbEx.Tab();

            //Assert
            Assert.IsTrue(verbEx.Test(text));
        }
        public void StartOfLine_WhenPlacedInRandomCallOrder_ShouldAppendAtTheBeginningOfTheExpression()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add("test")
                .Add("ing")
                .StartOfLine();

            string text = "testing1234";
            Assert.IsTrue(verbEx.IsMatch(text), "Should match that the text starts with test");
        }
        public void Then_VerbalExpressionsUrl_DoesMatchUrl()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.StartOfLine()
                  .Then(CommonRegex.Url);

            Assert.IsTrue(verbEx.IsMatch("http://www.google.com"), "Should match url address");
            Assert.IsTrue(verbEx.IsMatch("https://www.google.com"), "Should match url address");
            Assert.IsTrue(verbEx.IsMatch("http://google.com"), "Should match url address");
        }
        public void Then_VerbalExpressionsUrl_DoesNotMatchEmail()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.StartOfLine().Then(CommonRegex.Url);

            Assert.IsFalse(verbEx.IsMatch("*****@*****.**"), "Should not match email address");
        }
 public void Initialize()
 {
     verbEx = VerbalExpressions.DefaultExpression;
 }
        public void WithAnyCase_AddwwwWithAnyCase_DoesMatchwWw()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add("www")
                .WithAnyCase();

            var isMatch = verbEx.IsMatch("wWw");
            Assert.IsTrue(isMatch, "Should match any case");
        }
        public void TestingIfWeHaveAValidURL()
        {
            verbEx = VerbalExpressions.DefaultExpression
                        .StartOfLine()
                        .Then("http")
                        .Maybe("s")
                        .Then("://")
                        .Maybe("www.")
                        .AnythingBut(" ")
                        .EndOfLine();

            var testMe = "https://www.google.com";

            Assert.IsTrue(verbEx.Test(testMe), "The URL is incorrect");
        }
        public void Range_WhenOddNumberOfItemsInArray_ShouldAppendWithPipe()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            object[] range = new object[3] { 1, 6, 7 };
            string expectedExpression = "[1-6]|7";

            //Act
            verbEx.Range(range);

            //Assert
            Assert.AreEqual(expectedExpression, verbEx.ToString());
        }