/* * Returns a test, as read from the test's file. * May return null if the file is not opened correctly. */ public static Test readTest(Info.TestName testName) { List <String> lines = readLines(@"Tests\" + testName.ToString() + ".txt"); if (lines != null) { return(parseTest(lines, testName)); } else { return(null); } }
/* * Parses and returns a test. */ private static Test parseTest(List <String> lines, Info.TestName testName) { Test test = new Test(testName); // Current line index int linesIndex = 0; // The question and answer numbers expected next int expectedQuestionNumber = 1; int expectedAnswerNumber = 1; while (linesIndex < lines.Count) { // If the current line starts a question if (isText(lines[linesIndex]) && parseQuestionNumber(lines[linesIndex]) == expectedQuestionNumber) { // Secondary line index int secondaryLinesIndex = linesIndex + 1; // Find the end of the question while (secondaryLinesIndex < lines.Count && beginsWithAQuestionNumber(lines[secondaryLinesIndex]) == false) { secondaryLinesIndex++; } // Parse the question and add it to the test test.questions.Add(parseQuestion( convertLinesToString(lines.GetRange(linesIndex, secondaryLinesIndex - linesIndex)))); linesIndex = secondaryLinesIndex; expectedQuestionNumber++; } // If the current line starts an answer else if (isACorrectAnswer(lines[linesIndex]) && parseQuestionNumber(lines[linesIndex]) == expectedAnswerNumber) { // Secondary line index int secondaryLinesIndex = linesIndex + 1; // Find the end of the answer while (secondaryLinesIndex < lines.Count && beginsWithAQuestionNumber(lines[secondaryLinesIndex]) == false) { secondaryLinesIndex++; } // Parse the answer and add it to the test KeyValuePair <char, String> answer = parseAnswer( convertLinesToString(lines.GetRange(linesIndex, secondaryLinesIndex - linesIndex))); test.addAnswer(expectedAnswerNumber - 1, answer.Key, answer.Value); linesIndex = secondaryLinesIndex; expectedAnswerNumber++; } else { Console.WriteLine("Problem: Invalid input to parseTest()."); linesIndex++; } } return(test); }
/* * Constructor. */ public Test(Info.TestName testName) { this.testName = testName; this.questions = new List <TestQuestion>(); }