Skip to content

VerifyTests/Verify.DiffPlex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.DiffPlex

Discussions Build status NuGet Status

Extends Verify to allow comparison of text via DiffPlex.

See Milestones for release notes.

NuGet package

https://nuget.org/packages/Verify.DiffPlex/

Usage

Initialize

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).

public static class ModuleInitializer
{

    [ModuleInitializer]
    public static void Initialize() =>
        VerifyDiffPlex.Initialize();


    [ModuleInitializer]
    public static void OtherInitialize()
    {
        VerifierSettings.InitializePlugins();
        VerifierSettings.ScrubLinesContaining("DiffEngineTray");
        VerifierSettings.IgnoreStackTrace();
    }
}

snippet source | anchor

Verify text

Given an existing verified file:

The
before
text

And a test:

[Test]
public async Task Sample()
{
    var target = @"The
after
text";
    await Verifier.Verify(target);
}

Diff results

When the comparison fails, the resulting differences will be included in the test result displayed to the user. This example shows the Full style of output.

Results do not match.
Differences:
Received: Tests.Sample.received.txt
Verified: Tests.Sample.verified.txt
Compare Result:
  The
- before
+ after
  text

Output types

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

[ModuleInitializer]
public static void Init() =>
    VerifyDiffPlex.Initialize(OutputType.Compact);

snippet source | anchor

OutputType.Full is the default. It shows the full contents of the received file, with differences with the received file indicated by + and -. Here's an example of Full output.

  First line
- Second line
+ Second line changed
  Third line
  Fourth line
  Fifth line
- Sixth line
+ Sixth line changed
  Seventh line
  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.

1 First line
- Second line
+ Second line changed
3 Third line

5 Fifth line
- Sixth line
+ Sixth line changed
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

DiffPlex can be used at the test level:

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

snippet source | anchor

Or Fluently

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

snippet source | anchor

About

Extends Verify to allow comparison of text via DiffPlex.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Languages