Browse Source

Add tests for the SimpleRotate element

Marcin Ziąbek 3 months ago
parent
commit
b9d3d5b1e7

+ 295 - 0
Source/QuestPDF.LayoutTests/SimpleRotateTests.cs

@@ -0,0 +1,295 @@
+using QuestPDF.Helpers;
+
+namespace QuestPDF.LayoutTests;
+
+public class SimpleRotateTests
+{
+    private void DrawTestSubject(IContainer container)
+    {
+        container
+            .Column(column =>
+            {
+                column.Item().Row(row =>
+                {
+                    row.AutoItem().Mock("a").SolidBlock(100, 100);
+                    row.AutoItem().Mock("b").SolidBlock(100, 100);
+                    row.AutoItem().Mock("c").SolidBlock(100, 100);
+                });
+                
+                column.Item().Row(row =>
+                {
+                    row.AutoItem().Mock("d").SolidBlock(100, 100);
+                    row.AutoItem().Mock("e").SolidBlock(100, 100);
+                    row.AutoItem().Mock("f").SolidBlock(100, 100);
+                });
+            });
+    }
+    
+    #region Single Page
+    
+    [Test]
+    public void NoRotation()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(500, 500)
+            .ForContent(content =>
+            {
+                content.Shrink().Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(300, 200)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 0).Size(100, 100);
+                        page.Mock("b").Position(100, 0).Size(100, 100);
+                        page.Mock("c").Position(200, 0).Size(100, 100);
+                        page.Mock("d").Position(0, 100).Size(100, 100);
+                        page.Mock("e").Position(100, 100).Size(100, 100);
+                        page.Mock("f").Position(200, 100).Size(100, 100);
+                    });
+            });
+    }
+    
+    [Test]
+    public void OneRotation()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(500, 500)
+            .ForContent(content =>
+            {
+                content
+                    .Shrink()
+                    .RotateRight() // <-
+                    .Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(200, 300)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(200, 0).Size(100, 100);
+                        page.Mock("b").Position(200, 100).Size(100, 100);
+                        page.Mock("c").Position(200, 200).Size(100, 100);
+                        page.Mock("d").Position(100, 0).Size(100, 100);
+                        page.Mock("e").Position(100, 100).Size(100, 100);
+                        page.Mock("f").Position(100, 200).Size(100, 100);
+                    });
+            });
+    }
+    
+    [Test]
+    public void TwoRotations()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(500, 500)
+            .ForContent(content =>
+            {
+                content
+                    .Shrink()
+                    .RotateRight() // <-
+                    .RotateRight()
+                    .Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(300, 200)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(300, 200).Size(100, 100);
+                        page.Mock("b").Position(200, 200).Size(100, 100);
+                        page.Mock("c").Position(100, 200).Size(100, 100);
+                        page.Mock("d").Position(300, 100).Size(100, 100);
+                        page.Mock("e").Position(200, 100).Size(100, 100);
+                        page.Mock("f").Position(100, 100).Size(100, 100);
+                    });
+            });
+    }
+    
+    [Test]
+    public void ThreeRotation()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(500, 500)
+            .ForContent(content =>
+            {
+                content
+                    .Shrink()
+                    .RotateRight() // <-
+                    .RotateRight()
+                    .RotateRight()
+                    .Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(200, 300)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 300).Size(100, 100);
+                        page.Mock("b").Position(0, 200).Size(100, 100);
+                        page.Mock("c").Position(0, 100).Size(100, 100);
+                        page.Mock("d").Position(100, 300).Size(100, 100);
+                        page.Mock("e").Position(100, 200).Size(100, 100);
+                        page.Mock("f").Position(100, 100).Size(100, 100);
+                    });
+            });
+    }
+    
+    #endregion
+    
+    #region Paging
+    
+    [Test]
+    public void NoRotationWithPaging()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(500, 150)
+            .ForContent(content =>
+            {
+                content.Shrink().Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(300, 100)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 0).Size(100, 100);
+                        page.Mock("b").Position(100, 0).Size(100, 100);
+                        page.Mock("c").Position(200, 0).Size(100, 100);
+                    });
+                
+                document
+                    .Page()
+                    .RequiredAreaSize(300, 100)
+                    .Content(page =>
+                    {
+                        page.Mock("d").Position(0, 0).Size(100, 100);
+                        page.Mock("e").Position(100, 0).Size(100, 100);
+                        page.Mock("f").Position(200, 0).Size(100, 100);
+                    });
+            });
+    }
+    
+    [Test]
+    public void OneRotationWithPaging()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(150, 500)
+            .ForContent(content =>
+            {
+                content
+                    .Shrink()
+                    .RotateRight() // <-
+                    .Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(100, 300)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(100, 0).Size(100, 100);
+                        page.Mock("b").Position(100, 100).Size(100, 100);
+                        page.Mock("c").Position(100, 200).Size(100, 100);
+                    });
+                
+                document
+                    .Page()
+                    .RequiredAreaSize(100, 300)
+                    .Content(page =>
+                    {
+                        page.Mock("d").Position(100, 0).Size(100, 100);
+                        page.Mock("e").Position(100, 100).Size(100, 100);
+                        page.Mock("f").Position(100, 200).Size(100, 100);
+                    });
+            });
+    }
+    
+    [Test]
+    public void TwoRotationsWithPaging()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(500, 150)
+            .ForContent(content =>
+            {
+                content
+                    .Shrink()
+                    .RotateRight() // <-
+                    .RotateRight()
+                    .Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(300, 100)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(300, 100).Size(100, 100);
+                        page.Mock("b").Position(200, 100).Size(100, 100);
+                        page.Mock("c").Position(100, 100).Size(100, 100);
+                    });
+                
+                document
+                    .Page()
+                    .RequiredAreaSize(300, 100)
+                    .Content(page =>
+                    {
+                        page.Mock("d").Position(300, 100).Size(100, 100);
+                        page.Mock("e").Position(200, 100).Size(100, 100);
+                        page.Mock("f").Position(100, 100).Size(100, 100);
+                    });
+            });
+    }
+    
+    [Test]
+    public void ThreeRotationWithPaging()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(150, 500)
+            .ForContent(content =>
+            {
+                content
+                    .Shrink()
+                    .RotateRight() // <-
+                    .RotateRight()
+                    .RotateRight()
+                    .Element(DrawTestSubject);
+            })
+            .ExpectDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(100, 300)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 300).Size(100, 100);
+                        page.Mock("b").Position(0, 200).Size(100, 100);
+                        page.Mock("c").Position(0, 100).Size(100, 100);
+                    });
+                
+                document
+                    .Page()
+                    .RequiredAreaSize(100, 300)
+                    .Content(page =>
+                    {
+                        page.Mock("d").Position(0, 300).Size(100, 100);
+                        page.Mock("e").Position(0, 200).Size(100, 100);
+                        page.Mock("f").Position(0, 100).Size(100, 100);
+                    });
+            });
+    }
+    
+    #endregion
+}

