Add option for Should (-Be) to ignore, show and normalize whitespace
Summary of the feature request
When testing on multiple platforms working with strings (and also paths) is a hassle. You will get most differences in line endings or other whitespace which are invisible.
Add option to Should to
- Normalize line endings from \r\n to \n - this is a good tradeoff when you care about formatting but not about whitespace
- Show whitespace using \n \r \t and space using ␣ (keep newlines even when showing whitespace for easy comparison)
- Ignore whitespace (replace \s+ with " ")
How should it work? (optional)
Add an option to Should config object, and add parameters to Should -Be. -IgnoreWhiteSpace -NormalizeLineEndings -ShowWhiteSpace
You would use -IgnoreWhiteSpace when you don't care about formatting, and NormalizeLineEndings when you do care about formatting but not about which exact line endings are used.
You would use ShowWhitespace for debugging (or always):
Implementation something like this:
public static string ShowWhiteSpace(this string text)
{
// use mongolian vowel separator as placeholder for the newline that we add for formatting
var placeholder = "\u180E";
if (text.Contains(placeholder))
throw new InvalidOperationException("The text contains mongolian vowel separator character that we use as a placeholder.");
var whiteSpaced = text
.Replace("\r\n", "\\r\\n\u180E")
.Replace("\r", "\\r")
.Replace("\n", "\\n\u180E")
.Replace("\t", "\\t")
.Replace(" ", "␣")
.Replace("\u180E", "\n");
// prepend one newline to get better output from assertion where both expected
// and actual output start on the same position
return "\n" + whiteSpaced;
}
Moving to 5.3 that should be the milestone of Should.
We should first solve the issue with the maximum amount of assertions somehow... Then I can adopt assert and not exhaust all the slots for assertions.
This still needs a bit more thinking about assertions, and then all the work to port Assert into Pester. Which is more than I want to commit to in this milestone.
this would be nice to have, is there a way to workaround this in the meantime?