Skip to content

Commit

Permalink
Add AutoVerify delegate support (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Mar 12, 2024
1 parent 611dbe9 commit a9a5b6b
Show file tree
Hide file tree
Showing 23 changed files with 442 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs/build-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ if (BuildServerDetector.Detected)
});
}
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L86-L100' 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 @@ -25,7 +25,7 @@ The clipboard behavior can be enabled using the following:
```cs
ClipboardAccept.Enable();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L31-L35' title='Snippet source file'>snippet source</a> | <a href='#snippet-enableclipboard' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L30-L34' title='Snippet source file'>snippet source</a> | <a href='#snippet-enableclipboard' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
14 changes: 13 additions & 1 deletion docs/mdsource/verify-options.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@ In some scenarios it makes sense to auto-accept any changes as part of a given t

* Keeping a text representation of a Database schema in a `.verified.sql` file (see [Verify.SqlServer](https://github.com/VerifyTests/Verify.SqlServer)).

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

This can be done using `AutoVerify()`:


### Instance

snippet: AutoVerify

Or with a delegate:

snippet: AutoVerifyDelegate


### Fluent

snippet: AutoVerifyFluent

Or with a delegate:

snippet: AutoVerifyFluentDelegate


### Globally

snippet: StaticAutoVerify

Note that auto accepted changes in `.verified.` files remain visible in source control tooling.
Or with a delegate:

snippet: StaticAutoVerify


## OnHandlers
Expand Down
10 changes: 5 additions & 5 deletions docs/serializer-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Extra Json.NET settings can be made:
VerifierSettings.AddExtraSettings(
_ => _.TypeNameHandling = TypeNameHandling.All);
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L137-L142' 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#L153-L158' title='Snippet source file'>snippet source</a> | <a href='#snippet-extrasettingsglobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -237,7 +237,7 @@ var settings = new VerifySettings();
settings.AddExtraSettings(
_ => _.TypeNameHandling = TypeNameHandling.All);
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L144-L150' 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#L160-L166' title='Snippet source file'>snippet source</a> | <a href='#snippet-extrasettingsinstance' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -255,7 +255,7 @@ class CompanyConverter :
writer.WriteMember(company, company.Name, "Name");
}
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L164-L173' 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#L179-L188' title='Snippet source file'>snippet source</a> | <a href='#snippet-companyconverter' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

<!-- snippet: JsonConverter -->
Expand All @@ -264,7 +264,7 @@ class CompanyConverter :
VerifierSettings.AddExtraSettings(
_ => _.Converters.Add(new CompanyConverter()));
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L156-L161' 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#L171-L176' title='Snippet source file'>snippet source</a> | <a href='#snippet-jsonconverter' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down Expand Up @@ -1431,7 +1431,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#L67-L72' 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#L65-L70' title='Snippet source file'>snippet source</a> | <a href='#snippet-treatasstring' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
7 changes: 2 additions & 5 deletions docs/verify-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ public async Task VerifyExternalFile()
settings,
async verifySettings =>
{
using var verifier = new InnerVerifier(
sourceFile,
verifySettings
);
using var verifier = new InnerVerifier(sourceFile, verifySettings);
return await verify(verifier);
});
}
```
<sup><a href='/src/Verify.Tests/InnerVerifyTests.cs#L16-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfilewithoutunittest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/InnerVerifyTests.cs#L16-L36' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfilewithoutunittest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
53 changes: 48 additions & 5 deletions docs/verify-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ In some scenarios it makes sense to auto-accept any changes as part of a given t