+ 54 - 119
Source/QuestPDF.UnitTests/SimpleRotateTests.cs

@@ -1,160 +1,95 @@
 using NUnit.Framework;
-using QuestPDF.Drawing;
 using QuestPDF.Elements;
+using QuestPDF.Fluent;
 using QuestPDF.Infrastructure;
-using QuestPDF.UnitTests.TestEngine;
 
 namespace QuestPDF.UnitTests
 {
     [TestFixture]
     public class SimpleRotateTests
     {
-        #region measure
+        #region Cumulative rotation
         
         [Test]
-        public void Measure_Wrap()
+        public void RotateRightIsCumulative()
         {
-            TestPlan
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 0
-                })
-                .MeasureElement(new Size(400, 300))
-                .ExpectChildMeasure(new Size(400, 300), SpacePlan.Wrap("Mock"))
-                .CheckMeasureResult(SpacePlan.Wrap("Forwarded from child"));
-        }
+            var container = EmptyContainer.Create();
+            container
+                .RotateRight()
+                .RotateRight()
+                .RotateRight()
+                .RotateRight()
+                .RotateRight();
         
-        [Test]
-        public void Measure_PartialRender()
-        {
-            TestPlan
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 0
-                })
-                .MeasureElement(new Size(400, 300))
-                .ExpectChildMeasure(new Size(400, 300), SpacePlan.PartialRender(300, 200))
-                .CheckMeasureResult(SpacePlan.PartialRender(300, 200));
+            var rotation = container.Child as SimpleRotate;
+            Assert.That(rotation?.TurnCount, Is.EqualTo(5));
         }
         
         [Test]
