DateFieldTests.cs 11 KB

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