Skip to content

Commit 9e9957b

Browse files
committed
Updated test cases
1 parent 3fc14b7 commit 9e9957b

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

QRCoder/SvgQRCode.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex
214214

215215
private bool IsBlockedByLogo(double x, double y, ImageAttributes? attr, double pixelPerModule)
216216
{
217-
if (attr == null)
218-
return false;
219217
return x + pixelPerModule >= attr.Value.X && x <= attr.Value.X + attr.Value.Width && y + pixelPerModule >= attr.Value.Y && y <= attr.Value.Y + attr.Value.Height;
220218
}
221219

QRCoderTests/Helpers/HelperFunctions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,20 @@ public static string BitmapToHash(Bitmap bmp)
3636
imgBytes = ms.ToArray();
3737
ms.Dispose();
3838
}
39+
return ByteArrayToHash(imgBytes);
40+
}
41+
42+
public static string ByteArrayToHash(byte[] data)
43+
{
3944
var md5 = new MD5CryptoServiceProvider();
40-
var hash = md5.ComputeHash(imgBytes);
45+
var hash = md5.ComputeHash(data);
4146
return BitConverter.ToString(hash).Replace("-", "").ToLower();
4247
}
48+
49+
public static string StringToHash(string data)
50+
{
51+
return ByteArrayToHash(Encoding.UTF8.GetBytes(data));
52+
}
4353
#endif
4454

4555
}

QRCoderTests/SvgQRCodeRendererTests.cs

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using QRCoderTests.Helpers.XUnitExtenstions;
66
using System.IO;
77
using System.Security.Cryptography;
8+
using QRCoderTests.Helpers;
89
#if !NETCOREAPP1_1
910
using System.Drawing;
1011
#endif
@@ -27,6 +28,19 @@ private string GetAssemblyPath()
2728
#endif
2829
}
2930

31+
[Fact]
32+
[Category("QRRenderer/SvgQRCode")]
33+
public void can_render_svg_qrcode_simple()
34+
{
35+
//Create QR code
36+
var gen = new QRCodeGenerator();
37+
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
38+
var svg = new SvgQRCode(data).GetGraphic(5);
39+
40+
var result = HelperFunctions.StringToHash(svg);
41+
result.ShouldBe("5c251275a435a9aed7e591eb9c2e9949");
42+
}
43+
3044
[Fact]
3145
[Category("QRRenderer/SvgQRCode")]
3246
public void can_render_svg_qrcode()
@@ -36,11 +50,21 @@ public void can_render_svg_qrcode()
3650
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
3751
var svg = new SvgQRCode(data).GetGraphic(10, Color.Red, Color.White);
3852

39-
var md5 = new MD5CryptoServiceProvider();
40-
var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(svg));
41-
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
53+
var result = HelperFunctions.StringToHash(svg);
54+
result.ShouldBe("1baa8c6ac3bd8c1eabcd2c5422dd9f78");
55+
}
56+
57+
[Fact]
58+
[Category("QRRenderer/SvgQRCode")]
59+
public void can_render_svg_qrcode_viewbox_mode()
60+
{
61+
//Create QR code
62+
var gen = new QRCodeGenerator();
63+
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
64+
var svg = new SvgQRCode(data).GetGraphic(new Size(128,128));
4265

43-
result.ShouldBe("0ad8bc75675d04ba0caff51c7a89992c");
66+
var result = HelperFunctions.StringToHash(svg);
67+
result.ShouldBe("ec9a13c4484d246e3e2e0574958845c8");
4468
}
4569

4670
[Fact]
@@ -52,11 +76,8 @@ public void can_render_svg_qrcode_without_quietzones()
5276
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
5377
var svg = new SvgQRCode(data).GetGraphic(10, Color.Red, Color.White, false);
5478

55-
var md5 = new MD5CryptoServiceProvider();
56-
var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(svg));
57-
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
58-
59-
result.ShouldBe("24392f47d4c1c2c5097bd6b3f8eefccc");
79+
var result = HelperFunctions.StringToHash(svg);
80+
result.ShouldBe("2a582427d86b51504c08ebcbcf0472bd");
6081
}
6182

6283
[Fact]
@@ -72,12 +93,9 @@ public void can_render_svg_qrcode_with_png_logo()
7293
var logoObj = new SvgQRCode.SvgLogo(logoBitmap, 15);
7394

7495
var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj);
75-
File.WriteAllText(@"C:\Temp\qr_png.svg", svg);
76-
var md5 = new MD5CryptoServiceProvider();
77-
var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(svg));
78-
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
7996

80-
result.ShouldBe("4ff45872787f321524cc4d071239c25e");
97+
var result = HelperFunctions.StringToHash(svg);
98+
result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31");
8199
}
82100

83101
[Fact]
@@ -93,12 +111,29 @@ public void can_render_svg_qrcode_with_svg_logo()
93111
var logoObj = new SvgQRCode.SvgLogo(logoSvg, 30);
94112

95113
var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj);
96-
File.WriteAllText(@"C:\Temp\qr_svg.svg", svg);
97-
var md5 = new MD5CryptoServiceProvider();
98-
var hash = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(svg));
99-
var result = BitConverter.ToString(hash).Replace("-", "").ToLower();
100114

101-
result.ShouldBe("b4ded3964e2e640b6b6c74d1c89d71fa");
115+
var result = HelperFunctions.StringToHash(svg);
116+
result.ShouldBe("71f461136fdbe2ab85902d23ad2d7eb8");
117+
}
118+
119+
[Fact]
120+
[Category("QRRenderer/SvgQRCode")]
121+
public void can_instantate_parameterless()
122+
{
123+
var svgCode = new SvgQRCode();
124+
svgCode.ShouldNotBeNull();
125+
svgCode.ShouldBeOfType<SvgQRCode>();
126+
}
127+
128+
[Fact]
129+
[Category("QRRenderer/SvgQRCode")]
130+
public void can_render_svg_qrcode_from_helper()
131+
{
132+
//Create QR code
133+
var svg = SvgQRCodeHelper.GetQRCode("A", 2, "#000000", "#ffffff", QRCodeGenerator.ECCLevel.Q);
134+
135+
var result = HelperFunctions.StringToHash(svg);
136+
result.ShouldBe("f5ec37aa9fb207e3701cc0d86c4a357d");
102137
}
103138
#endif
104139
}

0 commit comments

Comments
 (0)