public void AddAndPrintTest() { TodoApp todoApp = new TodoApp(); todoApp.UseTestEnvironment(); // Retrieve output to command line, redirect it to the String Writer using (StringWriter sw = new StringWriter()) { // Set the console to print to sw Console.SetOut(sw); todoApp.Add("This is a test"); string expected = "#1 This is a test\r\n"; Assert.AreEqual(expected, sw.ToString()); } using (StringWriter sw = new StringWriter()) { // Set the console to print to sw Console.SetOut(sw); todoApp.Add("another item"); string expectedPrint = "#2 another item\r\n#1 This is a test\r\n#2 another item\r\n"; todoApp.Print(); Assert.AreEqual(expectedPrint, sw.ToString()); } }
static void Main(string[] args) { TodoApp todoApp; Console.WriteLine("- console - to use System.Console as input/output"); Console.WriteLine("- inmemory - to use in-memory input / output"); Console.WriteLine("- file - to use file input / output"); var choice = Console.ReadLine(); switch (choice.ToLower()) { case "console": var consoleIO = new ConsoleInputOutput(); todoApp = new TodoApp(consoleIO); break; case "inmemory": var inputs = new string[] { "buy a milk", "go to dentist" }; var inMemoryIO = new MemoryInputOutput(inputs); todoApp = new TodoApp(inMemoryIO); break; case "file": var fileIO = new FileInputOutput(); todoApp = new TodoApp(fileIO); break; default: throw new Exception("bad choice"); } while (true) { Console.WriteLine("Available commands: add, print"); choice = Console.ReadLine(); switch (choice.ToLower()) { case "add": try { todoApp.Add(); } catch (NotImplementedException) { Console.WriteLine("Option is not supported yet"); } catch (Exception exception) { Console.WriteLine(exception); } break; case "print": try { todoApp.Print(); } catch (NotImplementedException) { Console.WriteLine("Option is not supported yet"); } catch (Exception exception) { Console.WriteLine(exception); } break; default: continue; } } }