2
0
Эх сурвалжийг харах

Refactor tests to replace FluentAssertions with NUnit assertions

Marcin Ziąbek 8 сар өмнө
parent
commit
81763a4540

+ 3 - 3
Source/QuestPDF.UnitTests/DocumentCompressionTests.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Diagnostics;
 using System.Linq;
-using FluentAssertions;
 using NUnit.Framework;
 using QuestPDF.Fluent;
 using QuestPDF.Helpers;
@@ -62,9 +61,10 @@ public class DocumentCompressionTests
         var withCompression = MeasureDocumentSizeAndGenerationTime(true);
         
         var sizeRatio = withoutCompression.documentSize / (float)withCompression.documentSize;
-        sizeRatio.Should().BeGreaterThan(3);
+        Assert.That(sizeRatio, Is.GreaterThan(3));
 
-        (withCompression.generationTime / withoutCompression.generationTime).Should().BeLessThan(2f);
+        var generationTimeRatio = withCompression.generationTime / (float)withoutCompression.generationTime;
+        Assert.That(generationTimeRatio, Is.LessThan(2));
         
         (int documentSize, float generationTime) MeasureDocumentSizeAndGenerationTime(bool compress)
         {

+ 3 - 4
Source/QuestPDF.UnitTests/DynamicImageTests.cs

@@ -1,5 +1,4 @@
-using FluentAssertions;
-using NUnit.Framework;
+using NUnit.Framework;
 using QuestPDF.Drawing;
 using QuestPDF.Elements;
 using QuestPDF.Infrastructure;
@@ -74,8 +73,8 @@ namespace QuestPDF.UnitTests
                 .ExpectCanvasDrawImage(Position.Zero, new Size(400, 300))
                 .CheckDrawResult();
             
-            passedSize.Width.Should().Be(1200);
-            passedSize.Height.Should().Be(900);
+            Assert.That(passedSize.Width, Is.EqualTo(1200));
+            Assert.That(passedSize.Height, Is.EqualTo(900));
         }
         
         byte[] GenerateImage(ImageSize size)

+ 21 - 16
Source/QuestPDF.UnitTests/ImageGenerationTests.cs

@@ -1,8 +1,8 @@
 using System;
 using System.IO;
 using System.Linq;
-using FluentAssertions;
 using NUnit.Framework;
+using NUnit.Framework.Legacy;
 using QuestPDF.Fluent;
 using QuestPDF.Helpers;
 using QuestPDF.Infrastructure;
@@ -23,19 +23,20 @@ namespace QuestPDF.UnitTests
                     page.Size(testCase.PageSize);
                     page.Content().Text("Test");
                 }))
-                .GenerateImages(new ImageGenerationSettings { RasterDpi = testCase.TargetDpi });
+                .GenerateImages(new ImageGenerationSettings { RasterDpi = testCase.TargetDpi })
+                .ToList();
             
             // assert
-            images.Should().HaveCount(1);
+            Assert.That(images, Has.Exactly(1).Items);
 
             var imageData = images.First();
-            imageData.Should().NotBeNull();
+            Assert.That(imageData, Is.Not.Null);
             
             using var image = SKImage.FromEncodedData(imageData);
-            image.Should().NotBeNull();
+            Assert.That(image, Is.Not.Null);
             
-            image.Width.Should().Be(testCase.ExpectedImageSize.Width);
-            image.Height.Should().Be(testCase.ExpectedImageSize.Height);
+            Assert.That(image.Width, Is.EqualTo(testCase.ExpectedImageSize.Width));
+            Assert.That(image.Height, Is.EqualTo(testCase.ExpectedImageSize.Height));
         }
 
         public record GeneratedImageResolutionCorrespondsToTargetDpi_TestCaseItem(PageSize PageSize, int TargetDpi, ImageSize ExpectedImageSize);
@@ -64,17 +65,19 @@ namespace QuestPDF.UnitTests
             var imageSizeWithHighQuality = CheckImageSize(ImageCompressionQuality.High);
             
             // assert
