Files
Terminal.Gui/Tests/UnitTests/Text/AutocompleteTests.cs
Tig b0f32811eb Fixes #3930 - Splits tests to Tests/UnitTests, Tests/IntegrationTests, Tests/StressTests (#3954)
* Tons of API doc updates

* Removed stale test

* Removed stale tests

* Fixed Skipped Shadow test 1

* Fixed Skipped Shadow test 2

* Fixed Skipped Shadow test 3

* Removed stale test

* Removed stale test2

* Explicit unregister of event handler on Application.Driver!.ClearedContents

* Added Toplevels to dict

* code cleanup

* spelling error

* Removed stale test3

* Removed stale test4

* Removed stale test5

* added script

* tweaked script

* tweaked script

* Created StressTests project; moved some tests

* Created IntegrationTests project; moved some tests

* New yml

* made old yml just unit tests

* Tweaked Button_IsDefault_Raises_Accepted_Correctly

* tweaked script

* cleaned up ymls

* tweakled up ymls

* stress tests...

* stress tests on ubuntu only

* Fixed WindowsDriver in InvokeLeakTest

* Fixed WindowsDriver in InvokeLeakTest2

* Added Directory.Packages.props.
Added Directory.Build.props

* Shortened StressTest time

* Removed dupe file.

* DemoFiles

* Moved all tests to ./Tests dir.

* Fixed release build issue

* Fixed .sln file

* Fixed .sl* files

* Fixing ymls

* Fixing interation tests

* Create link to the file TestHelpers.

* Created Tests/UnitTestsParallelizable.
Moved all obviously parallelizable tests.
Updated yml.

* fixing logs

* fixing logs2

* fixing logs3

* don't require stress to pass for PRs

* Fix a failure?

* tweaked script

* Coudl this be it?

* Moved tons of tests to parallelizable

* Fixed some stuff

* Script to find duplicate tests

* Testing workflows

* Updated to v4

* Fix RelativeBasePath issue

* Replace powershell to pwsh

* Add ignore projects.

* Removed dupe unit tests

* Code cleanup of tests

* Cleaned up test warnings

* yml tweak

* Moved setter

* tweak ymls

* just randomly throwing spaghetti at a wall

* Enable runing 5 test runners in par

* Turned off DEBUG_DISPOSABLE for par tests

* RunningUnitTests=true

* code cleanup (forcing more Action runs)

* DISABLE_DEBUG_IDISPOSABLE

* Added View.DebugIDisposable. False by default.

* Remobed bogus tareet

* Remobed bogus tareet2

* fixed warning

* added api doc

* fixed warning

* fixed warning

* fixed warning2

* fixed warning3

* fixed warning4

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-03-05 23:44:27 -07:00

304 lines
13 KiB
C#

