/// <summary> /// Compares two files to make sure that they are identical. /// Allows non strict file comparison (e.g. ignoring white space and single-line comments) /// </summary> /// <param name="fileNameExpected">The path of the correct expected file.</param> /// <param name="fileNameActual">The path of the tested actual file.</param> /// <param name="strictLevel">How strict to be when comparing the two files.</param> public static void CheckFilesMatch(string fileNameExpected, string fileNameActual, MatchingStrictLevel strictLevel) { StreamReader readerExpected = new StreamReader(fileNameExpected); StreamReader readerActual = new StreamReader(fileNameActual); try { // Character by character check must be identical. if (strictLevel == MatchingStrictLevel.Strict) { Assert.AreEqual(readerExpected.ReadToEnd(), readerActual.ReadToEnd(), fileNameExpected + " does not match " + fileNameActual); } // Line by line check else { String lineExpected; String lineActual; int currentLineNumber = 1; while ((lineExpected = readerExpected.ReadLine()) != null) { lineActual = readerActual.ReadLine(); Assert.IsNotNull(lineActual, fileNameExpected + " does not match " + fileNameActual + ". " + fileNameActual + " has fewer lines than " + fileNameExpected); CheckLinesMatch(lineExpected, lineActual, fileNameExpected, fileNameActual, strictLevel, currentLineNumber); currentLineNumber++; } lineActual = readerActual.ReadLine(); Assert.AreEqual(readerActual.ReadLine(), null, fileNameExpected + " does not match " + fileNameActual + fileNameActual + " has more lines than " + fileNameExpected); } } finally { readerExpected.Close(); readerActual.Close(); } }
/// <summary> /// Strips off while space and Asserts that the lines match. /// </summary> /// <param name="lineExpected">A line in the expected file.</param> /// <param name="lineActual">The corresponding line in the actual file.</param> /// <param name="fileNameExpected">The name of the expected file.</param> /// <param name="fileNameActual">The name of the actual file.</param> /// <param name="strictLevel">How strict to be when comparing the two files.</param> /// <param name="currentLineNumber">The current line number used to display any error messages.</param> public static void CheckLinesMatch(string lineExpected, string lineActual, string fileNameExpected, string fileNameActual, MatchingStrictLevel strictLevel, int currentLineNumber) { if (strictLevel == MatchingStrictLevel.IgnoreIndent) { lineExpected = lineExpected.TrimStart(new char[] { ' ', '\t' }); lineActual = lineActual.TrimStart(new char[] { ' ', '\t' }); } else if (strictLevel == MatchingStrictLevel.Trim) { lineExpected = lineExpected.Trim(); lineActual = lineActual.Trim(); } else if (strictLevel == MatchingStrictLevel.IgnoreOneLineComments) { lineExpected = TrimSingleLineComment(lineExpected).Trim(); lineActual = TrimSingleLineComment(lineActual).Trim(); } Assert.AreEqual(lineExpected, lineActual, fileNameExpected + " does not match " + fileNameActual + Environment.NewLine + " line number: " + currentLineNumber); }
/// <summary> /// Strips off while space and Asserts that the lines match. /// </summary> /// <param name="lineExpected">A line in the expected file.</param> /// <param name="lineActual">The corresponding line in the actual file.</param> /// <param name="fileNameExpected">The name of the expected file.</param> /// <param name="fileNameActual">The name of the actual file.</param> /// <param name="strictLevel">How strict to be when comparing the two files.</param> /// <param name="currentLineNumber">The current line number used to display any error messages.</param> public static void CheckLinesMatch(string lineExpected, string lineActual, string fileNameExpected, string fileNameActual, MatchingStrictLevel strictLevel, int currentLineNumber) { if(strictLevel == MatchingStrictLevel.IgnoreIndent) { lineExpected = lineExpected.TrimStart(new char[]{' ','\t'}); lineActual = lineActual.TrimStart(new char[]{' ','\t'}); } else if(strictLevel == MatchingStrictLevel.Trim) { lineExpected = lineExpected.Trim(); lineActual = lineActual.Trim(); } else if(strictLevel == MatchingStrictLevel.IgnoreOneLineComments) { lineExpected = TrimSingleLineComment(lineExpected).Trim(); lineActual = TrimSingleLineComment(lineActual).Trim(); } Assert.AreEqual(lineExpected,lineActual,fileNameExpected + " does not match " + fileNameActual + Environment.NewLine + " line number: " + currentLineNumber); }
/// <summary> /// Compares two files to make sure that they are identical. /// Allows non strict file comparison (e.g. ignoring white space and single-line comments) /// </summary> /// <param name="fileNameExpected">The path of the correct expected file.</param> /// <param name="fileNameActual">The path of the tested actual file.</param> /// <param name="strictLevel">How strict to be when comparing the two files.</param> public static void CheckFilesMatch(string fileNameExpected, string fileNameActual, MatchingStrictLevel strictLevel) { StreamReader readerExpected = new StreamReader(fileNameExpected); StreamReader readerActual = new StreamReader(fileNameActual); try { // Character by character check must be identical. if( strictLevel == MatchingStrictLevel.Strict ) { Assert.AreEqual(readerExpected.ReadToEnd(),readerActual.ReadToEnd(), fileNameExpected + " does not match " + fileNameActual); } // Line by line check else { String lineExpected; String lineActual; int currentLineNumber = 1; while((lineExpected = readerExpected.ReadLine())!=null) { lineActual = readerActual.ReadLine(); Assert.IsNotNull(lineActual, fileNameExpected + " does not match " + fileNameActual + ". " + fileNameActual + " has fewer lines than " + fileNameExpected); CheckLinesMatch(lineExpected, lineActual, fileNameExpected, fileNameActual, strictLevel, currentLineNumber); currentLineNumber++; } lineActual = readerActual.ReadLine(); Assert.AreEqual(readerActual.ReadLine(),null, fileNameExpected + " does not match " + fileNameActual+ fileNameActual + " has more lines than " + fileNameExpected); } } finally { readerExpected.Close(); readerActual.Close(); } }