1
0

Refactoring

This commit is contained in:
Serghei Cebotari 2023-12-07 21:44:13 +03:00
parent 923f2faa1f
commit 2f7e2da782
2 changed files with 19 additions and 52 deletions

View File

@ -2,10 +2,7 @@ namespace Codeforces.Test
{ {
public class IOTester public class IOTester
{ {
private static readonly TextReader originalStdIn = Console.In;
private static StringReader? stdIn; private static StringReader? stdIn;
private static readonly TextWriter originalStdOut = Console.Out;
private static StringWriter? stdOut; private static StringWriter? stdOut;
public static void Start() public static void Start()
@ -14,14 +11,6 @@ namespace Codeforces.Test
Console.SetOut(stdOut); Console.SetOut(stdOut);
} }
public static void End()
{
stdOut = null;
stdIn = null;
Console.SetOut(originalStdOut);
Console.SetIn(originalStdIn);
}
public static void SetInput(params string[] lines) public static void SetInput(params string[] lines)
{ {
string input = string.Join(Environment.NewLine, lines); string input = string.Join(Environment.NewLine, lines);
@ -29,26 +18,12 @@ namespace Codeforces.Test
Console.SetIn(stdIn); Console.SetIn(stdIn);
} }
public static string GetOutput() public static IEnumerable<string> GetOutputLines()
{ {
if (stdOut == null) return stdOut?.ToString()
{ .Split(Environment.NewLine)
return string.Empty; .SkipLast(1)
} ?? Enumerable.Empty<string>();
else
{
return stdOut.ToString();
}
}
public static string[] GetOutputLines()
{
return GetOutput().Split(Environment.NewLine).SkipLast(1).ToArray();
}
public static IList<string> GetOutputLinesAsList()
{
return new List<string>(GetOutputLines());
} }
} }
} }

View File

@ -8,32 +8,28 @@ public class Tests
public void TestText() public void TestText()
{ {
IOTester.Start(); IOTester.Start();
string[] input = new[] string[] input =
{ [
"5", "5",
"256 42", "256 42",
"1000 1000", "1000 1000",
"-1000 1000", "-1000 1000",
"-1000 1000", "-1000 1000",
"20 22" "20 22"
}; ];
IOTester.SetInput(input); IOTester.SetInput(input);
Program.Main(); Program.Main();
string[] expectedOutput = new[] string[] expected =
{ [
"298", "298",
"2000", "2000",
"0", "0",
"0", "0",
"42" "42"
}; ];
string[] actualOutput = IOTester.GetOutputLines(); string[] actual = IOTester.GetOutputLines().ToArray();
Assert.Equal(expectedOutput.Length, actualOutput.Length); Assert.Equal(expected, actual);
for (int i = 0; i < expectedOutput.Length; i++)
{
Assert.Equal(expectedOutput[i], actualOutput[i]);
}
} }
[Theory] [Theory]
@ -46,13 +42,9 @@ public class Tests
Program.Main(); Program.Main();
string[] expectedOutput = File.ReadLines(output).ToArray(); var expectedOutput = File.ReadLines(output);
string[] actualOutput = IOTester.GetOutputLines(); var actualOutput = IOTester.GetOutputLines();
Assert.Equal(expectedOutput.Length, actualOutput.Length); Assert.Equal(expectedOutput, actualOutput);
for (int i = 0; i < expectedOutput.Length; i++)
{
Assert.Equal(expectedOutput[i], actualOutput[i]);
}
} }
} }
public class FileNameGenerator : IEnumerable<object[]> public class FileNameGenerator : IEnumerable<object[]>