Пример #1
0
        /// <summary>
        /// Приватный метод для работы с файлом.
        /// </summary>
        /// <param name="ts">TextSaver.</param>
        private void WorkWithFile(ref TextSaver ts)
        {
            string str;// Строка, для обработки ответа пользователя.

            while (true)
            {
                Console.WriteLine(ts.Text);
                Console.Write(
                    "1. Delete symbol or word.\n" +
                    "2. Count words.\n" +
                    "3. Every tenth word.\n" +
                    "4. Backward third sentence.\n" +
                    "5. Close file.\n" +
                    "(number) :"
                    );
                str = Console.ReadLine();
                if (int.TryParse(str, out int result))
                {
                    switch (result)
                    {
                    case 1:
                        Console.WriteLine("Symbol or word: ");
                        RemoverFromText.Delete(Console.ReadLine(), ref ts);
                        break;

                    case 2:
                        WordsCounter.CountWords(ts.Text);
                        break;

                    case 3:
                        WordsCounter.CountTenthWord(ts.Words);
                        break;

                    case 4:
                        SentencesReverser.ReverseThirdSentence(ts.Text);
                        break;

                    case 5:
                        Console.WriteLine("Save changes ?(yes,no)");
                        str = Console.ReadLine().ToLower();
                        if (str == "yes" || str == "y")
                        {
                            CreateFile(ts.Text);     //Creat backup
                        }
                        return;

                    default:
                        break;
                    }
                }
                Console.WriteLine("\t\t *enter*");
                Console.ReadKey();
                Console.Clear();
            }
        }
Пример #2
0
        public void CountWords_DictionaryIsNull_ArgumentNullException()
        {
            //Arrange
            string str = "Test String";
            Dictionary <string, int> outcome = null;

            //Act

            //Act => Assert
            Assert.ThrowsException <ArgumentNullException>(() => WordsCounter.CountWords(str, outcome));
        }
Пример #3
0
        public void CountWords_StringIsEmptyString_ArgumentException()
        {
            //Arrange
            var str = "";
            Dictionary <string, int> outcome = new Dictionary <string, int>();

            //Act

            //Act => Assert
            Assert.ThrowsException <ArgumentException>(() => WordsCounter.CountWords(str, outcome));
        }
Пример #4
0
        public void CountWords_ListIsNullList_ArgumentNullException()
        {
            //Arrange
            string str = "Test String";
            List <KeyValuePair <string, int> > outcome = null;

            //Act

            //Act => Assert
            Assert.ThrowsException <ArgumentNullException>(() => WordsCounter.CountWords(str, outcome));
        }
Пример #5
0
        public void CountWords_CountWordsFromPreparedString_ReturnsList()
        {
            //Arrange
            string str = "One Two Three Three Three Three Four Five";
            List <KeyValuePair <string, int> > outcome = new List <KeyValuePair <string, int> >();
            int wordsCountInString  = 0;
            int wordsCountInOutcome = 0;

            //Act
            WordsCounter.CountWords(str, outcome);
            //Assert
            Assert.IsInstanceOfType(outcome, typeof(List <KeyValuePair <string, int> >));
            foreach (KeyValuePair <string, int> item in outcome)
            {
                wordsCountInOutcome += item.Value;
            }
            int index = 0;

            while (index < str.Length)
            {
                // check if current char is part of a word
                while (index < str.Length && !char.IsWhiteSpace(str[index]))
                {
                    index++;
                }

                wordsCountInString++;

                // skip whitespace until next word
                while (index < str.Length && char.IsWhiteSpace(str[index]))
                {
                    index++;
                }
            }
            Assert.AreEqual(wordsCountInOutcome, wordsCountInString);
        }