Skip to content

Commit

Permalink
Shrink API (#722)
Browse files Browse the repository at this point in the history
* Shrink APIimplementation

* Code refactoring

* Improvements and fixes to the LayoutTestEngine

* Updated Shrink API tests to the new approach

* Fixed build
  • Loading branch information
MarcinZiabek committed Nov 21, 2023
1 parent bb73d53 commit a3ae684
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 285 deletions.
2 changes: 1 addition & 1 deletion Source/QuestPDF.Examples/Engine/RenderingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void Render(IDocument document)
if (ShowResult && ShowingResultsEnabled)
{
var firstImagePath = fileNameSchema(0);
GenerateExtensions.OpenFileUsingDefaultProgram(firstImagePath);
Helpers.Helpers.OpenFileUsingDefaultProgram(firstImagePath);
}
}

Expand Down
126 changes: 0 additions & 126 deletions Source/QuestPDF.LayoutTests/LayoutTestResult.cs

This file was deleted.

13 changes: 13 additions & 0 deletions Source/QuestPDF.LayoutTests/Setup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace QuestPDF.LayoutTests
{
[SetUpFixture]
public class Setup
{
[OneTimeSetUp]
public static void Configure()
{
QuestPDF.Settings.License = LicenseType.Community;
QuestPDF.LayoutTests.TestEngine.Settings.LayoutTestVisualizationStrategy = LayoutTestVisualizationStrategy.WhenFailure;
}
}
}
129 changes: 129 additions & 0 deletions Source/QuestPDF.LayoutTests/ShrinkTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
namespace QuestPDF.LayoutTests;

public class ShrinkTests
{
[Test]
public void Both()
{
LayoutTest
.HavingSpaceOfSize(100, 120)
.WithContent(content =>
{
content
.Shrink()
.Mock().Size(60, 200);
})
.ExpectedDrawResult(document =>
{
document
.Page()
.RequiredAreaSize(60, 120)
.Content(page =>
{
page.Mock().Position(0, 0).Size(60, 120);
});
document
.Page()
.RequiredAreaSize(60, 80)
.Content(page =>
{
page.Mock().Position(0, 0).Size(60, 80);
});
});
}

[Test]
public void Vertical()
{
LayoutTest
.HavingSpaceOfSize(100, 120)
.WithContent(content =>
{
content
.ShrinkVertical()
.Mock().Size(60, 200);
})
.ExpectedDrawResult(document =>
{
document
.Page()
.RequiredAreaSize(60, 120)
.Content(page =>
{
page.Mock().Position(0, 0).Size(100, 120);
});
document
.Page()
.RequiredAreaSize(60, 80)
.Content(page =>
{
page.Mock().Position(0, 0).Size(100, 80);
});
});
}

[Test]
public void Horizontal()
{
LayoutTest
.HavingSpaceOfSize(100, 120)
.WithContent(content =>
{
content
.ShrinkHorizontal()
.Mock().Size(60, 200);
})
.ExpectedDrawResult(document =>
{
document
.Page()
.RequiredAreaSize(60, 120)
.Content(page =>
{
page.Mock().Position(0, 0).Size(60, 120);
});
document
.Page()
.RequiredAreaSize(60, 80)
.Content(page =>
{
page.Mock().Position(0, 0).Size(60, 120);
});
});
}

[Test]
public void ContentFromRightToLeft()
{
LayoutTest
.HavingSpaceOfSize(100, 120)
.WithContent(content =>
{
content
.ContentFromRightToLeft()
.Shrink()
.Mock().Size(60, 200);
})
.ExpectedDrawResult(document =>
{
document
.Page()
.RequiredAreaSize(60, 120)
.Content(page =>
{
page.Mock().Position(40, 0).Size(60, 120);
});
document
.Page()
.RequiredAreaSize(60, 80)
.Content(page =>
{
page.Mock().Position(40, 0).Size(60, 80);
});
});
}
}
18 changes: 14 additions & 4 deletions Source/QuestPDF.LayoutTests/TestEngine/FluentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ExpectedPageLayoutDescriptor(LayoutTestResult.PageLayout pageLayout)
PageLayout = pageLayout;
}

