mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-02 01:03:29 +01:00
* Remove the old short two digits year and done some cleanup. * Fixes #3160. TextField doesn't update correctly the CursorPosition on Paste. * Using TextChanging instead of TextChanged event. * Removes old IsShortFormat. * Removing unnecessary private fields and done code cleanup. * Removes unnecessary GetShortDatePattern method, * Fix AdjCursorPosition method. * Create TestDateAttribute. * Reduces indentation and removes unused using. * Remove location from constructors parameters.
This commit is contained in:
@@ -1,185 +1,173 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace Terminal.Gui.ViewsTests {
|
||||
public class DateFieldTests {
|
||||
[Fact]
|
||||
public void Constructors_Defaults ()
|
||||
{
|
||||
var df = new DateField ();
|
||||
Assert.False (df.IsShortFormat);
|
||||
Assert.Equal (DateTime.MinValue, df.Date);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
|
||||
namespace Terminal.Gui.ViewsTests;
|
||||
public class DateFieldTests {
|
||||
[Fact, TestDate]
|
||||
public void Constructors_Defaults ()
|
||||
{
|
||||
var df = new DateField ();
|
||||
Assert.Equal (DateTime.MinValue, df.Date);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
|
||||
Assert.Equal (" 01/01/0001", df.Text);
|
||||
|
||||
var date = DateTime.Now;
|
||||
df = new DateField (date);
|
||||
Assert.False (df.IsShortFormat);
|
||||
Assert.Equal (date, df.Date);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
|
||||
var date = DateTime.Now;
|
||||
df = new DateField (date);
|
||||
Assert.Equal (date, df.Date);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.Equal (new Rect (0, 0, 12, 1), df.Frame);
|
||||
Assert.Equal ($" {date.ToString (CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)}", df.Text);
|
||||
|
||||
df = new DateField (1, 2, date);
|
||||
Assert.False (df.IsShortFormat);
|
||||
Assert.Equal (date, df.Date);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.Equal (new Rect (1, 2, 12, 1), df.Frame);
|
||||
df = new DateField (date) { X = 1, Y = 2 };
|
||||
Assert.Equal (date, df.Date);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.Equal (new Rect (1, 2, 12, 1), df.Frame);
|
||||
Assert.Equal ($" {date.ToString (CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)}", df.Text);
|
||||
}
|
||||
|
||||
df = new DateField (3, 4, date, true);
|
||||
Assert.True (df.IsShortFormat);
|
||||
Assert.Equal (date, df.Date);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.Equal (new Rect (3, 4, 10, 1), df.Frame);
|
||||
[Fact, TestDate]
|
||||
public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
|
||||
{
|
||||
var df = new DateField ();
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
df.CursorPosition = 0;
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
df.CursorPosition = 11;
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
}
|
||||
|
||||
df.IsShortFormat = false;
|
||||
Assert.Equal (new Rect (3, 4, 12, 1), df.Frame);
|
||||
Assert.Equal (12, df.Width);
|
||||
}
|
||||
[Fact, TestDate]
|
||||
public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format_After_Selection ()
|
||||
{
|
||||
var df = new DateField ();
|
||||
// Start selection
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask)));
|
||||
Assert.Equal (1, df.SelectedStart);
|
||||
Assert.Equal (1, df.SelectedLength);
|
||||
Assert.Equal (0, df.CursorPosition);
|
||||
// Without selection
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft)));
|
||||
Assert.Equal (-1, df.SelectedStart);
|
||||
Assert.Equal (0, df.SelectedLength);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
df.CursorPosition = 10;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
|
||||
Assert.Equal (10, df.SelectedStart);
|
||||
Assert.Equal (1, df.SelectedLength);
|
||||
Assert.Equal (11, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight)));
|
||||
Assert.Equal (-1, df.SelectedStart);
|
||||
Assert.Equal (0, df.SelectedLength);
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
|
||||
{
|
||||
var df = new DateField ();
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
df.CursorPosition = 0;
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
df.CursorPosition = 11;
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
df.IsShortFormat = true;
|
||||
df.CursorPosition = 0;
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
df.CursorPosition = 9;
|
||||
Assert.Equal (8, df.CursorPosition);
|
||||
}
|
||||
[Fact, TestDate]
|
||||
public void KeyBindings_Command ()
|
||||
{
|
||||
DateField df = new DateField (DateTime.Parse ("12/12/1971")) {
|
||||
ReadOnly = true
|
||||
};
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Delete)));
|
||||
Assert.Equal (" 12/12/1971", df.Text);
|
||||
df.ReadOnly = false;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D | KeyCode.CtrlMask)));
|
||||
Assert.Equal (" 02/12/1971", df.Text);
|
||||
df.CursorPosition = 4;
|
||||
df.ReadOnly = true;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Delete)));
|
||||
Assert.Equal (" 02/12/1971", df.Text);
|
||||
df.ReadOnly = false;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Backspace)));
|
||||
Assert.Equal (" 02/02/1971", df.Text);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Home)));
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.End)));
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.A | KeyCode.CtrlMask)));
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.E | KeyCode.CtrlMask)));
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft)));
|
||||
Assert.Equal (9, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight)));
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
// Non-numerics are ignored
|
||||
Assert.False (df.NewKeyDownEvent (new (KeyCode.A)));
|
||||
df.ReadOnly = true;
|
||||
df.CursorPosition = 1;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D1)));
|
||||
Assert.Equal (" 02/02/1971", df.Text);
|
||||
df.ReadOnly = false;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D1)));
|
||||
Assert.Equal (" 12/02/1971", df.Text);
|
||||
Assert.Equal (2, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D | KeyCode.AltMask)));
|
||||
Assert.Equal (" 10/02/1971", df.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format_After_Selection ()
|
||||
{
|
||||
var df = new DateField ();
|
||||
// Start selection
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask)));
|
||||
Assert.Equal (1, df.SelectedStart);
|
||||
Assert.Equal (1, df.SelectedLength);
|
||||
Assert.Equal (0, df.CursorPosition);
|
||||
// Without selection
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft)));
|
||||
Assert.Equal (-1, df.SelectedStart);
|
||||
Assert.Equal (0, df.SelectedLength);
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
df.CursorPosition = 10;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
|
||||
Assert.Equal (10, df.SelectedStart);
|
||||
Assert.Equal (1, df.SelectedLength);
|
||||
Assert.Equal (11, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight)));
|
||||
Assert.Equal (-1, df.SelectedStart);
|
||||
Assert.Equal (0, df.SelectedLength);
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void KeyBindings_Command ()
|
||||
{
|
||||
CultureInfo cultureBackup = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
DateField df = new DateField (DateTime.Parse ("12/12/1971"));
|
||||
df.ReadOnly = true;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Delete)));
|
||||
Assert.Equal (" 12/12/1971", df.Text);
|
||||
df.ReadOnly = false;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D | KeyCode.CtrlMask)));
|
||||
Assert.Equal (" 02/12/1971", df.Text);
|
||||
df.CursorPosition = 4;
|
||||
df.ReadOnly = true;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Delete)));
|
||||
Assert.Equal (" 02/12/1971", df.Text);
|
||||
df.ReadOnly = false;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Backspace)));
|
||||
Assert.Equal (" 02/02/1971", df.Text);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.Home)));
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.End)));
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.A | KeyCode.CtrlMask)));
|
||||
Assert.Equal (1, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.E | KeyCode.CtrlMask)));
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorLeft)));
|
||||
Assert.Equal (9, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight)));
|
||||
Assert.Equal (10, df.CursorPosition);
|
||||
// Non-numerics are ignored
|
||||
Assert.False (df.NewKeyDownEvent (new (KeyCode.A)));
|
||||
df.ReadOnly = true;
|
||||
df.CursorPosition = 1;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D1)));
|
||||
Assert.Equal (" 02/02/1971", df.Text);
|
||||
df.ReadOnly = false;
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D1)));
|
||||
Assert.Equal (" 12/02/1971", df.Text);
|
||||
Assert.Equal (2, df.CursorPosition);
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D | KeyCode.AltMask)));
|
||||
Assert.Equal (" 10/02/1971", df.Text);
|
||||
CultureInfo.CurrentCulture = cultureBackup;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Typing_With_Selection_Normalize_Format ()
|
||||
{
|
||||
CultureInfo cultureBackup = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
DateField df = new DateField (DateTime.Parse ("12/12/1971"));
|
||||
[Fact, TestDate]
|
||||
public void Typing_With_Selection_Normalize_Format ()
|
||||
{
|
||||
DateField df = new DateField (DateTime.Parse ("12/12/1971")) {
|
||||
// Start selection at before the first separator /
|
||||
df.CursorPosition = 2;
|
||||
// Now select the separator /
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
|
||||
Assert.Equal (2, df.SelectedStart);
|
||||
Assert.Equal (1, df.SelectedLength);
|
||||
Assert.Equal (3, df.CursorPosition);
|
||||
// Type 3 over the separator
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D3)));
|
||||
// The format was normalized and replaced again with /
|
||||
Assert.Equal (" 12/12/1971", df.Text);
|
||||
Assert.Equal (4, df.CursorPosition);
|
||||
CultureInfo.CurrentCulture = cultureBackup;
|
||||
}
|
||||
CursorPosition = 2
|
||||
};
|
||||
// Now select the separator /
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
|
||||
Assert.Equal (2, df.SelectedStart);
|
||||
Assert.Equal (1, df.SelectedLength);
|
||||
Assert.Equal (3, df.CursorPosition);
|
||||
// Type 3 over the separator
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D3)));
|
||||
// The format was normalized and replaced again with /
|
||||
Assert.Equal (" 12/12/1971", df.Text);
|
||||
Assert.Equal (4, df.CursorPosition);
|
||||
}
|
||||
|
||||
[Fact, AutoInitShutdown]
|
||||
public void Copy_Paste ()
|
||||
{
|
||||
CultureInfo cultureBackup = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
DateField df1 = new DateField (DateTime.Parse ("12/12/1971"));
|
||||
DateField df2 = new DateField (DateTime.Parse ("12/31/2023"));
|
||||
// Select all text
|
||||
Assert.True (df2.NewKeyDownEvent (new (KeyCode.End | KeyCode.ShiftMask)));
|
||||
Assert.Equal (1, df2.SelectedStart);
|
||||
Assert.Equal (10, df2.SelectedLength);
|
||||
Assert.Equal (11, df2.CursorPosition);
|
||||
// Copy from df2
|
||||
Assert.True (df2.NewKeyDownEvent (new (KeyCode.C | KeyCode.CtrlMask)));
|
||||
// Paste into df1
|
||||
Assert.True (df1.NewKeyDownEvent (new (KeyCode.V | KeyCode.CtrlMask)));
|
||||
Assert.Equal (" 12/31/2023", df1.Text);
|
||||
Assert.Equal (11, df1.CursorPosition);
|
||||
CultureInfo.CurrentCulture = cultureBackup;
|
||||
}
|
||||
[Fact, TestDate, AutoInitShutdown]
|
||||
public void Copy_Paste ()
|
||||
{
|
||||
DateField df1 = new DateField (DateTime.Parse ("12/12/1971"));
|
||||
DateField df2 = new DateField (DateTime.Parse ("12/31/2023"));
|
||||
// Select all text
|
||||
Assert.True (df2.NewKeyDownEvent (new (KeyCode.End | KeyCode.ShiftMask)));
|
||||
Assert.Equal (1, df2.SelectedStart);
|
||||
Assert.Equal (10, df2.SelectedLength);
|
||||
Assert.Equal (11, df2.CursorPosition);
|
||||
// Copy from df2
|
||||
Assert.True (df2.NewKeyDownEvent (new (KeyCode.C | KeyCode.CtrlMask)));
|
||||
// Paste into df1
|
||||
Assert.True (df1.NewKeyDownEvent (new (KeyCode.V | KeyCode.CtrlMask)));
|
||||
Assert.Equal (" 12/31/2023", df1.Text);
|
||||
Assert.Equal (11, df1.CursorPosition);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Date_Start_From_01_01_0001_And_End_At_12_31_9999 ()
|
||||
{
|
||||
CultureInfo cultureBackup = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
DateField df = new DateField (DateTime.Parse ("01/01/0001"));
|
||||
Assert.Equal (" 01/01/0001", df.Text);
|
||||
df.Date = DateTime.Parse ("12/31/9999");
|
||||
Assert.Equal (" 12/31/9999", df.Text);
|
||||
CultureInfo.CurrentCulture = cultureBackup;
|
||||
}
|
||||
[Fact, TestDate]
|
||||
public void Date_Start_From_01_01_0001_And_End_At_12_31_9999 ()
|
||||
{
|
||||
DateField df = new DateField (DateTime.Parse ("01/01/0001"));
|
||||
Assert.Equal (" 01/01/0001", df.Text);
|
||||
df.Date = DateTime.Parse ("12/31/9999");
|
||||
Assert.Equal (" 12/31/9999", df.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Using_Pt_Culture ()
|
||||
{
|
||||
CultureInfo cultureBackup = CultureInfo.CurrentCulture;
|
||||
CultureInfo.CurrentCulture = new CultureInfo ("pt-PT");
|
||||
DateField df = new DateField (DateTime.Parse ("12/12/1971")) {
|
||||
// Move to the first 2
|
||||
CursorPosition = 2
|
||||
};
|
||||
// Type 3 over the separator
|
||||
Assert.True (df.NewKeyDownEvent (new (KeyCode.D3)));
|
||||
// If InvariantCulture was used this will fail but not with PT culture
|
||||
Assert.Equal (" 13/12/1971", df.Text);
|
||||
Assert.Equal ("13/12/1971", df.Date.ToString (CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern));
|
||||
Assert.Equal (4, df.CursorPosition);
|
||||
CultureInfo.CurrentCulture = cultureBackup;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1634,4 +1634,29 @@ Les Miśerables", output);
|
||||
_textField.Paste ();
|
||||
Assert.Equal ("TextField with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!", _textField.Text);
|
||||
}
|
||||
|
||||
[Fact, TextFieldTestsAutoInitShutdown]
|
||||
public void Copy_Paste_Text_Changing_Updates_Cursor_Position ()
|
||||
{
|
||||
_textField.TextChanging += _textField_TextChanging;
|
||||
|
||||
void _textField_TextChanging (object sender, TextChangingEventArgs e)
|
||||
{
|
||||
if (e.NewText.GetRuneCount () > 11) {
|
||||
e.NewText = e.NewText [..11];
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal (32, _textField.CursorPosition);
|
||||
_textField.SelectAll ();
|
||||
_textField.Cut ();
|
||||
Assert.Equal ("TAB to jump between text fields.", Application.Driver.Clipboard.GetClipboardData ());
|
||||
Assert.Equal (string.Empty, _textField.Text);
|
||||
Assert.Equal (0, _textField.CursorPosition);
|
||||
_textField.Paste ();
|
||||
Assert.Equal ("TAB to jump", _textField.Text);
|
||||
Assert.Equal (11, _textField.CursorPosition);
|
||||
|
||||
_textField.TextChanging -= _textField_TextChanging;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user