-        public void Measure_RotateRight()
+        public void RotateLeftIsCumulative()
         {
-            TestPlan
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 1
-                })
-                .MeasureElement(new Size(400, 300))
-                .ExpectChildMeasure(new Size(300, 400), SpacePlan.FullRender(200, 300))
-                .CheckMeasureResult(SpacePlan.FullRender(300, 200));
-        }
+            var container = EmptyContainer.Create();
+            container
+                .RotateLeft()
+                .RotateLeft()
+                .RotateLeft()
+                .RotateLeft()
+                .RotateLeft()
+                .RotateLeft();
         
-        [Test]
-        public void Measure_RotateFlip()
-        {
-            TestPlan
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 2
-                })
-                .MeasureElement(new Size(400, 300))
-                .ExpectChildMeasure(new Size(400, 300), SpacePlan.FullRender(200, 100))
-                .CheckMeasureResult(SpacePlan.FullRender(200, 100));
+            var rotation = container.Child as SimpleRotate;
+            Assert.That(rotation?.TurnCount, Is.EqualTo(-6));
         }
         
         [Test]
-        public void Measure_RotateLeft()
+        public void RotateRightAndLeftCanBeCombined()
         {
-            TestPlan
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 3 // or -1
-                })
-                .MeasureElement(new Size(500, 400))
-                .ExpectChildMeasure(new Size(400, 500), SpacePlan.FullRender(300, 350))
-                .CheckMeasureResult(SpacePlan.FullRender(350, 300));
+            var container = EmptyContainer.Create();
+            container
+                .RotateRight()
+                .RotateRight()
+                .RotateRight()
+                .RotateRight()
+                .RotateLeft()
+                .RotateLeft()
+                .RotateLeft();
+        
+            var rotation = container.Child as SimpleRotate;
+            Assert.That(rotation?.TurnCount, Is.EqualTo(1));
         }
         
         #endregion
         
-        #region draw
+        #region Companion Hint
         
         [Test]
-        public void Draw_Simple()
+        public void NoRotationCompanionHint()
         {
-            TestPlan
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 0
-                })
-                .DrawElement(new Size(640, 480))
-                .ExpectCanvasTranslate(0, 0)
-                .ExpectCanvasRotate(0)
-                .ExpectChildDraw(new Size(640, 480))
-                .ExpectCanvasRotate(0)
-                .ExpectCanvasTranslate(0, 0)
-                .CheckDrawResult();
-        }
+            var container = EmptyContainer.Create();
+            container.RotateRight().RotateLeft();
         
-        [Test]
-        public void Draw_RotateRight()
-        {
-            TestPlan 
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 1
-                })
-                .DrawElement(new Size(640, 480))
-                .ExpectCanvasTranslate(640, 0)
-                .ExpectCanvasRotate(90)
-                .ExpectChildDraw(new Size(480, 640))
-                .ExpectCanvasRotate(-90)
-                .ExpectCanvasTranslate(-640, 0)
-                .CheckDrawResult();
+            var rotation = container.Child as SimpleRotate;
+            Assert.That(rotation?.GetCompanionHint(), Is.EqualTo("No rotation"));
         }
         
         [Test]
