Skip to content

Commit

Permalink
add onhandlers
Browse files Browse the repository at this point in the history
fixes #68
  • Loading branch information
SimonCropp committed Mar 29, 2020
1 parent fbc58a9 commit 645244f
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/diff-tool.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ This value can be changed:
```cs
DiffRunner.MaxInstancesToLaunch(10);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L88-L92' title='File snippet `maxinstancestolaunch` was extracted from'>snippet source</a> | <a href='#snippet-maxinstancestolaunch' title='Navigate to start of snippet `maxinstancestolaunch`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L145-L149' title='File snippet `maxinstancestolaunch` was extracted from'>snippet source</a> | <a href='#snippet-maxinstancestolaunch' title='Navigate to start of snippet `maxinstancestolaunch`'>anchor</a></sup>
<!-- endsnippet -->


Expand Down Expand Up @@ -381,7 +381,7 @@ This behavior is currently supported on Windows. On Linux and OSX, diff tool ins
var settings = new VerifySettings();
settings.DisableDiff();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L29-L34' title='File snippet `disablediff` was extracted from'>snippet source</a> | <a href='#snippet-disablediff' title='Navigate to start of snippet `disablediff`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L56-L61' title='File snippet `disablediff` was extracted from'>snippet source</a> | <a href='#snippet-disablediff' title='Navigate to start of snippet `disablediff`'>anchor</a></sup>
<!-- endsnippet -->


Expand Down
4 changes: 2 additions & 2 deletions docs/named-tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static (bool Member1, string Member2, string Member3) MethodWithNamedTuple()
return (true, "A", "B");
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L139-L146' title='File snippet `methodwithnamedtuple` was extracted from'>snippet source</a> | <a href='#snippet-methodwithnamedtuple' title='Navigate to start of snippet `methodwithnamedtuple`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L196-L203' title='File snippet `methodwithnamedtuple` was extracted from'>snippet source</a> | <a href='#snippet-methodwithnamedtuple' title='Navigate to start of snippet `methodwithnamedtuple`'>anchor</a></sup>
<!-- endsnippet -->

Can be verified:
Expand All @@ -31,7 +31,7 @@ Can be verified:
```cs
await Verify(() => MethodWithNamedTuple());
```
<sup><a href='/src/Verify.Tests/Tests.cs#L132-L136' title='File snippet `verifytuple` was extracted from'>snippet source</a> | <a href='#snippet-verifytuple' title='Navigate to start of snippet `verifytuple`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L189-L193' title='File snippet `verifytuple` was extracted from'>snippet source</a> | <a href='#snippet-verifytuple' title='Navigate to start of snippet `verifytuple`'>anchor</a></sup>
<!-- endsnippet -->

Resulting in:
Expand Down
28 changes: 14 additions & 14 deletions docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var target = new GuidTarget

await Verify(target);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L69-L82' title='File snippet `guid` was extracted from'>snippet source</a> | <a href='#snippet-guid' title='Navigate to start of snippet `guid`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L126-L139' title='File snippet `guid` was extracted from'>snippet source</a> | <a href='#snippet-guid' title='Navigate to start of snippet `guid`'>anchor</a></sup>
<!-- endsnippet -->

Results in the following:
Expand Down Expand Up @@ -147,7 +147,7 @@ var target = new DateTimeTarget

await Verify(target);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L672-L688' title='File snippet `date` was extracted from'>snippet source</a> | <a href='#snippet-date' title='Navigate to start of snippet `date`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L729-L745' title='File snippet `date` was extracted from'>snippet source</a> | <a href='#snippet-date' title='Navigate to start of snippet `date`'>anchor</a></sup>
<!-- endsnippet -->

Results in the following:
Expand Down Expand Up @@ -241,7 +241,7 @@ settings.AddExtraSettings(_ =>
_.TypeNameHandling = TypeNameHandling.All;
});
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L39-L48' title='File snippet `extrasettings` was extracted from'>snippet source</a> | <a href='#snippet-extrasettings' title='Navigate to start of snippet `extrasettings`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L66-L75' title='File snippet `extrasettings` was extracted from'>snippet source</a> | <a href='#snippet-extrasettings' title='Navigate to start of snippet `extrasettings`'>anchor</a></sup>
<!-- endsnippet -->


Expand All @@ -262,7 +262,7 @@ settings.ModifySerialization(
settings.AddExtraSettings(
_ => { _.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat; });
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L53-L67' title='File snippet `scopedserializer` was extracted from'>snippet source</a> | <a href='#snippet-scopedserializer' title='Navigate to start of snippet `scopedserializer`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L80-L94' title='File snippet `scopedserializer` was extracted from'>snippet source</a> | <a href='#snippet-scopedserializer' title='Navigate to start of snippet `scopedserializer`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand Down Expand Up @@ -303,7 +303,7 @@ var target = new IgnoreTypeTarget
};
await Verify(target, settings);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L200-L218' title='File snippet `addignoretype` was extracted from'>snippet source</a> | <a href='#snippet-addignoretype' title='Navigate to start of snippet `addignoretype`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L257-L275' title='File snippet `addignoretype` was extracted from'>snippet source</a> | <a href='#snippet-addignoretype' title='Navigate to start of snippet `addignoretype`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand Down Expand Up @@ -345,7 +345,7 @@ var target = new IgnoreInstanceTarget
};
await Verify(target, settings);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L164-L183' title='File snippet `addignoreinstance` was extracted from'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Navigate to start of snippet `addignoreinstance`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L221-L240' title='File snippet `addignoreinstance` was extracted from'>snippet source</a> | <a href='#snippet-addignoreinstance' title='Navigate to start of snippet `addignoreinstance`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand Down Expand Up @@ -387,7 +387,7 @@ public Task WithObsoleteProp()
return Verify(target);
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L439-L458' title='File snippet `withobsoleteprop` was extracted from'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Navigate to start of snippet `withobsoleteprop`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L496-L515' title='File snippet `withobsoleteprop` was extracted from'>snippet source</a> | <a href='#snippet-withobsoleteprop' title='Navigate to start of snippet `withobsoleteprop`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand Down Expand Up @@ -423,7 +423,7 @@ public Task WithObsoletePropIncluded()
return Verify(target, settings);
}
```
<sup><a href='/src/Verify.Tests/Tests.cs#L460-L475' title='File snippet `withobsoletepropincluded` was extracted from'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Navigate to start of snippet `withobsoletepropincluded`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L517-L532' title='File snippet `withobsoletepropincluded` was extracted from'>snippet source</a> | <a href='#snippet-withobsoletepropincluded' title='Navigate to start of snippet `withobsoletepropincluded`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand Down Expand Up @@ -465,7 +465,7 @@ var target = new IgnoreExplicitTarget
};
await Verify(target, settings);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L240-L259' title='File snippet `ignorememberbyexpression` was extracted from'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Navigate to start of snippet `ignorememberbyexpression`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L297-L316' title='File snippet `ignorememberbyexpression` was extracted from'>snippet source</a> | <a href='#snippet-ignorememberbyexpression' title='Navigate to start of snippet `ignorememberbyexpression`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand Down Expand Up @@ -508,7 +508,7 @@ var target = new IgnoreExplicitTarget
};
await Verify(target, settings);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L265-L287' title='File snippet `ignorememberbyname` was extracted from'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Navigate to start of snippet `ignorememberbyname`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L322-L344' title='File snippet `ignorememberbyname` was extracted from'>snippet source</a> | <a href='#snippet-ignorememberbyname' title='Navigate to start of snippet `ignorememberbyname`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand Down Expand Up @@ -543,7 +543,7 @@ settings.ModifySerialization(_ => _.IgnoreMembersThatThrow<CustomException>());
var target = new WithCustomException();
await Verify(target, settings);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L315-L323' title='File snippet `ignoremembersthatthrow` was extracted from'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Navigate to start of snippet `ignoremembersthatthrow`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L372-L380' title='File snippet `ignoremembersthatthrow` was extracted from'>snippet source</a> | <a href='#snippet-ignoremembersthatthrow' title='Navigate to start of snippet `ignoremembersthatthrow`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand All @@ -568,7 +568,7 @@ settings.ModifySerialization(
var target = new WithExceptionIgnoreMessage();
await Verify(target, settings);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L380-L389' title='File snippet `ignoremembersthatthrowexpression` was extracted from'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowexpression' title='Navigate to start of snippet `ignoremembersthatthrowexpression`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L437-L446' title='File snippet `ignoremembersthatthrowexpression` was extracted from'>snippet source</a> | <a href='#snippet-ignoremembersthatthrowexpression' title='Navigate to start of snippet `ignoremembersthatthrowexpression`'>anchor</a></sup>
<!-- endsnippet -->

