PosTests.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. Assert.Equal (pos1, pos2);
  58. }
  59. [Fact]
  60. public void Left_SetsValue ()
  61. {
  62. var pos = Pos.Left (null);
  63. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  64. var testVal = Rect.Empty;
  65. pos = Pos.Left (new View ());
  66. Assert.Equal ($"Pos.View(side=x, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  67. pos = Pos.Left (new View (testVal));
  68. Assert.Equal ($"Pos.View(side=x, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  69. testVal = new Rect (1, 2, 3, 4);
  70. pos = Pos.Left (new View (testVal));
  71. Assert.Equal ($"Pos.View(side=x, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  72. }
  73. // TODO: Test Left, Top, Right bottom Equal
  74. [Fact]
  75. public void Top_SetsValue ()
  76. {
  77. var pos = Pos.Top (null);
  78. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  79. var testVal = Rect.Empty;
  80. pos = Pos.Top (new View ());
  81. Assert.Equal ($"Pos.View(side=y, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  82. testVal = new Rect (1, 2, 3, 4);
  83. pos = Pos.Top (new View (testVal));
  84. Assert.Equal ($"Pos.View(side=y, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  85. }
  86. [Fact]
  87. public void Right_SetsValue ()
  88. {
  89. var pos = Pos.Right (null);
  90. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  91. var testVal = Rect.Empty;
  92. pos = Pos.Right (new View ());
  93. Assert.Equal ($"Pos.View(side=right, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  94. testVal = Rect.Empty;
  95. pos = Pos.Right (new View (testVal));
  96. Assert.Equal ($"Pos.View(side=right, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  97. testVal = new Rect (1, 2, 3, 4);
  98. pos = Pos.Right (new View (testVal));
  99. Assert.Equal ($"Pos.View(side=right, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  100. }
  101. [Fact]
  102. public void Bottom_SetsValue ()
  103. {
  104. var pos = Pos.Bottom (null);
  105. Assert.Throws<NullReferenceException> (() => pos.ToString ());
  106. var testVal = Rect.Empty;
  107. pos = Pos.Bottom (new View ());
  108. Assert.Equal ($"Pos.View(side=bottom, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  109. testVal = Rect.Empty;
  110. pos = Pos.Bottom (new View (testVal));
  111. Assert.Equal ($"Pos.View(side=bottom, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  112. testVal = new Rect (1, 2, 3, 4);
  113. pos = Pos.Bottom (new View (testVal));
  114. Assert.Equal ($"Pos.View(side=bottom, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", pos.ToString ());
  115. //Assert.Throws<ArgumentException> (() => pos = Pos.Bottom (new View (new Rect (0, 0, -3, -4))));
  116. }
  117. [Fact]
  118. public void Center_SetsValue ()
  119. {
  120. var pos = Pos.Center ();
  121. Assert.Equal ("Pos.Center", pos.ToString ());
  122. }
  123. [Fact]
  124. public void Percent_SetsValue ()
  125. {
  126. float f = 0;
  127. var pos = Pos.Percent (f);
  128. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  129. f = 0.5F;
  130. pos = Pos.Percent (f);
  131. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  132. f = 100;
  133. pos = Pos.Percent (f);
  134. Assert.Equal ($"Pos.Factor({f / 100:0.###})", pos.ToString ());
  135. }
  136. [Fact]
  137. public void Percent_Equal ()
  138. {
  139. var n1 = 0;
  140. var n2 = 0;
  141. var pos1 = Pos.Percent (n1);
  142. var pos2 = Pos.Percent (n2);
  143. // BUGBUG: Pos.Percent should support equality
  144. Assert.NotEqual (pos1, pos2);
  145. }
  146. [Fact]
  147. public void Percent_ThrowsOnIvalid ()
  148. {
  149. var pos = Pos.Percent (0);
  150. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  151. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  152. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  153. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  154. }
  155. // TODO: Test operators
  156. }
  157. }