FluentExtensions.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. }
  55. internal static class FluentExtensions
  56. {
  57. public const string DefaultMockId = "$mock";
  58. public static IContainer Mock(this IContainer element, string id)
  59. {
  60. return element.Element(new ElementObserver
  61. {
  62. ObserverId = id
  63. });
  64. }
  65. public static IContainer ElementObserverSetter(this IContainer element, DrawingRecorder recorder)
  66. {
  67. return element.Element(new ElementObserverSetter
  68. {
  69. Recorder = recorder
  70. });
  71. }
  72. public static IContainer Size(this IContainer element, float width, float height)
  73. {
  74. return element.Width(width).Height(height);
  75. }
  76. public static void ContinuousBlock(this IContainer element, float width = 1f, float height = 1f)
  77. {
  78. element.Element(new ContinuousBlock
  79. {
  80. TotalWidth = width,
  81. TotalHeight = height
  82. });
  83. }
  84. public static void SolidBlock(this IContainer element, float width = 1f, float height = 1f)
  85. {
  86. element.Element(new SolidBlock
  87. {
  88. TotalWidth = width,
  89. TotalHeight = height
  90. });
  91. }
  92. }