This commit is contained in:
Simon Cropp 2020-11-07 12:25:22 +11:00
Родитель 9a5da18d96
Коммит 0cb1bb9839
6 изменённых файлов: 42 добавлений и 20 удалений

Просмотреть файл

@ -20,9 +20,9 @@ For samples purposes only the image sizes will be compared:
<a id='snippet-imagecomparer'></a>
```cs
static Task<CompareResult> CompareImages(
VerifySettings settings,
Stream received,
Stream verified)
Stream verified,
IReadOnlyDictionary<string, object> context)
{
// Fake comparison
if (received.Length == verified.Length)
@ -34,7 +34,7 @@ static Task<CompareResult> CompareImages(
return Task.FromResult(result);
}
```
<sup><a href='/src/Verify.Tests/Snippets/ComparerSnippets.cs#L44-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-imagecomparer' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/ComparerSnippets.cs#L45-L62' title='Snippet source file'>snippet source</a> | <a href='#snippet-imagecomparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
The returned `CompareResult.NotEqual` takes an optional message that will be rendered in the resulting text displayed to the user on test failure.
@ -62,7 +62,7 @@ public Task InstanceComparerFluent()
.UseExtension("png");
}
```
<sup><a href='/src/Verify.Tests/Snippets/ComparerSnippets.cs#L11-L30' title='Snippet source file'>snippet source</a> | <a href='#snippet-instancecomparer' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/ComparerSnippets.cs#L12-L31' title='Snippet source file'>snippet source</a> | <a href='#snippet-instancecomparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
@ -76,7 +76,7 @@ VerifierSettings.RegisterComparer(
compare: CompareImages);
await Verifier.VerifyFile("TheImage.png");
```
<sup><a href='/src/Verify.Tests/Snippets/ComparerSnippets.cs#L34-L41' title='Snippet source file'>snippet source</a> | <a href='#snippet-staticcomparer' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/ComparerSnippets.cs#L35-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-staticcomparer' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Просмотреть файл

@ -52,7 +52,7 @@ This sample uses a typed approach. So the converter acts on an in memory instanc
<a id='snippet-registerfileconvertertype'></a>
```cs
VerifierSettings.RegisterFileConverter<Image>(
canConvert: (target, settings) => Equals(target.RawFormat, ImageFormat.Tiff),
canConvert: (target, extension, context) => Equals(target.RawFormat, ImageFormat.Tiff),
conversion: (image, settings) =>
{
var pages = image.GetFrameCount(FrameDimension.Page);
@ -64,7 +64,7 @@ VerifierSettings.RegisterFileConverter<Image>(
var page = new MemoryStream();
image.Save(page, ImageFormat.Png);
streams.Add(new ConversionStream("png",page));
streams.Add(new ConversionStream("png", page));
}
return new ConversionResult(
@ -85,7 +85,7 @@ VerifierSettings.RegisterFileConverter<Image>(
using var stream = File.OpenRead("sample.tif");
await Verifier.Verify(Image.FromStream(stream));
```
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L47-L50' title='Snippet source file'>snippet source</a> | <a href='#snippet-fileconvertertypeverify' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L47-L52' title='Snippet source file'>snippet source</a> | <a href='#snippet-fileconvertertypeverify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Note that this sample also uses the optional `canConvert` to ensure that only `Image`s that are tiffs are converted.
@ -93,9 +93,9 @@ Note that this sample also uses the optional `canConvert` to ensure that only `I
<!-- snippet: ConverterCanConvert -->
<a id='snippet-convertercanconvert'></a>
```cs
canConvert: (target, settings) => Equals(target.RawFormat, ImageFormat.Tiff),
canConvert: (target, extension, context) => Equals(target.RawFormat, ImageFormat.Tiff),
```
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L19-L21' title='Snippet source file'>snippet source</a> | <a href='#snippet-convertercanconvert' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L20-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-convertercanconvert' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
@ -120,7 +120,7 @@ VerifierSettings.RegisterFileConverter(
var page = new MemoryStream();
image.Save(page, ImageFormat.Png);
streams.Add(new ConversionStream("png",page));
streams.Add(new ConversionStream("png", page));
}
return new ConversionResult(
@ -132,7 +132,7 @@ VerifierSettings.RegisterFileConverter(
streams);
});
```
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L56-L83' title='Snippet source file'>snippet source</a> | <a href='#snippet-registerfileconverterextension' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L58-L86' title='Snippet source file'>snippet source</a> | <a href='#snippet-registerfileconverterextension' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
<!-- snippet: FileConverterExtensionVerify -->
@ -140,7 +140,7 @@ VerifierSettings.RegisterFileConverter(
```cs
await Verifier.VerifyFile("sample.tif");
```
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L84-L86' title='Snippet source file'>snippet source</a> | <a href='#snippet-fileconverterextensionverify' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.Tests/Snippets/ConverterSnippets.cs#L88-L92' title='Snippet source file'>snippet source</a> | <a href='#snippet-fileconverterextensionverify' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Просмотреть файл

@ -1,4 +1,5 @@
#if DEBUG
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using VerifyTests;
@ -44,9 +45,9 @@ public class ComparerSnippets
#region ImageComparer
static Task<CompareResult> CompareImages(
VerifySettings settings,
Stream received,
Stream verified)
Stream verified,
IReadOnlyDictionary<string, object> context)
{
// Fake comparison
if (received.Length == verified.Length)

Просмотреть файл

@ -15,9 +15,10 @@ public class ConverterSnippets
public async Task Type()
{
#region RegisterFileConverterType
VerifierSettings.RegisterFileConverter<Image>(
#region ConverterCanConvert
canConvert: (target, settings) => Equals(target.RawFormat, ImageFormat.Tiff),
canConvert: (target, extension, context) => Equals(target.RawFormat, ImageFormat.Tiff),
#endregion
conversion: (image, settings) =>
{
@ -30,7 +31,7 @@ public class ConverterSnippets
var page = new MemoryStream();
image.Save(page, ImageFormat.Png);
streams.Add(new ConversionStream("png",page));
streams.Add(new ConversionStream("png", page));
}
return new ConversionResult(
@ -41,12 +42,13 @@ public class ConverterSnippets
},
streams);
});
#endregion
#region FileConverterTypeVerify
using var stream = File.OpenRead("sample.tif");
await Verifier.Verify(Image.FromStream(stream));
#endregion
}
@ -54,6 +56,7 @@ public class ConverterSnippets
public async Task Extension()
{
#region RegisterFileConverterExtension
VerifierSettings.RegisterFileConverter(
fromExtension: "tif",
conversion: (stream, settings) =>
@ -68,7 +71,7 @@ public class ConverterSnippets
var page = new MemoryStream();
image.Save(page, ImageFormat.Png);
streams.Add(new ConversionStream("png",page));
streams.Add(new ConversionStream("png", page));
}
return new ConversionResult(
@ -81,9 +84,12 @@ public class ConverterSnippets
});
#endregion
#region FileConverterExtensionVerify
await Verifier.VerifyFile("sample.tif");
#endregion
}
}
#endif
#endif

Просмотреть файл

@ -0,0 +1 @@
Foo

Просмотреть файл

@ -19,6 +19,20 @@ public class Tests
VerifierSettings.AddExtraDatetimeOffsetFormat("F");
}
[Fact]
public Task TreatAsString()
{
VerifierSettings.TreatAsString<ClassWithToString>(
(target, _) => target.Property);
return Verifier.Verify(new ClassWithToString {Property = "Foo"});
}
class ClassWithToString
{
public string Property { get; set; } = null!;
}
[Fact]
public async Task OnVerifyMismatch()
{