Result:
Expand All @@ -591,7 +591,7 @@ By default newlines in json are escaped with `\r\n`:
```cs
return Verify(new {Property ="a\r\nb"});
```
<sup><a href='/src/Verify.Tests/Tests.cs#L51-L53' title='File snippet `newlineescapedinproperty` was extracted from'>snippet source</a> | <a href='#snippet-newlineescapedinproperty' title='Navigate to start of snippet `newlineescapedinproperty`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L108-L110' title='File snippet `newlineescapedinproperty` was extracted from'>snippet source</a> | <a href='#snippet-newlineescapedinproperty' title='Navigate to start of snippet `newlineescapedinproperty`'>anchor</a></sup>
<!-- endsnippet -->

<!-- snippet: Tests.NewLineEscapedInProperty.verified.txt -->
Expand All @@ -613,7 +613,7 @@ var settings = new VerifySettings();
settings.DisableNewLineEscaping();
await Verify(new {Property = "a\r\nb"}, settings);
```
<sup><a href='/src/Verify.Tests/Tests.cs#L59-L63' title='File snippet `disablenewlineescaping` was extracted from'>snippet source</a> | <a href='#snippet-disablenewlineescaping' title='Navigate to start of snippet `disablenewlineescaping`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Tests.cs#L116-L120' title='File snippet `disablenewlineescaping` was extracted from'>snippet source</a> | <a href='#snippet-disablenewlineescaping' title='Navigate to start of snippet `disablenewlineescaping`'>anchor</a></sup>
<!-- endsnippet -->

<!-- snippet: Tests.NewLineNotEscapedInProperty.verified.txt -->
Expand Down
37 changes: 35 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Support is available via a [Tidelift Subscription](https://tidelift.com/subscrip
* [Subsequent Verification](#subsequent-verification)
* [Disable Clipboard](#disable-clipboard)
* [AutoVerify](#autoverify)
* [OnHandlers](#onhandlers)
* [Received and Verified](#received-and-verified)
* [Not valid json](#not-valid-json)
* [Extensions](#extensions)
Expand Down Expand Up @@ -451,7 +452,7 @@ The clipboard behavior can be disable using the following:
var settings = new VerifySettings();
settings.DisableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L9-L14' title='File snippet `disableclipboard` was extracted from'>snippet source</a> | <a href='#snippet-disableclipboard' title='Navigate to start of snippet `disableclipboard`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L36-L41' title='File snippet `disableclipboard` was extracted from'>snippet source</a> | <a href='#snippet-disableclipboard' title='Navigate to start of snippet `disableclipboard`'>anchor</a></sup>
<!-- endsnippet -->


Expand All @@ -469,12 +470,44 @@ This can be done using `AutoVerify()`:
var settings = new VerifySettings();
settings.AutoVerify();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L19-L24' title='File snippet `autoverify` was extracted from'>snippet source</a> | <a href='#snippet-autoverify' title='Navigate to start of snippet `autoverify`'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L46-L51' title='File snippet `autoverify` was extracted from'>snippet source</a> | <a href='#snippet-autoverify' title='Navigate to start of snippet `autoverify`'>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.

<!-- snippet: OnHandlers -->
<a id='snippet-onhandlers'/></a>
```cs
public async Task OnHandlersSample()
{
var settings = new VerifySettings();
settings.OnFirstVerify(
receivedFile =>
{
Debug.WriteLine(receivedFile);
return Task.CompletedTask;
});
settings.OnVerifyMismatch(
(receivedFile, verifiedFile) =>
{
Debug.WriteLine(receivedFile);
Debug.WriteLine(verifiedFile);
return Task.CompletedTask;
});
await Verify("value", settings);
}
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L13-L32' title='File snippet `onhandlers` was extracted from'>snippet source</a> | <a href='#snippet-onhandlers' title='Navigate to start of snippet `onhandlers`'>anchor</a></sup>
<!-- endsnippet -->


