DateFieldTests.cs 3.7 KB

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