Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slang25 committed Sep 17, 2023
1 parent e65251b commit abb9b1a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
47 changes: 37 additions & 10 deletions test/Spectre.Console.Tests/Unit/Prompts/ListPromptStateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ namespace Spectre.Console.Tests.Unit;

public sealed class ListPromptStateTests
{
private ListPromptState<string> CreateListPromptState(int count, int pageSize, bool shouldWrap)
=> new(Enumerable.Repeat(new ListPromptItem<string>(string.Empty), count).ToList(), pageSize, shouldWrap, SelectionMode.Independent, true, false);
private ListPromptState<string> CreateListPromptState(int count, int pageSize, bool shouldWrap, bool searchEnabled)
=> new(Enumerable.Range(0, count).Select(i => new ListPromptItem<string>(i.ToString())).ToList(), pageSize, shouldWrap, SelectionMode.Independent, true, searchEnabled);

[Fact]
public void Should_Have_Start_Index_Zero()
{
// Given
var state = CreateListPromptState(100, 10, false);
var state = CreateListPromptState(100, 10, false, false);

// When
/* noop */
Expand All @@ -24,7 +24,7 @@ public void Should_Have_Start_Index_Zero()
public void Should_Increase_Index(bool wrap)
{
// Given
var state = CreateListPromptState(100, 10, wrap);
var state = CreateListPromptState(100, 10, wrap, false);
var index = state.Index;

// When
Expand All @@ -40,7 +40,7 @@ public void Should_Increase_Index(bool wrap)
public void Should_Go_To_End(bool wrap)
{
// Given
var state = CreateListPromptState(100, 10, wrap);
var state = CreateListPromptState(100, 10, wrap, false);

// When
state.Update(ConsoleKey.End.ToConsoleKeyInfo());
Expand All @@ -53,7 +53,7 @@ public void Should_Go_To_End(bool wrap)
public void Should_Clamp_Index_If_No_Wrap()
{
// Given
var state = CreateListPromptState(100, 10, false);
var state = CreateListPromptState(100, 10, false, false);
state.Update(ConsoleKey.End.ToConsoleKeyInfo());

// When
Expand All @@ -67,7 +67,7 @@ public void Should_Clamp_Index_If_No_Wrap()
public void Should_Wrap_Index_If_Wrap()
{
// Given
var state = CreateListPromptState(100, 10, true);
var state = CreateListPromptState(100, 10, true, false);
state.Update(ConsoleKey.End.ToConsoleKeyInfo());

// When
Expand All @@ -81,7 +81,7 @@ public void Should_Wrap_Index_If_Wrap()
public void Should_Wrap_Index_If_Wrap_And_Down()
{
// Given
var state = CreateListPromptState(100, 10, true);
var state = CreateListPromptState(100, 10, true, false);

// When
state.Update(ConsoleKey.UpArrow.ToConsoleKeyInfo());
Expand All @@ -94,7 +94,7 @@ public void Should_Wrap_Index_If_Wrap_And_Down()
public void Should_Wrap_Index_If_Wrap_And_Page_Up()
{
// Given
var state = CreateListPromptState(10, 100, true);
var state = CreateListPromptState(10, 100, true, false);

// When
state.Update(ConsoleKey.PageUp.ToConsoleKeyInfo());
Expand All @@ -107,7 +107,7 @@ public void Should_Wrap_Index_If_Wrap_And_Page_Up()
public void Should_Wrap_Index_If_Wrap_And_Offset_And_Page_Down()
{
// Given
var state = CreateListPromptState(10, 100, true);
var state = CreateListPromptState(10, 100, true, false);
state.Update(ConsoleKey.End.ToConsoleKeyInfo());
state.Update(ConsoleKey.UpArrow.ToConsoleKeyInfo());

Expand All @@ -117,4 +117,31 @@ public void Should_Wrap_Index_If_Wrap_And_Offset_And_Page_Down()
// Then
state.Index.ShouldBe(8);
}

[Fact]
public void Should_Jump_To_First_Matching_Item_When_Searching()
{
// Given
var state = CreateListPromptState(10, 100, true, true);

// When
state.Update(ConsoleKey.D3.ToConsoleKeyInfo());

// Then
state.Index.ShouldBe(3);
}

[Fact]
public void Should_Jump_Back_To_First_Item_When_Clearing_Search_Term()
{
// Given
var state = CreateListPromptState(10, 100, true, true);

// When
state.Update(ConsoleKey.D3.ToConsoleKeyInfo());
state.Update(ConsoleKey.Backspace.ToConsoleKeyInfo());

// Then
state.Index.ShouldBe(0);
}
}
44 changes: 44 additions & 0 deletions test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,48 @@ public void Should_Not_Throw_When_Selecting_An_Item_With_Escaped_Markup()
// Then
console.Output.ShouldContain(@"[red]This text will never be red[/]");
}

[Fact]
public void Should_Select_The_First_Leaf_Item()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Enter);

// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.Mode(SelectionMode.Leaf)
.AddChoiceGroup("Group one", "A", "B")
.AddChoiceGroup("Group two", "C", "D");
var selection = prompt.Show(console);

// Then
selection.ShouldBe("A");
}

[Fact]
public void Should_Select_The_Last_Leaf_Item_When_Wrapping_Around()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.UpArrow);
console.Input.PushKey(ConsoleKey.Enter);

// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.Mode(SelectionMode.Leaf)
.WrapAround()
.AddChoiceGroup("Group one", "A", "B")
.AddChoiceGroup("Group two", "C", "D");
var selection = prompt.Show(console);

// Then
selection.ShouldBe("D");
}

Check failure on line 65 in test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Check failure on line 65 in test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs

View workflow job for this annotation

GitHub Actions / Build (linux)


}

Check failure on line 67 in test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

Check failure on line 67 in test/Spectre.Console.Tests/Unit/Prompts/SelectionPromptTests.cs

View workflow job for this annotation

GitHub Actions / Build (linux)

0 comments on commit abb9b1a

Please sign in to comment.