-            imageSizeWithLowQuality.Should().BeLessThan(imageSizeWithMediumQuality);
-            imageSizeWithMediumQuality.Should().BeLessThan(imageSizeWithHighQuality);
+            Assert.That(imageSizeWithLowQuality, Is.LessThan(imageSizeWithMediumQuality));
+            Assert.That(imageSizeWithMediumQuality, Is.LessThan(imageSizeWithHighQuality));
 
             int CheckImageSize(ImageCompressionQuality quality)
             {
-                var images = document.GenerateImages(new ImageGenerationSettings() { ImageFormat = ImageFormat.Jpeg, ImageCompressionQuality = quality });
+                var images = document
+                    .GenerateImages(new ImageGenerationSettings() { ImageFormat = ImageFormat.Jpeg, ImageCompressionQuality = quality })
+                    .ToList();
                 
-                images.Should().HaveCount(1);
+                Assert.That(images, Has.Exactly(1).Items);
 
                 var image = images.First();
-                image.Should().NotBeNull();
+                Assert.That(image, Is.Not.Null);
 
                 return image.Length;
             }
@@ -94,17 +97,19 @@ namespace QuestPDF.UnitTests
                         page.Content().Padding(25).AspectRatio(2).Background(Colors.Red.Medium);
                     });
                 })
-                .GenerateImages(new ImageGenerationSettings() { ImageFormat = imageFormat });
+                .GenerateImages(new ImageGenerationSettings() { ImageFormat = imageFormat })
+                .ToList();
             
-            images.Should().HaveCount(1);
+            Assert.That(images, Has.Exactly(1).Items);
 
             var imageData = images.First();
-            imageData.Should().NotBeNull();
+            Assert.That(imageData, Is.Not.Null);
 
             using var imageStream = new MemoryStream(imageData);
             using var imageCodec = SKCodec.Create(imageStream);
 
-            imageCodec.EncodedFormat.ToString().Should().Be(imageFormat.ToString());
+            Assert.That(imageCodec, Is.Not.Null);
+            Assert.That(imageCodec.EncodedFormat.ToString(), Is.EqualTo(imageFormat.ToString()));
         }
     }
 }

+ 12 - 8
Source/QuestPDF.UnitTests/ImageTests.cs

@@ -3,7 +3,6 @@ using System.IO;
 using System.Linq;
 using System.Net.Mime;
 using System.Threading;
-using FluentAssertions;
 using NUnit.Framework;
 using QuestPDF.Drawing;
 using QuestPDF.Drawing.Exceptions;
@@ -68,7 +67,7 @@ namespace QuestPDF.UnitTests
         public void ImageObject_ThrowsEncodingException_WhenImageDataIsIncorrect()
         {
             Func<Infrastructure.Image> action = () => Infrastructure.Image.FromBinaryData(new byte[] { 1, 2, 3 });
-            action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot decode the provided image.");
+            Assert.That(action, Throws.Exception.TypeOf<DocumentComposeException>().With.Message.EqualTo("Cannot decode the provided image."));
         }
         
         [Test]
@@ -80,14 +79,14 @@ namespace QuestPDF.UnitTests
                 return Infrastructure.Image.FromStream(stream);
             };
 
-            action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot decode the provided image.");
+            Assert.That(action, Throws.Exception.TypeOf<DocumentComposeException>().With.Message.EqualTo("Cannot decode the provided image."));
         }
         
         [Test]
         public void ImageObject_ThrowsFileNotFoundException_FileIsNotFound()
         {
             Func<Infrastructure.Image> action = () => Infrastructure.Image.FromFile("non-existing-file.jpg");
-            action.Should().ThrowExactly<DocumentComposeException>().WithMessage("Cannot load provided image, file not found: *");
+            Assert.That(action, Throws.Exception.TypeOf<DocumentComposeException>().With.Message.EqualTo("Cannot load provided image, file not found: non-existing-file.jpg"));
         }
 
         [Test]
@@ -120,8 +119,11 @@ namespace QuestPDF.UnitTests
                 });
             });
 
-            (documentWithMultipleImagesSize / (float)documentWithSingleImageSize).Should().BeInRange(9.9f, 10);
-            (documentWithSingleImageUsedMultipleTimesSize / (float)documentWithSingleImageSize).Should().BeInRange(1f, 1.05f);
+            var documentWithMultipleImagesSizeRatio = (documentWithMultipleImagesSize / (float)documentWithSingleImageSize);
+            Assert.That(documentWithMultipleImagesSizeRatio, Is.InRange(9.9f, 10));
+            
+            var documentWithSingleImageUsedMultipleTimesSizeRatio = (documentWithSingleImageUsedMultipleTimesSize / (float)documentWithSingleImageSize);
+            Assert.That(documentWithSingleImageUsedMultipleTimesSizeRatio, Is.InRange(1f, 1.05f));
         }
         
         [Test]
