Browse Source

Add tests for the Rotate element

Marcin Ziąbek 3 months ago
parent
commit
72f247bd17

+ 39 - 0
Source/QuestPDF.LayoutTests/RotateTests.cs

@@ -0,0 +1,39 @@
+namespace QuestPDF.LayoutTests;
+
+public class RotateTests
+{
+    [Test]
+    public void SimpleRotation()
+    {
+        const float angle = 60;
+        const float armLength = 100;
+        
+        var expectedX = armLength * MathF.Cos(float.DegreesToRadians(angle));
+        var expectedY = armLength * MathF.Sin(float.DegreesToRadians(angle));
+        
+        Assert.That(expectedX, Is.EqualTo(50).Within(0.1f));
+        Assert.That(expectedY, Is.EqualTo(86.6).Within(0.1f));
+        
+        LayoutTest
+            .HavingSpaceOfSize(500, 500)
+            .ForContent(content =>
+            {
+                content
+                    .Shrink()
+                    .Rotate(angle)
+                    .TranslateX(armLength)
+                    .Mock("a")
+                    .SolidBlock(100, 100);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(100, 100)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(expectedX, expectedY).Size(100, 100);
+                    });
+            });
+    }
+}

+ 36 - 0
Source/QuestPDF.UnitTests/RotateUnitTests.cs

@@ -0,0 +1,36 @@
+using NUnit.Framework;
+using QuestPDF.Elements;
+using QuestPDF.Fluent;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.UnitTests;
+
+public class RotateUnitTests
+{
+    [Test]
+    public void RotateIsCumulative()
+    {
+        var container = EmptyContainer.Create();
+        
+        container
+            .Rotate(45)
+            .Rotate(-15)
+            .Rotate(20);
+        
+        var rotation = container.Child as Rotate;
+        Assert.That(rotation?.Angle, Is.EqualTo(50));
+    }
+    
+    [TestCase(0, ExpectedResult = "No rotation")]
+    [TestCase(45, ExpectedResult = "45 deg clockwise")]
+    [TestCase(-75, ExpectedResult = "75 deg counter-clockwise")]
+    [TestCase(12.345f, ExpectedResult = "12.3 deg clockwise")]
+    public string RotateCompanionHint(float angle)
+    {
+        var container = EmptyContainer.Create();
+        container.Rotate(angle);
+        
+        var rotation = container.Child as Rotate;
+        return rotation?.GetCompanionHint();
+    }
+}

BIN
Source/QuestPDF.VisualTests/ExpectedOutput/Rotate/Rotate(-15).png


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/Rotate/Rotate(0).png


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/Rotate/Rotate(30).png


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/Rotate/Rotate(45).png


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/Rotate/Rotate(60).png


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/Rotate/Rotate(90).png


+ 33 - 0
Source/QuestPDF.VisualTests/RotateTests.cs

@@ -0,0 +1,33 @@
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+
+namespace QuestPDF.VisualTests;
+
+public class RotateTests
+{
+    [Test]
+    public void Rotate(
+        [Values(-15, 0, 30, 45, 60, 90)] int angle)
+    {
+        VisualTest.PerformWithDefaultPageSettings(container =>
+        {
+            container
+                .Width(300)
+                .Height(300)
+                .AlignCenter()
+                .AlignMiddle()
+                .Unconstrained()
+                
+                .Rotate(angle) // <-
+                
+                .TranslateX(-100)
+                .TranslateY(-50)
+                .Width(200)
+                .Height(100)
+                .Background(Colors.Grey.Lighten3)
+                .AlignCenter()
+                .AlignMiddle()
+                .Text($"Rotation: {angle} deg");
+        });
+    }
+}

+ 15 - 3
Source/QuestPDF/Elements/Rotate.cs

@@ -1,4 +1,5 @@
-using QuestPDF.Infrastructure;
+using System;
+using QuestPDF.Infrastructure;
 
 
 namespace QuestPDF.Elements
 namespace QuestPDF.Elements
 {
 {
@@ -12,7 +13,18 @@ namespace QuestPDF.Elements
             Child?.Draw(availableSpace);
             Child?.Draw(availableSpace);
             Canvas.Rotate(-Angle);
             Canvas.Rotate(-Angle);
         }
         }
-        
-        internal override string? GetCompanionHint() => $"{Angle} deg clockwise";
+
+        internal override string? GetCompanionHint()
+        {
+            if (Angle == 0)
+                return "No rotation";
+
+            var degrees = Math.Abs(Angle);
+            
+            // Stryker disable once equality: TurnCount = 0 is handled above
+            var direction = Angle > 0 ? "clockwise" : "counter-clockwise"; 
+            
+            return $"{degrees:0.#} deg {direction}";
+        }
     }
     }
 }
 }

+ 3 - 4
Source/QuestPDF/Fluent/RotateExtensions.cs

@@ -43,10 +43,9 @@ namespace QuestPDF.Fluent
         /// <param name="angle">Rotation angle in degrees. A value of 360 degrees represents a full rotation.</param>
         /// <param name="angle">Rotation angle in degrees. A value of 360 degrees represents a full rotation.</param>
         public static IContainer Rotate(this IContainer element, float angle)
         public static IContainer Rotate(this IContainer element, float angle)
         {
         {
-            return element.Element(new Rotate
-            {
-                Angle = angle
-            });
+            var scale = element as Rotate ?? new Rotate();
+            scale.Angle += angle;
+            return element.Element(scale);
         }
         }
     }
     }
 }
 }