public void Or_AddComOrOrg_DoesMatchComAndOrg()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add("com").Or("org");

            Console.WriteLine(verbEx);
            Assert.IsTrue(verbEx.IsMatch("org"), "Should match 'org'");
            Assert.IsTrue(verbEx.IsMatch("com"), "Should match 'com'");
        }
        public void Or_VerbalExpressionsUrlOrVerbalExpressionEmail_DoesMatchEmailAndUrl()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add(CommonRegex.Url)
            .Or(CommonRegex.Email);

            Assert.IsTrue(verbEx.IsMatch("*****@*****.**"), "Should match email address");
            Assert.IsTrue(verbEx.IsMatch("http://www.google.com"), "Should match url address");
        }
        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");
        }
Esempio n. 5
0
        /// <summary>
        /// Handles the incoming message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="userName">Name of the user.</param>
        /// <param name="channel">The channel.</param>
        /// <returns>The parsed message.</returns>
        public string HandleMessage(string message, string userName, ISocketMessageChannel channel)
        {
            message = Regex.Replace(message, @"\r\n?|\n", " ");
            StringBuilder returnMessage = new StringBuilder();

            if (emojiExpression.IsMatch(message) ||
                animatedEmojiExpression.IsMatch(message) ||
                userMentionExpression.IsMatch(message) ||
                channelExpression.IsMatch(message) ||
                commandExpression.IsMatch(message))
            {
                string[] messageParts = message.Split(' ');
                bool     firstPart    = true;
                foreach (string messagePart in messageParts)
                {
                    if (firstPart &&
                        commandExpression.IsMatch(messagePart))
                    {
                        CommandHandler.HandleCommand(messagePart, userName, channel);
                    }
                    if (emojiExpression.IsMatch(messagePart) ||
                        animatedEmojiExpression.IsMatch(messagePart))
                    {
                        string[] splitString = messagePart.Split('>');
                        returnMessage.Append($":{splitString[0].Split(':')[1]}: ");
                        if (splitString.Length > 1)
                        {
                            returnMessage.Append(splitString[1]);
                        }
                    }
                    else if (userMentionExpression.IsMatch(messagePart))
                    {
                        returnMessage.Append($"{ResolveUserMention(messagePart).Replace(' ', '_')} ");
                    }
                    else if (channelExpression.IsMatch(messagePart))
                    {
                        returnMessage.Append(ResolveChannelName(messagePart));
                    }
                    else
                    {
                        returnMessage.Append($"{messagePart} ");
                    }
                    firstPart = false;
                }
            }
            return(returnMessage.Length > 0 ? returnMessage.ToString() : message);
        }
Esempio n. 6
0
        public void AddModifier_AddModifierI_RemovesCase()
        {
            VerbalExpressions verbEx = VerbalExpressions.DefaultExpression;

            verbEx.Add("teststring").AddModifier('i');

            Assert.IsTrue(verbEx.IsMatch("TESTSTRING"));
        }
Esempio n. 7
0
        public void AddModifier_AddModifierX_IgnoreWhitspace()
        {
            VerbalExpressions verbEx = VerbalExpressions.DefaultExpression;

            verbEx.Add("test string").AddModifier('x');

            Assert.IsTrue(verbEx.IsMatch("test string #comment"));
        }
        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 Something_NullAsParameter_Throws()
        {
            // Arange
            VerbalExpressions verbEx     = VerbalExpressions.DefaultExpression.Something();
            string            testString = null;

            // Act and Assert
            Assert.Throws <ArgumentNullException>(() => verbEx.IsMatch(testString));
        }
        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 Then_VerbalExpressionsEmail_DoesMatchEmail()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.StartOfLine().Then(CommonRegex.Email);

            var isMatch = verbEx.IsMatch("*****@*****.**");

            Assert.IsTrue(isMatch, "Should match email address");
        }
        public void EndOfLine_AddDotComEndOfLine_DoesNotMatchSlashInEnd()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add(".com")
            .EndOfLine();

            var isMatch = verbEx.IsMatch("http://www.google.com/");

            Assert.IsFalse(isMatch, "Should not match '/' in end");
        }
        public void EndOfLine_AddDotComtEndOfLine_DoesMatchDotComInEnd()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add(".com")
            .EndOfLine();

            var isMatch = verbEx.IsMatch("www.google.com");

            Assert.IsTrue(isMatch, "Should match '.com' in end");
        }
