public void Should_Throw_ArgumentNullException_With_Null_Argument()
        {
            // Arrange
            string[] arrayOfWords = null;
            var wordCounter = new WordCounter();

            // Act
            var results = wordCounter.CountWords(arrayOfWords);

            // Assert
            // should throw
        }
Exemple #2
0
        static void Main(string[] args)
        {
            try
            {
                if (string.IsNullOrEmpty(_fileInputPath.Trim()))
                {
                    throw new ConfigurationErrorsException("Please specify the path to the file input using the FileInputPath in the configuration file.");
                }

                if (!File.Exists(_fileInputPath))
                {
                    throw new FileNotFoundException(string.Format("Could not find file at path '{0}'", _fileInputPath));
                }

                // StreamReader.ReadToEnd is possibly more efficient, but this is only a small text file
                string fileText = File.ReadAllText(_fileInputPath);

                // Split the words from the file into a new string array,
                var wordSplitter = new WordSplitter();
                string[] allWords = wordSplitter.SplitWordsAndSort(fileText);

                // Get the counted words
                var wordCounter = new WordCounter();
                List<WordCountResult> countedWords = wordCounter.CountWords(allWords);

                // show the report on screen
                ShowWordCountReport(countedWords);

                Console.WriteLine();

                // Apply the step-two filter

                // we only need the unique words and the count, so map them
                // using a linq Select() projection
                string[] uniqueWords = countedWords.Select(s => s.Word).ToArray();

                // Get a string of words that match the filter
                var wordFilter = new WordFilter();
                string filteredWords = wordFilter.FilterWords(uniqueWords);

                Console.WriteLine("Words matching the filter:");
                Console.WriteLine(filteredWords);
            }
            catch (Exception ex)
            {
                LogErrorToConsole(ex.Message);
            }

            Console.Write("\nFinished. Press any key to quit...");
            Console.Read();
        }
        public void Should_Count_Words()
        {
            // Arrange
            var arrayOfWords = new[] { "hello", "goodbye", "hello", "goodbye", "hello" };
            var wordCounter = new WordCounter();

            var expectedHellos = 3;
            var expectedGoodbyes = 2;

            // Act
            var results = wordCounter.CountWords(arrayOfWords);

            // Assert

            Assert.AreEqual(expectedHellos, results[0].Count);
            Assert.AreEqual(expectedGoodbyes, results[1].Count);
        }