Scrubbers run on the final string before doing the verification action.
Multiple scrubbers can be defined at multiple levels.
By default scrubber are executed in reverse order. So the most recent added method scrubber through to earliest added global scrubber. All scrubber APIs support a ScrubberLocation location
. To execute a scrubber last use ScrubberLocation.Last
.
Scrubbers can be added multiple times to have them execute multiple times. This can be helpful when compounding multiple scrubbers together.
Scrubbers can be added to an instance of VerifySettings
or globally on VerifierSettings
.
- The current solution directory will be replaced with
{SolutionDirectory}
. To disable useVerifierSettings.DontScrubSolutionDirectory()
in a module initializer. - The current project directory will be replaced with
{ProjectDirectory}
. To disable useVerifierSettings.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}
.
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()
Allows lines to be selectively removed using a Func
.
For example remove lines containing text
:
verifySettings.ScrubLines(line => line.Contains("text"));
Remove all lines containing any of the defined strings.
For example remove lines containing text1
or text2
verifySettings.ScrubLinesContaining("text1", "text2");
Case insensitive by default (StringComparison.OrdinalIgnoreCase).
StringComparison
can be overridden:
verifySettings.ScrubLinesContaining(StringComparison.Ordinal, "text1", "text2");
Allows lines to be selectively replaced using a Func
.
For example converts lines to upper case:
verifySettings.ScrubLinesWithReplace(line => line.ToUpper());
Replaces Environment.MachineName
with TheMachineName
.
verifySettings.ScrubMachineName();
Replaces Environment.UserName
with TheUserName
.
verifySettings.ScrubUserName();
Adds a scrubber with full control over the text via a Func
[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 Verify(
settings: settings,
target: @"
LineA
LineB
LineC
LineD
LineE
LineH
LineI
LineJ
");
}
[Fact]
public Task EmptyLine()
{
var settings = new VerifySettings();
settings.ScrubLinesWithReplace(
replaceLine: _ => "");
return Verify(
settings: settings,
target: @"");
}
[Fact]
public Task LinesFluent() =>
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(
_ => _.Replace("7D3", "TheRowVersion"));
return Verify(target, settings);
}
[Fact]
public Task AfterSerializationFluent()
{
var target = new ToBeScrubbed
{
RowVersion = "7D3"
};
return Verify(target)
.AddScrubber(_ => _.Replace("7D3", "TheRowVersion"));
}
[Fact]
public Task RemoveOrReplace() =>
Verify(
target: @"
LineA
LineB
LineC
")
.ScrubLinesWithReplace(
replaceLine: line =>
{
if (line.Contains("LineB"))
{
return null;
}
return line.ToLower();
});
[Fact]
public Task EmptyLines() =>
Verify(
target: @"
LineA
LineC
")
.ScrubEmptyLines();
}
[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 Verify(
settings: settings,
target: @"
LineA
LineB
LineC
LineD
LineE
LineH
LineI
LineJ
");
}
[Test]
public Task LinesFluent() =>
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(_ => _.Replace("7D3", "TheRowVersion"));
return Verify(target, settings);
}
[Test]
public Task AfterSerializationFluent()
{
var target = new ToBeScrubbed
{
RowVersion = "7D3"
};
return Verify(target)
.AddScrubber(_ => _.Replace("7D3", "TheRowVersion"));
}
[Test]
public Task RemoveOrReplace() =>
Verify(
target: @"
LineA
LineB
LineC
")
.ScrubLinesWithReplace(
replaceLine: line =>
{
if (line.Contains("LineB"))
{
return null;
}
return line.ToLower();
});
[Test]
public Task EmptyLines() =>
Verify(
target: @"
LineA
LineC
")
.ScrubEmptyLines();
}
[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() =>
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(_ => _.Replace("7D3", "TheRowVersion"));
return Verify(target, settings);
}
[TestMethod]
public Task AfterSerializationFluent()
{
var target = new ToBeScrubbed
{
RowVersion = "7D3"
};
return Verify(target)
.AddScrubber(_ => _.Replace("7D3", "TheRowVersion"));
}
[TestMethod]
public Task RemoveOrReplace() =>
Verify(
target: @"
LineA
LineB
LineC
")
.ScrubLinesWithReplace(
replaceLine: line =>
{
if (line.Contains("LineB"))
{
return null;
}
return line.ToLower();
});
[TestMethod]
public Task EmptyLines() =>
Verify(
target: @"
LineA
LineC
")
.ScrubEmptyLines();
}
LineA
LineC
NoMoreLineE
LineI
{
RowVersion: TheRowVersion
}
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.
[UsesVerify]
public class ScrubberLevelsSample
{
VerifySettings classLevelSettings;
public ScrubberLevelsSample()
{
classLevelSettings = new();
classLevelSettings.AddScrubber(_ => _.Replace("Three", "C"));
}
[Fact]
public Task Usage()
{
var settings = new VerifySettings(classLevelSettings);
settings.AddScrubber(_ => _.Replace("Two", "B"));
return Verify("One Two Three", settings);
}
[Fact]
public Task UsageFluent() =>
Verify("One Two Three", classLevelSettings)
.AddScrubber(_ => _.Replace("Two", "B"));
[ModuleInitializer]
public static void Initialize() =>
VerifierSettings.AddScrubber(_ => _.Replace("One", "A"));
}
[TestFixture]
public class ScrubberLevelsSample
{
VerifySettings classLevelSettings;
public ScrubberLevelsSample()
{
classLevelSettings = new();
classLevelSettings.AddScrubber(_ => _.Replace("Three", "C"));
}
[Test]
public Task Simple()
{
var settings = new VerifySettings(classLevelSettings);
settings.AddScrubber(_ => _.Replace("Two", "B"));
return Verify("One Two Three", settings);
}
[Test]
public Task SimpleFluent() =>
Verify("One Two Three", classLevelSettings)
.AddScrubber(_ => _.Replace("Two", "B"));
[OneTimeSetUp]
public static void Setup() =>
VerifierSettings.AddScrubber(_ => _.Replace("One", "A"));
}
[TestClass]
public class ScrubberLevelsSample :
VerifyBase
{
VerifySettings classLevelSettings;
public ScrubberLevelsSample()
{
classLevelSettings = new();
classLevelSettings.AddScrubber(_ => _.Replace("Three", "C"));
}
[TestMethod]
public Task Simple()
{
var settings = new VerifySettings(classLevelSettings);
settings.AddScrubber(_ => _.Replace("Two", "B"));
return Verify("One Two Three", settings);
}
[TestMethod]
public Task SimpleFluent() =>
Verify("One Two Three", classLevelSettings)
.AddScrubber(_ => _.Replace("Two", "B"));
[AssemblyInitialize]
public static void Setup(TestContext testContext) =>
VerifierSettings.AddScrubber(_ => _.Replace("One", "A"));
}
A B C