Skip to content

Latest commit

 

History

History
695 lines (577 loc) · 19.8 KB

scrubbers.md

File metadata and controls

695 lines (577 loc) · 19.8 KB

Scrubbers

Scrubbers run on the final string before 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 VerifierSettings.

Directory Scrubbers

  • The current solution directory will be replaced with {SolutionDirectory}. To disable use VerifierSettings.DontScrubSolutionDirectory() in a module initializer.
  • The current project directory will be replaced with {ProjectDirectory}. To disable use VerifierSettings.DontScrubProjectDirectory() in a module initializer.
  • The AppDomain.CurrentDomain.BaseDirectory will be replaced with {CurrentDirectory}.
  • The Assembly.CodeBase will be replaced with {CurrentDirectory}.
  • The Path.GetTempPath() will be replaced with {TempPath}.

Attribute data

The solution and project directory replacement functionality is achieved by adding attributes to the target assembly at compile time. For any project that references Verify, the following attributes will be added:

[assembly: AssemblyMetadata("Verify.ProjectDirectory", "C:\Code\TheSolution\Project\")]
[assembly: AssemblyMetadata("Verify.SolutionDirectory", "C:\Code\TheSolution\")]

This information can be useful to consumers when writing tests, so it is exposed via AttributeReader:

  • Project directory for an assembly: AttributeReader.GetProjectDirectory(assembly)
  • Project directory for the current executing assembly: AttributeReader.GetProjectDirectory()
  • Solution directory for an assembly: AttributeReader.GetSolutionDirectory(assembly)
  • Solution directory for the current executing assembly: AttributeReader.GetSolutionDirectory()

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

[UsesVerify]
public class ScrubbersSample
{
    [Fact]
    public Task Lines()
    {
        var settings = new VerifySettings();
        settings.ScrubLinesWithReplace(
            replaceLine: line =>
            {
                if (line.Contains("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
                    ");
    }

    [Fact]
    public Task LinesFluent()
    {
        return Verifier.Verify(
                target: @"
                        LineA
                        LineB
                        LineC
                        LineD
                        LineE
                        LineH
                        LineI
                        LineJ
                        ")
            .ScrubLinesWithReplace(
                replaceLine: line =>
                {
                    if (line.Contains("LineE"))
                    {
                        return "NoMoreLineE";
                    }

                    return line;
                })
            .ScrubLines(removeLine: line => line.Contains("J"))
            .ScrubLinesContaining("b", "D")
            .ScrubLinesContaining(StringComparison.Ordinal, "H");
    }

    [Fact]
    public Task AfterSerialization()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "7D3"
        };

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

    [Fact]
    public Task AfterSerializationFluent()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "7D3"
        };

        return Verifier.Verify(target)
            .AddScrubber(
                input => input.Replace("7D3", "TheRowVersion"));
    }

    [Fact]
    public Task RemoveOrReplace()
    {
        return Verifier.Verify(
                target: @"
                        LineA
                        LineB
                        LineC
                        ")
            .ScrubLinesWithReplace(
                replaceLine: line =>
                {
                    if (line.Contains("LineB"))
                    {
                        return null;
                    }

                    return line.ToLower();
                });
    }

    [Fact]
    public Task EmptyLines()
    {
        return Verifier.Verify(
                target: @"
                        LineA
                        
                        LineC
                        ")
            .ScrubEmptyLines();
    }
}

snippet source | anchor

NUnit

[TestFixture]
public class ScrubbersSample
{
    [Test]
    public Task Lines()
    {
        var settings = new VerifySettings();
        settings.ScrubLinesWithReplace(
            replaceLine: line =>
            {
                if (line.Contains("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 LinesFluent()
    {
        return Verifier.Verify(
                target: @"
                        LineA
                        LineB
                        LineC
                        LineD
                        LineE
                        LineH
                        LineI
                        LineJ
                        ")
            .ScrubLinesWithReplace(
                replaceLine: line =>
                {
                    if (line.Contains("LineE"))
                    {
                        return "NoMoreLineE";
                    }

                    return line;
                })
            .ScrubLines(removeLine: line => line.Contains("J"))
            .ScrubLinesContaining("b", "D")
            .ScrubLinesContaining(StringComparison.Ordinal, "H");
    }

    [Test]
    public Task AfterSerialization()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "7D3"
        };

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

    [Test]
    public Task AfterSerializationFluent()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "7D3"
        };

        return Verifier.Verify(target)
            .AddScrubber(
                s => s.Replace("7D3", "TheRowVersion"));
    }

    [Test]
    public Task RemoveOrReplace()
    {
        return Verifier.Verify(
                target: @"
                        LineA
                        LineB
                        LineC
                        ")
            .ScrubLinesWithReplace(
                replaceLine: line =>
                {
                    if (line.Contains("LineB"))
                    {
                        return null;
                    }

                    return line.ToLower();
                });
    }

    [Test]
    public Task EmptyLines()
    {
        return Verifier.Verify(
                target: @"
                        LineA
                        
                        LineC
                        ")
            .ScrubEmptyLines();
    }
}

snippet source | anchor

MSTest

[TestClass]
public class ScrubbersSample :
    VerifyBase
{
    [TestMethod]
    public Task Lines()
    {
        var settings = new VerifySettings();
        settings.ScrubLinesWithReplace(
            replaceLine: line =>
            {
                if (line.Contains("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 LinesFluent()
    {
        return Verify(
                target: @"
                    LineA
                    LineB
                    LineC
                    LineD
                    LineE
                    LineH
                    LineI
                    LineJ
                    ")
            .ScrubLinesWithReplace(
                replaceLine: line =>
                {
                    if (line.Contains("LineE"))
                    {
                        return "NoMoreLineE";
                    }

                    return line;
                })
            .ScrubLines(removeLine: line => line.Contains("J"))
            .ScrubLinesContaining("b", "D")
            .ScrubLinesContaining(StringComparison.Ordinal, "H");
    }

    [TestMethod]
    public Task AfterSerialization()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "7D3"
        };

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

    [TestMethod]
    public Task AfterSerializationFluent()
    {
        var target = new ToBeScrubbed
        {
            RowVersion = "7D3"
        };

        return Verify(target)
            .AddScrubber(
                input => input.Replace("7D3", "TheRowVersion"));
    }

    [TestMethod]
    public Task RemoveOrReplace()
    {
        return Verify(
                target: @"
                    LineA
                    LineB
                    LineC
                    ")
            .ScrubLinesWithReplace(
                replaceLine: line =>
                {
                    if (line.Contains("LineB"))
                    {
                        return null;
                    }

                    return line.ToLower();
                });
    }

    [TestMethod]
    public Task EmptyLines()
    {
        return Verify(
                target: @"
                    LineA
                    
                    LineC
                    ")
            .ScrubEmptyLines();
    }
}

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.

xUnit

[UsesVerify]
public class ScrubberLevelsSample
{
    VerifySettings classLevelSettings;

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

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

    [Fact]
    public Task UsageFluent()
    {
        return Verifier.Verify("One Two Three", classLevelSettings)
            .AddScrubber(s => s.Replace("Two", "B"));
    }

    [ModuleInitializer]
    public static void Initialize()
    {
        VerifierSettings.AddScrubber(s => s.Replace("One", "A"));
    }
}

snippet source | anchor

NUnit

[TestFixture]
public class ScrubberLevelsSample
{
    VerifySettings classLevelSettings;

    public ScrubberLevelsSample()
    {
        classLevelSettings = new();
        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);
    }

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

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

snippet source | anchor

MSTest

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

    public ScrubberLevelsSample()
    {
        classLevelSettings = new();
        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);
    }

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

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

snippet source | anchor

Result

A B C

snippet source | anchor