public void TestReadUntilNextKeywordWithValidKeywords()
        {
            InputFileReader reader = InputFileReader.GetReader(TestFileNameContents.Example2FileName);

            Assert.AreEqual("the", reader.ReadUntilNextKeyword("the", "canal"));
            Assert.AreEqual("canal", reader.ReadUntilNextKeyword("panama", "canal"));
        }
        public void TestConstructor()
        {
            InputFileReader reader = InputFileReader.GetReader(TestFileNameContents.Example2FileName);

            //If no exception is thrown, this test passes
            Assert.IsTrue(true);
        }
        public void TestGetRangeOfWordsWithValidRange()
        {
            InputFileReader reader = InputFileReader.GetReader(TestFileNameContents.Example2FileName);

            //Have to read before we can get the range of words
            reader.ReadUntilNextKeyword("the", "man");

            Assert.AreEqual(2, reader.GetRangeOfWords(2).Length);
            Assert.AreEqual("the", reader.GetRangeOfWords(2)[0]);
            Assert.AreEqual("man", reader.GetRangeOfWords(2)[1]);
        }
        public void TestReadUntilNextKeywordWithNullKeyword()
        {
            InputFileReader reader = InputFileReader.GetReader(TestFileNameContents.Example2FileName);

            try
            {
                reader.ReadUntilNextKeyword(null, "canal");
            }
            catch (ArgumentException e)
            {
                Assert.AreEqual(ErrorMessageConstants.InvalidKeywordArgumentErrorMessage, e.Message);
            }
        }
        public void TestGetRangeOfWordsWithoutReading()
        {
            InputFileReader reader = InputFileReader.GetReader(TestFileNameContents.Example2FileName);

            try
            {
                reader.GetRangeOfWords(2);
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual(ErrorMessageConstants.InputFileReaderGetRangeWithoutReadingErrorMessage, e.Message);
            }
        }
示例#6
0
        /// <summary>
        /// Main search method, initializes the reader and loops from keyword to keyword
        /// in the InputFile while keeping track of the proximityCount
        /// </summary>
        public int Search()
        {
            reader = InputFileReader.GetReader(this.file);

            //Get the first keyword in the file
            string currentWord    = reader.ReadUntilNextKeyword(keywordOne, keywordTwo);
            int    proximityCount = 0;

            do
            {
                //Calculate the proximity of the range of words from the current position
                proximityCount += CalculateProximity(reader.GetRangeOfWords(range));

                //Get the next keyword in the file
                currentWord = reader.ReadUntilNextKeyword(keywordOne, keywordTwo);
            }while (!string.IsNullOrEmpty(currentWord)); //ReadUntilNextKeyword returns an empty string, we have reached the end of the file and can stop looping

            return(proximityCount);
        }
        public void TestReadUntilNextKeywordWithKeywordsThatAreNotInValidTestFile()
        {
            InputFileReader reader = InputFileReader.GetReader(TestFileNameContents.Example2FileName);

            Assert.AreEqual("", reader.ReadUntilNextKeyword("MissingKeywordOne", "MissingKeywordTwo"));
        }