Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove VT100 chars before Assert #123

Merged
merged 3 commits into from
Mar 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/Chapter10.Tests/Listing10.12.Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter10.Listing10_12.Tests
{
Expand All @@ -13,11 +15,44 @@ public void Main_HospitalEmergencyCodes_DisplaysCodes()
@"info: Console[0]
Hospital Emergency Codes: = 'black', 'blue', 'brown', 'CBR', 'orange', 'purple', 'red', 'yellow'
warn: Console[0]
This is a test of the emergency...";
This is a test of the emergency...
";

IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, (Action<string[]>)(Program.Main),
new string[] { "black", "blue", "brown", "CBR", "orange", "purple", "red", "yellow" });
Action act = () => Program.Main(new[] {"black", "blue", "brown", "CBR",
"orange", "purple", "red", "yellow"});

var result = Execute(act);

Assert.AreEqual(expected, result);
}

private static string Execute(Action action, bool removeVT100 = true)
{
TextWriter savedOutputStream = Console.Out;
try
{
string output;
using (TextWriter writer = new StringWriter())
{
Console.SetOut(writer);
action();

output = removeVT100
? RemoveVT100(writer.ToString())
: writer.ToString();
}

return output;
}
finally
{
Console.SetOut(savedOutputStream);
}
}

private static string RemoveVT100(string removeFrom)
{
return Regex.Replace(removeFrom, "\u001b\\[\\d{1,3}m", "");
}
}
}