## Received and Verified

* **All `*.verified.*` files should be committed to source control.**
Expand Down
9 changes: 9 additions & 0 deletions readme.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ snippet: AutoVerify
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.

snippet: OnHandlers


## Received and Verified

* **All `*.verified.*` files should be committed to source control.**
Expand Down
34 changes: 33 additions & 1 deletion src/Verify.Tests/Snippets/Snippets.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Verify;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;

public class Snippets
public class Snippets:
VerifyBase
{
#region OnHandlers
public async Task OnHandlersSample()
{
var settings = new VerifySettings();
settings.OnFirstVerify(
receivedFile =>
{
Debug.WriteLine(receivedFile);
return Task.CompletedTask;
});
settings.OnVerifyMismatch(
(receivedFile, verifiedFile) =>
{
Debug.WriteLine(receivedFile);
Debug.WriteLine(verifiedFile);
return Task.CompletedTask;
});
await Verify("value", settings);
}
#endregion

void DisableClipboard()
{
#region DisableClipboard
Expand Down Expand Up @@ -66,4 +93,9 @@ public void ScopedSerializer()

#endregion
}

public Snippets(ITestOutputHelper output) :
base(output)
{
}
}
1 change: 1 addition & 0 deletions src/Verify.Tests/Tests.OnVerifyMismatch.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
othervalue
57 changes: 57 additions & 0 deletions src/Verify.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

