Skip to content

Commit

Permalink
add test level settings
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 9, 2023
1 parent ee8e9c1 commit 02dab2c
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 15 deletions.
36 changes: 36 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,39 @@ This output type gives the most information, but if verified files are long, it
+ Sixth line changed
7 Seventh line
```


### Test level settings

DiffPlex can be used at the test leved:

<!-- snippet: TestLevelUsage -->
<a id='snippet-testlevelusage'></a>
```cs
[Test]
public Task TestLevelUsage()
{
var target = "The text";
var settings = new VerifySettings();
settings.UseDiffPlex();
return Verify(target, settings);
}
```
<sup><a href='/src/Tests/Tests.cs#L97-L108' title='Snippet source file'>snippet source</a> | <a href='#snippet-testlevelusage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or Fluently

<!-- snippet: TestLevelUsageFluent -->
<a id='snippet-testlevelusagefluent'></a>
```cs
[Test]
public Task TestLevelUsageFluent()
{
var target = "The text";
return Verify(target)
.UseDiffPlex();
}
```
<sup><a href='/src/Tests/Tests.cs#L110-L120' title='Snippet source file'>snippet source</a> | <a href='#snippet-testlevelusagefluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649</NoWarn>
<Version>2.0.1</Version>
<Version>2.1.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>DiffPlex, Verify</PackageTags>
<Description>Extends Verify (https://github.com/VerifyTests/Verify) to allow comparison of text via DiffPlex (https://github.com/mmanela/diffplex).</Description>
Expand Down
22 changes: 22 additions & 0 deletions src/Tests/Tests.AtTestLevel.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
Type: VerifyException,
Message:
Directory: {ProjectDirectory}
NotEqual:
- Received: Tests.AtTestLevelFake.received.txt
Verified: Tests.AtTestLevelFake.verified.txt

FileContent:

NotEqual:

Received: Tests.AtTestLevelFake.received.txt
Verified: Tests.AtTestLevelFake.verified.txt
Compare Result:
The
- before
+ after
text


}
22 changes: 22 additions & 0 deletions src/Tests/Tests.AtTestLevelCompact.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
Type: VerifyException,
Message:
Directory: {ProjectDirectory}
NotEqual:
- Received: Tests.AtTestLevelCompactFake.received.txt
Verified: Tests.AtTestLevelCompactFake.verified.txt

FileContent:

NotEqual:

Received: Tests.AtTestLevelCompactFake.received.txt
Verified: Tests.AtTestLevelCompactFake.verified.txt
Compare Result:
13 The
- before
+ after
15 text


}
26 changes: 26 additions & 0 deletions src/Tests/Tests.AtTestLevelCompactFake.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
The
The
The
The
The
The
The
The
The
The
The
The
The
before
text
text
text
text
text
text
text
text
text
text
text
text
3 changes: 3 additions & 0 deletions src/Tests/Tests.AtTestLevelFake.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The
before
text
1 change: 1 addition & 0 deletions src/Tests/Tests.TestLevelUsage.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The text
1 change: 1 addition & 0 deletions src/Tests/Tests.TestLevelUsageFluent.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The text
76 changes: 75 additions & 1 deletion src/Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[TestFixture]
using VerifyTests.DiffPlex;

[TestFixture]
public class Tests
{
static Tests()
Expand All @@ -23,7 +25,14 @@ public Task Simple()
""",
settings));
}

[Test]
public Task AtTestLevel()
{
var settings = new VerifySettings();
settings.UseMethodName("AtTestLevelFake");
settings.DisableDiff();
settings.UseDiffPlex();

return ThrowsTask(() =>
Verify("""
Expand All @@ -34,6 +43,46 @@ public Task Simple()
settings));
}

[Test]
public Task AtTestLevelCompact()
{
var settings = new VerifySettings();
settings.UseMethodName("AtTestLevelCompactFake");
settings.DisableDiff();
settings.UseDiffPlex(OutputType.Compact);

return ThrowsTask(() =>
Verify("""
The
The
The
The
The
The
The
The
The
The
The
The
The
after
text
text
text
text
text
text
text
text
text
text
text
text
""",
settings));
}

[Test]
public Task Sample()
{
Expand All @@ -44,4 +93,29 @@ public Task Sample()
""";
return Verify(target);
}

#region TestLevelUsage

[Test]
public Task TestLevelUsage()
{
var target = "The text";
var settings = new VerifySettings();
settings.UseDiffPlex();
return Verify(target, settings);
}

#endregion

#region TestLevelUsageFluent

[Test]
public Task TestLevelUsageFluent()
{
var target = "The text";
return Verify(target)
.UseDiffPlex();
}

#endregion
}
38 changes: 25 additions & 13 deletions src/Verify.DiffPlex/VerifyDiffPlex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,37 @@ public static class VerifyDiffPlex
{
public static void Initialize() => Initialize(OutputType.Full);

public static void Initialize(OutputType outputType)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
Func<string, string, StringBuilder> compareFunc = outputType switch
static Func<string, string, StringBuilder> GetCompareFunc(OutputType outputType) =>
outputType switch
{
OutputType.Compact => CompactCompare,
_ => VerboseCompare,
_ => VerboseCompare
};

VerifierSettings.SetDefaultStringComparer((received, verified, _) =>
{
var builder = compareFunc(received, verified);
builder.TrimEnd();
var message = builder.ToString();
var compareResult = CompareResult.NotEqual(message);
return Task.FromResult(compareResult);
});
public static void Initialize(OutputType outputType)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
VerifierSettings.SetDefaultStringComparer((received, verified, _) => GetResult(outputType, received, verified));
}

static Task<CompareResult> GetResult(OutputType outputType, string received, string verified)
{
var compare = GetCompareFunc(outputType);
var builder = compare(received, verified);
builder.TrimEnd();
var message = builder.ToString();
var result = CompareResult.NotEqual(message);
return Task.FromResult(result);
}

public static void UseDiffPlex(this VerifySettings settings, OutputType outputType = OutputType.Full) =>
settings.UseStringComparer(
(received, verified, _) => GetResult(outputType, received, verified));

public static SettingsTask UseDiffPlex(this SettingsTask settings, OutputType outputType = OutputType.Full) =>
settings.UseStringComparer(
(received, verified, _) => GetResult(outputType, received, verified));

static StringBuilder VerboseCompare(string received, string verified)
{
var diff = InlineDiffBuilder.Diff(verified, received);
Expand Down

0 comments on commit 02dab2c

Please sign in to comment.