public ExpectedPageLayoutDescriptor TakenAreaSize(float width, float height)
public ExpectedPageLayoutDescriptor RequiredAreaSize(float width, float height)
{
PageLayout.RequiredArea = new Size(width, height);
return this;
Expand All @@ -49,7 +49,7 @@ internal class ExpectedPageContentDescriptor
{
public List<LayoutTestResult.MockLayoutPosition> MockPositions { get;} = new();

public ExpectedMockPositionDescriptor Mock(string mockId)
public ExpectedMockPositionDescriptor Mock(string mockId = MockFluent.DefaultMockId)
{
var child = new LayoutTestResult.MockLayoutPosition { MockId = mockId };
MockPositions.Add(child);
Expand Down Expand Up @@ -79,9 +79,11 @@ public ExpectedMockPositionDescriptor Size(float width, float height)
}
}

internal static class ElementExtensions
internal static class MockFluent
{
public static MockDescriptor Mock(this IContainer element, string id)
public const string DefaultMockId = "$mock";

public static MockDescriptor Mock(this IContainer element, string id = DefaultMockId)
{
var mock = new ElementMock
{
Expand Down Expand Up @@ -109,4 +111,12 @@ public MockDescriptor Size(float width, float height)

return this;
}
}

internal static class WrapFluent
{
public static void Wrap(this IContainer element)
{
element.Element(new WrapChild());
}
}
26 changes: 17 additions & 9 deletions Source/QuestPDF.LayoutTests/TestEngine/LayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,26 @@ public void ExpectedDrawResult(Action<ExpectedDocumentLayoutDescriptor> handler)

TestResult.ExpectedLayout = builder.DocumentLayout;

GenerateTestPreview();
LayoutTestValidator.Validate(TestResult);
try
{
LayoutTestValidator.Validate(TestResult);
}
catch
{
if (Settings.LayoutTestVisualizationStrategy != LayoutTestVisualizationStrategy.Never)
GenerateTestPreview();

throw;
}
finally
{
if (Settings.LayoutTestVisualizationStrategy == LayoutTestVisualizationStrategy.Always)
GenerateTestPreview();
}
}

private void GenerateTestPreview()
{
if (!Debugger.IsAttached)
{
Console.WriteLine("Debugger is not attached. Skipping test preview generation");
return;
}

var path = Path.Combine(Path.GetTempPath(), $"{TestIdentifier}.pdf");

if (File.Exists(path))
Expand All @@ -64,6 +72,6 @@ private void GenerateTestPreview()
LayoutTestResultVisualization.Visualize(TestResult, stream);
stream.Dispose();

Console.WriteLine($"Generated test case preview: {path}");
Helpers.Helpers.OpenFileUsingDefaultProgram(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ICollection<LayoutTestResult.PageLayout> CollectMockInformation(ICollection<Size
.GroupBy(x => x.PageNumber)
.Select(x => new LayoutTestResult.PageLayout
{
RequiredArea = pageSizes.ElementAt(x.Key - 1),
RequiredArea = pageSizes.ElementAt(x.Key),
Mocks = x
.Select(y => new LayoutTestResult.MockLayoutPosition
{
Expand Down
4 changes: 2 additions & 2 deletions Source/QuestPDF.LayoutTests/TestEngine/LayoutTestValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public static void Validate(LayoutTestResult result)
static void ValidatePage(LayoutTestResult.PageLayout actualLayout, LayoutTestResult.PageLayout expectedLayout)
{
if (Math.Abs(actualLayout.RequiredArea.Width - expectedLayout.RequiredArea.Width) > Size.Epsilon)
throw new LayoutTestException($"Taken horizontal area is equal to {actualLayout.RequiredArea.Width} but expected {expectedLayout.RequiredArea.Width}");
throw new LayoutTestException($"Required horizontal area is equal to {actualLayout.RequiredArea.Width} but expected {expectedLayout.RequiredArea.Width}");

if (Math.Abs(actualLayout.RequiredArea.Height - expectedLayout.RequiredArea.Height) > Size.Epsilon)
throw new LayoutTestException($"Taken vertical area is equal to {actualLayout.RequiredArea.Height} but expected {expectedLayout.RequiredArea.Height}");
throw new LayoutTestException($"Required vertical area is equal to {actualLayout.RequiredArea.Height} but expected {expectedLayout.RequiredArea.Height}");

if (actualLayout.Mocks.Count != expectedLayout.Mocks.Count)
throw new LayoutTestException($"Visible {actualLayout.Mocks.Count} mocks but expected {expectedLayout.Mocks.Count}");
Expand Down

0 comments on commit a3ae684

Please sign in to comment.