DateFieldTests.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Globalization;
  3. using Xunit;
  4. namespace Terminal.Gui.ViewsTests;
  5. public class DateFieldTests {
  6. [Fact, TestDate]
  7. public void Constructors_Defaults ()
  8. {
  9. var df = new DateField ();
  10. Assert.Equal (DateTime.MinValue, df.Date);
  11. Assert.Equal (1, df.CursorPosition);
  12. Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
  13. Assert.Equal (" 01/01/0001", df.Text);
  14. var date = DateTime.Now;
  15. df = new DateField (date);
  16. Assert.Equal (date, df.Date);
  17. Assert.Equal (1, df.CursorPosition);
  18. Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
  19. Assert.Equal ($" {date.ToString (CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)}", df.Text);
  20. df = new DateField (date) { X = 1, Y = 2 };
  21. Assert.Equal (date, df.Date);
  22. Assert.Equal (1, df.CursorPosition);
  23. Assert.Equal (new Rect (1, 2, 12, 1), df.Frame);
  24. Assert.Equal ($" {date.ToString (CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)}", df.Text);
  25. }
  26. [Fact, TestDate]
  27. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
  28. {
  29. var df = new DateField ();
  30. Assert.Equal (1, df.CursorPosition);
  31. df.CursorPosition = 0;
  32. Assert.Equal (1, df.CursorPosition);
  33. df.CursorPosition = 11;
  34. Assert.Equal (10, df.CursorPosition);
  35. }
  36. [Fact, TestDate]
  37. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format_After_Selection ()
  38. {
  39. var df = new DateField ();
  40. // Start selection
  41. Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask)));
  42. Assert.Equal (1, df.SelectedStart);
  43. Assert.Equal (1, df.SelectedLength);
  44. Assert.Equal (0, df.CursorPosition);
  45. // Without selection
  46. Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft)));
  47. Assert.Equal (-1, df.SelectedStart);
  48. Assert.Equal (0, df.SelectedLength);
  49. Assert.Equal (1, df.CursorPosition);
  50. df.CursorPosition = 10;
  51. Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
  52. Assert.Equal (10, df.SelectedStart);
  53. Assert.Equal (1, df.SelectedLength);
  54. Assert.Equal (11, df.CursorPosition);
  55. Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight)));
  56. Assert.Equal (-1, df.SelectedStart);
  57. Assert.Equal (0, df.SelectedLength);
  58. Assert.Equal (10, df.CursorPosition);
  59. }
  60. [Fact, TestDate]
  61. public void KeyBindings_Command ()
  62. {
  63. DateField df = new DateField (DateTime.Parse ("12/12/1971")) {
  64. ReadOnly = true
  65. };
  66. Assert.True (df.NewKeyDownEvent (new (KeyCode.Delete)));
  67. Assert.Equal (" 12/12/1971", df.Text);
  68. df.ReadOnly = false;
  69. Assert.True (df.NewKeyDownEvent (new (KeyCode.D | KeyCode.CtrlMask)));
  70. Assert.Equal (" 02/12/1971", df.Text);
  71. df.CursorPosition = 4;
  72. df.ReadOnly = true;
  73. Assert.True (df.NewKeyDownEvent (new (KeyCode.Delete)));
  74. Assert.Equal (" 02/12/1971", df.Text);
  75. df.ReadOnly = false;
  76. Assert.True (df.NewKeyDownEvent (new (KeyCode.Backspace)));
  77. Assert.Equal (" 02/02/1971", df.Text);
  78. Assert.True (df.NewKeyDownEvent (new (KeyCode.Home)));
  79. Assert.Equal (1, df.CursorPosition);
  80. Assert.True (df.NewKeyDownEvent (new (KeyCode.End)));
  81. Assert.Equal (10, df.CursorPosition);
  82. Assert.True (df.NewKeyDownEvent (new (KeyCode.A | KeyCode.CtrlMask)));
  83. Assert.Equal (1, df.CursorPosition);
  84. Assert.True (df.NewKeyDownEvent (new (KeyCode.E | KeyCode.CtrlMask)));
  85. Assert.Equal (10, df.CursorPosition);
  86. Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft)));
  87. Assert.Equal (9, df.CursorPosition);
  88. Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight)));
  89. Assert.Equal (10, df.CursorPosition);
  90. // Non-numerics are ignored
  91. Assert.False (df.NewKeyDownEvent (new (KeyCode.A)));
  92. df.ReadOnly = true;
  93. df.CursorPosition = 1;
  94. Assert.True (df.NewKeyDownEvent (new (KeyCode.D1)));
  95. Assert.Equal (" 02/02/1971", df.Text);
  96. df.ReadOnly = false;
  97. Assert.True (df.NewKeyDownEvent (new (KeyCode.D1)));
  98. Assert.Equal (" 12/02/1971", df.Text);
  99. Assert.Equal (2, df.CursorPosition);
  100. Assert.True (df.NewKeyDownEvent (new (KeyCode.D | KeyCode.AltMask)));
  101. Assert.Equal (" 10/02/1971", df.Text);
  102. }
  103. [Fact, TestDate]
  104. public void Typing_With_Selection_Normalize_Format ()
  105. {
  106. DateField df = new DateField (DateTime.Parse ("12/12/1971")) {
  107. // Start selection at before the first separator /
  108. CursorPosition = 2
  109. };
  110. // Now select the separator /
  111. Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
  112. Assert.Equal (2, df.SelectedStart);
  113. Assert.Equal (1, df.SelectedLength);
  114. Assert.Equal (3, df.CursorPosition);
  115. // Type 3 over the separator
  116. Assert.True (df.NewKeyDownEvent (new (KeyCode.D3)));
  117. // The format was normalized and replaced again with /
  118. Assert.Equal (" 12/12/1971", df.Text);
  119. Assert.Equal (4, df.CursorPosition);
  120. }
  121. [Fact, TestDate, AutoInitShutdown]
  122. public void Copy_Paste ()
  123. {
  124. DateField df1 = new DateField (DateTime.Parse ("12/12/1971"));
  125. DateField df2 = new DateField (DateTime.Parse ("12/31/2023"));
  126. // Select all text
  127. Assert.True (df2.NewKeyDownEvent (new (KeyCode.End | KeyCode.ShiftMask)));
  128. Assert.Equal (1, df2.SelectedStart);
  129. Assert.Equal (10, df2.SelectedLength);
  130. Assert.Equal (11, df2.CursorPosition);
  131. // Copy from df2
  132. Assert.True (df2.NewKeyDownEvent (new (KeyCode.C | KeyCode.CtrlMask)));
  133. // Paste into df1
  134. Assert.True (df1.NewKeyDownEvent (new (KeyCode.V | KeyCode.CtrlMask)));
  135. Assert.Equal (" 12/31/2023", df1.Text);
  136. Assert.Equal (11, df1.CursorPosition);
  137. }
  138. [Fact, TestDate]
  139. public void Date_Start_From_01_01_0001_And_End_At_12_31_9999 ()
  140. {
  141. DateField df = new DateField (DateTime.Parse ("01/01/0001"));
  142. Assert.Equal (" 01/01/0001", df.Text);
  143. df.Date = DateTime.Parse ("12/31/9999");
  144. Assert.Equal (" 12/31/9999", df.Text);
  145. }
  146. [Fact]
  147. public void Using_Pt_Culture ()
  148. {
  149. CultureInfo cultureBackup = CultureInfo.CurrentCulture;
  150. CultureInfo.CurrentCulture = new CultureInfo ("pt-PT");
  151. DateField df = new DateField (DateTime.Parse ("12/12/1971")) {
  152. // Move to the first 2
  153. CursorPosition = 2
  154. };
  155. // Type 3 over the separator
  156. Assert.True (df.NewKeyDownEvent (new (KeyCode.D3)));
  157. // If InvariantCulture was used this will fail but not with PT culture
  158. Assert.Equal (" 13/12/1971", df.Text);
  159. Assert.Equal ("13/12/1971", df.Date.ToString (CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern));
  160. Assert.Equal (4, df.CursorPosition);
  161. CultureInfo.CurrentCulture = cultureBackup;
  162. }
  163. [Fact]
  164. public void Using_All_Culture_StandardizeDateFormat ()
  165. {
  166. CultureInfo cultureBackup = CultureInfo.CurrentCulture;
  167. DateTime date = DateTime.Parse ("1/1/1971");
  168. foreach (var culture in CultureInfo.GetCultures (CultureTypes.AllCultures)) {
  169. CultureInfo.CurrentCulture = culture;
  170. var separator = culture.DateTimeFormat.DateSeparator.Trim ();
  171. if (separator.Length > 1 && separator.Contains ('\u200f')) {
  172. separator = separator.Replace ("\u200f", "");
  173. }
  174. var format = culture.DateTimeFormat.ShortDatePattern;
  175. DateField df = new DateField (date);
  176. if ((!culture.TextInfo.IsRightToLeft || (culture.TextInfo.IsRightToLeft && !df.Text.Contains ('\u200f')))
  177. && (format.StartsWith ('d') || format.StartsWith ('M'))) {
  178. switch (culture.Name) {
  179. case "ar-SA":
  180. Assert.Equal ($" 04{separator}11{separator}1390", df.Text);
  181. break;
  182. case "th":
  183. case "th-TH":
  184. Assert.Equal ($" 01{separator}01{separator}2514", df.Text);
  185. break;
  186. default:
  187. Assert.Equal ($" 01{separator}01{separator}1971", df.Text);
  188. break;
  189. }
  190. } else if (culture.TextInfo.IsRightToLeft) {
  191. if (df.Text.Contains ('\u200f')) {
  192. // It's a Unicode Character (U+200F) - Right-to-Left Mark (RLM)
  193. Assert.True (df.Text.Contains ('\u200f'));
  194. switch (culture.Name) {
  195. case "ar-SA":
  196. Assert.Equal ($" 04‏{separator}11‏{separator}1390", df.Text);
  197. break;
  198. default:
  199. Assert.Equal ($" 01‏{separator}01‏{separator}1971", df.Text);
  200. break;
  201. }
  202. } else {
  203. switch (culture.Name) {
  204. case "ckb-IR":
  205. case "fa":
  206. case "fa-AF":
  207. case "fa-IR":
  208. case "lrc":
  209. case "lrc-IR":
  210. case "mzn":
  211. case "mzn-IR":
  212. case "ps":
  213. case "ps-AF":
  214. case "uz-Arab":
  215. case "uz-Arab-AF":
  216. Assert.Equal ($" 1349{separator}10{separator}11", df.Text);
  217. break;
  218. default:
  219. Assert.Equal ($" 1971{separator}01{separator}01", df.Text);
  220. break;
  221. }
  222. }
  223. } else {
  224. switch (culture.Name) {
  225. default:
  226. Assert.Equal ($" 1971{separator}01{separator}01", df.Text);
  227. break;
  228. }
  229. }
  230. }
  231. CultureInfo.CurrentCulture = cultureBackup;
  232. }
  233. }