FluentExtensions.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using QuestPDF.Fluent;
  2. using QuestPDF.Infrastructure;
  3. namespace QuestPDF.LayoutTests.TestEngine;
  4. internal class ExpectedDocumentLayoutDescriptor
  5. {
  6. public LayoutTestResult.DocumentLayout DocumentLayout { get; } = new();
  7. public ExpectedPageLayoutDescriptor Page()
  8. {
  9. var page = new LayoutTestResult.PageLayout();
  10. DocumentLayout.Pages.Add(page);
  11. return new ExpectedPageLayoutDescriptor(page);
  12. }
  13. public void ExpectInfiniteLayoutException()
  14. {
  15. DocumentLayout.GeneratesInfiniteLayout = true;
  16. }
  17. }
  18. internal class ExpectedPageLayoutDescriptor
  19. {
  20. private LayoutTestResult.PageLayout PageLayout { get; }
  21. public ExpectedPageLayoutDescriptor(LayoutTestResult.PageLayout pageLayout)
  22. {
  23. PageLayout = pageLayout;
  24. }
  25. public ExpectedPageLayoutDescriptor RequiredAreaSize(float width, float height)
  26. {
  27. PageLayout.RequiredArea = new Size(width, height);
  28. return this;
  29. }
  30. public ExpectedPageLayoutDescriptor Content(Action<ExpectedPageContentDescriptor> content)
  31. {
  32. var pageContent = new ExpectedPageContentDescriptor();
  33. content(pageContent);
  34. PageLayout.Mocks = pageContent.MockPositions;
  35. return this;
  36. }
  37. }
  38. internal class ExpectedPageContentDescriptor
  39. {
  40. public List<LayoutTestResult.MockLayoutPosition> MockPositions { get;} = new();
  41. public ExpectedMockPositionDescriptor Mock(string mockId = MockFluent.DefaultMockId)
  42. {
  43. var child = new LayoutTestResult.MockLayoutPosition { MockId = mockId };
  44. MockPositions.Add(child);
  45. return new ExpectedMockPositionDescriptor(child);
  46. }
  47. }
  48. internal class ExpectedMockPositionDescriptor
  49. {
  50. private LayoutTestResult.MockLayoutPosition MockLayoutPosition { get; }
  51. public ExpectedMockPositionDescriptor(LayoutTestResult.MockLayoutPosition mockLayoutPosition)
  52. {
  53. MockLayoutPosition = mockLayoutPosition;
  54. }
  55. public ExpectedMockPositionDescriptor Position(float x, float y)
  56. {
  57. MockLayoutPosition.Position = new Position(x, y);
  58. return this;
  59. }
  60. public ExpectedMockPositionDescriptor Size(float width, float height)
  61. {
  62. MockLayoutPosition.Size = new Size(width, height);
  63. return this;
  64. }
  65. }
  66. internal static class MockFluent
  67. {
  68. public const string DefaultMockId = "$mock";
  69. public static MockDescriptor Mock(this IContainer element, string id = DefaultMockId)
  70. {
  71. var mock = new ElementMock
  72. {
  73. MockId = id
  74. };
  75. element.Element(mock);
  76. return new MockDescriptor(mock);
  77. }
  78. }
  79. internal class MockDescriptor
  80. {
  81. private ElementMock Mock { get; }
  82. public MockDescriptor(ElementMock mock)
  83. {
  84. Mock = mock;
  85. }
  86. public MockDescriptor Size(float width, float height)
  87. {
  88. Mock.TotalWidth = width;
  89. Mock.TotalHeight = height;
  90. return this;
  91. }
  92. }
  93. internal static class WrapFluent
  94. {
  95. public static void Wrap(this IContainer element)
  96. {
  97. element.Element(new WrapChild());
  98. }
  99. }