Skip to content

Commit

Permalink
Add OutputType.Minimal (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnyrup committed Oct 26, 2023
1 parent 5a4eb9b commit e2f4e26
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 6 deletions.
19 changes: 14 additions & 5 deletions readme.md
Expand Up @@ -20,7 +20,7 @@ https://nuget.org/packages/Verify.DiffPlex/

### Initialize

Call `VerifyDiffPlex.Initialize()` in a `[ModuleInitializer]`. Alternatively, use `VerifyDiffPlex.Initialize(OutputType.Full)` or `VerifyDiffPlex.Initialize(OutputType.Compact)` to specify the type of output (see below).
Call `VerifyDiffPlex.Initialize()` in a `[ModuleInitializer]`. Alternatively, use `VerifyDiffPlex.Initialize(OutputType.Full)`, `VerifyDiffPlex.Initialize(OutputType.Compact)` or `VerifyDiffPlex.Initialize(OutputType.Minimal)` to specify the type of output (see below).

<!-- snippet: ModuleInitializer.cs -->
<a id='snippet-ModuleInitializer.cs'></a>
Expand Down Expand Up @@ -89,7 +89,7 @@ Compare Result:

### Output types

The library currently supports two different types of diff outputs; the desired type can be specified during library initialization.
The library currently supports three different types of diff outputs; the desired type can be specified during library initialization.

<!-- snippet: OutputTypeCompact -->
<a id='snippet-outputtypecompact'></a>
Expand All @@ -116,7 +116,7 @@ public static void Init() =>
Eighth line
```

This output type gives the most information, but if verified files are long, it can be difficult to read through and find the actual differences. `OutputType.Compact` will show only the changed lines, with one line of context (with line number) before and after each changed section to help identify where the change is. The `Full` output above would look like this as `Compact`.
This output type gives the most information, but if verified files are long, it can be difficult to read through and find the actual differences. `OutputType.Compact` will show only the changed lines, with one line of context (with line number) before and after each changed section to help identify where the change is.

```
1 First line
Expand All @@ -130,6 +130,15 @@ This output type gives the most information, but if verified files are long, it
7 Seventh line
```

Lastly, there is `OutputType.Minimal` which will show only the changed lines.

```
- Second line
+ Second line changed
- Sixth line
+ Sixth line changed
```


### Test level settings

Expand All @@ -147,7 +156,7 @@ public Task TestLevelUsage()
return Verify(target, settings);
}
```
<sup><a href='/src/Tests/Tests.cs#L93-L104' title='Snippet source file'>snippet source</a> | <a href='#snippet-testlevelusage' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L111-L122' title='Snippet source file'>snippet source</a> | <a href='#snippet-testlevelusage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or Fluently
Expand All @@ -163,5 +172,5 @@ public Task TestLevelUsageFluent()
.UseDiffPlex();
}
```
<sup><a href='/src/Tests/Tests.cs#L106-L116' title='Snippet source file'>snippet source</a> | <a href='#snippet-testlevelusagefluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Tests.cs#L124-L134' title='Snippet source file'>snippet source</a> | <a href='#snippet-testlevelusagefluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
20 changes: 20 additions & 0 deletions src/Tests/Tests.AtTestLevelMinimal.verified.txt
@@ -0,0 +1,20 @@
{
Type: VerifyException,
Message:
Directory: {ProjectDirectory}
NotEqual:
- Received: Tests.AtTestLevelMinimalFake.received.txt
Verified: Tests.AtTestLevelMinimalFake.verified.txt

FileContent:

NotEqual:

Received: Tests.AtTestLevelMinimalFake.received.txt
Verified: Tests.AtTestLevelMinimalFake.verified.txt
Compare Result:
- before
+ after


}
3 changes: 3 additions & 0 deletions src/Tests/Tests.AtTestLevelMinimalFake.verified.txt
@@ -0,0 +1,3 @@
The
before
text
18 changes: 18 additions & 0 deletions src/Tests/Tests.cs
Expand Up @@ -79,6 +79,24 @@ public Task AtTestLevelCompact()
settings));
}

[Test]
public Task AtTestLevelMinimal()
{
var settings = new VerifySettings();
settings.UseMethodName("AtTestLevelMinimalFake");
settings.DisableDiff();
settings.UseDiffPlex(OutputType.Minimal);

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

[Test]
public Task Sample()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Verify.DiffPlex/OutputType.cs
@@ -1,3 +1,3 @@
namespace VerifyTests.DiffPlex;

public enum OutputType { Full, Compact }
public enum OutputType { Full, Compact, Minimal }
27 changes: 27 additions & 0 deletions src/Verify.DiffPlex/VerifyDiffPlex.cs
Expand Up @@ -10,6 +10,7 @@ public static class VerifyDiffPlex
outputType switch
{
OutputType.Compact => CompactCompare,
OutputType.Minimal => MinimalCompare,
_ => VerboseCompare
};

Expand Down Expand Up @@ -69,6 +70,32 @@ static StringBuilder VerboseCompare(string received, string verified)
return builder;
}

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

var builder = new StringBuilder();
foreach (var line in diff.Lines)
{
switch (line.Type)
{
case ChangeType.Inserted:
builder.Append("+ ");
break;
case ChangeType.Deleted:
builder.Append("- ");
break;
default:
// omit unchanged files
continue;
}

builder.AppendLine(line.Text);
}

return builder;
}

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

0 comments on commit e2f4e26

Please sign in to comment.