Pester icon indicating copy to clipboard operation
Pester copied to clipboard

Add option for Should (-Be) to ignore, show and normalize whitespace

Open nohwnd opened this issue 5 years ago • 4 comments

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;
        }

nohwnd avatar Nov 06 '20 11:11 nohwnd

Moving to 5.3 that should be the milestone of Should.

nohwnd avatar Apr 07 '21 14:04 nohwnd

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.

nohwnd avatar Jun 21 '21 16:06 nohwnd

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.

nohwnd avatar Jun 27 '21 13:06 nohwnd

this would be nice to have, is there a way to workaround this in the meantime?

stevenbrix avatar May 06 '24 18:05 stevenbrix