LayoutTest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System.Runtime.CompilerServices;
  2. using System.Text;
  3. using QuestPDF.Elements;
  4. using QuestPDF.Helpers;
  5. namespace QuestPDF.LayoutTests.TestEngine;
  6. internal class LayoutTest
  7. {
  8. private string TestIdentifier { get; set; }
  9. private Size AvailableSpace { get; set; }
  10. private DrawingRecorder ActualDrawingRecorder { get; } = new();
  11. private DrawingRecorder ExpectedDrawingRecorder { get; } = new();
  12. private IContainer? Content { get; set; }
  13. public static LayoutTest HavingSpaceOfSize(float width, float height, [CallerMemberName] string testIdentifier = "test")
  14. {
  15. var layoutTest = new LayoutTest
  16. {
  17. TestIdentifier = testIdentifier,
  18. AvailableSpace = new Size(width, height)
  19. };
  20. return layoutTest;
  21. }
  22. public LayoutTest ForContent(Action<IContainer> handler)
  23. {
  24. if (Content != null)
  25. throw new InvalidOperationException("Content has already been defined.");
  26. Content = new Container();
  27. Content
  28. .Width(AvailableSpace.Width)
  29. .Height(AvailableSpace.Height)
  30. .ElementObserverSetter(ActualDrawingRecorder)
  31. .Mock("$document")
  32. .Element(handler);
  33. return this;
  34. }
  35. public void ExpectDrawResult(Action<ExpectedDocumentLayoutDescriptor> handler)
  36. {
  37. if (!ActualDrawingRecorder.GetDrawingEvents().Any())
  38. PerformTest();
  39. var builder = new ExpectedDocumentLayoutDescriptor(ExpectedDrawingRecorder);
  40. handler(builder);
  41. var actualDrawingEvents = ActualDrawingRecorder.GetDrawingEvents();
  42. var expectedDrawingEvents = ExpectedDrawingRecorder.GetDrawingEvents();
  43. if (CheckIfIdentical(actualDrawingEvents, expectedDrawingEvents))
  44. {
  45. Assert.Pass();
  46. }
  47. else
  48. {
  49. DrawLog(actualDrawingEvents, expectedDrawingEvents);
  50. Assert.Fail($"The drawing operations do not match the expected result. See the log above for details. Test identifier: '{TestIdentifier}'.");
  51. }
  52. static bool CheckIfIdentical(IReadOnlyCollection<ElementDrawingEvent> actual, IReadOnlyCollection<ElementDrawingEvent> expected)
  53. {
  54. if (actual.Count != expected.Count)
  55. return false;
  56. return actual.Zip(expected, Compare).All(x => x);
  57. }
  58. static bool Compare(ElementDrawingEvent? actual, ElementDrawingEvent? expected)
  59. {
  60. if (actual == null && expected == null)
  61. return true;
  62. if (actual == null || expected == null)
  63. return false;
  64. return actual.ObserverId == expected.ObserverId &&
  65. actual.PageNumber == expected.PageNumber &&
  66. Position.Equal(actual.Position, expected.Position) &&
  67. Size.Equal(actual.Size, expected.Size);
  68. }
  69. static void DrawLog(IReadOnlyCollection<ElementDrawingEvent> actualEvents, IReadOnlyCollection<ElementDrawingEvent> expectedEvents)
  70. {
  71. var identicalLines = actualEvents.Zip(expectedEvents, Compare).TakeWhile(x => x).Count();
  72. if (identicalLines > 0)
  73. {
  74. TestContext.Out.WriteLine("IDENTICAL");
  75. TestContext.Out.WriteLine(DrawHeader());
  76. foreach (var actualEvent in actualEvents.Take(identicalLines))
  77. TestContext.Out.WriteLine($"🟩\t{GetEventAsText(actualEvent)}");
  78. }
  79. if (expectedEvents.Count > identicalLines)
  80. {
  81. TestContext.Out.WriteLine();
  82. TestContext.Out.WriteLine("EXPECTED");
  83. TestContext.Out.WriteLine(DrawHeader());
  84. foreach (var expectedEvent in expectedEvents.Skip(identicalLines))
  85. TestContext.Out.WriteLine($"🟧\t{GetEventAsText(expectedEvent)}");
  86. }
  87. if (actualEvents.Count > identicalLines)
  88. {
  89. TestContext.Out.WriteLine();
  90. TestContext.Out.WriteLine("ACTUAL");
  91. TestContext.Out.WriteLine(DrawHeader());
  92. foreach (var actualEvent in actualEvents.Skip(identicalLines))
  93. TestContext.Out.WriteLine($"🟥\t{GetEventAsText(actualEvent)}");
  94. }
  95. }
  96. static string DrawHeader()
  97. {
  98. var mock = "Mock".PadRight(12);
  99. var page = "Page".PadRight(6);
  100. var x = "X".PadRight(8);
  101. var y = "Y".PadRight(8);
  102. var width = "W".PadRight(10);
  103. var height = "H";
  104. return $"\t{mock} {page} {x} {y} {width} {height}";
  105. }
  106. static string GetEventAsText(ElementDrawingEvent drawingEvent)
  107. {
  108. var observerId = drawingEvent.ObserverId.PadRight(12);
  109. var pageNumber = $"{drawingEvent.PageNumber}".PadRight(6);
  110. var positionX = $"{drawingEvent.Position.X}".PadRight(8);
  111. var positionY = $"{drawingEvent.Position.Y}".PadRight(8);
  112. var sizeWidth = $"{drawingEvent.Size.Width}".PadRight(10);
  113. var sizeHeight = $"{drawingEvent.Size.Height}";
  114. return $"{observerId} {pageNumber} {positionX} {positionY} {sizeWidth} {sizeHeight}";
  115. }
  116. }
  117. private void PerformTest()
  118. {
  119. Document
  120. .Create(document =>
  121. {
  122. document.Page(page =>
  123. {
  124. page.MinSize(new PageSize(0, 0));
  125. page.MaxSize(new PageSize(Size.Infinity, Size.Infinity));
  126. page.Content().Element(Content);
  127. });
  128. })
  129. .GenerateAndDiscard();
  130. }
  131. public LayoutTest VisualizeOutput()
  132. {
  133. if (Content == null)
  134. throw new InvalidOperationException("Content has not been defined.");
  135. Document
  136. .Create(document =>
  137. {
  138. document.Page(page =>
  139. {
  140. page.MinSize(new PageSize(0, 0));
  141. page.MaxSize(new PageSize(Size.Infinity, Size.Infinity));
  142. page.Content().Element(Content);
  143. });
  144. })
  145. .GeneratePdfAndShow();
  146. return this;
  147. }
  148. }