DateFieldTests.cs 11 KB

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