@@ -132,7 +134,8 @@ namespace QuestPDF.UnitTests
             var veryLowCompressionSize = GetDocumentSize(container => container.Image(photo).WithCompressionQuality(ImageCompressionQuality.VeryLow));
             var bestCompressionSize = GetDocumentSize(container => container.Image(photo).WithCompressionQuality(ImageCompressionQuality.Best));
 
-            (bestCompressionSize / (float)veryLowCompressionSize).Should().BeGreaterThan(10);
+            var compressionSizeRatio = (bestCompressionSize / (float)veryLowCompressionSize);
+            Assert.That(compressionSizeRatio, Is.GreaterThan(10));
         }
         
         [Test]
@@ -143,7 +146,8 @@ namespace QuestPDF.UnitTests
             var lowDpiSize = GetDocumentSize(container => container.Image(photo).WithRasterDpi(12));
             var highDpiSize = GetDocumentSize(container => container.Image(photo).WithRasterDpi(144));
 
-            (highDpiSize / (float)lowDpiSize).Should().BeGreaterThan(40);
+            var dpiSizeRatio = (highDpiSize / (float)lowDpiSize);
+            Assert.That(dpiSizeRatio, Is.GreaterThan(40));
         }
         
         private static int GetDocumentSize(Action<IContainer> container)

+ 0 - 1
Source/QuestPDF.UnitTests/QuestPDF.UnitTests.csproj

@@ -7,7 +7,6 @@
     </PropertyGroup>
 
     <ItemGroup>
-        <PackageReference Include="FluentAssertions" Version="7.2.0" />
         <PackageReference Include="nunit" Version="4.3.2" />
         <PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
         <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />

+ 0 - 1
Source/QuestPDF.UnitTests/TestEngine/TestPlan.cs

@@ -1,7 +1,6 @@
 using System;
 using System.Collections.Generic;
 using System.Text.Json;
-using FluentAssertions;
 using NUnit.Framework;
 using NUnit.Framework.Legacy;
 using QuestPDF.Drawing;

+ 0 - 22
Source/QuestPDF.UnitTests/TestsBase.cs

@@ -1,22 +0,0 @@
-using FluentAssertions;
-using NUnit.Framework;
-
-namespace QuestPDF.UnitTests
-{
-    [SetUpFixture]
-    public class TestsBase
-    {
-        [OneTimeSetUp]
-        public void RunBeforeAnyTests()
-        {
-            AssertionOptions.AssertEquivalencyUsing(options => options
-                .IncludingNestedObjects()
-                .IncludingInternalProperties()
-                .IncludingInternalFields()
-                .AllowingInfiniteRecursion()
-                .RespectingRuntimeTypes()
-                .WithTracing()
-                .WithStrictOrdering());
-        }
-    }
-}

+ 4 - 3
Source/QuestPDF.UnitTests/TextStyleTests.cs

@@ -1,5 +1,4 @@
 using System.Collections.Generic;
-using FluentAssertions;
 using NUnit.Framework;
 using QuestPDF.Fluent;
 using QuestPDF.Helpers;
@@ -49,8 +48,10 @@ namespace QuestPDF.UnitTests
                 HasStrikethrough = true
             };
 
-            spanTextStyle.Id.Should().BeGreaterThan(1);
-            targetStyle.Should().BeEquivalentTo(expectedStyle);
+            Assert.That(targetStyle, Is.Not.Null);
+            Assert.That(targetStyle.Id, Is.GreaterThan(1));
+
+            Assert.That(targetStyle.ToString(), Is.EqualTo(expectedStyle.ToString()));
         }
     }
 }

+ 1 - 1
Source/QuestPDF/Infrastructure/TextStyle.cs

@@ -46,7 +46,7 @@ namespace QuestPDF.Infrastructure
             Id = 0
         };
         
-        internal static TextStyle LibraryDefault { get; } = new()
+        internal static TextStyle  LibraryDefault { get; } = new()
         {
             Id = 1,
             Color = Colors.Black,