private string[] GetLinesWithPossibleMismatches(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; string output = string.Empty; OutputWriter.WriteMessageOnNewLine("Comparing files..."); int minOutputLines = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { hasMismatch = true; minOutputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.DisplayException(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); } string[] mismatches = new string[minOutputLines]; for (int i = 0; i < minOutputLines; i++) { string actualLine = actualOutputLines[i]; string expectedLine = expectedOutputLines[i]; if (!actualLine.Equals(expectedLine)) { output = string.Format($"Mismatch at line {i} -- expected: \"{expectedLine}\", actual: \"{actualLine}\""); output += Environment.NewLine; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[i] = output; } return(mismatches); }
public void CompareContent(string userOutputPath, string expectedOutputPath) { try { OutputWriter.WriteMessageOnNewLine("Reading files..."); string mismatchesPath = GetMismatchPath(expectedOutputPath); string[] actualOutputLines = File.ReadAllLines(userOutputPath); string[] expectedOutputLines = File.ReadAllLines(expectedOutputPath); bool hasMismatch; string[] mismatches = GetLinesWithPossibleMismatches(actualOutputLines, expectedOutputLines, out hasMismatch); this.PrintOutput(mismatches, hasMismatch, mismatchesPath); OutputWriter.WriteMessageOnNewLine("Files read!"); } catch (IOException) { //OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } }
public static string[] GetLineWithPossibleMismatches(string[] actualOutputLines, string[] expectedOutputLines, out bool hasMismatch) { hasMismatch = false; string output = string.Empty; string[] mismatches = new string[actualOutputLines.Length]; OutputWriter.WriteMessageOnNewLine("Comparing files..."); int minOutputLines = actualOutputLines.Length; if (actualOutputLines.Length != expectedOutputLines.Length) { ; } { hasMismatch = true; minOutputLines = Math.Min(actualOutputLines.Length, expectedOutputLines.Length); OutputWriter.DisplayException(ExceptionMessages.ComparisonOfFilesWithDifferentSizes); mismatches = new string[minOutputLines]; } for (int index = 0; index < minOutputLines; index++) { string actualLine = actualOutputLines[index]; string expectedLine = expectedOutputLines[index]; if (actualLine != expectedLine) { output = $"Mismatch at line {index} -- expected: \"{expectedLine}\", actual: \"{actualLine}\""; hasMismatch = true; } else { output = actualLine; output += Environment.NewLine; } mismatches[index] = output; } return(mismatches); }
private static void PrintOutput(string[] mismatches, bool hasMismatch, string mismatchesPath) { if (hasMismatch) { foreach (var line in mismatches) { OutputWriter.WriteMessageOnNewLine(line); } try { File.WriteAllLines(mismatchesPath, mismatches); } catch (DirectoryNotFoundException) { OutputWriter.DisplayException(ExceptionMessages.InvalidPath); } return; } OutputWriter.WriteMessageOnNewLine("Files are identical. There are no mismatches"); }
public static void TraverseDirectory(int depth) { OutputWriter.WriteEmptyLine(); int initialIdentation = SessionData.currentPath.Split('\\').Length; Queue <string> subFolders = new Queue <string>(); subFolders.Enqueue(SessionData.currentPath); while (subFolders.Count != 0 && depth >= 0) { string currentPath = subFolders.Dequeue(); int identation = currentPath.Split('\\').Length - initialIdentation; if (depth - identation < 0) { break; } try { foreach (string directoryPath in Directory.GetDirectories(currentPath)) { subFolders.Enqueue(directoryPath); } OutputWriter.WriteMessageOnNewLine(string.Format("{0}{1}", new string('-', identation), currentPath)); foreach (string file in Directory.GetFiles(currentPath)) { int indexOfLastSlash = file.LastIndexOf(@"\"); string fileName = file.Substring(indexOfLastSlash); OutputWriter.WriteMessageOnNewLine(new string('-', indexOfLastSlash) + fileName); } } catch (UnauthorizedAccessException) { OutputWriter.DisplayException(ExceptionMessages.UnauthorizedAccessException); } } depth--; }