Explorar o código

Improved layout testing

Marcin Ziąbek %!s(int64=2) %!d(string=hai) anos
pai
achega
f735440642

+ 13 - 17
Source/QuestPDF.LayoutTests/ShowWhenTests.cs → Source/QuestPDF.LayoutTests/ShowIfTests.cs

@@ -1,6 +1,6 @@
 namespace QuestPDF.LayoutTests;
 
-public class ShowWhenTests
+public class ShowIfTests
 {
     [Test]
     public void Scenario()
@@ -11,9 +11,9 @@ public class ShowWhenTests
             {
                 content.Decoration(decoration =>
                 {
-                    decoration.Before().ShowWhen(c => c.PageNumber % 2 == 0).Mock("before").Size(80, 20);
-                    decoration.Content().Mock("content").Size(70, 400);
-                    decoration.After().ShowWhen(c => c.PageNumber % 3 == 0).Mock("after").Size(90, 30);
+                    decoration.Before().ShowIf(c => c.PageNumber % 2 == 0).Mock("before").Size(80, 20);
+                    decoration.Content().Mock("content").Size(70, 460);
+                    decoration.After().ShowIf(c => c.PageNumber % 3 == 0).Mock("after").Size(90, 30);
                 });
             })
             .ExpectedDrawResult(document =>
@@ -40,8 +40,7 @@ public class ShowWhenTests
                     .RequiredAreaSize(90, 100)
                     .Content(page =>
                     {
-                        page.Mock("before").Position(0, 0).Size(90, 20);
-                        page.Mock("content").Position(0, 20).Size(90, 50);
+                        page.Mock("content").Position(0, 0).Size(90, 70);
                         page.Mock("after").Position(0, 70).Size(90, 30);
                     });
                 
@@ -50,29 +49,26 @@ public class ShowWhenTests
                     .RequiredAreaSize(80, 100)
                     .Content(page =>
                     {
-                        page.Mock("before").Position(0, 0).Size(60, 120);
-                        page.Mock("content").Position(0, 0).Size(60, 120);
-                        page.Mock("after").Position(0, 0).Size(60, 120);
+                        page.Mock("before").Position(0, 0).Size(80, 20);
+                        page.Mock("content").Position(0, 20).Size(80, 80);
                     });
                 
                 document
                     .Page()
-                    .RequiredAreaSize(60, 100)
+                    .RequiredAreaSize(70, 100)
                     .Content(page =>
                     {
-                        page.Mock("before").Position(0, 0).Size(60, 120);
-                        page.Mock("content").Position(0, 0).Size(60, 120);
-                        page.Mock("after").Position(0, 0).Size(60, 120);
+                        page.Mock("content").Position(0, 0).Size(70, 100);
                     });
                 
                 document
                     .Page()
-                    .RequiredAreaSize(90, 100)
+                    .RequiredAreaSize(90, 80)
                     .Content(page =>
                     {
-                        page.Mock("before").Position(0, 0).Size(60, 120);
-                        page.Mock("content").Position(0, 0).Size(60, 120);
-                        page.Mock("after").Position(0, 0).Size(60, 120);
+                        page.Mock("before").Position(0, 0).Size(90, 20);
+                        page.Mock("content").Position(0, 20).Size(90, 30);
+                        page.Mock("after").Position(0, 50).Size(90, 30);
                     });
             });
     }

+ 1 - 1
Source/QuestPDF.LayoutTests/ShrinkTests.cs

@@ -20,7 +20,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 120)
                     .Content(page =>
                     {
-                        page.Mock().Position(0, 0).Size(60, 120);
+                        page.Mock().Position(0, 0).Size(60, 110);
                     });
                 
                 document

+ 6 - 3
Source/QuestPDF.LayoutTests/TestEngine/LayoutTestExecutor.cs

