зеркало из https://github.com/VerifyTests/Verify.git
pass settings to comparer
This commit is contained in:
Родитель
fb88a0ce66
Коммит
d6d3c2e2fa
|
@ -116,5 +116,5 @@ static async Task<int> ReadBufferAsync(Stream stream, byte[] buffer)
|
|||
return bytesRead;
|
||||
}
|
||||
```
|
||||
<sup><a href='/src/Verify/Compare/FileComparer.cs#L71-L119' title='File snippet `defualtcompare` was extracted from'>snippet source</a> | <a href='#snippet-defualtcompare' title='Navigate to start of snippet `defualtcompare`'>anchor</a></sup>
|
||||
<sup><a href='/src/Verify/Compare/FileComparer.cs#L75-L123' title='File snippet `defualtcompare` was extracted from'>snippet source</a> | <a href='#snippet-defualtcompare' title='Navigate to start of snippet `defualtcompare`'>anchor</a></sup>
|
||||
<!-- endsnippet -->
|
||||
|
|
|
@ -21,7 +21,7 @@ static (bool Member1, string Member2, string Member3) MethodWithNamedTuple()
|
|||
return (true, "A", "B");
|
||||
}
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L93-L100' 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#L110-L117' 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:
|
||||
|
@ -31,7 +31,7 @@ Can be verified:
|
|||
```cs
|
||||
await Verify(() => MethodWithNamedTuple());
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L86-L90' 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#L103-L107' 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:
|
||||
|
|
|
@ -146,7 +146,7 @@ var target = new DateTimeTarget
|
|||
|
||||
await Verify(target);
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L623-L639' 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#L640-L656' 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:
|
||||
|
@ -302,7 +302,7 @@ var target = new IgnoreTypeTarget
|
|||
};
|
||||
await Verify(target, settings);
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L154-L172' 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#L171-L189' 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:
|
||||
|
@ -344,7 +344,7 @@ var target = new IgnoreInstanceTarget
|
|||
};
|
||||
await Verify(target, settings);
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L118-L137' 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#L135-L154' 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:
|
||||
|
@ -386,7 +386,7 @@ public Task WithObsoleteProp()
|
|||
return Verify(target);
|
||||
}
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L390-L409' 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#L407-L426' 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:
|
||||
|
@ -422,7 +422,7 @@ public Task WithObsoletePropIncluded()
|
|||
return Verify(target, settings);
|
||||
}
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L411-L426' 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#L428-L443' 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:
|
||||
|
@ -464,7 +464,7 @@ var target = new IgnoreExplicitTarget
|
|||
};
|
||||
await Verify(target, settings);
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L194-L213' 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#L211-L230' 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:
|
||||
|
@ -505,7 +505,7 @@ var target = new IgnoreExplicitTarget
|
|||
};
|
||||
await Verify(target, settings);
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L219-L239' 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#L236-L256' 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:
|
||||
|
@ -540,7 +540,7 @@ settings.ModifySerialization(_ => _.IgnoreMembersThatThrow<CustomException>());
|
|||
var target = new WithCustomException();
|
||||
await Verify(target, settings);
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L266-L274' 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#L283-L291' 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:
|
||||
|
@ -565,7 +565,7 @@ settings.ModifySerialization(
|
|||
var target = new WithExceptionIgnoreMessage();
|
||||
await Verify(target, settings);
|
||||
```
|
||||
<sup><a href='/src/Verify.Tests/Tests.cs#L331-L340' 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#L348-L357' 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:
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<Project>
|
||||
<PropertyGroup>
|
||||
<NoWarn>CS1591;CS0649</NoWarn>
|
||||
<Version>1.21.0</Version>
|
||||
<Version>1.22.0</Version>
|
||||
<PackageTags>Json, Testing, Verify, Snapshot, Approvals</PackageTags>
|
||||
<Description>Enables simple verification of complex models and documents.</Description>
|
||||
<!-- below required for TargetLib and Shared -->
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Verify;
|
||||
using VerifyXunit;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
@ -13,13 +14,14 @@ public class FileComparerTests :
|
|||
File.Copy("sample.bmp", "sample.tmp", true);
|
||||
try
|
||||
{
|
||||
Assert.True(await FileComparer.DefaultCompare("sample.bmp", "sample.tmp"));
|
||||
Assert.True(await FileComparer.DefaultCompare(VerifySettings.Default, "sample.bmp", "sample.tmp"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete("sample.tmp");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BinaryNotEqualsSameLength()
|
||||
{
|
||||
|
@ -32,7 +34,7 @@ public class FileComparerTests :
|
|||
|
||||
try
|
||||
{
|
||||
Assert.False(await FileComparer.DefaultCompare("sample.bmp", "sample.tmp"));
|
||||
Assert.False(await FileComparer.DefaultCompare(VerifySettings.Default, "sample.bmp", "sample.tmp"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
@ -43,7 +45,7 @@ public class FileComparerTests :
|
|||
[Fact]
|
||||
public async Task BinaryNotEquals()
|
||||
{
|
||||
Assert.False(await FileComparer.DefaultCompare("sample.bmp", "sample.txt"));
|
||||
Assert.False(await FileComparer.DefaultCompare(VerifySettings.Default, "sample.bmp", "sample.txt"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -57,7 +59,7 @@ public class FileComparerTests :
|
|||
FileAccess.Read,
|
||||
FileShare.Read))
|
||||
{
|
||||
Assert.True(await FileComparer.DefaultCompare("sample.bmp", "sample.tmp"));
|
||||
Assert.True(await FileComparer.DefaultCompare(VerifySettings.Default, "sample.bmp", "sample.tmp"));
|
||||
}
|
||||
}
|
||||
finally
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
|
|
@ -63,6 +63,23 @@ public class Tests :
|
|||
#endregion
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SettingsArePassed()
|
||||
{
|
||||
VerifySettings? fromGlobal = null;
|
||||
SharedVerifySettings.RegisterComparer(
|
||||
"SettingsArePassed",
|
||||
(verifySettings, stream1, stream2) =>
|
||||
{
|
||||
fromGlobal = verifySettings;
|
||||
return true;
|
||||
});
|
||||
var settings = new VerifySettings();
|
||||
settings.UseExtension("SettingsArePassed");
|
||||
await Verify(new MemoryStream(new byte[]{1}), settings);
|
||||
Assert.Same(fromGlobal, settings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldUseShortTypeName()
|
||||
{
|
||||
|
|
|
@ -29,11 +29,11 @@ static class FileComparer
|
|||
{
|
||||
if (settings.comparer != null)
|
||||
{
|
||||
return DoCompare(file.Received, file.Verified, settings.comparer);
|
||||
return DoCompare(settings, file.Received, file.Verified, settings.comparer);
|
||||
}
|
||||
if (SharedVerifySettings.TryGetComparer(file.Extension, out var comparer))
|
||||
{
|
||||
return DoCompare(file.Received, file.Verified, comparer);
|
||||
return DoCompare(settings, file.Received, file.Verified, comparer);
|
||||
}
|
||||
|
||||
if (!FilesAreSameSize(file))
|
||||
|
@ -41,12 +41,16 @@ static class FileComparer
|
|||
return Task.FromResult(false);
|
||||
}
|
||||
|
||||
return DefaultCompare(file.Received, file.Verified);
|
||||
return DefaultCompare(settings, file.Received, file.Verified);
|
||||
}
|
||||
|
||||
public static Task<bool> DefaultCompare(string received, string verified)
|
||||
public static Task<bool> DefaultCompare(VerifySettings settings, string received, string verified)
|
||||
{
|
||||
return DoCompare(received, verified, StreamsAreEqual);
|
||||
return DoCompare(
|
||||
settings,
|
||||
received,
|
||||
verified,
|
||||
(verifySettings, stream1, stream2) => StreamsAreEqual(stream1, stream2));
|
||||
}
|
||||
|
||||
static bool FilesAreSameSize(FilePair file)
|
||||
|
@ -56,7 +60,7 @@ static class FileComparer
|
|||
return first.Length == second.Length;
|
||||
}
|
||||
|
||||
static async Task<bool> DoCompare(string first, string second, Func<Stream, Stream, Task<bool>> compare)
|
||||
static async Task<bool> DoCompare(VerifySettings settings, string first, string second, Func<VerifySettings, Stream, Stream, Task<bool>> compare)
|
||||
{
|
||||
#if NETSTANDARD2_1
|
||||
await using var fs1 = FileHelpers.OpenRead(first);
|
||||
|
@ -65,7 +69,7 @@ static class FileComparer
|
|||
using var fs1 = FileHelpers.OpenRead(first);
|
||||
using var fs2 = FileHelpers.OpenRead(second);
|
||||
#endif
|
||||
return await compare(fs1, fs2);
|
||||
return await compare(settings, fs1, fs2);
|
||||
}
|
||||
|
||||
#region DefualtCompare
|
||||
|
|
|
@ -7,9 +7,9 @@ namespace Verify
|
|||
{
|
||||
public static partial class SharedVerifySettings
|
||||
{
|
||||
static Dictionary<string, Func<Stream, Stream, Task<bool>>> comparers = new Dictionary<string, Func<Stream, Stream, Task<bool>>>();
|
||||
static Dictionary<string, Func<VerifySettings, Stream, Stream, Task<bool>>> comparers = new Dictionary<string, Func<VerifySettings, Stream, Stream, Task<bool>>>();
|
||||
|
||||
internal static bool TryGetComparer(string extension, out Func<Stream, Stream, Task<bool>> comparer)
|
||||
internal static bool TryGetComparer(string extension, out Func<VerifySettings, Stream, Stream, Task<bool>> comparer)
|
||||
{
|
||||
return comparers.TryGetValue(extension, out comparer);
|
||||
}
|
||||
|
@ -21,12 +21,32 @@ namespace Verify
|
|||
Guard.AgainstNull(func, nameof(func));
|
||||
RegisterComparer(
|
||||
extension,
|
||||
(stream1, stream2) => Task.FromResult(func(stream1, stream2)));
|
||||
(settings, stream1, stream2) => Task.FromResult(func(stream1, stream2)));
|
||||
}
|
||||
|
||||
public static void RegisterComparer(
|
||||
string extension,
|
||||
Func<VerifySettings, Stream, Stream, bool> func)
|
||||
{
|
||||
Guard.AgainstNull(func, nameof(func));
|
||||
RegisterComparer(
|
||||
extension,
|
||||
(settings, stream1, stream2) => Task.FromResult(func(settings, stream1, stream2)));
|
||||
}
|
||||
|
||||
public static void RegisterComparer(
|
||||
string extension,
|
||||
Func<Stream, Stream, Task<bool>> func)
|
||||
{
|
||||
Guard.AgainstNull(func, nameof(func));
|
||||
RegisterComparer(
|
||||
extension,
|
||||
(settings, stream1, stream2) => func(stream1, stream2));
|
||||
}
|
||||
|
||||
public static void RegisterComparer(
|
||||
string extension,
|
||||
Func<VerifySettings, Stream, Stream, Task<bool>> func)
|
||||
{
|
||||
Guard.AgainstNull(func, nameof(func));
|
||||
Guard.AgainstBadExtension(extension, nameof(extension));
|
||||
|
|
|
@ -6,7 +6,7 @@ namespace Verify
|
|||
{
|
||||
public partial class VerifySettings
|
||||
{
|
||||
internal Func<Stream, Stream, Task<bool>>? comparer;
|
||||
internal Func<VerifySettings, Stream, Stream, Task<bool>>? comparer;
|
||||
|
||||
public void UseComparer(
|
||||
Func<Stream, Stream, bool> func)
|
||||
|
@ -20,7 +20,7 @@ namespace Verify
|
|||
Func<Stream, Stream, Task<bool>> func)
|
||||
{
|
||||
Guard.AgainstNull(func, nameof(func));
|
||||
comparer = func;
|
||||
comparer = (settings, stream1, stream2) => func(stream1, stream2);
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче