From 76f75e8d53a523aafc7ccbf404befacfe4a5e127 Mon Sep 17 00:00:00 2001 From: "Manuel Bl." Date: Thu, 30 May 2024 19:16:48 +0200 Subject: [PATCH] Increase coverage --- Core/QRBill.cs | 4 +++ CoreTest/PdfCanvasTest.cs | 11 +++++- CoreTest/StringsTest.cs | 15 ++++++++ PixelCanvasTest/PNGCanvasTest.cs | 1 - PixelCanvasTest/PngProcessorTest.cs | 56 +++++++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 PixelCanvasTest/PngProcessorTest.cs diff --git a/Core/QRBill.cs b/Core/QRBill.cs index 0a6da23..20ffd01 100644 --- a/Core/QRBill.cs +++ b/Core/QRBill.cs @@ -178,6 +178,10 @@ public static void Draw(Bill bill, ICanvas canvas) { throw; } + catch (QRBillGenerationException) + { + throw; + } catch (Exception e) { throw new QRBillGenerationException("Failed to generate QR bill", e); diff --git a/CoreTest/PdfCanvasTest.cs b/CoreTest/PdfCanvasTest.cs index 530ee76..dd06865 100644 --- a/CoreTest/PdfCanvasTest.cs +++ b/CoreTest/PdfCanvasTest.cs @@ -63,7 +63,7 @@ public Task LocaleIndependence() } [Fact] - public void CharactersOutsideWinANSI_RaisesException() + public void DrawWithCharactersOutsideWinANSI_RaisesException() { var bill = SampleData.CreateExample8(); bill.CharacterSet = SpsCharacterSet.ExtendedLatin; @@ -71,5 +71,14 @@ public void CharactersOutsideWinANSI_RaisesException() Assert.Throws(() => QRBill.Draw(bill, canvas)); } + [Fact] + public void GenerateWithCharactersOutsideWinANSI_RaisesException() + { + var bill = SampleData.CreateExample8(); + bill.CharacterSet = SpsCharacterSet.ExtendedLatin; + bill.Format.GraphicsFormat = GraphicsFormat.PDF; + + Assert.Throws(() => QRBill.Generate(bill)); + } } } diff --git a/CoreTest/StringsTest.cs b/CoreTest/StringsTest.cs index 70f873a..d716838 100644 --- a/CoreTest/StringsTest.cs +++ b/CoreTest/StringsTest.cs @@ -6,6 +6,7 @@ // using Codecrete.SwissQRBill.Generator; +using System.Text; using Xunit; namespace Codecrete.SwissQRBill.CoreTest @@ -48,5 +49,19 @@ public void ManySpaces_Cleaned() { Assert.Equal("a b c", " a b c ".SpacesCleaned()); } + + [Theory] + [InlineData(0x00DC, "Ü")] + [InlineData(0x0409, "Љ")] + [InlineData(0x1F609, "😉")] + [InlineData(0x1F680, "🚀")] + public void AppendCodePoint_ExpectedResult(int codePoint, string expectedResult) + { + var sb = new StringBuilder(); + sb.Append("xx"); + StringExtensions.AppendCodePoint(sb, codePoint); + sb.Append("yy"); + Assert.Equal($"xx{expectedResult}yy", sb.ToString()); + } } } diff --git a/PixelCanvasTest/PNGCanvasTest.cs b/PixelCanvasTest/PNGCanvasTest.cs index 3895e1f..ae63440 100644 --- a/PixelCanvasTest/PNGCanvasTest.cs +++ b/PixelCanvasTest/PNGCanvasTest.cs @@ -68,6 +68,5 @@ public void PngSaveAs() QRBill.Draw(bill, canvas); canvas.SaveAs("qrbill.png"); } - } } diff --git a/PixelCanvasTest/PngProcessorTest.cs b/PixelCanvasTest/PngProcessorTest.cs new file mode 100644 index 0000000..6d56869 --- /dev/null +++ b/PixelCanvasTest/PngProcessorTest.cs @@ -0,0 +1,56 @@ +// +// Swiss QR Bill Generator for .NET +// Copyright (c) 2018 Manuel Bleichenbacher +// Licensed under MIT License +// https://opensource.org/licenses/MIT +// + +using Codecrete.SwissQRBill.PixelCanvas; +using System; +using Xunit; + +namespace Codecrete.SwissQRBill.PixelCanvasTest +{ + public class PngProcessorTest + { + [Fact] + public void HeaderIncomplete_Fails() + { + var data = new byte[] { 0, 0, 0, 0, 0 }; + Assert.Throws(() => + { + PngProcessor.InsertDpi(new System.IO.MemoryStream(data), new System.IO.MemoryStream(), 96); + }); + } + + [Fact] + public void NoPhysChunk_Fails() + { + var data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }; + Assert.Throws(() => + { + PngProcessor.InsertDpi(new System.IO.MemoryStream(data), new System.IO.MemoryStream(), 96); + }); + } + + [Fact] + public void FirstChunkIncomplete_Fails() + { + var data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + Assert.Throws(() => + { + PngProcessor.InsertDpi(new System.IO.MemoryStream(data), new System.IO.MemoryStream(), 96); + }); + } + + [Fact] + public void Incomplete_Fails() + { + var data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + Assert.Throws(() => + { + PngProcessor.InsertDpi(new System.IO.MemoryStream(data), new System.IO.MemoryStream(), 96); + }); + } + } +}