@@ -42,11 +42,15 @@ internal static class LayoutTestExecutor
         
             while(true)
             {
+                pageContext.IncrementPageNumber();
+                
                 var spacePlan = container.Measure(pageSize);
                 pageSizes.Add(spacePlan);
 
                 if (spacePlan.Type == SpacePlanType.Wrap)
                 {
+                    pageContext.DecrementPageNumber();
+                    
                     canvas.EndDocument();
                     return (pageSizes, true);
                 }
@@ -55,8 +59,6 @@ internal static class LayoutTestExecutor
                 {
                     canvas.BeginPage(pageSize);
                     container.Draw(pageSize);
-                
-                    pageContext.IncrementPageNumber();
                 }
                 catch (Exception exception)
                 {
@@ -81,9 +83,10 @@ internal static class LayoutTestExecutor
             return mocks
                 .SelectMany(x => x.DrawingCommands)
                 .GroupBy(x => x.PageNumber)
+                .OrderBy(x => x.Key)
                 .Select(x => new LayoutTestResult.PageLayout
                 {
-                    RequiredArea = pageSizes.ElementAt(x.Key),
+                    RequiredArea = pageSizes.ElementAt(x.Key - 1),
                     Mocks = x
                         .Select(y => new LayoutTestResult.MockLayoutPosition
                         {

+ 1 - 1
Source/QuestPDF.LayoutTests/TestEngine/LayoutTestOutputVisualization.cs

@@ -150,7 +150,7 @@ internal static class LayoutTestResultVisualization
                 DrawMock(mock);
             
             foreach (var mock in pageLayout.Mocks.GetOverlappingItems())
-                DrawOccludedMock(mock.belowMockId);
+                DrawOccludedMock(mock.Below);
             
             DrawGridLines();
         }

+ 1 - 1
Source/QuestPDF.LayoutTests/TestEngine/LayoutTestResult.cs

@@ -31,7 +31,7 @@ internal sealed class LayoutTestResult
 
 internal static class LayoutTestResultHelpers
 {
-    public static IEnumerable<(LayoutTestResult.MockLayoutPosition belowMockId, LayoutTestResult.MockLayoutPosition aboveMockId)> GetOverlappingItems(this ICollection<LayoutTestResult.MockLayoutPosition> items)
+    public static IEnumerable<(LayoutTestResult.MockLayoutPosition Below, LayoutTestResult.MockLayoutPosition Above)> GetOverlappingItems(this ICollection<LayoutTestResult.MockLayoutPosition> items)
     {
         for (var i = 0; i < items.Count; i++)
         {

+ 3 - 3
Source/QuestPDF.LayoutTests/TestEngine/LayoutTestValidator.cs

@@ -64,7 +64,7 @@ internal static class LayoutTestValidator
                     .Count();
 
                 if (matchingActualMock == 0)
-                    throw new Exception($"Cannot find '{expectedMock.MockId}' mock on position {expectedMock.Position} and size {expectedMock.Size}");
+                    throw new Exception($"Cannot find '{expectedMock.MockId}' mock on position {expectedMock.Position.ToString()} and size {expectedMock.Size}");
                 
                 if (matchingActualMock > 1)
                     throw new Exception($"Found multiple '{expectedMock.MockId}' mocks on position {expectedMock.Position} and size {expectedMock.Size}");
@@ -78,10 +78,10 @@ internal static class LayoutTestValidator
             
             foreach (var expectedOverlap in expectedOverlaps)
             {
-                var matchingActualElements = actualOverlaps.Count(actualOverlap => actualOverlap.belowMockId == expectedOverlap.belowMockId && actualOverlap.aboveMockId == expectedOverlap.aboveMockId);
+                var matchingActualElements = actualOverlaps.Count(actualOverlap => actualOverlap.Below.MockId == expectedOverlap.Below.MockId && actualOverlap.Above.MockId == expectedOverlap.Above.MockId);
 
                 if (matchingActualElements != 1)
-                    throw new Exception($"Mock '{expectedOverlap.belowMockId}' should be visible below '{expectedOverlap.aboveMockId}' mock");
+                    throw new Exception($"Mock '{expectedOverlap.Below.MockId}' should be visible below '{expectedOverlap.Above.MockId}' mock");
             }
         }
     }

+ 4 - 1
Source/QuestPDF.LayoutTests/TestEngine/MockChild.cs

@@ -33,7 +33,7 @@ internal class ElementMock : Element
 
         var remainingHeight = TotalHeight - HeightOffset;
 
-        if (remainingHeight == 0)
+        if (remainingHeight < Size.Epsilon)
             return SpacePlan.FullRender(Size.Zero);
         
         if (remainingHeight > availableSpace.Height)
@@ -63,5 +63,8 @@ internal class ElementMock : Element
             Position = new Position(matrix.TransX / matrix.ScaleX, matrix.TransY / matrix.ScaleY),
             Size = availableSpace
         });
+
+        if (HeightOffset > TotalHeight - Size.Epsilon)
+            HeightOffset = 0;
     }
 }