* Keeping a text representation of a Database schema in a `.verified.sql` file (see [Verify.SqlServer](https://github.com/VerifyTests/Verify.SqlServer)).

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

This can be done using `AutoVerify()`:


Expand All @@ -25,7 +27,20 @@ This can be done using `AutoVerify()`:
var settings = new VerifySettings();
settings.AutoVerify();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L106-L111' 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 -->

Or with a delegate:

<!-- snippet: AutoVerifyDelegate -->
<a id='snippet-autoverifydelegate'></a>
```cs
var settings = new VerifySettings();
settings.AutoVerify(
verifiedFile =>
Path.GetExtension(verifiedFile) == "png");
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L111-L118' title='Snippet source file'>snippet source</a> | <a href='#snippet-autoverifydelegate' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -39,7 +54,22 @@ public Task AutoVerifyFluent() =>
Verify("Value")
.AutoVerify();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L114-L121' title='Snippet source file'>snippet source</a> | <a href='#snippet-autoverifyfluent' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L121-L128' title='Snippet source file'>snippet source</a> | <a href='#snippet-autoverifyfluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or with a delegate:

<!-- snippet: AutoVerifyFluentDelegate -->
<a id='snippet-autoverifyfluentdelegate'></a>
```cs
[Fact]
public Task AutoVerifyFluentDelegate() =>
Verify("Value")
.AutoVerify(
verifiedFile =>
Path.GetExtension(verifiedFile) == "png");
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L130-L139' title='Snippet source file'>snippet source</a> | <a href='#snippet-autoverifyfluentdelegate' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -58,7 +88,20 @@ public static class ModuleInitializer
<sup><a href='/src/ModuleInitDocs/AutoVerify.cs#L3-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-staticautoverify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Note that auto accepted changes in `.verified.` files remain visible in source control tooling.
Or with a delegate:

<!-- snippet: StaticAutoVerify -->
<a id='snippet-staticautoverify'></a>
```cs
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifierSettings.AutoVerify();
}
```
<sup><a href='/src/ModuleInitDocs/AutoVerify.cs#L3-L12' title='Snippet source file'>snippet source</a> | <a href='#snippet-staticautoverify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## OnHandlers
Expand Down Expand Up @@ -92,7 +135,7 @@ public Task OnHandlersSample()
return Verify("value");
}
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L38-L62' 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#L37-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-onhandlers' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -111,5 +154,5 @@ To disable diff launching:
var settings = new VerifySettings();
settings.DisableDiff();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L126-L131' title='Snippet source file'>snippet source</a> | <a href='#snippet-disablediff' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L143-L148' title='Snippet source file'>snippet source</a> | <a href='#snippet-disablediff' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ await Verify(
Bars = await repo.GetBars(id)
});
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L186-L195' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfuncoftaskoft' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L200-L209' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfuncoftaskoft' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
16 changes: 16 additions & 0 deletions src/ModuleInitDocs/AutoVerifyDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ReSharper disable UnusedParameter.Local
public class AutoVerifyDelegate
{
#region StaticAutoVerifyDelegate

public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init() =>
VerifierSettings.AutoVerify(
(typeName, methodName, verifiedFile) =>
Path.GetExtension(verifiedFile) == "png");
}

#endregion
}
94 changes: 94 additions & 0 deletions src/StaticSettingsTests/AutoVerify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,98 @@ public async Task Simple()
await Verify("Foo");
File.Delete(fullPath);
}

[Fact]
public async Task DelegateTrue()
{
var funcCalled = false;
VerifierSettings.AutoVerify(
(typeName, methodName, verifiedFile) =>
{
Assert.NotNull(typeName);
Assert.NotNull(methodName);
Assert.NotNull(verifiedFile);
funcCalled = true;
return true;
});
var path = CurrentFile.Relative("AutoVerify.DelegateTrue.verified.txt");
var fullPath = Path.GetFullPath(path);
File.Delete(fullPath);
await Verify("Foo");
Assert.True(funcCalled);
File.Delete(fullPath);
}

[Fact]
public async Task DelegateTrueCustomNames()
{
var funcCalled = false;
VerifierSettings.AutoVerify(
(typeName, methodName, verifiedFile) =>
{
Assert.Equal("diffTypeNameTrue", typeName);
Assert.Equal("diffMethodName", methodName);
Assert.NotNull(verifiedFile);
funcCalled = true;
return true;
});
var path = CurrentFile.Relative("diffTypeNameTrue.diffMethodName.verified.txt");
var fullPath = Path.GetFullPath(path);
File.Delete(fullPath);
await Verify("Foo")
.UseTypeName("diffTypeNameTrue")
.UseMethodName("diffMethodName");
Assert.True(funcCalled);
File.Delete(fullPath);
}

[Fact]
public async Task DelegateFalse()
{
var funcCalled = false;
VerifierSettings.AutoVerify(
(typeName, methodName, verifiedFile) =>
{
Assert.NotNull(typeName);
Assert.NotNull(methodName);
Assert.NotNull(verifiedFile);
funcCalled = true;
return false;
});
var path = CurrentFile.Relative("AutoVerify.DelegateFalse.verified.txt");
var fullPath = Path.GetFullPath(path);
File.Delete(fullPath);
await Assert.ThrowsAsync<VerifyException>(
() => Verify("Foo")
.DisableDiff());
Assert.False(File.Exists(fullPath));
Assert.True(funcCalled);
File.Delete(fullPath);
}

[Fact]
public async Task DelegateFalseCustomNames()
{
var funcCalled = false;
VerifierSettings.AutoVerify(
(typeName, methodName, verifiedFile) =>
{
Assert.Equal("diffTypeNameFalse", typeName);
Assert.Equal("diffMethodName", methodName);
Assert.NotNull(verifiedFile);
funcCalled = true;
return false;
});
var path = CurrentFile.Relative("diffTypeNameFalse.diffMethodName.verified.txt");
var fullPath = Path.GetFullPath(path);
File.Delete(fullPath);
await Assert.ThrowsAsync<VerifyException>(
() => Verify("Foo")
.DisableDiff()
.UseTypeName("diffTypeNameFalse")
.UseMethodName("diffMethodName"));
Assert.False(File.Exists(fullPath));
Assert.True(funcCalled);
File.Delete(fullPath);
}
}

0 comments on commit a9a5b6b

Please sign in to comment.