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.
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();
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 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();
}
}
[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();
}
}
[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();
}
}
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(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"));
}
}
[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"));
}
}
[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"));
}
}
A B C