mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 08:50:25 +01:00
Added Attributes tests; balanced Application.Init/Shutdown
This commit is contained in:
@@ -1,28 +1,48 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Xunit;
|
||||
|
||||
namespace Terminal.Gui.Views {
|
||||
public class TextViewTests {
|
||||
private TextView _textView;
|
||||
private static TextView _textView;
|
||||
|
||||
public TextViewTests ()
|
||||
{
|
||||
Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
|
||||
// This class enables test functions annoated with the [InitShutdown] attribute
|
||||
// to have a function called before the test function is called and after.
|
||||
//
|
||||
// This is necessary because a) Application is a singleton and Init/Shutdown must be called
|
||||
// as a pair, and b) all unit test functions should be atomic.
|
||||
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public class InitShutdown : Xunit.Sdk.BeforeAfterTestAttribute {
|
||||
|
||||
// 1 2 3
|
||||
// 01234567890123456789012345678901=32 (Length)
|
||||
var txt = "TAB to jump between text fields.";
|
||||
var buff = new byte [txt.Length];
|
||||
for (int i = 0; i < txt.Length; i++) {
|
||||
buff [i] = (byte)txt [i];
|
||||
public override void Before (MethodInfo methodUnderTest)
|
||||
{
|
||||
if (_textView != null) {
|
||||
throw new InvalidOperationException ("After did not run.");
|
||||
}
|
||||
|
||||
Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
|
||||
|
||||
// 1 2 3
|
||||
// 01234567890123456789012345678901=32 (Length)
|
||||
var txt = "TAB to jump between text fields.";
|
||||
var buff = new byte [txt.Length];
|
||||
for (int i = 0; i < txt.Length; i++) {
|
||||
buff [i] = (byte)txt [i];
|
||||
}
|
||||
var ms = new System.IO.MemoryStream (buff).ToArray ();
|
||||
_textView = new TextView () { Width = 30, Height = 10 };
|
||||
_textView.Text = ms;
|
||||
}
|
||||
|
||||
public override void After (MethodInfo methodUnderTest)
|
||||
{
|
||||
_textView = null;
|
||||
Application.Shutdown ();
|
||||
}
|
||||
var ms = new System.IO.MemoryStream (buff).ToArray ();
|
||||
_textView = new TextView () { Width = 30, Height = 10 };
|
||||
_textView.Text = ms;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Changing_Selection_Or_CursorPosition_Update_SelectedLength_And_SelectedText ()
|
||||
{
|
||||
_textView.SelectionStartColumn = 2;
|
||||
@@ -38,7 +58,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("B to jump between ", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Selection_With_Value_Less_Than_Zero_Changes_To_Zero ()
|
||||
{
|
||||
_textView.SelectionStartColumn = -2;
|
||||
@@ -49,7 +69,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Selection_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (2, 0);
|
||||
@@ -61,7 +81,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("B to jump between text fields.", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Selection_With_Empty_Text ()
|
||||
{
|
||||
_textView = new TextView ();
|
||||
@@ -74,7 +94,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Selection_And_CursorPosition_With_Value_Greater_Than_Text_Length_Changes_Both_To_Text_Length ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (33, 2);
|
||||
@@ -88,7 +108,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void CursorPosition_With_Value_Less_Than_Zero_Changes_To_Zero ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (-1, -1);
|
||||
@@ -98,7 +118,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void CursorPosition_With_Value_Greater_Than_Text_Length_Changes_To_Text_Length ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (33, 1);
|
||||
@@ -108,7 +128,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordForward_With_No_Selection ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (0, 0);
|
||||
@@ -170,7 +190,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordBackward_With_No_Selection ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (_textView.Text.Length, 0);
|
||||
@@ -232,7 +252,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordForward_With_Selection ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (0, 0);
|
||||
@@ -296,7 +316,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordBackward_With_Selection ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (_textView.Text.Length, 0);
|
||||
@@ -360,7 +380,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordForward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (10, 0);
|
||||
@@ -408,7 +428,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordBackward_With_The_Same_Values_For_SelectedStart_And_CursorPosition_And_Not_Starting_At_Beginning_Of_The_Text ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (10, 0);
|
||||
@@ -448,7 +468,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordForward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
|
||||
{
|
||||
// 1 2 3 4 5
|
||||
@@ -545,7 +565,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordBackward_With_No_Selection_And_With_More_Than_Only_One_Whitespace_And_With_Only_One_Letter ()
|
||||
{
|
||||
// 1 2 3 4 5
|
||||
@@ -650,7 +670,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordBackward_Multiline_With_Selection ()
|
||||
{
|
||||
// 4 3 2 1
|
||||
@@ -764,7 +784,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void WordForward_Multiline_With_Selection ()
|
||||
{
|
||||
// 1 2 3 4
|
||||
@@ -877,7 +897,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Kill_To_End_Delete_Forwards_And_Copy_To_The_Clipboard ()
|
||||
{
|
||||
_textView.Text = "This is the first line.\nThis is the second line.";
|
||||
@@ -912,7 +932,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Kill_To_Start_Delete_Backwards_And_Copy_To_The_Clipboard ()
|
||||
{
|
||||
_textView.Text = "This is the first line.\nThis is the second line.";
|
||||
@@ -948,7 +968,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Kill_Delete_WordForward ()
|
||||
{
|
||||
_textView.Text = "This is the first line.";
|
||||
@@ -991,7 +1011,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Kill_Delete_WordBackward ()
|
||||
{
|
||||
_textView.Text = "This is the first line.";
|
||||
@@ -1035,7 +1055,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Kill_Delete_WordForward_Multiline ()
|
||||
{
|
||||
_textView.Text = "This is the first line.\nThis is the second line.";
|
||||
@@ -1114,7 +1134,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Kill_Delete_WordBackward_Multiline ()
|
||||
{
|
||||
_textView.Text = "This is the first line.\nThis is the second line.";
|
||||
@@ -1193,7 +1213,7 @@ namespace Terminal.Gui.Views {
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Copy_Or_Cut_Null_If_No_Selection ()
|
||||
{
|
||||
_textView.SelectionStartColumn = 0;
|
||||
@@ -1204,7 +1224,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Copy_Or_Cut_Not_Null_If_Has_Selection ()
|
||||
{
|
||||
_textView.SelectionStartColumn = 20;
|
||||
@@ -1216,7 +1236,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Copy_Or_Cut_And_Paste_With_Selection ()
|
||||
{
|
||||
_textView.SelectionStartColumn = 20;
|
||||
@@ -1234,7 +1254,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("TAB to jump between text fields.", _textView.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Copy_Or_Cut_And_Paste_With_No_Selection ()
|
||||
{
|
||||
_textView.SelectionStartColumn = 20;
|
||||
@@ -1262,7 +1282,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("TAB to jump between texttext fields.", _textView.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Cut_Not_Allowed_If_ReadOnly_Is_True ()
|
||||
{
|
||||
_textView.ReadOnly = true;
|
||||
@@ -1282,7 +1302,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Paste_Always_Clear_The_SelectedText ()
|
||||
{
|
||||
_textView.SelectionStartColumn = 20;
|
||||
@@ -1294,7 +1314,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("", _textView.SelectedText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void TextChanged_Event ()
|
||||
{
|
||||
_textView.TextChanged += () => {
|
||||
@@ -1308,7 +1328,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("changed", _textView.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Used_Is_True_By_Default ()
|
||||
{
|
||||
_textView.CursorPosition = new Point (10, 0);
|
||||
@@ -1323,7 +1343,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("TAB to jumusedp between text fields.", _textView.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Used_Is_False ()
|
||||
{
|
||||
_textView.Used = false;
|
||||
@@ -1339,7 +1359,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("TAB to jumusedtween text fields.", _textView.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Copy_Without_Selection ()
|
||||
{
|
||||
_textView.Text = "This is the first line.\nThis is the second line.\n";
|
||||
@@ -1357,7 +1377,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal (new Point (3, 3), _textView.CursorPosition);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void TabWidth_Setting_To_Zero_Changes_AllowsTab_To_False_If_True ()
|
||||
{
|
||||
Assert.Equal (4, _textView.TabWidth);
|
||||
@@ -1375,7 +1395,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("TAB to jump between text fields.", _textView.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void AllowsTab_Setting_To_True_Changes_TabWidth_To_Default_If_It_Is_Zero ()
|
||||
{
|
||||
_textView.TabWidth = 0;
|
||||
@@ -1390,7 +1410,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.True (_textView.Multiline);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void AllowsReturn_Setting_To_True_Changes_Multiline_To_True_If_It_Is_False ()
|
||||
{
|
||||
Assert.True (_textView.AllowsReturn);
|
||||
@@ -1411,7 +1431,7 @@ namespace Terminal.Gui.Views {
|
||||
"TAB to jump between text fields.", _textView.Text);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Multiline_Setting_Changes_AllowsReturn_And_AllowsTab_And_Height ()
|
||||
{
|
||||
Assert.True (_textView.Multiline);
|
||||
@@ -1438,7 +1458,7 @@ namespace Terminal.Gui.Views {
|
||||
Assert.Equal ("Dim.Absolute(10)", _textView.Height.ToString ());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Tab_Test_Follow_By_BackTab ()
|
||||
{
|
||||
Application.Top.Add (_textView);
|
||||
@@ -1473,7 +1493,7 @@ namespace Terminal.Gui.Views {
|
||||
Application.Run ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void BackTab_Test_Follow_By_Tab ()
|
||||
{
|
||||
Application.Top.Add (_textView);
|
||||
@@ -1515,7 +1535,7 @@ namespace Terminal.Gui.Views {
|
||||
Application.Run ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight ()
|
||||
{
|
||||
Application.Top.Add (_textView);
|
||||
@@ -1557,7 +1577,7 @@ namespace Terminal.Gui.Views {
|
||||
Application.Run ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Tab_Test_Follow_By_BackTab_With_Text ()
|
||||
{
|
||||
Application.Top.Add (_textView);
|
||||
@@ -1592,7 +1612,7 @@ namespace Terminal.Gui.Views {
|
||||
Application.Run ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Tab_Test_Follow_By_Home_And_Then_Follow_By_End_And_Then_Follow_By_BackTab_With_Text ()
|
||||
{
|
||||
Application.Top.Add (_textView);
|
||||
@@ -1649,7 +1669,7 @@ namespace Terminal.Gui.Views {
|
||||
Application.Run ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Fact][InitShutdown]
|
||||
public void Tab_Test_Follow_By_CursorLeft_And_Then_Follow_By_CursorRight_With_Text ()
|
||||
{
|
||||
Application.Top.Add (_textView);
|
||||
|
||||
Reference in New Issue
Block a user