Skip to content

Latest commit

 

History

History
431 lines (350 loc) · 13.9 KB

scrubbers.md

File metadata and controls

431 lines (350 loc) · 13.9 KB

Scrubbers

Scrubbers run on the final string prior to doing the verification action.

Multiple scrubbers can be defined at multiple levels.

Scrubber are executed in reveres order. So the most recent added method scrubber through to earliest added global scrubber.

Available Scrubbers

Scrubbers can be added to an instance of VerifySettings or globally on SharedVerifySettings.

ScrubLines

Allows lines to be selectively removed using a Func.

For example remove lines containing text:

verifySettings.ScrubLines(line => line.Contains("text"));

snippet source | anchor

ScrubLinesContaining

Remove all lines containing any of the defined strings.

For example remove lines containing text1 or text2

verifySettings.ScrubLinesContaining("text1", "text2");

snippet source | anchor

Case insensitive by default (StringComparison.OrdinalIgnoreCase).

StringComparison can be overridden:

verifySettings.ScrubLinesContaining(StringComparison.Ordinal, "text1", "text2");

snippet source | anchor

ScrubLinesWithReplace

Allows lines to be selectively replaced using a Func.

For example converts lines to upper case:

verifySettings.ScrubLinesWithReplace(line => line.ToUpper());

snippet source | anchor

ScrubMachineName

Replaces Environment.MachineName with TheMachineName.

verifySettings.ScrubMachineName();

snippet source | anchor

AddScrubber

Adds a scrubber with full control over the text via a Func

More complete example

xUnit

public class ScrubbersSample :
    VerifyBase
{
    public ScrubbersSample(ITestOutputHelper output) :
        base(output)
    {
    }
    [Fact]
    public Task Lines()
    {
        var settings = new VerifySettings();
        settings.ScrubLinesWithReplace(
            replaceLine: line =>
            {
                if (line == "LineE")
                {
                    return "NoMoreLineE";
                }
                return line;
            });
        settings.ScrubLines(removeLine: line => line.Contains("J"));
        settings.ScrubLinesContaining("b", "D");
        settings.ScrubLinesContaining(StringComparison.Ordinal, "H");
        return Verify(
            settings: settings,
            target: @"
LineA
LineB
LineC
LineD
LineE
LineH
LineI
LineJ
");
    }

    [Fact]
    public Task ScrubberAppliedAfterJsonSerialization()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "0x00000000000007D3"
        };

        var settings = new VerifySettings();
        settings.AddScrubber(
            input => input.Replace("0x00000000000007D3", "TheRowVersion"));
        return Verify(target, settings);
    }
}

snippet source | anchor

NUnit

[TestFixture]
public class ScrubbersSample
{
    [Test]
    public Task Lines()
    {
        var settings = new VerifySettings();
        settings.ScrubLinesWithReplace(
            replaceLine: line =>
            {
                if (line == "LineE")
                {
                    return "NoMoreLineE";
                }

                return line;
            });
        settings.ScrubLines(removeLine: line => line.Contains("J"));
        settings.ScrubLinesContaining("b", "D");
        settings.ScrubLinesContaining(StringComparison.Ordinal, "H");
        return Verifier.Verify(
            settings: settings,
            target: @"
LineA
LineB
LineC
LineD
LineE
LineH
LineI
LineJ
");
    }

    [Test]
    public Task ScrubberAppliedAfterJsonSerialization()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "0x00000000000007D3"
        };

        var settings = new VerifySettings();
        settings.AddScrubber(
            s => s.Replace("0x00000000000007D3", "TheRowVersion"));
        return Verifier.Verify(target, settings);
    }
}

snippet source | anchor

MSTest

[TestClass]
public class ScrubbersSample :
    VerifyBase
{
    [TestMethod]
    public Task Lines()
    {
        var settings = new VerifySettings();
        settings.ScrubLinesWithReplace(
            replaceLine: line =>
            {
                if (line == "LineE")
                {
                    return "NoMoreLineE";
                }
                return line;
            });
        settings.ScrubLines(removeLine: line => line.Contains("J"));
        settings.ScrubLinesContaining("b", "D");
        settings.ScrubLinesContaining(StringComparison.Ordinal, "H");
        return Verify(
            settings: settings,
            target: @"
LineA
LineB
LineC
LineD
LineE
LineH
LineI
LineJ
");
    }

    [TestMethod]
    public Task ScrubberAppliedAfterJsonSerialization()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "0x00000000000007D3"
        };

        var settings = new VerifySettings();
        settings.AddScrubber(
            input => input.Replace("0x00000000000007D3", "TheRowVersion"));
        return Verify(target, settings);
    }
}

snippet source | anchor

Results

LineA
LineC
NoMoreLineE
LineI

snippet source | anchor

{
  RowVersion: 'TheRowVersion'
}

snippet source | anchor

Scrubber levels

Scrubbers can be defined at three levels:

  • Method: Will run the verification in the current test method.
  • Class: As a class level 'VerifySettings' field then re-used at the method level.
  • Global: Will run for test methods on all tests.

Global scrubbers should be defined only once at appdomain startup. In this example the scrubber is configured using the Global Setup of XunitContext. It could also be configured using a Module Initializer.

xUnit

public class ScrubberLevelsSample :
    VerifyBase
{
    VerifySettings classLevelSettings;

    public ScrubberLevelsSample(ITestOutputHelper output) :
        base(output)
    {
        classLevelSettings = new VerifySettings();
        classLevelSettings.AddScrubber(s => s.Replace("Three", "C"));
    }

    [Fact]
    public Task Simple()
    {
        var settings = new VerifySettings(classLevelSettings);
        settings.AddScrubber(s => s.Replace("Two", "B"));
        return Verify("One Two Three", settings);
    }

    [GlobalSetUp]
    public static class GlobalSetup
    {
        public static void Setup()
        {
            SharedVerifySettings.AddScrubber(s => s.Replace("One", "A"));
        }
    }
}

snippet source | anchor

NUnit

[TestFixture]
public class ScrubberLevelsSample
{
    VerifySettings classLevelSettings;

    public ScrubberLevelsSample()
    {
        classLevelSettings = new VerifySettings();
        classLevelSettings.AddScrubber(s => s.Replace("Three", "C"));
    }

    [Test]
    public Task Simple()
    {
        var settings = new VerifySettings(classLevelSettings);
        settings.AddScrubber(s => s.Replace("Two", "B"));
        return Verifier.Verify("One Two Three", settings);
    }

    [OneTimeSetUp]
    public static void Setup()
    {
        SharedVerifySettings.AddScrubber(s => s.Replace("One", "A"));
    }
}

snippet source | anchor

MSTest

[TestClass]
public class ScrubberLevelsSample :
    VerifyBase
{
    VerifySettings classLevelSettings;

    public ScrubberLevelsSample()
    {
        classLevelSettings = new VerifySettings();
        classLevelSettings.AddScrubber(s => s.Replace("Three", "C"));
    }

    [TestMethod]
    public Task Simple()
    {
        var settings = new VerifySettings(classLevelSettings);
        settings.AddScrubber(s => s.Replace("Two", "B"));
        return Verify("One Two Three", settings);
    }

    [AssemblyInitialize]
    public static void Setup(TestContext testContext)
    {
        SharedVerifySettings.AddScrubber(s => s.Replace("One", "A"));
    }
}

snippet source | anchor

Result

A B C

snippet source | anchor