// Non-nullable field is uninitialized.
#pragma warning disable CS8618
Expand All @@ -27,6 +28,62 @@ public Task ScrubCurrentDirectory()
return Verify(Environment.CurrentDirectory.TrimEnd('/', '\\'));
}

[Fact]
public async Task OnVerifyMismatch()
{
var settings = new VerifySettings();
settings.DisableDiff();
settings.DisableClipboard();
var onFirstVerifyCalled = false;
var onVerifyMismatchCalled = false;
settings.OnFirstVerify(
receivedFile =>
{
onFirstVerifyCalled = true;
return Task.CompletedTask;
});
settings.OnVerifyMismatch(
(receivedFile, verifiedFile) =>
{
Assert.NotEmpty(receivedFile);
Assert.NotNull(receivedFile);
Assert.NotEmpty(verifiedFile);
Assert.NotNull(verifiedFile);
onVerifyMismatchCalled = true;
return Task.CompletedTask;
});
await Assert.ThrowsAsync<XunitException>(() => Verify("value", settings));
Assert.False(onFirstVerifyCalled);
Assert.True(onVerifyMismatchCalled);
}

[Fact]
public async Task OnFirstVerify()
{
var settings = new VerifySettings();
settings.DisableDiff();
settings.DisableClipboard();
var onFirstVerifyCalled = false;
var onVerifyMismatchCalled = false;
settings.OnFirstVerify(
receivedFile =>
{
Assert.NotEmpty(receivedFile);
Assert.NotNull(receivedFile);
onFirstVerifyCalled = true;
return Task.CompletedTask;
});
settings.OnVerifyMismatch(
(receivedFile, verifiedFile) =>
{
onVerifyMismatchCalled = true;
return Task.CompletedTask;
});
await Assert.ThrowsAsync<XunitException>(() => Verify("value", settings));
Assert.True(onFirstVerifyCalled);
Assert.False(onVerifyMismatchCalled);
}

[Fact]
public Task ScrubTempPath()
{
Expand Down
6 changes: 6 additions & 0 deletions src/Verify/Callbacks/FirstVerify.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Threading.Tasks;

namespace Verify
{
public delegate Task FirstVerify(string receivedFile);
}
6 changes: 6 additions & 0 deletions src/Verify/Callbacks/VerifyMismatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Threading.Tasks;

namespace Verify
{
public delegate Task VerifyMismatch(string receivedFile, string verifiedFile);
}
Loading

0 comments on commit 645244f

Please sign in to comment.