Skip to content

Commit

Permalink
Use split mode for unique directory configurability per test (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jan 9, 2023
1 parent 460e438 commit cdbef11
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 13 deletions.
20 changes: 10 additions & 10 deletions docs/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var settings = new VerifySettings();
settings.UseDirectory("CustomDirectory");
await Verify("valueUseDirectory", settings);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L179-L185' title='Snippet source file'>snippet source</a> | <a href='#snippet-usedirectory' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L202-L208' title='Snippet source file'>snippet source</a> | <a href='#snippet-usedirectory' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

<!-- snippet: UseDirectoryFluent -->
Expand All @@ -36,7 +36,7 @@ await Verify("valueUseDirectory", settings);
await Verify("valueUseDirectoryFluent")
.UseDirectory("CustomDirectory");
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L191-L196' title='Snippet source file'>snippet source</a> | <a href='#snippet-usedirectoryfluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L214-L219' title='Snippet source file'>snippet source</a> | <a href='#snippet-usedirectoryfluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in `CustomDirectory/TypeName.MethodName.verified.txt`.
Expand All @@ -55,7 +55,7 @@ var settings = new VerifySettings();
settings.UseTypeName("CustomTypeName");
await Verify("valueUseTypeName", settings);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L290-L296' title='Snippet source file'>snippet source</a> | <a href='#snippet-usetypename' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L313-L319' title='Snippet source file'>snippet source</a> | <a href='#snippet-usetypename' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

<!-- snippet: UseTypeNameFluent -->
Expand All @@ -64,7 +64,7 @@ await Verify("valueUseTypeName", settings);
await Verify("valueUseTypeNameFluent")
.UseTypeName("CustomTypeName");
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L302-L307' title='Snippet source file'>snippet source</a> | <a href='#snippet-usetypenamefluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L325-L330' title='Snippet source file'>snippet source</a> | <a href='#snippet-usetypenamefluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in `CustomTypeName.MethodName.verified.txt`.
Expand All @@ -81,7 +81,7 @@ var settings = new VerifySettings();
settings.UseMethodName("CustomMethodName");
await Verify("valueUseMethodName", settings);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L313-L319' title='Snippet source file'>snippet source</a> | <a href='#snippet-usemethodname' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L336-L342' title='Snippet source file'>snippet source</a> | <a href='#snippet-usemethodname' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in `TestClass.CustomMethodName.verified.txt`.
Expand All @@ -92,7 +92,7 @@ Will result in `TestClass.CustomMethodName.verified.txt`.
await Verify("valueUseMethodNameFluent")
.UseMethodName("CustomMethodNameFluent");
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L325-L330' title='Snippet source file'>snippet source</a> | <a href='#snippet-usemethodnamefluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L348-L353' title='Snippet source file'>snippet source</a> | <a href='#snippet-usemethodnamefluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in `TestClass.CustomMethodNameFluent.verified.txt`.
Expand Down Expand Up @@ -129,7 +129,7 @@ var settings = new VerifySettings();
settings.UseFileName("CustomFileName");
await Verify("valueUseFileName", settings);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L147-L153' title='Snippet source file'>snippet source</a> | <a href='#snippet-usefilename' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L170-L176' title='Snippet source file'>snippet source</a> | <a href='#snippet-usefilename' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in `CustomFileName.verified.txt`.
Expand All @@ -140,7 +140,7 @@ Will result in `CustomFileName.verified.txt`.
await Verify("valueUseFileNameFluent")
.UseFileName("CustomFileNameFluent");
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L168-L173' title='Snippet source file'>snippet source</a> | <a href='#snippet-usefilenamefluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L191-L196' title='Snippet source file'>snippet source</a> | <a href='#snippet-usefilenamefluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in `UseFileNameFluent.verified.txt`.
Expand Down Expand Up @@ -497,7 +497,7 @@ To access the current Namer `Runtime` or `RuntimeAndVersion` strings use:
Debug.WriteLine(Namer.Runtime);
Debug.WriteLine(Namer.RuntimeAndVersion);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L336-L341' title='Snippet source file'>snippet source</a> | <a href='#snippet-accessnamerruntimeandversion' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L359-L364' title='Snippet source file'>snippet source</a> | <a href='#snippet-accessnamerruntimeandversion' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -612,7 +612,7 @@ var settings = new VerifySettings();
settings.UseUniqueDirectory();
await Verify("TheValue", settings);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L202-L208' title='Snippet source file'>snippet source</a> | <a href='#snippet-useuniquedirectory' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L225-L231' title='Snippet source file'>snippet source</a> | <a href='#snippet-useuniquedirectory' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
4 changes: 2 additions & 2 deletions docs/parameterised.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public Task UseTextForParametersFluent(string arg) =>
Verify(arg + "UseTextForParametersFluent")
.UseTextForParameters(arg);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L370-L389' title='Snippet source file'>snippet source</a> | <a href='#snippet-usetextforparameters' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L393-L412' title='Snippet source file'>snippet source</a> | <a href='#snippet-usetextforparameters' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in:
Expand Down Expand Up @@ -416,7 +416,7 @@ public Task IgnoreParametersForVerifiedFluent(string arg) =>
Verify("valueIgnoreParametersForVerifiedFluent")
.IgnoreParametersForVerified(arg);
```
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L442-L461' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoreparametersforverified' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Naming/NamerTests.cs#L465-L484' title='Snippet source file'>snippet source</a> | <a href='#snippet-ignoreparametersforverified' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Results in:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Value
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Value
23 changes: 23 additions & 0 deletions src/SplitUniqueDirectoryMode/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,27 @@ public Task FileName() =>
Verify("FileName")
.UseUniqueDirectory()
.UseFileName("TheFileName");

#region DontUseSplitModeForUniqueDirectory

[Fact]
public Task DontUseSplitModeForUniqueDirectory()
{
var settings = new VerifySettings();
settings.UseUniqueDirectory();
settings.DontUseSplitModeForUniqueDirectory();
return Verify("Value", settings);
}

#endregion

#region DontUseSplitModeForUniqueDirectory_Fluent

[Fact]
public Task DontUseSplitModeForUniqueDirectory_Fluent() =>
Verify("Value")
.UseUniqueDirectory()
.DontUseSplitModeForUniqueDirectory();

#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Value
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Value
23 changes: 23 additions & 0 deletions src/Verify.Tests/Naming/NamerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ public Task Runtime()
return Verify(Namer.Runtime, settings);
}

#region UseSplitModeForUniqueDirectoryForTest

[Fact]
public Task UseSplitModeForUniqueDirectory()
{
var settings = new VerifySettings();
settings.UseUniqueDirectory();
settings.UseSplitModeForUniqueDirectory();
return Verify("Value", settings);
}

#endregion

#region UseSplitModeForUniqueDirectoryForTestFluent

[Fact]
public Task UseSplitModeForUniqueDirectoryFluent() =>
Verify("Value")
.UseUniqueDirectory()
.UseSplitModeForUniqueDirectory();

#endregion

[Fact]
public Task RuntimeFluent() =>
Verify(Namer.Runtime)
Expand Down
16 changes: 16 additions & 0 deletions src/Verify/Naming/VerifySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,20 @@ public void UniqueForOSPlatform() =>
/// </summary>
public void DisableRequireUniquePrefix() =>
UniquePrefixDisabled = true;

/// <summary>
/// Use the current runtime to make the test results unique.
/// Used when a test produces different results based on runtime.
/// </summary>
public void UseSplitModeForUniqueDirectory() =>
UseUniqueDirectorySplitMode = true;

/// <summary>
/// Dont use the current runtime to make the test results unique.
/// Overrides <see cref="VerifierSettings.UseSplitModeForUniqueDirectory"/>.
/// </summary>
public void DontUseSplitModeForUniqueDirectory() =>
UseUniqueDirectorySplitMode = false;

internal bool? UseUniqueDirectorySplitMode;
}
20 changes: 20 additions & 0 deletions src/Verify/SettingsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ public SettingsTask UseTextForParameters(string parametersText)
return this;
}

/// <summary>
/// Use the current runtime to make the test results unique.
/// Used when a test produces different results based on runtime.
/// </summary>
public SettingsTask UseSplitModeForUniqueDirectory()
{
CurrentSettings.UseSplitModeForUniqueDirectory();
return this;
}

/// <summary>
/// Dont use the current runtime to make the test results unique.
/// Overrides <see cref="VerifierSettings.UseSplitModeForUniqueDirectory"/>.
/// </summary>
public SettingsTask DontUseSplitModeForUniqueDirectory()
{
CurrentSettings.DontUseSplitModeForUniqueDirectory();
return this;
}

public VerifySettings CurrentSettings => settings ??= new();

public Task<VerifyResult> ToTask() =>
Expand Down
12 changes: 11 additions & 1 deletion src/Verify/Verifier/InnerVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void InitForDirectoryConvention(string uniquenessVerified, string typeAndMethod,
verifiedPrefix = $"{typeAndMethod}{parameters}{uniquenessVerified}";
}

if (VerifierSettings.UseUniqueDirectorySplitMode)
if (ShouldUseUniqueDirectorySplitMode(settings))
{
var directoryPrefix = Path.Combine(directory, verifiedPrefix);
var verifiedDirectory = $"{directoryPrefix}.verified";
Expand Down Expand Up @@ -145,6 +145,16 @@ void InitForDirectoryConvention(string uniquenessVerified, string typeAndMethod,
}
}

static bool ShouldUseUniqueDirectorySplitMode(VerifySettings settings)
{
if (settings.UseUniqueDirectorySplitMode == null)
{
return VerifierSettings.UseUniqueDirectorySplitMode;
}

return settings.UseUniqueDirectorySplitMode.Value;
}

void InitForFileConvention(string sharedUniqueness, Namer namer, string uniquenessVerified, string typeAndMethod, string parameters)
{
var uniquenessReceived = sharedUniqueness;
Expand Down
1 change: 1 addition & 0 deletions src/Verify/VerifySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public VerifySettings(VerifySettings? settings)
parametersText = settings.parametersText;
fileName = settings.fileName;
UniquePrefixDisabled = settings.UniquePrefixDisabled;
UseUniqueDirectorySplitMode = settings.UseUniqueDirectorySplitMode;
Namer = new(settings.Namer);
foreach (var append in settings.Appends)
{
Expand Down

0 comments on commit cdbef11

Please sign in to comment.