SliderTests.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using Xunit;
  2. using Terminal.Gui;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace Terminal.Gui.ViewsTests;
  9. public class SliderOptionTests {
  10. [Fact]
  11. public void Slider_Option_Default_Constructor ()
  12. {
  13. var o = new SliderOption<int> ();
  14. Assert.Null (o.Legend);
  15. Assert.Equal (default, o.LegendAbbr);
  16. Assert.Equal (default, o.Data);
  17. }
  18. [Fact]
  19. public void Slider_Option_Values_Constructor ()
  20. {
  21. var o = new SliderOption<int> ("1 thousand", new Rune ('y'), 1000);
  22. Assert.Equal ("1 thousand", o.Legend);
  23. Assert.Equal (new Rune ('y'), o.LegendAbbr);
  24. Assert.Equal (1000, o.Data);
  25. }
  26. [Fact]
  27. public void OnSet_Should_Raise_SetEvent ()
  28. {
  29. // Arrange
  30. var sliderOption = new SliderOption<int> ();
  31. var eventRaised = false;
  32. sliderOption.Set += (sender, args) => eventRaised = true;
  33. // Act
  34. sliderOption.OnSet ();
  35. // Assert
  36. Assert.True (eventRaised);
  37. }
  38. [Fact]
  39. public void OnUnSet_Should_Raise_UnSetEvent ()
  40. {
  41. // Arrange
  42. var sliderOption = new SliderOption<int> ();
  43. var eventRaised = false;
  44. sliderOption.UnSet += (sender, args) => eventRaised = true;
  45. // Act
  46. sliderOption.OnUnSet ();
  47. // Assert
  48. Assert.True (eventRaised);
  49. }
  50. [Fact]
  51. public void OnChanged_Should_Raise_ChangedEvent ()
  52. {
  53. // Arrange
  54. var sliderOption = new SliderOption<int> ();
  55. var eventRaised = false;
  56. sliderOption.Changed += (sender, args) => eventRaised = true;
  57. // Act
  58. sliderOption.OnChanged (true);
  59. // Assert
  60. Assert.True (eventRaised);
  61. }
  62. [Fact]
  63. public void SliderOption_ToString_WhenEmpty ()
  64. {
  65. var sliderOption = new SliderOption<object> ();
  66. Assert.Equal ("{Legend=, LegendAbbr=\0, Data=}", sliderOption.ToString ());
  67. }
  68. [Fact]
  69. public void SliderOption_ToString_WhenPopulated_WithInt ()
  70. {
  71. var sliderOption = new SliderOption<int> {
  72. Legend = "Lord flibble",
  73. LegendAbbr = new Rune ('l'),
  74. Data = 1,
  75. };
  76. Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data=1}", sliderOption.ToString ());
  77. }
  78. [Fact]
  79. public void SliderOption_ToString_WhenPopulated_WithSizeF ()
  80. {
  81. var sliderOption = new SliderOption<SizeF> {
  82. Legend = "Lord flibble",
  83. LegendAbbr = new Rune ('l'),
  84. Data = new SizeF(32,11),
  85. };
  86. Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data={Width=32, Height=11}}", sliderOption.ToString ());
  87. }
  88. }
  89. public class SliderEventArgsTests {
  90. [Fact]
  91. public void Constructor_Sets_Options ()
  92. {
  93. // Arrange
  94. var options = new Dictionary<int, SliderOption<int>> ();
  95. // Act
  96. var sliderEventArgs = new SliderEventArgs<int> (options);
  97. // Assert
  98. Assert.Equal (options, sliderEventArgs.Options);
  99. }
  100. [Fact]
  101. public void Constructor_Sets_Focused ()
  102. {
  103. // Arrange
  104. var options = new Dictionary<int, SliderOption<int>> ();
  105. var focused = 42;
  106. // Act
  107. var sliderEventArgs = new SliderEventArgs<int> (options, focused);
  108. // Assert
  109. Assert.Equal (focused, sliderEventArgs.Focused);
  110. }
  111. [Fact]
  112. public void Constructor_Sets_Cancel_Default_To_False ()
  113. {
  114. // Arrange
  115. var options = new Dictionary<int, SliderOption<int>> ();
  116. var focused = 42;
  117. // Act
  118. var sliderEventArgs = new SliderEventArgs<int> (options, focused);
  119. // Assert
  120. Assert.False (sliderEventArgs.Cancel);
  121. }
  122. }
  123. public class SliderTests {
  124. [Fact]
  125. public void Constructor_Default ()
  126. {
  127. // Arrange & Act
  128. var slider = new Slider<int> ();
  129. // Assert
  130. Assert.NotNull (slider);
  131. Assert.NotNull (slider.Options);
  132. Assert.Empty (slider.Options);
  133. Assert.Equal (Orientation.Horizontal, slider.Orientation);
  134. Assert.False (slider.AllowEmpty);
  135. Assert.True (slider.ShowLegends);
  136. Assert.False (slider.ShowSpacing);
  137. Assert.Equal (SliderType.Single, slider.Type);
  138. Assert.Equal (0, slider.InnerSpacing);
  139. Assert.False (slider.AutoSize);
  140. Assert.Equal (0, slider.FocusedOption);
  141. }
  142. [Fact]
  143. public void Constructor_With_Options ()
  144. {
  145. // Arrange
  146. var options = new List<int> { 1, 2, 3 };
  147. // Act
  148. var slider = new Slider<int> (options);
  149. // Assert
  150. Assert.NotNull (slider);
  151. Assert.NotNull (slider.Options);
  152. Assert.Equal (options.Count, slider.Options.Count);
  153. }
  154. [Fact]
  155. public void OnOptionsChanged_Event_Raised ()
  156. {
  157. // Arrange
  158. var slider = new Slider<int> ();
  159. bool eventRaised = false;
  160. slider.OptionsChanged += (sender, args) => eventRaised = true;
  161. // Act
  162. slider.OnOptionsChanged ();
  163. // Assert
  164. Assert.True (eventRaised);
  165. }
  166. [Fact]
  167. public void OnOptionFocused_Event_Raised ()
  168. {
  169. // Arrange
  170. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  171. bool eventRaised = false;
  172. slider.OptionFocused += (sender, args) => eventRaised = true;
  173. int newFocusedOption = 1;
  174. var args = new SliderEventArgs<int> (new Dictionary<int, SliderOption<int>> (), newFocusedOption);
  175. // Act
  176. slider.OnOptionFocused (newFocusedOption, args);
  177. // Assert
  178. Assert.True (eventRaised);
  179. }
  180. [Fact]
  181. public void OnOptionFocused_Event_Cancelled ()
  182. {
  183. // Arrange
  184. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  185. bool eventRaised = false;
  186. bool cancel = false;
  187. slider.OptionFocused += (sender, args) => eventRaised = true;
  188. int newFocusedOption = 1;
  189. // Create args with cancel set to false
  190. cancel = false;
  191. var args = new SliderEventArgs<int> (new Dictionary<int, SliderOption<int>> (), newFocusedOption) {
  192. Cancel = cancel
  193. };
  194. Assert.Equal (0, slider.FocusedOption);
  195. // Act
  196. slider.OnOptionFocused (newFocusedOption, args);
  197. // Assert
  198. Assert.True (eventRaised); // Event should be raised
  199. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should change
  200. // Create args with cancel set to true
  201. cancel = true;
  202. args = new SliderEventArgs<int> (new Dictionary<int, SliderOption<int>> (), newFocusedOption) {
  203. Cancel = cancel
  204. };
  205. // Act
  206. slider.OnOptionFocused (2, args);
  207. // Assert
  208. Assert.True (eventRaised); // Event should be raised
  209. Assert.Equal (newFocusedOption, slider.FocusedOption); // Focused option should not change
  210. }
  211. [Theory]
  212. [InlineData (0, 0, 0)]
  213. [InlineData (1, 3, 0)]
  214. [InlineData (3, 9, 0)]
  215. public void TryGetPositionByOption_ValidOptionHorizontal_Success (int option, int expectedX, int expectedY)
  216. {
  217. // Arrange
  218. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  219. slider.AutoSize = true; // Set auto size to true to enable testing
  220. slider.InnerSpacing = 2;
  221. // 0123456789
  222. // 1--2--3--4
  223. // Act
  224. bool result = slider.TryGetPositionByOption (option, out var position);
  225. // Assert
  226. Assert.True (result);
  227. Assert.Equal (expectedX, position.x);
  228. Assert.Equal (expectedY, position.y);
  229. }
  230. [Theory]
  231. [InlineData (0, 0, 0)]
  232. [InlineData (1, 0, 3)]
  233. [InlineData (3, 0, 9)]
  234. public void TryGetPositionByOption_ValidOptionVertical_Success (int option, int expectedX, int expectedY)
  235. {
  236. // Arrange
  237. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  238. slider.Orientation = Orientation.Vertical;
  239. slider.AutoSize = true; // Set auto size to true to enable testing
  240. slider.InnerSpacing = 2;
  241. // Act
  242. bool result = slider.TryGetPositionByOption (option, out var position);
  243. // Assert
  244. Assert.True (result);
  245. Assert.Equal (expectedX, position.x);
  246. Assert.Equal (expectedY, position.y);
  247. }
  248. [Fact]
  249. public void TryGetPositionByOption_InvalidOption_Failure ()
  250. {
  251. // Arrange
  252. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  253. int option = -1;
  254. var expectedPosition = (-1, -1);
  255. // Act
  256. bool result = slider.TryGetPositionByOption (option, out var position);
  257. // Assert
  258. Assert.False (result);
  259. Assert.Equal (expectedPosition, position);
  260. }
  261. [Theory]
  262. [InlineData (0, 0, 0, 1)]
  263. [InlineData (3, 0, 0, 2)]
  264. [InlineData (9, 0, 0, 4)]
  265. [InlineData (0, 0, 1, 1)]
  266. [InlineData (3, 0, 1, 2)]
  267. [InlineData (9, 0, 1, 4)]
  268. public void TryGetOptionByPosition_ValidPositionHorizontal_Success (int x, int y, int threshold, int expectedData)
  269. {
  270. // Arrange
  271. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  272. slider.AutoSize = true; // Set auto size to true to enable testing
  273. slider.InnerSpacing = 2;
  274. // 0123456789
  275. // 1--2--3--4
  276. // Arrange
  277. // Act
  278. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  279. // Assert
  280. Assert.True (result);
  281. Assert.Equal (expectedData, slider.Options [option].Data);
  282. }
  283. [Theory]
  284. [InlineData (0, 0, 0, 1)]
  285. [InlineData (0, 3, 0, 2)]
  286. [InlineData (0, 9, 0, 4)]
  287. [InlineData (0, 0, 1, 1)]
  288. [InlineData (0, 3, 1, 2)]
  289. [InlineData (0, 9, 1, 4)]
  290. public void TryGetOptionByPosition_ValidPositionVertical_Success (int x, int y, int threshold, int expectedData)
  291. {
  292. // Arrange
  293. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  294. slider.Orientation = Orientation.Vertical;
  295. slider.AutoSize = true; // Set auto size to true to enable testing
  296. slider.InnerSpacing = 2;
  297. // 0 1
  298. // 1 |
  299. // 2 |
  300. // 3 2
  301. // 4 |
  302. // 5 |
  303. // 6 3
  304. // 7 |
  305. // 8 |
  306. // 9 4
  307. slider.CalcSpacingConfig ();
  308. // Arrange
  309. // Act
  310. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  311. // Assert
  312. Assert.True (result);
  313. Assert.Equal (expectedData, slider.Options [option].Data);
  314. }
  315. [Fact]
  316. public void TryGetOptionByPosition_InvalidPosition_Failure ()
  317. {
  318. // Arrange
  319. var slider = new Slider<int> (new List<int> { 1, 2, 3 });
  320. int x = 10;
  321. int y = 10;
  322. int threshold = 2;
  323. int expectedOption = -1;
  324. // Act
  325. bool result = slider.TryGetOptionByPosition (x, y, threshold, out int option);
  326. // Assert
  327. Assert.False (result);
  328. Assert.Equal (expectedOption, option);
  329. }
  330. [Fact]
  331. public void MovePlus_Should_MoveFocusRight_When_OptionIsAvailable ()
  332. {
  333. // Arrange
  334. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  335. slider.AutoSize = true;
  336. // Act
  337. bool result = slider.MovePlus ();
  338. // Assert
  339. Assert.True (result);
  340. Assert.Equal (1, slider.FocusedOption);
  341. }
  342. [Fact]
  343. public void MovePlus_Should_NotMoveFocusRight_When_AtEnd ()
  344. {
  345. // Arrange
  346. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  347. slider.AutoSize = true;
  348. slider.FocusedOption = 3;
  349. // Act
  350. bool result = slider.MovePlus ();
  351. // Assert
  352. Assert.False (result);
  353. Assert.Equal (3, slider.FocusedOption);
  354. }
  355. // Add similar tests for other methods like MoveMinus, MoveStart, MoveEnd, Set, etc.
  356. [Fact]
  357. public void Set_Should_SetFocusedOption ()
  358. {
  359. // Arrange
  360. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 });
  361. slider.AutoSize = true;
  362. // Act
  363. slider.FocusedOption = 2;
  364. bool result = slider.Set ();
  365. // Assert
  366. Assert.True (result);
  367. Assert.Equal (2, slider.FocusedOption);
  368. Assert.Single (slider.GetSetOptions ());
  369. }
  370. [Fact]
  371. public void Set_Should_Not_UnSetFocusedOption_When_EmptyNotAllowed ()
  372. {
  373. // Arrange
  374. var slider = new Slider<int> (new List<int> { 1, 2, 3, 4 }) {
  375. AllowEmpty = false
  376. };
  377. slider.AutoSize = true;
  378. Assert.NotEmpty (slider.GetSetOptions ());
  379. // Act
  380. bool result = slider.UnSetOption (slider.FocusedOption);
  381. // Assert
  382. Assert.False (result);
  383. Assert.NotEmpty (slider.GetSetOptions ());
  384. }
  385. // Add more tests for different scenarios and edge cases.
  386. }