DateFieldTests.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 DateFieldTests {
  9. [Fact]
  10. public void Constructors_Defaults ()
  11. {
  12. var df = new DateField ();
  13. Assert.False (df.IsShortFormat);
  14. Assert.Equal (DateTime.MinValue, df.Date);
  15. Assert.Equal (1, df.CursorPosition);
  16. Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
  17. var date = DateTime.Now;
  18. df = new DateField (date);
  19. Assert.False (df.IsShortFormat);
  20. Assert.Equal (date, df.Date);
  21. Assert.Equal (1, df.CursorPosition);
  22. Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
  23. df = new DateField (1, 2, date);
  24. Assert.False (df.IsShortFormat);
  25. Assert.Equal (date, df.Date);
  26. Assert.Equal (1, df.CursorPosition);
  27. Assert.Equal (new Rect (1, 2, 12, 1), df.Frame);
  28. df = new DateField (3, 4, date, true);
  29. Assert.True (df.IsShortFormat);
  30. Assert.Equal (date, df.Date);
  31. Assert.Equal (1, df.CursorPosition);
  32. Assert.Equal (new Rect (3, 4, 10, 1), df.Frame);
  33. df.IsShortFormat = false;
  34. Assert.Equal (new Rect (3, 4, 12, 1), df.Frame);
  35. Assert.Equal (12, df.Width);
  36. }
  37. [Fact]
  38. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
  39. {
  40. var df = new DateField ();
  41. Assert.Equal (1, df.CursorPosition);
  42. df.CursorPosition = 0;
  43. Assert.Equal (1, df.CursorPosition);
  44. df.CursorPosition = 11;
  45. Assert.Equal (10, df.CursorPosition);
  46. df.IsShortFormat = true;
  47. df.CursorPosition = 0;
  48. Assert.Equal (1, df.CursorPosition);
  49. df.CursorPosition = 9;
  50. Assert.Equal (8, df.CursorPosition);
  51. }
  52. [Fact]
  53. public void KeyBindings_Command ()
  54. {
  55. DateField df = new DateField (DateTime.Parse ("12/12/1971"));
  56. df.ReadOnly = true;
  57. Assert.True (df.ProcessKey (new KeyEvent (Key.DeleteChar, new KeyModifiers ())));
  58. Assert.Equal (" 12/12/1971", df.Text);
  59. df.ReadOnly = false;
  60. Assert.True (df.ProcessKey (new KeyEvent (Key.D | Key.CtrlMask, new KeyModifiers ())));
  61. Assert.Equal (" 02/12/1971", df.Text);
  62. df.CursorPosition = 4;
  63. df.ReadOnly = true;
  64. Assert.True (df.ProcessKey (new KeyEvent (Key.Delete, new KeyModifiers ())));
  65. Assert.Equal (" 02/12/1971", df.Text);
  66. df.ReadOnly = false;
  67. Assert.True (df.ProcessKey (new KeyEvent (Key.Backspace, new KeyModifiers ())));
  68. Assert.Equal (" 02/02/1971", df.Text);
  69. Assert.True (df.ProcessKey (new KeyEvent (Key.Home, new KeyModifiers ())));
  70. Assert.Equal (1, df.CursorPosition);
  71. Assert.True (df.ProcessKey (new KeyEvent (Key.End, new KeyModifiers ())));
  72. Assert.Equal (10, df.CursorPosition);
  73. Assert.True (df.ProcessKey (new KeyEvent (Key.A | Key.CtrlMask, new KeyModifiers ())));
  74. Assert.Equal (1, df.CursorPosition);
  75. Assert.True (df.ProcessKey (new KeyEvent (Key.E | Key.CtrlMask, new KeyModifiers ())));
  76. Assert.Equal (10, df.CursorPosition);
  77. Assert.True (df.ProcessKey (new KeyEvent (Key.CursorLeft, new KeyModifiers ())));
  78. Assert.Equal (9, df.CursorPosition);
  79. Assert.True (df.ProcessKey (new KeyEvent (Key.CursorRight, new KeyModifiers ())));
  80. Assert.Equal (10, df.CursorPosition);
  81. Assert.False (df.ProcessKey (new KeyEvent (Key.A, new KeyModifiers ())));
  82. df.ReadOnly = true;
  83. df.CursorPosition = 1;
  84. Assert.True (df.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers ())));
  85. Assert.Equal (" 02/02/1971", df.Text);
  86. df.ReadOnly = false;
  87. Assert.True (df.ProcessKey (new KeyEvent (Key.D1, new KeyModifiers ())));
  88. Assert.Equal (" 12/02/1971", df.Text);
  89. }
  90. }
  91. }