Browse Source

Refactored fluent extensions for layout tests

Marcin Ziąbek 2 years ago
parent
commit
a25a526db2

+ 107 - 0
Source/QuestPDF.LayoutTests/TestEngine/FluentExtensions.cs

@@ -0,0 +1,107 @@
+using QuestPDF.Fluent;
+using QuestPDF.Infrastructure;
+
+namespace QuestPDF.LayoutTests.TestEngine;
+
+internal class ExpectedDocumentLayoutDescriptor
+{
+    public List<LayoutTestResult.PageLayoutSnapshot> PageLayouts { get; } = new(); 
+    
+    public ExpectedPageLayoutDescriptor Page()
+    {
+        var page = new LayoutTestResult.PageLayoutSnapshot();
+        PageLayouts.Add(page);
+        return new ExpectedPageLayoutDescriptor(page);
+    }
+}
+
+internal class ExpectedPageLayoutDescriptor
+{
+    private LayoutTestResult.PageLayoutSnapshot PageLayout { get; }
+
+    public ExpectedPageLayoutDescriptor(LayoutTestResult.PageLayoutSnapshot pageLayout)
+    {
+        PageLayout = pageLayout;
+    }
+
+    public ExpectedPageLayoutDescriptor TakenAreaSize(float width, float height)
+    {
+        PageLayout.RequiredArea = new Size(width, height);
+        return this;
+    }
+    
+    public ExpectedPageLayoutDescriptor Content(Action<ExpectedPageContentDescriptor> content)
+    {
+        var pageContent = new ExpectedPageContentDescriptor();
+        content(pageContent);
+        
+        PageLayout.MockPositions = pageContent.MockPositions;
+        return this;
+    }
+}
+
+internal class ExpectedPageContentDescriptor
+{
+    public List<LayoutTestResult.MockLayoutPosition> MockPositions { get;} = new();
+    
+    public ExpectedMockPositionDescriptor Mock(string mockId)
+    {
+        var child = new LayoutTestResult.MockLayoutPosition { MockId = mockId };
+        MockPositions.Add(child);
+        return new ExpectedMockPositionDescriptor(child);
+    }
+}
+
+internal class ExpectedMockPositionDescriptor
+{
+    private LayoutTestResult.MockLayoutPosition MockLayoutPosition { get; }
+
+    public ExpectedMockPositionDescriptor(LayoutTestResult.MockLayoutPosition mockLayoutPosition)
+    {
+        MockLayoutPosition = mockLayoutPosition;
+    }
+
+    public ExpectedMockPositionDescriptor Position(float x, float y)
+    {
+        MockLayoutPosition.Position = new Position(x, y);
+        return this;
+    }
+    
+    public ExpectedMockPositionDescriptor Size(float width, float height)
+    {
+        MockLayoutPosition.Size = new Size(width, height);
+        return this;
+    }
+}
+
+internal static class ElementExtensions
+{
+    public static MockDescriptor Mock(this IContainer element, string id)
+    {
+        var mock = new ElementMock
+        {
+            MockId = id
+        };
+        
+        element.Element(mock);
+        return new MockDescriptor(mock);
+    } 
+}
+
+internal class MockDescriptor
+{
+    private ElementMock Mock { get; }
+
+    public MockDescriptor(ElementMock mock)
+    {
+        Mock = mock;
+    }
+
+    public MockDescriptor Size(float width, float height)
+    {
+        Mock.TotalWidth = width;
+        Mock.TotalHeight = height;
+
+        return this;
+    }
+}

+ 4 - 114
Source/QuestPDF.LayoutTests/TestEngine/LayoutTest.cs

@@ -9,117 +9,7 @@ using SkiaSharp;
 
 
 namespace QuestPDF.LayoutTests.TestEngine;
 namespace QuestPDF.LayoutTests.TestEngine;
 
 
-internal class LayoutBuilderDescriptor
-{
-    public void Compose(Action<IContainer> container)
-    {
-        
-    }
-}
-
-internal class DocumentLayoutBuilder
-{
-    public List<LayoutTestResult.PageLayoutSnapshot> Commands { get; } = new(); 
-    
-    public PageLayoutDescriptor Page()
-    {
-        var page = new LayoutTestResult.PageLayoutSnapshot();
-        Commands.Add(page);
-        return new PageLayoutDescriptor(page);
-    }
-}
-
-internal class PageLayoutDescriptor
-{
-    private LayoutTestResult.PageLayoutSnapshot Command { get; }
-
-    public PageLayoutDescriptor(LayoutTestResult.PageLayoutSnapshot command)
-    {
-        Command = command;
-    }
-
-    public PageLayoutDescriptor TakenAreaSize(float width, float height)
-    {
-        Command.RequiredArea = new Size(width, height);
-        return this;
-    }
-    
-    public PageLayoutDescriptor Content(Action<PageLayoutBuilder> content)
-    {
-        var pageContent = new PageLayoutBuilder();
-        content(pageContent);
-        Command.MockPositions = pageContent.Commands;
-        return this;
-    }
-}
-
-internal class PageLayoutBuilder
-{
-    public List<LayoutTestResult.MockLayoutPosition> Commands { get;} = new();
-    
-    public ChildLayoutDescriptor Child(string mockId)
-    {
-        var child = new LayoutTestResult.MockLayoutPosition { MockId = mockId };
-        Commands.Add(child);
-        return new ChildLayoutDescriptor(child);
-    }
-}
-
-internal class ChildLayoutDescriptor
-{
-    private LayoutTestResult.MockLayoutPosition Command { get; }
-
-    public ChildLayoutDescriptor(LayoutTestResult.MockLayoutPosition command)
-    {
-        Command = command;
-    }
-
-    public ChildLayoutDescriptor Position(float x, float y)
-    {
-        Command.Position = new Position(x, y);
-        return this;
-    }
-    
-    public ChildLayoutDescriptor Size(float width, float height)
-    {
-        Command.Size = new Size(width, height);
-        return this;
-    }
-}
-
-internal static class ElementExtensions
-{
-    public static MockDescriptor Mock(this IContainer element, string id)
-    {
-        var mock = new ElementMock
-        {
-            MockId = id
-        };
-        
-        element.Element(mock);
-        return new MockDescriptor(mock);
-    } 
-}
-
-internal class MockDescriptor
-{
-    private ElementMock Mock { get; }
-
-    public MockDescriptor(ElementMock mock)
-    {
-        Mock = mock;
-    }
-
-    public MockDescriptor Size(float width, float height)
-    {
-        Mock.TotalWidth = width;
-        Mock.TotalHeight = height;
-
-        return this;
-    }
-}
-
-internal class LayoutTest
+internal sealed class LayoutTest
 {
 {
     private LayoutTestResult TestResult { get; } = new LayoutTestResult();
     private LayoutTestResult TestResult { get; } = new LayoutTestResult();
   
   
@@ -146,12 +36,12 @@ internal class LayoutTest
         
         
     }
     }
     
     
-    public LayoutTest ExpectedDrawResult(Action<DocumentLayoutBuilder> handler)
+    public LayoutTest ExpectedDrawResult(Action<ExpectedDocumentLayoutDescriptor> handler)
     {
     {
-        var builder = new DocumentLayoutBuilder();
+        var builder = new ExpectedDocumentLayoutDescriptor();
         handler(builder);
         handler(builder);
 
 
-        TestResult.ExpectedLayout = builder.Commands;
+        TestResult.ExpectedLayout = builder.PageLayouts;
         return this;
         return this;
     }
     }