PosTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Xunit;
  8. // Alais Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui {
  11. public class PosTests {
  12. [Fact]
  13. public void New_Works ()
  14. {
  15. var pos = new Pos ();
  16. Assert.Equal ("Terminal.Gui.Pos", pos.ToString ());
  17. }
  18. [Fact]
  19. public void AnchorEnd_SetsValue ()
  20. {
  21. var n = 0;
  22. var pos = Pos.AnchorEnd ();
  23. Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
  24. n = 5;
  25. pos = Pos.AnchorEnd (n);
  26. Assert.Equal ($"Pos.AnchorEnd(margin={n})", pos.ToString ());
  27. }
  28. [Fact]
  29. public void AnchorEnd_Equal ()
  30. {
  31. var n1 = 0;
  32. var n2 = 0;
  33. var pos1 = Pos.AnchorEnd (n1);
  34. var pos2 = Pos.AnchorEnd (n2);
  35. Assert.Equal (pos1, pos2);
  36. // Test inequality
  37. n2 = 5;
  38. pos2 = Pos.AnchorEnd (n2);
  39. Assert.NotEqual (pos1, pos2);
  40. }
  41. [Fact]
  42. public void At_SetsValue ()
  43. {
  44. var pos = Pos.At (0);
  45. Assert.Equal ("Pos.Absolute(0)", pos.ToString ());
  46. pos = Pos.At (5);
  47. Assert.Equal ("Pos.Absolute(5)", pos.ToString ());
  48. //Assert.Throws<ArgumentException> (() => pos = Pos.At (-1));
  49. }
  50. [Fact]
  51. public void At_Equal ()
  52. {
  53. var n1 = 0;
  54. var n2 = 0;
  55. var pos1 = Pos.At (n1);
  56. var pos2 = Pos.At (n2);
  57. // BUGBUG: Pos should implement equality and this should change to Equal
  58. Assert.NotEqual (pos1, pos2);
  59. }
  60. [Fact]
  61. public void Left_SetsValue ()
  62. {
  63. var pos = Pos.Left (null);
  64. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  65. var testVal = Rect.Empty;
  66. pos = Pos.Left (new View ());
  67. Assert.Equal ($"Pos.View(side=x, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  68. pos = Pos.Left (new View (testVal));
  69. Assert.Equal ($"Pos.View(side=x, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  70. testVal = new Rect (1, 2, 3, 4);
  71. pos = Pos.Left (new View (testVal));
  72. Assert.Equal ($"Pos.View(side=x, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  73. }
  74. // TODO: Test Left, Top, Right bottom Equal
  75. [Fact]
  76. public void Top_SetsValue ()
  77. {
  78. var pos = Pos.Top (null);
  79. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  80. var testVal = Rect.Empty;
  81. pos = Pos.Top (new View ());
  82. Assert.Equal ($"Pos.View(side=y, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  83. testVal = new Rect (1, 2, 3, 4);
  84. pos = Pos.Top (new View (testVal));
  85. Assert.Equal ($"Pos.View(side=y, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  86. }
  87. [Fact]
  88. public void Right_SetsValue ()
  89. {
  90. var pos = Pos.Right (null);
  91. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  92. var testVal = Rect.Empty;
  93. pos = Pos.Right (new View ());
  94. Assert.Equal ($"Pos.View(side=right, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  95. testVal = Rect.Empty;
  96. pos = Pos.Right (new View (testVal));
  97. Assert.Equal ($"Pos.View(side=right, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  98. testVal = new Rect (1, 2, 3, 4);
  99. pos = Pos.Right (new View (testVal));
  100. Assert.Equal ($"Pos.View(side=right, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  101. }
  102. [Fact]
  103. public void Bottom_SetsValue ()
  104. {
  105. var pos = Pos.Bottom (null);
  106. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  107. var testVal = Rect.Empty;
  108. pos = Pos.Bottom (new View ());
  109. Assert.Equal ($"Pos.View(side=bottom, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  110. testVal = Rect.Empty;
  111. pos = Pos.Bottom (new View (testVal));
  112. Assert.Equal ($"Pos.View(side=bottom, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  113. testVal = new Rect (1, 2, 3, 4);
  114. pos = Pos.Bottom (new View (testVal));
  115. Assert.Equal ($"Pos.View(side=bottom, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  116. //Assert.Throws<ArgumentException> (() => pos = Pos.Bottom (new View (new Rect (0, 0, -3, -4))));
  117. }
  118. [Fact]
  119. public void Center_SetsValue ()
  120. {
  121. var pos = Pos.Center ();
  122. Assert.Equal ("Pos.Center", pos.ToString ());
  123. }
  124. [Fact]
  125. public void Percent_SetsValue ()
  126. {
  127. var pos = Pos.Percent (0);
  128. Assert.Equal ("Pos.Factor(0)", pos.ToString ());
  129. pos = Pos.Percent (0.5F);
  130. Assert.Equal ("Pos.Factor(0.005)", pos.ToString ());
  131. pos = Pos.Percent (100);
  132. Assert.Equal ("Pos.Factor(1)", pos.ToString ());
  133. }
  134. [Fact]
  135. public void Percent_Equal ()
  136. {
  137. var n1 = 0;
  138. var n2 = 0;
  139. var pos1 = Pos.Percent (n1);
  140. var pos2 = Pos.Percent (n2);
  141. // BUGBUG: Pos.Percent should support equality
  142. Assert.NotEqual (pos1, pos2);
  143. }
  144. [Fact]
  145. public void Percent_ThrowsOnIvalid ()
  146. {
  147. var pos = Pos.Percent (0);
  148. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  149. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  150. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  151. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  152. }
  153. // TODO: Test operators
  154. }
  155. }