using System.Text.RegularExpressions;
using UnitTests;
using UnitTests;
using Xunit.Abstractions;
namespace Terminal.Gui.TextTests;
public class AutocompleteTests (ITestOutputHelper output)
{
[Fact]
[AutoInitShutdown]
public void CursorLeft_CursorRight_Mouse_Button_Pressed_Does_Not_Show_Popup ()
{
var tv = new TextView { Width = 50, Height = 5, Text = "This a long line and against TextView." };
var g = (SingleWordSuggestionGenerator)tv.Autocomplete.SuggestionGenerator;
g.AllSuggestions = Regex.Matches (tv.Text, "\\w+")
.Select (s => s.Value)
.Distinct ()
.ToList ();
Toplevel top = new ();
top.Add (tv);
RunState rs = Application.Begin (top);
for (var i = 0; i < 7; i++)
{
Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
top.SetNeedsDraw();
Application.RunIteration (ref rs);
if (i < 4 || i > 5)
{
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This a long line and against TextView.",
output
);
}
else
{
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This a long line and against TextView.
and
against ",
output
);
}
}
Assert.True (
tv.NewMouseEvent (
new() { Position = new (6, 0), Flags = MouseFlags.Button1Pressed }
)
);
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This a long line and against TextView.
and
against ",
output
);
Assert.True (tv.NewKeyDownEvent (Key.G));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This ag long line and against TextView.
against ",
output
);
Assert.True (tv.NewKeyDownEvent (Key.CursorLeft));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This ag long line and against TextView.
against ",
output
);
Assert.True (tv.NewKeyDownEvent (Key.CursorLeft));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This ag long line and against TextView.
against ",
output
);
Assert.True (tv.NewKeyDownEvent (Key.CursorLeft));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This ag long line and against TextView.",
output
);
for (var i = 0; i < 3; i++)
{
Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This ag long line and against TextView.
against ",
output
);
}
Assert.True (tv.NewKeyDownEvent (Key.Backspace));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This a long line and against TextView.
and
against ",
output
);
Assert.True (tv.NewKeyDownEvent (Key.N));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This an long line and against TextView.
and ",
output
);
Assert.True (tv.NewKeyDownEvent (Key.CursorRight));
top.SetNeedsDraw ();
Application.RunIteration (ref rs);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
This an long line and against TextView.",
output
);
top.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void KeyBindings_Command ()
{
var tv = new TextView { Width = 10, Height = 2, Text = " Fortunately super feature." };
Toplevel top = new ();
top.Add (tv);
Application.Begin (top);
Assert.Equal (Point.Empty, tv.CursorPosition);
Assert.NotNull (tv.Autocomplete);
var g = (SingleWordSuggestionGenerator)tv.Autocomplete.SuggestionGenerator;
Assert.Empty (g.AllSuggestions);
g.AllSuggestions = Regex.Matches (tv.Text, "\\w+")
.Select (s => s.Value)
.Distinct ()
.ToList ();
Assert.Equal (3, g.AllSuggestions.Count);
Assert.Equal ("Fortunately", g.AllSuggestions [0]);
Assert.Equal ("super", g.AllSuggestions [1]);
Assert.Equal ("feature", g.AllSuggestions [^1]);
Assert.Equal (0, tv.Autocomplete.SelectedIdx);
Assert.Empty (tv.Autocomplete.Suggestions);
Assert.True (tv.NewKeyDownEvent (Key.F.WithShift));
top.Draw ();
Assert.Equal ("F Fortunately super feature.", tv.Text);
Assert.Equal (new (1, 0), tv.CursorPosition);
Assert.Equal (2, tv.Autocomplete.Suggestions.Count);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [0].Replacement);
Assert.Equal ("feature", tv.Autocomplete.Suggestions [^1].Replacement);
Assert.Equal (0, tv.Autocomplete.SelectedIdx);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [tv.Autocomplete.SelectedIdx].Replacement);
Assert.True (tv.NewKeyDownEvent (Key.CursorDown));
top.Draw ();
Assert.Equal ("F Fortunately super feature.", tv.Text);
Assert.Equal (new (1, 0), tv.CursorPosition);
Assert.Equal (2, tv.Autocomplete.Suggestions.Count);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [0].Replacement);
Assert.Equal ("feature", tv.Autocomplete.Suggestions [^1].Replacement);
Assert.Equal (1, tv.Autocomplete.SelectedIdx);
Assert.Equal ("feature", tv.Autocomplete.Suggestions [tv.Autocomplete.SelectedIdx].Replacement);
Assert.True (tv.NewKeyDownEvent (Key.CursorDown));
top.Draw ();
Assert.Equal ("F Fortunately super feature.", tv.Text);
Assert.Equal (new (1, 0), tv.CursorPosition);
Assert.Equal (2, tv.Autocomplete.Suggestions.Count);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [0].Replacement);
Assert.Equal ("feature", tv.Autocomplete.Suggestions [^1].Replacement);
Assert.Equal (0, tv.Autocomplete.SelectedIdx);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [tv.Autocomplete.SelectedIdx].Replacement);
Assert.True (tv.NewKeyDownEvent (Key.CursorUp));
top.Draw ();
Assert.Equal ("F Fortunately super feature.", tv.Text);
Assert.Equal (new (1, 0), tv.CursorPosition);
Assert.Equal (2, tv.Autocomplete.Suggestions.Count);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [0].Replacement);
Assert.Equal ("feature", tv.Autocomplete.Suggestions [^1].Replacement);
Assert.Equal (1, tv.Autocomplete.SelectedIdx);
Assert.Equal ("feature", tv.Autocomplete.Suggestions [tv.Autocomplete.SelectedIdx].Replacement);
Assert.True (tv.NewKeyDownEvent (Key.CursorUp));
top.Draw ();
Assert.Equal ("F Fortunately super feature.", tv.Text);
Assert.Equal (new (1, 0), tv.CursorPosition);
Assert.Equal (2, tv.Autocomplete.Suggestions.Count);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [0].Replacement);
Assert.Equal ("feature", tv.Autocomplete.Suggestions [^1].Replacement);
Assert.Equal (0, tv.Autocomplete.SelectedIdx);
Assert.Equal ("Fortunately", tv.Autocomplete.Suggestions [tv.Autocomplete.SelectedIdx].Replacement);
Assert.True (tv.Autocomplete.Visible);
top.Draw ();
Assert.True (tv.NewKeyDownEvent (new (tv.Autocomplete.CloseKey)));
Assert.Equal ("F Fortunately super feature.", tv.Text);
Assert.Equal (new (1, 0), tv.CursorPosition);
Assert.Empty (tv.Autocomplete.Suggestions);
Assert.Equal (3, g.AllSuggestions.Count);
Assert.False (tv.Autocomplete.Visible);
tv.PositionCursor ();
Assert.True (tv.NewKeyDownEvent (new (tv.Autocomplete.Reopen)));
Assert.Equal ("F Fortunately super feature.", tv.Text);
Assert.Equal (new (1, 0), tv.CursorPosition);
Assert.Equal (2, tv.Autocomplete.Suggestions.Count);
Assert.Equal (3, g.AllSuggestions.Count);
Assert.True (tv.NewKeyDownEvent (new (tv.Autocomplete.SelectionKey)));
tv.PositionCursor ();
Assert.Equal ("Fortunately Fortunately super feature.", tv.Text);
Assert.Equal (new (11, 0), tv.CursorPosition);
Assert.Empty (tv.Autocomplete.Suggestions);
Assert.Equal (3, g.AllSuggestions.Count);
Assert.False (tv.Autocomplete.Visible);
top.Dispose ();
}
[Fact]
public void Test_GenerateSuggestions_Simple ()
{
var ac = new TextViewAutocomplete ();
((SingleWordSuggestionGenerator)ac.SuggestionGenerator).AllSuggestions =
new() { "fish", "const", "Cobble" };
var tv = new TextView ();
tv.InsertText ("co");
ac.HostControl = tv;
ac.GenerateSuggestions (
new (
Cell.ToCellList (tv.Text),
2
)
);
Assert.Equal (2, ac.Suggestions.Count);
Assert.Equal ("const", ac.Suggestions [0].Title);
Assert.Equal ("Cobble", ac.Suggestions [1].Title);
}
[Fact]
public void TestSettingColorSchemeOnAutocomplete ()
{
var tv = new TextView ();
// to begin with we should be using the default menu color scheme
Assert.Same (Colors.ColorSchemes ["Menu"], tv.Autocomplete.ColorScheme);
// allocate a new custom scheme
tv.Autocomplete.ColorScheme = new()
{
Normal = new (Color.Black, Color.Blue), Focus = new (Color.Black, Color.Cyan)
};
// should be separate instance
Assert.NotSame (Colors.ColorSchemes ["Menu"], tv.Autocomplete.ColorScheme);
// with the values we set on it
Assert.Equal (new (Color.Black), tv.Autocomplete.ColorScheme.Normal.Foreground);
Assert.Equal (new (Color.Blue), tv.Autocomplete.ColorScheme.Normal.Background);
Assert.Equal (new (Color.Black), tv.Autocomplete.ColorScheme.Focus.Foreground);
Assert.Equal (new (Color.Cyan), tv.Autocomplete.ColorScheme.Focus.Background);
}
}