TestPlan.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using NUnit.Framework;
  5. using QuestPDF.Drawing.SpacePlan;
  6. using QuestPDF.Infrastructure;
  7. using QuestPDF.UnitTests.TestEngine.Operations;
  8. namespace QuestPDF.UnitTests.TestEngine
  9. {
  10. internal class TestPlan
  11. {
  12. private Element Element { get; set; }
  13. private ICanvas Canvas { get; }
  14. private Size OperationInput { get; set; }
  15. private Queue<OperationBase> Operations { get; } = new Queue<OperationBase>();
  16. public TestPlan()
  17. {
  18. Canvas = CreateCanvas();
  19. }
  20. public static TestPlan For(Func<TestPlan, Element> create)
  21. {
  22. var plan = new TestPlan();
  23. plan.Element = create(plan);
  24. return plan;
  25. }
  26. private T GetExpected<T>() where T : OperationBase
  27. {
  28. if (Operations.TryDequeue(out var value) && value is T result)
  29. return result;
  30. var gotType = value?.GetType()?.Name ?? "null";
  31. Assert.Fail($"Expected: {typeof(T).Name}, got {gotType}: {JsonSerializer.Serialize(value)}");
  32. return null;
  33. }
  34. private ICanvas CreateCanvas()
  35. {
  36. return new CanvasMock
  37. {
  38. TranslateFunc = position =>
  39. {
  40. var expected = GetExpected<CanvasTranslateOperationBase>();
  41. Assert.AreEqual(expected.Position.X, position.X);
  42. Assert.AreEqual(expected.Position.Y, position.Y);
  43. //position.Should().BeEquivalentTo(expected.Position);
  44. },
  45. DrawRectFunc = (position, size, color) =>
  46. {
  47. var expected = GetExpected<CanvasDrawRectangleOperationBase>();
  48. Assert.AreEqual(expected.Position.X, position.X);
  49. Assert.AreEqual(expected.Position.Y, position.Y);
  50. Assert.AreEqual(expected.Size.Width, size.Width);
  51. Assert.AreEqual(expected.Size.Height, size.Height);
  52. Assert.AreEqual(expected.Color, color);
  53. /*position.Should().BeEquivalentTo(expected.Position);
  54. size.Should().BeEquivalentTo(expected.Size);
  55. color.Should().Be(expected.Color);*/
  56. },
  57. DrawTextFunc = (text, position, style) =>
  58. {
  59. var expected = GetExpected<CanvasDrawTextOperationBase>();
  60. Assert.AreEqual(expected.Text, text);
  61. Assert.AreEqual(expected.Position.X, position.X);
  62. Assert.AreEqual(expected.Position.Y, position.Y);
  63. Assert.AreEqual(expected.Style.Color, style.Color);
  64. Assert.AreEqual(expected.Style.FontType, style.FontType);
  65. Assert.AreEqual(expected.Style.Size, style.Size);
  66. /*text.Should().Be(expected.Text);
  67. position.Should().BeEquivalentTo(expected.Position);
  68. style.Should().BeEquivalentTo(expected.Style);*/
  69. },
  70. DrawImageFunc = (image, position, size) =>
  71. {
  72. var expected = GetExpected<CanvasDrawImageOperationBase>();
  73. Assert.AreEqual(expected.Position.X, position.X);
  74. Assert.AreEqual(expected.Position.Y, position.Y);
  75. Assert.AreEqual(expected.Size.Width, size.Width);
  76. Assert.AreEqual(expected.Size.Height, size.Height);
  77. /*position.Should().BeEquivalentTo(expected.Position);
  78. size.Should().BeEquivalentTo(expected.Size);*/
  79. }
  80. };
  81. }
  82. public Element CreateChild(string id)
  83. {
  84. return new ElementMock
  85. {
  86. Id = id,
  87. MeasureFunc = availableSpace =>
  88. {
  89. var expected = GetExpected<ChildMeasureOperationBase>();
  90. Assert.AreEqual(expected.ChildId, id);
  91. Assert.AreEqual(expected.Input.Width, availableSpace.Width);
  92. Assert.AreEqual(expected.Input.Height, availableSpace.Height);
  93. // id.Should().Be(expected.ChildId);
  94. // availableSpace.Should().Be(expected.Input);
  95. return expected.Output;
  96. },
  97. DrawFunc = availableSpace =>
  98. {
  99. var expected = GetExpected<ChildDrawOperationBase>();
  100. Assert.AreEqual(expected.ChildId, id);
  101. Assert.AreEqual(expected.Input.Width, availableSpace.Width);
  102. Assert.AreEqual(expected.Input.Height, availableSpace.Height);
  103. /*id.Should().Be(expected.ChildId);
  104. availableSpace.Should().Be(expected.Input);*/
  105. }
  106. };
  107. }
  108. public TestPlan MeasureElement(Size input)
  109. {
  110. OperationInput = input;
  111. return this;
  112. }
  113. public TestPlan DrawElement(Size input)
  114. {
  115. OperationInput = input;
  116. return this;
  117. }
  118. private TestPlan AddOperation(OperationBase operationBase)
  119. {
  120. Operations.Enqueue(operationBase);
  121. return this;
  122. }
  123. public TestPlan ExpectChildMeasure(string child, Size expectedInput, ISpacePlan returns)
  124. {
  125. return AddOperation(new ChildMeasureOperationBase(child, expectedInput, returns));
  126. }
  127. public TestPlan ExpectChildDraw(string child, Size expectedInput)
  128. {
  129. return AddOperation(new ChildDrawOperationBase(child, expectedInput));
  130. }
  131. public TestPlan ExpectCanvasTranslate(Position position)
  132. {
  133. return AddOperation(new CanvasTranslateOperationBase(position));
  134. }
  135. public TestPlan ExpectCanvasTranslate(float left, float top)
  136. {
  137. return AddOperation(new CanvasTranslateOperationBase(new Position(left, top)));
  138. }
  139. public TestPlan ExpectCanvasDrawRectangle(Position position, Size size, string color)
  140. {
  141. return AddOperation(new CanvasDrawRectangleOperationBase(position, size, color));
  142. }
  143. public TestPlan ExpectCanvasDrawText(string text, Position position, TextStyle style)
  144. {
  145. return AddOperation(new CanvasDrawTextOperationBase(text, position, style));
  146. }
  147. public TestPlan ExpectCanvasDrawImage(Position position, Size size)
  148. {
  149. return AddOperation(new CanvasDrawImageOperationBase(position, size));
  150. }
  151. public TestPlan CheckMeasureResult(ISpacePlan expected)
  152. {
  153. var actual = Element.Measure(OperationInput);
  154. Assert.AreEqual(expected.GetType(), actual.GetType());
  155. var expectedSize = expected as Size;
  156. var actualSize = actual as Size;
  157. if (expectedSize != null)
  158. {
  159. Assert.AreEqual(expectedSize.Width, actualSize.Width);
  160. Assert.AreEqual(expectedSize.Height, actualSize.Height);
  161. }
  162. return this;
  163. }
  164. public TestPlan CheckDrawResult()
  165. {
  166. Element.Draw(Canvas, OperationInput);
  167. return this;
  168. }
  169. }
  170. }