Skip to content

Commit

Permalink
Verifier before after callbacks (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored May 5, 2021
1 parent 0fdd133 commit 50b68b4
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docs/build-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,5 @@ if (BuildServerDetector.Detected)
});
}
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L79-L93' title='Snippet source file'>snippet source</a> | <a href='#snippet-derivepathinfoappveyor' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L82-L96' title='Snippet source file'>snippet source</a> | <a href='#snippet-derivepathinfoappveyor' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion docs/clipboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The clipboard behavior can be disable using the following:
```cs
VerifierSettings.DisableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L54-L58' title='Snippet source file'>snippet source</a> | <a href='#snippet-disableclipboardglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L57-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-disableclipboardglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
6 changes: 3 additions & 3 deletions docs/mdsource/verify-options.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Note that auto accepted changes in `.verified.` files remain visible in source c

### OnHandlers

`OnFirstVerify` is called when there is no verified file.

`OnVerifyMismatch` is called when a received file does not match the existing verified file.
* `OnVerify` takes two actiosn that are called before and after each verification.
* `OnFirstVerify` is called when there is no verified file.
* `OnVerifyMismatch` is called when a received file does not match the existing verified file.

snippet: OnHandlers
2 changes: 1 addition & 1 deletion docs/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ VerifierSettings.DerivePathInfo(
methodName: method.Name);
});
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L63-L74' title='Snippet source file'>snippet source</a> | <a href='#snippet-derivepathinfo' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L66-L77' title='Snippet source file'>snippet source</a> | <a href='#snippet-derivepathinfo' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Return null to any of the values to use the standard behavior. The returned path can be relative to the directory sourceFile exists in.
Expand Down
10 changes: 5 additions & 5 deletions docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ VerifierSettings.AddExtraSettings(_ =>
_.TypeNameHandling = TypeNameHandling.All;
});
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L118-L126' title='Snippet source file'>snippet source</a> | <a href='#snippet-extrasettingsglobal' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L121-L129' title='Snippet source file'>snippet source</a> | <a href='#snippet-extrasettingsglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -310,7 +310,7 @@ settings.AddExtraSettings(_ =>
_.TypeNameHandling = TypeNameHandling.All;
});
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L128-L137' title='Snippet source file'>snippet source</a> | <a href='#snippet-extrasettingsinstance' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L131-L140' title='Snippet source file'>snippet source</a> | <a href='#snippet-extrasettingsinstance' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -334,7 +334,7 @@ class CompanyConverter :
}
}
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L153-L168' title='Snippet source file'>snippet source</a> | <a href='#snippet-companyconverter' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L156-L171' title='Snippet source file'>snippet source</a> | <a href='#snippet-companyconverter' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

<!-- snippet: JsonConverter -->
Expand All @@ -346,7 +346,7 @@ VerifierSettings.AddExtraSettings(
_.Converters.Add(new CompanyConverter());
});
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L142-L150' title='Snippet source file'>snippet source</a> | <a href='#snippet-jsonconverter' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L145-L153' title='Snippet source file'>snippet source</a> | <a href='#snippet-jsonconverter' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -909,7 +909,7 @@ Extra types can be added to this mapping:
VerifierSettings.TreatAsString<ClassWithToString>(
(target, settings) => target.Property);
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L39-L44' title='Snippet source file'>snippet source</a> | <a href='#snippet-treatasstring' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L42-L47' title='Snippet source file'>snippet source</a> | <a href='#snippet-treatasstring' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
13 changes: 8 additions & 5 deletions docs/verify-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ This can be done using `AutoVerify()`:
VerifySettings settings = new();
settings.AutoVerify();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L98-L103' title='Snippet source file'>snippet source</a> | <a href='#snippet-autoverify' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L101-L106' title='Snippet source file'>snippet source</a> | <a href='#snippet-autoverify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Note that auto accepted changes in `.verified.` files remain visible in source control tooling.


### OnHandlers

`OnFirstVerify` is called when there is no verified file.

`OnVerifyMismatch` is called when a received file does not match the existing verified file.
* `OnVerify` takes two actiosn that are called before and after each verification.
* `OnFirstVerify` is called when there is no verified file.
* `OnVerifyMismatch` is called when a received file does not match the existing verified file.

<!-- snippet: OnHandlers -->
<a id='snippet-onhandlers'></a>
```cs
public Task OnHandlersSample()
{
VerifierSettings.OnVerify(
before: () => { Debug.WriteLine("before"); },
after: () => { Debug.WriteLine("after"); });
VerifierSettings.OnFirstVerify(
receivedFile =>
{
Expand All @@ -56,5 +59,5 @@ public Task OnHandlersSample()
return Verifier.Verify("value");
}
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L14-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-onhandlers' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L14-L38' title='Snippet source file'>snippet source</a> | <a href='#snippet-onhandlers' 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;xUnit1026;xUnit1013</NoWarn>
<Version>11.10.3</Version>
<Version>11.11.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Json, Testing, Verify, Snapshot, Approvals</PackageTags>
<Description>Enables verification of complex models and documents.</Description>
Expand Down
3 changes: 3 additions & 0 deletions src/Verify.Tests/Snippets/Snippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class Snippets

public Task OnHandlersSample()
{
VerifierSettings.OnVerify(
before: () => { Debug.WriteLine("before"); },
after: () => { Debug.WriteLine("after"); });
VerifierSettings.OnFirstVerify(
receivedFile =>
{
Expand Down
51 changes: 48 additions & 3 deletions src/Verify/Callbacks/VerifySettings.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,66 @@
namespace VerifyTests
using System;
using System.Threading.Tasks;

namespace VerifyTests
{
public static partial class VerifierSettings
{
internal static FirstVerify? handleOnFirstVerify;
static FirstVerify? handleOnFirstVerify;

public static void OnFirstVerify(FirstVerify firstVerify)
{
Guard.AgainstNull(firstVerify, nameof(firstVerify));
handleOnFirstVerify += firstVerify;
}

internal static VerifyMismatch? handleOnVerifyMismatch;
internal static Task RunOnFirstVerify(FilePair item)
{
if (handleOnFirstVerify == null)
{
return Task.CompletedTask;
}

return handleOnFirstVerify(item);
}

static VerifyMismatch? handleOnVerifyMismatch;

internal static Task RunOnVerifyMismatch(FilePair item, string? message)
{
if (handleOnVerifyMismatch == null)
{
return Task.CompletedTask;
}

return handleOnVerifyMismatch(item, message);
}

public static void OnVerifyMismatch(VerifyMismatch verifyMismatch)
{
Guard.AgainstNull(verifyMismatch, nameof(verifyMismatch));
handleOnVerifyMismatch += verifyMismatch;
}

public static void OnVerify(Action before, Action after)
{
Guard.AgainstNull(before, nameof(before));
Guard.AgainstNull(after, nameof(after));
beforeVerify += before;
afterVerify += after;
}

static Action? beforeVerify;

internal static void RunBeforeCallbacks()
{
beforeVerify?.Invoke();
}

static Action? afterVerify;

internal static void RunAfterCallbacks()
{
afterVerify?.Invoke();
}
}
}
2 changes: 1 addition & 1 deletion src/Verify/Counters/CounterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public int NextDateTime(DateTime input)
[ModuleInitializer]
public static void Init()
{
InnerVerifier.AddTestCallback(Start, Stop);
VerifierSettings.OnVerify(Start, Stop);
}

public static CounterContext Current
Expand Down
16 changes: 2 additions & 14 deletions src/Verify/Verifier/InnerVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace VerifyTests
public partial class InnerVerifier :
IDisposable
{
static List<(Action start, Action stop)> testCallbacks = new();
VerifySettings settings;
FileNameBuilder fileNameBuilder;

Expand All @@ -22,23 +21,12 @@ public InnerVerifier(string sourceFile, Type type, VerifySettings settings, Meth

this.settings = settings;

foreach (var (start, _) in testCallbacks)
{
start();
}
}

public static void AddTestCallback(Action start, Action stop)
{
testCallbacks.Add((start, stop));
VerifierSettings.RunBeforeCallbacks();
}

public void Dispose()
{
foreach (var (_, stop) in testCallbacks)
{
stop();
}
VerifierSettings.RunAfterCallbacks();
}
}
}
10 changes: 2 additions & 8 deletions src/Verify/Verifier/VerifyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,7 @@ void ProcessEquals()

async Task ProcessNotEquals(StringBuilder builder, FilePair item, string? message)
{
if (VerifierSettings.handleOnVerifyMismatch != null)
{
await VerifierSettings.handleOnVerifyMismatch(item, message);
}
await VerifierSettings.RunOnVerifyMismatch(item, message);

builder.AppendLine($"Received: {Path.GetFileName(item.Received)}");
builder.AppendLine($"Verified: {Path.GetFileName(item.Verified)}");
Expand Down Expand Up @@ -295,10 +292,7 @@ async Task ProcessMissing(StringBuilder builder)

async Task ProcessMissing(StringBuilder builder, FilePair item)
{
if (VerifierSettings.handleOnFirstVerify != null)
{
await VerifierSettings.handleOnFirstVerify(item);
}
await VerifierSettings.RunOnFirstVerify(item);

builder.AppendLine($"{Path.GetFileName(item.Verified)}: Empty or does not exist");
if (EmptyFiles.Extensions.IsText(item.Extension))
Expand Down

0 comments on commit 50b68b4

Please sign in to comment.