I had a problem with testing "Console in" and "Console" in a C # console application.
If I use Console.WriteLine and Console.ReadLine, I can use the NUnit framework to test the application, but if I use a helper class to input and output to the console, I cannot get it to work.
The helper class is https://open.kattis.com/download/Kattio.cs and uses BufferedStream to write to the console, but I cannot get my test to read it ..
It uses StreamReader for "Console in", but I get a "NoMoreTokenException", which I think does not get any input ...
I would like to use a helper class, but I cannot check with it ...
Example: TestCase:
[Test] public void test_hello_world () { using (var sw = new StringWriter ()) { Console.SetOut (sw); using (var sr = new StringReader ("Start")) { Console.SetIn (sr); MainClass.Main(new string[]{}); string expected = "Hello World!\n"; Assert.AreEqual (sw.ToString (), expected); } } }
Example: code that works:
string line = ""; if (Console.ReadLine().Equals("Start")) line = "Hello World!"; else line = "No such luck!"; Console.WriteLine (line);
Ex: code that doesn't work:
string line = ""; Scanner sc = new Scanner (); if (sc.Next ().Equals ("Start")) line = "Hello World!"; else line = "No such luck!"; BufferedStdoutWriter outWritter = new BufferedStdoutWriter (); outWritter.WriteLine (line); outWritter.Flush ();
Does anyone know how to solve this?
source share