FluentExtensions.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using QuestPDF.Helpers;
  2. namespace QuestPDF.LayoutTests.TestEngine;
  3. internal class ExpectedDocumentLayoutDescriptor(DrawingRecorder DrawingRecorder)
  4. {
  5. private int CurrentPage { get; set; } = 1;
  6. public ExpectedPageLayoutDescriptor Page()
  7. {
  8. return new ExpectedPageLayoutDescriptor(DrawingRecorder, CurrentPage++);
  9. }
  10. }
  11. internal class ExpectedPageLayoutDescriptor(DrawingRecorder DrawingRecorder, int CurrentPageNumber)
  12. {
  13. public ExpectedPageLayoutDescriptor RequiredAreaSize(float width, float height)
  14. {
  15. DrawingRecorder.Record(new ElementDrawingEvent
  16. {
  17. ObserverId = "$document",
  18. PageNumber = CurrentPageNumber,
  19. Size = new Size(width, height)
  20. });
  21. return this;
  22. }
  23. public void Content(Action<ExpectedPageContentDescriptor> content)
  24. {
  25. var pageContent = new ExpectedPageContentDescriptor(DrawingRecorder, CurrentPageNumber);
  26. content(pageContent);
  27. }
  28. }
  29. internal class ExpectedPageContentDescriptor(DrawingRecorder drawingRecorder, int CurrentPageNumber)
  30. {
  31. public ExpectedMockPositionDescriptor Mock(string mockId)
  32. {
  33. var elementDrawingEvent = new ElementDrawingEvent
  34. {
  35. ObserverId = mockId,
  36. PageNumber = CurrentPageNumber,
  37. };
  38. drawingRecorder.Record(elementDrawingEvent);
  39. return new ExpectedMockPositionDescriptor(elementDrawingEvent);
  40. }
  41. }
  42. internal class ExpectedMockPositionDescriptor(ElementDrawingEvent drawingEvent)
  43. {
  44. public ExpectedMockPositionDescriptor Position(float x, float y)
  45. {
  46. drawingEvent.Position = new Position(x, y);
  47. return this;
  48. }
  49. public ExpectedMockPositionDescriptor Size(float width, float height)
  50. {
  51. drawingEvent.Size = new Size(width, height);
  52. return this;
  53. }
  54. public ExpectedMockPositionDescriptor State(object state)
  55. {
  56. drawingEvent.StateAfterDrawing = state;
  57. return this;
  58. }
  59. }
  60. internal static class FluentExtensions
  61. {
  62. public const string DefaultMockId = "$mock";
  63. public static IContainer Mock(this IContainer element, string id)
  64. {
  65. return element.Element(new ElementObserver
  66. {
  67. ObserverId = id
  68. });
  69. }
  70. public static IContainer ElementObserverSetter(this IContainer element, DrawingRecorder recorder)
  71. {
  72. return element.Element(new ElementObserverSetter
  73. {
  74. Recorder = recorder
  75. });
  76. }
  77. public static IContainer Size(this IContainer element, float width, float height)
  78. {
  79. return element.Width(width).Height(height);
  80. }
  81. public static void ContinuousBlock(this IContainer element, float width = 1f, float height = 1f)
  82. {
  83. element.Element(new ContinuousBlock
  84. {
  85. TotalWidth = width,
  86. TotalHeight = height
  87. });
  88. }
  89. public static void SolidBlock(this IContainer element, float width = 1f, float height = 1f)
  90. {
  91. element.Element(new SolidBlock
  92. {
  93. TotalWidth = width,
  94. TotalHeight = height
  95. });
  96. }
  97. }