using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; /// /// Help you to better test dotnet console apps /// by providing all lines which will be read from user at runtime, /// and all lines which will be written to user at runtime /// then validate the actual lines written by the program by the expected one /// public static class FakeConsole { /// /// Paste the text which should be splitted by lines at runtime, to be used in FakeConsole.ReadLine /// public static string ReadLines = @""; /// /// Paste the text which should be splitted by lines at runtime, to compare it with the ones written through FakeConsole.WriteLine /// public static string ExpectedWriteLines = @""; /// /// Reads next line from the ones set at /// /// public static string ReadLine() { if (_readLinesQueue == null) { _readLinesQueue = new Queue(); var lines = ReadLines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); foreach (var line in lines) _readLinesQueue.Enqueue(line); } return _readLinesQueue.Dequeue(); } /// /// Store the passed line in , then emit it to /// /// public static void WriteLine(object line) { _writeLines.Add(line.ToString()); System.Console.WriteLine(line); } /// /// Compares expected lines with the actual ones written at runtime /// /// public static bool Validate() { var expectedLines = ExpectedWriteLines.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); if (_writeLines.Count != expectedLines.Length) return false; bool isValid = true; for (var i = 0; i < _writeLines.Count; i++) isValid &= _writeLines[i] == expectedLines[i]; return isValid; } static List _writeLines = new List(); static Queue _readLinesQueue = null; }