Skip to content

Commit 77183bb

Browse files
committed
Enhance image approval methods to support monochrome conversion for improved accuracy in tests
1 parent 33e64d7 commit 77183bb

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed
-1.03 KB
Loading

QRCoderTests/Base64QRCodeRendererTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void can_render_base64_qrcode_jpeg()
5454
{
5555
var base64QRCode = new Base64QRCode(_data).GetGraphic(5, Color.Black, Color.White, true, Base64QRCode.ImageType.Jpeg);
5656
var data = Convert.FromBase64String(base64QRCode);
57-
data.ShouldMatchApprovedImage();
57+
data.ShouldMatchApprovedImage(asMonochrome: true); // remove JPEG compression artifacts by converting to monochrome
5858
}
5959
#endif
6060
}

QRCoderTests/Helpers/ShouldMatchApprovedBinaryExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ namespace Shouldly;
1717
[EditorBrowsable(EditorBrowsableState.Never)]
1818
public static partial class ShouldMatchApprovedExtensions
1919
{
20-
public static void ShouldMatchApprovedImage(this byte[] imageBytes, string? discriminator = null, string? customMessage = null)
20+
public static void ShouldMatchApprovedImage(this byte[] imageBytes, string? discriminator = null, string? customMessage = null, bool asMonochrome = false)
2121
{
2222
using var ms = new MemoryStream(imageBytes);
2323
using var image = (Bitmap)Image.FromStream(ms);
24-
image.ShouldMatchApproved(discriminator, customMessage);
24+
image.ShouldMatchApproved(discriminator, customMessage, asMonochrome);
2525
}
26-
public static void ShouldMatchApproved(this Bitmap image, string? discriminator = null, string? customMessage = null)
26+
public static void ShouldMatchApproved(this Bitmap image, string? discriminator = null, string? customMessage = null, bool asMonochrome = false)
2727
{
2828
// encode to gif first for easier visual verification, and using a third party lib to avoid platform-specific compression differences
2929
var readableBitmapData = image.GetReadableBitmapData();
30+
if (asMonochrome)
31+
readableBitmapData = readableBitmapData.Clone(KnownPixelFormat.Format1bppIndexed);
3032
var ms = new MemoryStream();
3133
GifEncoder.EncodeImage(readableBitmapData, ms);
3234
ms.ToArray().ShouldMatchApproved("gif", discriminator, customMessage);

0 commit comments

Comments
 (0)