public void OnCounting_WordsCounter_CountsSingleWord()
        {
            WordsCounter counter = new WordsCounter();
            var word = "random";

            counter.ProcessEach(word);

            var listAppearances = counter.Appearances.ToList();
            CounterEntry entryForNew = listAppearances.Find(c => c.Word == word);
            Assert.IsNotNull(entryForNew);
            Assert.AreEqual(word, entryForNew.Word);
            Assert.AreEqual(1, entryForNew.Count);
        }
        public void OnCounting_WordsCounter_CountsWordsInAnArray()
        {
            WordsCounter counter = new WordsCounter();
            // 16 words, the and loan occur 2 times each.
            var words = new string[] { "Our", "time", "in", "the", "world", "is", "on", "loan", "we", "must", "pay", "back", "the", "loan", "with", "interest" };

            foreach (var word in words)
            {
                counter.ProcessEach(word);
            }

            var listAppearances = counter.Appearances.ToList();
            var loanEntry = listAppearances.Find(w => w.Word == "loan");
            var theEntry = listAppearances.Find(w => w.Word == "the");

            Assert.AreEqual(14, listAppearances.Count);
            Assert.AreEqual(12, listAppearances.FindAll(c=>c.Count==1).Count);
            Assert.AreEqual(2, loanEntry.Count);
            Assert.AreEqual("loan", loanEntry.Word);
            Assert.AreEqual(2, theEntry.Count);
            Assert.AreEqual("the", theEntry.Word);
        }
Пример #3
0
        static void ProcessFile(string file)
        {
            Console.WriteLine(string.Format("Processing File: {0}", file));

            try
            {
                string fileContent = File.ReadAllText(file);

                WordsParser parser = new WordsParser();

                IEnumerable<string> words = parser.GetWords(fileContent);

                StandardWordTraverser traverser = new StandardWordTraverser(words);

                // Part 1
                WordsCounter counter = new WordsCounter();
                traverser.AcceptProcessor(counter);
                counter.ReportTo(Console.WriteLine);

                // Part 2
                CompositionMatcher compositonMatcher = new CompositionMatcher(words, 6);
                traverser.AcceptProcessor(compositonMatcher);
                compositonMatcher.ReportTo(Console.WriteLine);
            }
            catch (IOException ex)
            {
                Console.WriteLine("Unable to read the file.");
                Console.WriteLine(string.Format("Message: {0}", ex.Message));
                Console.WriteLine(string.Format("Details: {0}", ex.StackTrace));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Oops... Unexpected error occurred.");
                Console.WriteLine(string.Format("Message: {0}", ex.Message));
                Console.WriteLine(string.Format("Details: {0}", ex.StackTrace));
            }
        }