Exemple #1
0
        public void CannotFilterBasedOnNullWord()
        {
            MessageFilter filter   = new MessageFilter();
            var           messages = ReadMessages();
            string        content  = null;

            List <Message> returnMessages = filter.Filter(messages, new string[] { content }).ToList();

            Assert.IsNotNull(returnMessages);
            CollectionAssert.AreEqual(messages, returnMessages);
        }
Exemple #2
0
        public void CanFilterBasedOnMessageInChat()
        {
            MessageFilter filter   = new MessageFilter();
            var           messages = ReadMessages();
            string        content  = "pie";

            List <Message> returnMessages = filter.Filter(messages, new string[] { content }).ToList();

            Assert.IsNotNull(returnMessages);
            Assert.AreNotEqual(messages, returnMessages);
            Assert.IsTrue(returnMessages.Count() > 0);
        }
Exemple #3
0
        public void CannotFilterBasedOnNonExistantWord()
        {
            try
            {
                MessageFilter filter   = new MessageFilter();
                var           messages = ReadMessages();
                string        content  = "Non-Existant";

                List <Message> returnMessages = filter.Filter(messages, new string[] { content }).ToList();
                Assert.IsNotNull(returnMessages);
                Assert.AreNotEqual(messages, returnMessages);
                Assert.IsTrue(returnMessages.Count() == 0);
            }
            catch (MessageNotFoundException)
            {
                Assert.IsTrue(true);
            }
        }
        /// <summary>
        /// Helper method to read the conversation from <paramref name="inputFilePath"/>.
        /// </summary>
        /// <param name="inputFilePath">
        /// The input file path.
        /// </param>
        /// <param name="userName">
        /// The string representing the username to filter messages with.
        /// </param>
        /// <param name="keyword">
        /// The string used in to match message content by keyword.
        /// </param>
        /// <param name="blacklist">
        /// The strings to be blacklisted from message content.
        /// </param>
        /// <param name="encryptUsernames">
        /// Boolean flag indicating whether to encrypt usernames or not.
        /// </param>
        /// <param name="hideNumbers">
        /// Boolean flag indicating whether to hide numbers or not.
        /// </param>
        /// <returns>
        /// A <see cref="Conversation"/> model representing the conversation.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Thrown when the input file could not be found.
        /// </exception>
        /// <exception cref="Exception">
        /// Thrown when something else went wrong.
        /// </exception>
        public Conversation ReadConversation(string inputFilePath, string userName, string keyword, string blacklist)
        {
            try
            {
                FileStream stream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
                // using statement to properly dispose reader at end of scope.
                using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
                {
                    string conversationName = reader.ReadLine();
                    var    messages         = new List <Message>();

                    string line;

                    while ((line = reader.ReadLine()) != null)
                    {
                        Message msg = MessageGenerator.CreateNewMessage(line, blacklist);

                        if (MessageFilter.Filter(msg, userName, keyword))
                        {
                            // add the newly instantiated message in List.
                            messages.Add(msg);
                        }
                    }

                    return(new Conversation(conversationName, messages));
                }
            }
            catch (FileNotFoundException)
            {
                throw new FileNotFoundException("The file was not found.");
            }
            catch (IOException)
            {
                throw new IOException("Something went wrong in the IO.");
            }
        }