-        public void Draw_RotateFlip()
+        public void RotateRightCompanionHint()
         {
-            TestPlan 
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 2
-                })
-                .DrawElement(new Size(640, 480))
-                .ExpectCanvasTranslate(640, 480)
-                .ExpectCanvasRotate(180)
-                .ExpectChildDraw(new Size(640, 480))
-                .ExpectCanvasRotate(-180)
-                .ExpectCanvasTranslate(-640, -480)
-                .CheckDrawResult();
+            var container = EmptyContainer.Create();
+            container.RotateRight();
+        
+            var rotation = container.Child as SimpleRotate;
+            Assert.That(rotation?.GetCompanionHint(), Is.EqualTo("90 deg clockwise"));
         }
         
         [Test]
-        public void Draw_RotateLeft()
+        public void DoubleRotateLeftCompanionHint()
         {
-            TestPlan 
-                .For(x => new SimpleRotate
-                {
-                    Child = x.CreateChild(),
-                    TurnCount = 3 // or -1
-                })
-                .DrawElement(new Size(640, 480))
-                .ExpectCanvasTranslate(0, 480)
-                .ExpectCanvasRotate(270)
-                .ExpectChildDraw(new Size(480, 640))
-                .ExpectCanvasRotate(-270)
-                .ExpectCanvasTranslate(0, -480)
-                .CheckDrawResult();
+            var container = EmptyContainer.Create();
+            container.RotateLeft().RotateLeft();
+        
+            var rotation = container.Child as SimpleRotate;
+            Assert.That(rotation?.GetCompanionHint(), Is.EqualTo("180 deg counter-clockwise"));
         }
         
         #endregion

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


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/SimpleRotate/Rotate(1).png


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/SimpleRotate/Rotate(2).png


BIN
Source/QuestPDF.VisualTests/ExpectedOutput/SimpleRotate/Rotate(3).png


+ 30 - 0
Source/QuestPDF.VisualTests/SimpleRotateTests.cs

@@ -0,0 +1,30 @@
+using QuestPDF.Fluent;
+using QuestPDF.Helpers;
+
+namespace QuestPDF.VisualTests;
+
+public class SimpleRotateTests
+{
+    [Test]
+    public void Rotate(
+        [Values(0, 1, 2, 3)] int rotationCount)
+    {
+        VisualTest.PerformWithDefaultPageSettings(container =>
+        {
+            container
+                .Width(150)
+                .Height(150)
+                .Element(element =>
+                {
+                    foreach (var i in Enumerable.Range(0, rotationCount))
+                        element = element.RotateRight();
+    
+                    return element;
+                })
+                .Shrink()
+                .Background(Colors.Grey.Lighten3)
+                .Padding(10)
+                .Text($"Rotation #{rotationCount}");
+        });
+    }
+}

+ 15 - 2
Source/QuestPDF/Elements/SimpleRotate.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Text;
 using QuestPDF.Drawing;
 using QuestPDF.Infrastructure;
 
@@ -28,6 +29,7 @@ namespace QuestPDF.Elements
             if (childSpace.Type == SpacePlanType.PartialRender)
                 return SpacePlan.PartialRender(targetSpace);
 
+            // Stryker disable once: unreachable code
             throw new ArgumentException();
         }
         
@@ -50,7 +52,18 @@ namespace QuestPDF.Elements
             Canvas.Rotate(-rotate);
             Canvas.Translate(translate.Reverse());
         }
-        
-        internal override string? GetCompanionHint() => $"{NormalizedTurnCount * 90} deg clockwise";
+
+        internal override string? GetCompanionHint()
+        {
+            if (TurnCount == 0)
+                return "No rotation";
+
+            var degrees = Math.Abs(TurnCount) * 90;
+            
+            // Stryker disable once equality: TurnCount = 0 is handled above
+            var direction = TurnCount > 0 ? "clockwise" : "counter-clockwise"; 
+            
+            return $"{degrees} deg {direction}";
+        }
     }
 }