TimeFieldTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xunit;
  7. namespace Terminal.Gui.ViewsTests {
  8. public class TimeFieldTests {
  9. [Fact]
  10. public void Constructors_Defaults ()
  11. {
  12. var tf = new TimeField ();
  13. Assert.False (tf.IsShortFormat);
  14. Assert.Equal (TimeSpan.MinValue, tf.Time);
  15. Assert.Equal (1, tf.CursorPosition);
  16. Assert.Equal (new Rect (0, 0, 10, 1), tf.Frame);
  17. var time = DateTime.Now.TimeOfDay;
  18. tf = new TimeField (time);
  19. Assert.False (tf.IsShortFormat);
  20. Assert.Equal (time, tf.Time);
  21. Assert.Equal (1, tf.CursorPosition);
  22. Assert.Equal (new Rect (0, 0, 10, 1), tf.Frame);
  23. tf = new TimeField (1, 2, time);
  24. Assert.False (tf.IsShortFormat);
  25. Assert.Equal (time, tf.Time);
  26. Assert.Equal (1, tf.CursorPosition);
  27. Assert.Equal (new Rect (1, 2, 10, 1), tf.Frame);
  28. tf = new TimeField (3, 4, time, true);
  29. Assert.True (tf.IsShortFormat);
  30. Assert.Equal (time, tf.Time);
  31. Assert.Equal (1, tf.CursorPosition);
  32. Assert.Equal (new Rect (3, 4, 7, 1), tf.Frame);
  33. tf.IsShortFormat = false;
  34. Assert.Equal (new Rect (3, 4, 10, 1), tf.Frame);
  35. Assert.Equal (10, tf.Width);
  36. }
  37. [Fact]
  38. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
  39. {
  40. var tf = new TimeField ();
  41. Assert.Equal (1, tf.CursorPosition);
  42. tf.CursorPosition = 0;
  43. Assert.Equal (1, tf.CursorPosition);
  44. tf.CursorPosition = 9;
  45. Assert.Equal (8, tf.CursorPosition);
  46. tf.IsShortFormat = true;
  47. tf.CursorPosition = 0;
  48. Assert.Equal (1, tf.CursorPosition);
  49. tf.CursorPosition = 6;
  50. Assert.Equal (5, tf.CursorPosition);
  51. }
  52. [Fact]
  53. public void KeyBindings_Command ()
  54. {
  55. TimeField tf = new TimeField (TimeSpan.Parse ("12:12:19"));
  56. tf.ReadOnly = true;
  57. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Delete)));
  58. Assert.Equal (" 12:12:19", tf.Text);
  59. tf.ReadOnly = false;
  60. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D | KeyCode.CtrlMask)));
  61. Assert.Equal (" 02:12:19", tf.Text);
  62. tf.CursorPosition = 4;
  63. tf.ReadOnly = true;
  64. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Delete)));
  65. Assert.Equal (" 02:12:19", tf.Text);
  66. tf.ReadOnly = false;
  67. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Backspace)));
  68. Assert.Equal (" 02:02:19", tf.Text);
  69. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Home)));
  70. Assert.Equal (1, tf.CursorPosition);
  71. Assert.True (tf.NewKeyDownEvent (new (KeyCode.End)));
  72. Assert.Equal (8, tf.CursorPosition);
  73. Assert.True (tf.NewKeyDownEvent (new (KeyCode.A | KeyCode.CtrlMask)));
  74. Assert.Equal (1, tf.CursorPosition);
  75. Assert.True (tf.NewKeyDownEvent (new (KeyCode.E | KeyCode.CtrlMask)));
  76. Assert.Equal (8, tf.CursorPosition);
  77. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorLeft)));
  78. Assert.Equal (7, tf.CursorPosition);
  79. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorRight)));
  80. Assert.Equal (8, tf.CursorPosition);
  81. // Non-numerics are ignored
  82. Assert.True (tf.NewKeyDownEvent (new (KeyCode.A)));
  83. tf.ReadOnly = true;
  84. tf.CursorPosition = 1;
  85. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D1)));
  86. Assert.Equal (" 02:02:19", tf.Text);
  87. tf.ReadOnly = false;
  88. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D1)));
  89. Assert.Equal (" 12:02:19", tf.Text);
  90. }
  91. }
  92. }