NumericUpDownTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System.Globalization;
  2. namespace UnitTests_Parallelizable.ViewsTests;
  3. public class NumericUpDownTests
  4. {
  5. [Fact]
  6. public void WhenCreated_ShouldHaveDefaultValues_int ()
  7. {
  8. NumericUpDown<int> numericUpDown = new ();
  9. Assert.Equal (0, numericUpDown.Value);
  10. Assert.Equal (1, numericUpDown.Increment);
  11. }
  12. [Fact]
  13. public void WhenCreated_ShouldHaveDefaultValues_long ()
  14. {
  15. NumericUpDown<long> numericUpDown = new ();
  16. Assert.Equal (0, numericUpDown.Value);
  17. Assert.Equal (1, numericUpDown.Increment);
  18. }
  19. [Fact]
  20. public void WhenCreated_ShouldHaveDefaultValues_float ()
  21. {
  22. NumericUpDown<float> numericUpDown = new ();
  23. Assert.Equal (0F, numericUpDown.Value);
  24. Assert.Equal (1.0F, numericUpDown.Increment);
  25. }
  26. [Fact]
  27. public void WhenCreated_ShouldHaveDefaultValues_double ()
  28. {
  29. NumericUpDown<double> numericUpDown = new ();
  30. Assert.Equal (0F, numericUpDown.Value);
  31. Assert.Equal (1.0F, numericUpDown.Increment);
  32. }
  33. [Fact]
  34. public void WhenCreated_ShouldHaveDefaultValues_decimal ()
  35. {
  36. NumericUpDown<decimal> numericUpDown = new ();
  37. Assert.Equal (0, numericUpDown.Value);
  38. Assert.Equal (1, numericUpDown.Increment);
  39. }
  40. [Fact]
  41. public void WhenCreatedWithCustomValues_ShouldHaveCustomValues_int ()
  42. {
  43. NumericUpDown<int> numericUpDown = new()
  44. {
  45. Value = 10,
  46. Increment = 2
  47. };
  48. Assert.Equal (10, numericUpDown.Value);
  49. Assert.Equal (2, numericUpDown.Increment);
  50. }
  51. [Fact]
  52. public void WhenCreatedWithCustomValues_ShouldHaveCustomValues_float ()
  53. {
  54. NumericUpDown<float> numericUpDown = new()
  55. {
  56. Value = 10.5F,
  57. Increment = 2.5F
  58. };
  59. Assert.Equal (10.5F, numericUpDown.Value);
  60. Assert.Equal (2.5F, numericUpDown.Increment);
  61. }
  62. [Fact]
  63. public void WhenCreatedWithCustomValues_ShouldHaveCustomValues_decimal ()
  64. {
  65. NumericUpDown<decimal> numericUpDown = new ()
  66. {
  67. Value = 10.5m,
  68. Increment = 2.5m
  69. };
  70. Assert.Equal (10.5m, numericUpDown.Value);
  71. Assert.Equal (2.5m, numericUpDown.Increment);
  72. }
  73. [Fact]
  74. public void WhenCreatedWithInvalidType_ShouldThrowInvalidOperationException ()
  75. {
  76. Assert.Throws<InvalidOperationException> (() => new NumericUpDown<string> ());
  77. }
  78. [Fact]
  79. public void WhenCreatedWithInvalidTypeObject_ShouldNotThrowInvalidOperationException ()
  80. {
  81. Exception exception = Record.Exception (() => new NumericUpDown<object> ());
  82. Assert.Null (exception);
  83. }
  84. [Fact]
  85. public void WhenCreatedWithValidNumberType_ShouldThrowInvalidOperationException_UnlessTheyAreRegisterAsValid ()
  86. {
  87. Exception exception = Record.Exception (() => new NumericUpDown<short> ());
  88. Assert.NotNull (exception);
  89. }
  90. [Fact]
  91. public void WhenCreated_ShouldHaveDefaultWidthAndHeight_int ()
  92. {
  93. NumericUpDown<int> numericUpDown = new ();
  94. numericUpDown.SetRelativeLayout (new (100, 100));
  95. Assert.Equal (3, numericUpDown.Frame.Width);
  96. Assert.Equal (1, numericUpDown.Frame.Height);
  97. }
  98. [Fact]
  99. public void WhenCreated_ShouldHaveDefaultWidthAndHeight_float ()
  100. {
  101. NumericUpDown<float> numericUpDown = new ();
  102. numericUpDown.SetRelativeLayout (new (100, 100));
  103. Assert.Equal (3, numericUpDown.Frame.Width);
  104. Assert.Equal (1, numericUpDown.Frame.Height);
  105. }
  106. [Fact]
  107. public void WhenCreated_ShouldHaveDefaultWidthAndHeight_double ()
  108. {
  109. NumericUpDown<double> numericUpDown = new ();
  110. numericUpDown.SetRelativeLayout (new (100, 100));
  111. Assert.Equal (3, numericUpDown.Frame.Width);
  112. Assert.Equal (1, numericUpDown.Frame.Height);
  113. }
  114. [Fact]
  115. public void WhenCreated_ShouldHaveDefaultWidthAndHeight_long ()
  116. {
  117. NumericUpDown<long> numericUpDown = new ();
  118. numericUpDown.SetRelativeLayout (new (100, 100));
  119. Assert.Equal (3, numericUpDown.Frame.Width);
  120. Assert.Equal (1, numericUpDown.Frame.Height);
  121. }
  122. [Fact]
  123. public void WhenCreated_ShouldHaveDefaultWidthAndHeight_decimal ()
  124. {
  125. NumericUpDown<decimal> numericUpDown = new ();
  126. numericUpDown.SetRelativeLayout (new (100, 100));
  127. Assert.Equal (3, numericUpDown.Frame.Width);
  128. Assert.Equal (1, numericUpDown.Frame.Height);
  129. }
  130. [Fact]
  131. public void WhenCreated_Text_Should_Be_Correct_int ()
  132. {
  133. NumericUpDown<int> numericUpDown = new ();
  134. Assert.Equal ("0", numericUpDown.Text);
  135. }
  136. [Fact]
  137. public void WhenCreated_Text_Should_Be_Correct_float ()
  138. {
  139. NumericUpDown<float> numericUpDown = new ();
  140. Assert.Equal ("0", numericUpDown.Text);
  141. }
  142. [Fact]
  143. public void Format_Default ()
  144. {
  145. NumericUpDown<float> numericUpDown = new ();
  146. Assert.Equal ("{0}", numericUpDown.Format);
  147. }
  148. [Theory]
  149. [InlineData (0F, "{0}", "0")]
  150. [InlineData (1.1F, "{0}", "1.1")]
  151. [InlineData (0F, "{0:0%}", "0%")]
  152. [InlineData (.75F, "{0:0%}", "75%")]
  153. public void Format_decimal (float value, string format, string expectedText)
  154. {
  155. CultureInfo currentCulture = CultureInfo.CurrentCulture;
  156. CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
  157. NumericUpDown<float> numericUpDown = new ();
  158. numericUpDown.Format = format;
  159. numericUpDown.Value = value;
  160. Assert.Equal (expectedText, numericUpDown.Text);
  161. CultureInfo.CurrentCulture = currentCulture;
  162. }
  163. [Theory]
  164. [InlineData (0, "{0}", "0")]
  165. [InlineData (11, "{0}", "11")]
  166. [InlineData (-1, "{0}", "-1")]
  167. [InlineData (911, "{0:X}", "38F")]
  168. [InlineData (911, "0x{0:X04}", "0x038F")]
  169. public void Format_int (int value, string format, string expectedText)
  170. {
  171. CultureInfo currentCulture = CultureInfo.CurrentCulture;
  172. CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
  173. NumericUpDown<int> numericUpDown = new ();
  174. numericUpDown.Format = format;
  175. numericUpDown.Value = value;
  176. Assert.Equal (expectedText, numericUpDown.Text);
  177. CultureInfo.CurrentCulture = currentCulture;
  178. }
  179. [Fact]
  180. public void KeyDown_CursorUp_Increments ()
  181. {
  182. NumericUpDown<int> numericUpDown = new ();
  183. numericUpDown.NewKeyDownEvent (Key.CursorUp);
  184. Assert.Equal (1, numericUpDown.Value);
  185. }
  186. [Fact]
  187. public void KeyDown_CursorDown_Decrements ()
  188. {
  189. NumericUpDown<int> numericUpDown = new ();
  190. numericUpDown.NewKeyDownEvent (Key.CursorDown);
  191. Assert.Equal (-1, numericUpDown.Value);
  192. }
  193. }