Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The following will use ImageHash to compare the images instead of the default Di
```cs
[Test]
public Task CompareImage() =>
VerifyFile("sample.jpg");
VerifyFile("sample1.jpg");
```
<sup><a href='/src/Tests/Samples.cs#L4-L10' title='Snippet source file'>snippet source</a> | <a href='#snippet-CompareImage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Expand Down
4 changes: 2 additions & 2 deletions 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;NU1608;NU1109</NoWarn>
<Version>3.0.2</Version>
<Version>3.0.3</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand All @@ -13,4 +13,4 @@
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
</PropertyGroup>
</Project>
</Project>
9 changes: 9 additions & 0 deletions src/Tests.CustomThreshold/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
static class ModuleInitializer
{
[ModuleInitializer]
public static void Init()
{
VerifyImageSharpCompare.RegisterComparers(threshold: 10);
VerifierSettings.InitializePlugins();
}
}
File renamed without changes
17 changes: 17 additions & 0 deletions src/Tests.CustomThreshold/Tests.CustomThreshold.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Verify.NUnit" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<ProjectReference Include="..\Verify.ImageSharp.Compare\Verify.ImageSharp.Compare.csproj" />
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
<None Update=".\sample*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions src/Tests.CustomThreshold/Tests.FailingCompare.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Type: VerifyException,
Message:
Directory: {ProjectDirectory}
NotEqual:
- Received: Tests.FailingCompareInner.received.jpg
Verified: Tests.FailingCompareInner.verified.jpg

FileContent:

NotEqual:

Received: Tests.FailingCompareInner.received.jpg
Verified: Tests.FailingCompareInner.verified.jpg
Compare Result:
similarity(159345906) > threshold(10).
If this difference is acceptable, use:

* Globally: VerifyImageSharpCompare.RegisterComparers(159345906);
* For one test: Verifier.VerifyFile("file.jpg").UseImageHash(159345906);


}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions src/Tests.CustomThreshold/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[TestFixture]
public class Tests
{
[Test]
public Task Working() =>
VerifyFile("sample1.jpg");

[Test]
public Task FailingCompare() =>
ThrowsTask(async () =>
{
await VerifyFile("sample2.jpg")
.DisableDiff()
.UseMethodName("FailingCompareInner");
})
.IgnoreStackTrace()
.ScrubLinesContaining("clipboard", "DiffEngineTray");
}
Binary file added src/Tests.CustomThreshold/sample1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Tests.CustomThreshold/sample2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/Tests/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Samples

[Test]
public Task CompareImage() =>
VerifyFile("sample.jpg");
VerifyFile("sample1.jpg");

#endregion
}
}
6 changes: 5 additions & 1 deletion src/Tests/Tests.FailingCompare.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ NotEqual:
Received: Tests.FailingCompareInner.received.jpg
Verified: Tests.FailingCompareInner.verified.jpg
Compare Result:
Size of images differ.
similarity(159345906) > threshold(85).
If this difference is acceptable, use:

* Globally: VerifyImageSharpCompare.RegisterComparers(159345906);
* For one test: Verifier.VerifyFile("file.jpg").UseImageHash(159345906);


}
Binary file modified src/Tests/Tests.FailingCompareInner.verified.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Tests/Tests.Working.verified.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions src/Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
[TestFixture]
public class Tests
{
[Test]
public Task Working() =>
VerifyFile("sample1.jpg");

[Test]
public Task FailingCompare() =>
ThrowsTask(async () =>
{
await VerifyFile("sample.jpg")
await VerifyFile("sample2.jpg")
.DisableDiff()
.UseMethodName("FailingCompareInner")
.UseImageHash(85);
})
.IgnoreStackTrace()
.ScrubLinesContaining("clipboard", "DiffEngineTray");
}
}
4 changes: 2 additions & 2 deletions src/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MarkdownSnippets.MsBuild" PrivateAssets="all" />
Expand All @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<ProjectReference Include="..\Verify.ImageSharp.Compare\Verify.ImageSharp.Compare.csproj" />
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
<None Update="sample.*">
<None Update=".\sample*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
Binary file added src/Tests/sample1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Tests/sample2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/Verify.ImageSharp.Compare.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<File Path="mdsnippets.json" />
<File Path="nuget.md" />
</Folder>
<Project Path="Tests.CustomThreshold/Tests.CustomThreshold.csproj" />
<Project Path="Tests/Tests.csproj" />
<Project Path="Verify.ImageSharp.Compare/Verify.ImageSharp.Compare.csproj" />
</Solution>
14 changes: 12 additions & 2 deletions src/Verify.ImageSharp.Compare/VerifyImageSharpCompare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

public static class VerifyImageSharpCompare
{
static HashSet<string> registeredComparers = [];

public static bool Initialized { get; private set; }

/// <summary>
Expand All @@ -23,6 +25,7 @@ public static void Initialize()
public static void RegisterComparers(int threshold = 5)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();

RegisterComparer(threshold, "png");
RegisterComparer(threshold, "bmp");
RegisterComparer(threshold, "jpg");
Expand All @@ -36,10 +39,17 @@ public static SettingsTask UseImageHash(this SettingsTask settings, int threshol
settings.UseStreamComparer(
(received, verified, _) => Compare(threshold, received, verified));

public static void RegisterComparer(int threshold, string extension) =>
public static void RegisterComparer(int threshold, string extension)
{
if (!registeredComparers.Add(extension))
{
return;
}

VerifierSettings.RegisterStreamComparer(
extension,
(received, verified, _) => Compare(threshold, received, verified));
}

static Task<CompareResult> Compare(int threshold, Stream received, Stream verified)
{
Expand Down Expand Up @@ -72,4 +82,4 @@ static Task<CompareResult> Compare(int threshold, Stream received, Stream verifi
* For one test: Verifier.VerifyFile("file.jpg").UseImageHash({absoluteError});
"""));
}
}
}