DateFieldTests.cs 10 KB

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