LayoutTest.cs 6.7 KB

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