| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System.Diagnostics;
- using System.Runtime.CompilerServices;
- using QuestPDF.Elements;
- using QuestPDF.Fluent;
- using QuestPDF.Infrastructure;
- namespace QuestPDF.LayoutTests.TestEngine;
- internal sealed class LayoutTest
- {
- private string TestIdentifier { get; set; }
- private LayoutTestResult TestResult { get; } = new LayoutTestResult();
-
- public static LayoutTest HavingSpaceOfSize(float width, float height, [CallerMemberName] string testIdentifier = "test")
- {
- var layoutTest = new LayoutTest
- {
- TestIdentifier = testIdentifier,
-
- TestResult =
- {
- PageSize = new Size(width, height)
- }
- };
- return layoutTest;
- }
- public LayoutTest WithContent(Action<IContainer> handler)
- {
- var container = new Container();
- container.Element(handler);
- TestResult.ActualLayout = LayoutTestExecutor.Execute(TestResult.PageSize, container);
-
- return this;
- }
- public void ExpectedDrawResult(Action<ExpectedDocumentLayoutDescriptor> handler)
- {
- var builder = new ExpectedDocumentLayoutDescriptor();
- handler(builder);
- TestResult.ExpectedLayout = builder.DocumentLayout;
- try
- {
- LayoutTestValidator.Validate(TestResult);
- }
- catch
- {
- if (Settings.LayoutTestVisualizationStrategy != LayoutTestVisualizationStrategy.Never)
- GenerateTestPreview();
-
- throw;
- }
- finally
- {
- if (Settings.LayoutTestVisualizationStrategy == LayoutTestVisualizationStrategy.Always)
- GenerateTestPreview();
- }
- }
- private void GenerateTestPreview()
- {
- var path = Path.Combine(Path.GetTempPath(), $"{TestIdentifier}.pdf");
-
- if (File.Exists(path))
- File.Delete(path);
-
- var stream = new FileStream(path, FileMode.CreateNew);
- LayoutTestResultVisualization.Visualize(TestResult, stream);
- stream.Dispose();
-
- Helpers.Helpers.OpenFileUsingDefaultProgram(path);
- }
- }
|