Esempio n. 14
0
        public void AddModifier_AddModifierS_SingleLine()
        {
            //Arrange
            VerbalExpressions verbEx     = VerbalExpressions.DefaultExpression;
            string            testString = "First string" + Environment.NewLine + "Second string";

            //Act
            verbEx.Add("First string").Anything().Then("Second string");

            //Assert
            Assert.IsFalse(
                verbEx.IsMatch(testString),
                "The dot matches a single character, except line break characters.");

            verbEx.AddModifier('s');
            Assert.IsTrue(
                verbEx.IsMatch(testString),
                "The dot matches a single character and line break characters.");
        }
        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 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 WithAnyCase_AddwwwWithAnyCaseFalse_DoesNotMatchwWw()
        {
            verbEx = VerbalExpressions.DefaultExpression;
            verbEx.Add("www")
            .WithAnyCase(false);


            var isMatch = verbEx.IsMatch("wWw");

            Assert.IsFalse(isMatch, "Should not match any case");
        }
        public void Something_EmptyStringAsParameter_DoesNotMatch()
        {
            // Arange
            VerbalExpressions verbEx     = VerbalExpressions.DefaultExpression.Something();
            string            testString = string.Empty;

            // Act
            bool isMatch = verbEx.IsMatch(testString);

            // Assert
            Assert.IsFalse(isMatch, "Test string should be empty.");
        }
Esempio n. 19
0
        private bool IsValidUrlContent(CreatePostRequestBo post)
        {
            if (post.Type == PostTypeEnum.Image)
            {
                var urlExpression = new VerbalExpressions().StartOfLine().Then("http").Maybe("s").Then("://")
                                    .Maybe("www.").AnythingBut(" ").EndOfLine();

                return(urlExpression.IsMatch(post.Content));
            }

            return(true);
        }
        public void Something_SomeStringAsParameter_DoesMatch()
        {
            // Arange
            VerbalExpressions verbEx      = VerbalExpressions.DefaultExpression.Something();
            const string      TEST_STRING = "Test string";

            // Act
            bool isMatch = verbEx.IsMatch(TEST_STRING);

            // Assert
            Assert.IsTrue(isMatch, "Test string should not be empty.");
        }
        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 SomethingBut_TestStringStartsIncorrect_DoesNotMatch()
        {
            // Arange
            const string      START_STRING = "Test";
            VerbalExpressions verbEx       = VerbalExpressions.DefaultExpression.SomethingBut(START_STRING);
            const string      TEST_STRING  = "string";

            // Act
            bool isMatch = verbEx.IsMatch(TEST_STRING);

            // Assert
            Assert.IsTrue(isMatch, "Test string starts with \"" + START_STRING + "\".");
        }
        public void Range_WhenOddNumberOfItemsInArray_ShouldAppendLastElementWithOrClause()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            string text = "abcd7sdadqascdaswde";

            object[] range = new object[3] {
                1, 6, 7
            };

            //Act
            verbEx.Range(range);
            //Assert
            Assert.IsTrue(verbEx.IsMatch(text));
        }
        public void Range_WhenArrayParameterHasValuesInReverseOrder_ReturnsCorrectResultForCorrectOrder()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            object[] inversedOrderArray = new object[2] {
                9, 2
            };
            verbEx.Range(inversedOrderArray);
            string lookupString = "testing 8 another test";

            //Act
            bool isMatch = verbEx.IsMatch(lookupString);

            //Assert
            Assert.IsTrue(isMatch);
        }
        public void Range_WhenArrayContainsNullParameter_ItIsIgnoredAndRemovedFromList()
        {
            //Arrange
            verbEx = VerbalExpressions.DefaultExpression;
            object[] inversedOrderArray = new object[4] {
                1, null, null, 7
            };
            verbEx.Range(inversedOrderArray);
            string lookupString = "testing 5 testing";

            //Act
            bool isMatch = verbEx.IsMatch(lookupString);

            //Assert
            Assert.IsTrue(isMatch);
        }
        public void Maybe_WhenCalled_UsesCommonRegexUrl()
        {
            verbEx.Maybe(CommonRegex.Url);

            Assert.IsTrue(verbEx.IsMatch("http://www.google.com"), "Should match url address");
        }