Browse Source

Fixed bugs / unit tests

Marcin Ziąbek 1 year ago
parent
commit
2bfa091a43

+ 2 - 1
Source/QuestPDF.UnitTests/TextStyleTests.cs

@@ -38,7 +38,7 @@ namespace QuestPDF.UnitTests
             // assert
             var expectedStyle = TextStyle.LibraryDefault with
             {
-                Id = 20, // expect to break when adding new TextStyle properties
+                Id = targetStyle.Id, // expect to break when adding new TextStyle properties, so use the real one
                 Size = 20, 
                 FontFamily = "Times New Roman",
                 FontWeight = FontWeight.Bold,
@@ -46,6 +46,7 @@ namespace QuestPDF.UnitTests
                 HasStrikethrough = true
             };
 
+            spanTextStyle.Id.Should().BeGreaterThan(1);
             targetStyle.Should().BeEquivalentTo(expectedStyle);
         }
     }

+ 18 - 8
Source/QuestPDF/Infrastructure/Image.cs

@@ -80,8 +80,7 @@ namespace QuestPDF.Infrastructure
         public static Image FromBinaryData(byte[] imageBytes)
         {
             using var imageData = SkData.FromBinary(imageBytes);
-            var image = SkImage.FromData(imageData);
-            return new Image(image);
+            return DecodeImage(imageData);
         }
 
         /// <summary>
@@ -91,12 +90,11 @@ namespace QuestPDF.Infrastructure
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.remarks"]/*' />
         public static Image FromFile(string filePath)
         {
-            if (File.Exists(filePath))
-                new DocumentComposeException($"Cannot load provided image, file not found: ${filePath}");
+            if (!File.Exists(filePath))
+                throw new DocumentComposeException($"Cannot load provided image, file not found: ${filePath}");
             
             using var imageData = SkData.FromFile(filePath);
-            var image = SkImage.FromData(imageData);
-            return new Image(image);
+            return DecodeImage(imageData);
         }
 
         /// <summary>
@@ -107,8 +105,20 @@ namespace QuestPDF.Infrastructure
         public static Image FromStream(Stream stream)
         {
             using var imageData = SkData.FromStream(stream);
-            var image = SkImage.FromData(imageData);
-            return new Image(image);
+            return DecodeImage(imageData);
+        }
+        
+        private static Image DecodeImage(SkData imageData)
+        {
+            try
+            {
+                var image = SkImage.FromData(imageData);
+                return new Image(image);
+            }
+            catch
+            {
+                throw new DocumentComposeException("Cannot decode the provided image.");
+            }
         }
 
         #endregion

+ 2 - 2
Source/QuestPDF/Infrastructure/SvgImage.cs

@@ -32,8 +32,8 @@ public class SvgImage
     /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="image.remarks"]/*' />
     public static SvgImage FromFile(string filePath)
     {
-        if (File.Exists(filePath))
-            new DocumentComposeException($"Cannot load provided image, file not found: ${filePath}");
+        if (!File.Exists(filePath))
+            throw new DocumentComposeException($"Cannot load provided image, file not found: ${filePath}");
             
         var svg = File.ReadAllText(filePath);
         return new SvgImage(svg);

+ 4 - 1
Source/QuestPDF/Previewer/PreviewerRefreshCommand.cs

@@ -1,4 +1,7 @@
-#if NET6_0_OR_GREATER
+using System;
+using System.Collections.Generic;
+
+#if NET6_0_OR_GREATER
 
 namespace QuestPDF.Previewer
 {