TimeFieldTests.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.ViewTests {
  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.ProcessKey (new KeyEvent (Key.DeleteChar, new KeyModifiers ())));
  58. Assert.Equal (" 12:12:19", tf.Text);
  59. tf.ReadOnly = false;
  60. Assert.True (tf.ProcessKey (new KeyEvent (Key.D | Key.CtrlMask, new KeyModifiers ())));
  61. Assert.Equal (" 02:12:19", tf.Text);
  62. tf.CursorPosition = 4;
  63. tf.ReadOnly = true;
  64. Assert.True (tf.ProcessKey (new KeyEvent (Key.Delete, new KeyModifiers ())));
  65. Assert.Equal (" 02:12:19", tf.Text);
  66. tf.ReadOnly = false;
  67. Assert.True (tf.ProcessKey (new KeyEvent (Key.Backspace, new KeyModifiers ())));
  68. Assert.Equal (" 02:02:19", tf.Text);
  69. Assert.True (tf.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  70. Assert.Equal (1, tf.CursorPosition);
  71. Assert.True (tf.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  72. Assert.Equal (8, tf.CursorPosition);
  73. Assert.True (tf.ProcessKey (new KeyEvent (Key.A | Key.CtrlMask, new KeyModifiers ())));
  74. Assert.Equal (1, tf.CursorPosition);
  75. Assert.True (tf.ProcessKey (new KeyEvent (Key.E | Key.CtrlMask, new KeyModifiers ())));
  76. Assert.Equal (8, tf.CursorPosition);
  77. Assert.True (tf.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  78. Assert.Equal (7, tf.CursorPosition);
  79. Assert.True (tf.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  80. Assert.Equal (8, tf.CursorPosition);
  81. Assert.False (tf.ProcessKey (new KeyEvent (Key.A, new KeyModifiers ())));
  82. tf.ReadOnly = true;
  83. tf.CursorPosition = 1;
  84. Assert.True (tf.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers ())));
  85. Assert.Equal (" 02:02:19", tf.Text);
  86. tf.ReadOnly = false;
  87. Assert.True (tf.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers ())));
  88. Assert.Equal (" 12:02:19", tf.Text);
  89. }
  90. }
  91. }