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 TestGetRangeOfWordsWithoutReading() { InputFileReader reader = InputFileReader.GetReader(TestFileNameContents.Example2FileName); try { reader.GetRangeOfWords(2); } catch (InvalidOperationException e) { Assert.AreEqual(ErrorMessageConstants.InputFileReaderGetRangeWithoutReadingErrorMessage, e.Message); } }
/// <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); }