Add global usings (#668)

* Use global usings

* Fix namespace declarations for test projects
This commit is contained in:
Patrik Svensson
2021-12-23 16:50:31 +01:00
committed by GitHub
parent eb6a9d8d04
commit 52c1d9122b
514 changed files with 10659 additions and 12441 deletions

View File

@ -1,156 +1,149 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Shouldly;
using Xunit;
namespace Spectre.Console.Tests.Unit;
namespace Spectre.Console.Tests.Unit
public sealed class MultiSelectionPromptTests
{
public sealed class MultiSelectionPromptTests
private class CustomItem
{
private class CustomItem
public int X { get; set; }
public int Y { get; set; }
public class Comparer : IEqualityComparer<CustomItem>
{
public int X { get; set; }
public int Y { get; set; }
public class Comparer : IEqualityComparer<CustomItem>
public bool Equals(CustomItem x, CustomItem y)
{
public bool Equals(CustomItem x, CustomItem y)
{
return x.X == y.X && x.Y == y.Y;
}
return x.X == y.X && x.Y == y.Y;
}
public int GetHashCode([DisallowNull] CustomItem obj)
{
throw new NotImplementedException();
}
public int GetHashCode([DisallowNull] CustomItem obj)
{
throw new NotSupportedException();
}
}
[Fact]
public void Should_Not_Mark_Item_As_Selected_By_Default()
{
// Given
var prompt = new MultiSelectionPrompt<int>();
// When
var choice = prompt.AddChoice(32);
// Then
choice.IsSelected.ShouldBeFalse();
}
[Fact]
public void Should_Mark_Item_As_Selected()
{
// Given
var prompt = new MultiSelectionPrompt<int>();
var choice = prompt.AddChoice(32);
// When
prompt.Select(32);
// Then
choice.IsSelected.ShouldBeTrue();
}
[Fact]
public void Should_Mark_Custom_Item_As_Selected_If_The_Same_Reference_Is_Used()
{
// Given
var prompt = new MultiSelectionPrompt<CustomItem>();
var item = new CustomItem { X = 18, Y = 32 };
var choice = prompt.AddChoice(item);
// When
prompt.Select(item);
// Then
choice.IsSelected.ShouldBeTrue();
}
[Fact]
public void Should_Mark_Custom_Item_As_Selected_If_A_Comparer_Is_Provided()
{
// Given
var prompt = new MultiSelectionPrompt<CustomItem>(new CustomItem.Comparer());
var choice = prompt.AddChoice(new CustomItem { X = 18, Y = 32 });
// When
prompt.Select(new CustomItem { X = 18, Y = 32 });
// Then
choice.IsSelected.ShouldBeTrue();
}
[Fact]
public void Should_Get_The_Direct_Parent()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root").AddChild("level-1").AddChild("level-2").AddChild("item");
// When
var actual = prompt.GetParent("item");
// Then
actual.ShouldBe("level-2");
}
[Fact]
public void Should_Get_The_List_Of_All_Parents()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root").AddChild("level-1").AddChild("level-2").AddChild("item");
// When
var actual = prompt.GetParents("item");
// Then
actual.ShouldBe(new[] { "root", "level-1", "level-2" });
}
[Fact]
public void Should_Get_An_Empty_List_Of_Parents_For_Root_Node()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root");
// When
var actual = prompt.GetParents("root");
// Then
actual.ShouldBeEmpty();
}
[Fact]
public void Should_Get_Null_As_Direct_Parent_Of_Root_Node()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root");
// When
var actual = prompt.GetParent("root");
// Then
actual.ShouldBeNull();
}
[Fact]
public void Should_Throw_When_Getting_Parents_Of_Non_Existing_Node()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root").AddChild("level-1").AddChild("level-2").AddChild("item");
// When
Action action = () => prompt.GetParents("non-existing");
// Then
action.ShouldThrow<ArgumentOutOfRangeException>();
}
}
}
[Fact]
public void Should_Not_Mark_Item_As_Selected_By_Default()
{
// Given
var prompt = new MultiSelectionPrompt<int>();
// When
var choice = prompt.AddChoice(32);
// Then
choice.IsSelected.ShouldBeFalse();
}
[Fact]
public void Should_Mark_Item_As_Selected()
{
// Given
var prompt = new MultiSelectionPrompt<int>();
var choice = prompt.AddChoice(32);
// When
prompt.Select(32);
// Then
choice.IsSelected.ShouldBeTrue();
}
[Fact]
public void Should_Mark_Custom_Item_As_Selected_If_The_Same_Reference_Is_Used()
{
// Given
var prompt = new MultiSelectionPrompt<CustomItem>();
var item = new CustomItem { X = 18, Y = 32 };
var choice = prompt.AddChoice(item);
// When
prompt.Select(item);
// Then
choice.IsSelected.ShouldBeTrue();
}
[Fact]
public void Should_Mark_Custom_Item_As_Selected_If_A_Comparer_Is_Provided()
{
// Given
var prompt = new MultiSelectionPrompt<CustomItem>(new CustomItem.Comparer());
var choice = prompt.AddChoice(new CustomItem { X = 18, Y = 32 });
// When
prompt.Select(new CustomItem { X = 18, Y = 32 });
// Then
choice.IsSelected.ShouldBeTrue();
}
[Fact]
public void Should_Get_The_Direct_Parent()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root").AddChild("level-1").AddChild("level-2").AddChild("item");
// When
var actual = prompt.GetParent("item");
// Then
actual.ShouldBe("level-2");
}
[Fact]
public void Should_Get_The_List_Of_All_Parents()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root").AddChild("level-1").AddChild("level-2").AddChild("item");
// When
var actual = prompt.GetParents("item");
// Then
actual.ShouldBe(new[] { "root", "level-1", "level-2" });
}
[Fact]
public void Should_Get_An_Empty_List_Of_Parents_For_Root_Node()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root");
// When
var actual = prompt.GetParents("root");
// Then
actual.ShouldBeEmpty();
}
[Fact]
public void Should_Get_Null_As_Direct_Parent_Of_Root_Node()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root");
// When
var actual = prompt.GetParent("root");
// Then
actual.ShouldBeNull();
}
[Fact]
public void Should_Throw_When_Getting_Parents_Of_Non_Existing_Node()
{
// Given
var prompt = new MultiSelectionPrompt<string>();
prompt.AddChoice("root").AddChild("level-1").AddChild("level-2").AddChild("item");
// When
Action action = () => prompt.GetParents("non-existing");
// Then
action.ShouldThrow<ArgumentOutOfRangeException>();
}
}

View File

@ -1,30 +1,24 @@
using System;
using Shouldly;
using Spectre.Console.Testing;
using Xunit;
namespace Spectre.Console.Tests.Unit;
namespace Spectre.Console.Tests.Unit
public sealed class SelectionPromptTests
{
public sealed class SelectionPromptTests
[Fact]
[GitHubIssue(608)]
public void Should_Not_Throw_When_Selecting_An_Item_With_Escaped_Markup()
{
[Fact]
[GitHubIssue(608)]
public void Should_Not_Throw_When_Selecting_An_Item_With_Escaped_Markup()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Enter);
var input = "[red]This text will never be red[/]".EscapeMarkup();
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Enter);
var input = "[red]This text will never be red[/]".EscapeMarkup();
// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.AddChoices(input);
prompt.Show(console);
// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.AddChoices(input);
prompt.Show(console);
// Then
console.Output.ShouldContain(@"[red]This text will never be red[/]");
}
// Then
console.Output.ShouldContain(@"[red]This text will never be red[/]");
}
}
}

View File

@ -1,253 +1,244 @@
using System;
using System.Threading.Tasks;
using Shouldly;
using Spectre.Console.Testing;
using Spectre.Verify.Extensions;
using VerifyXunit;
using Xunit;
namespace Spectre.Console.Tests.Unit;
namespace Spectre.Console.Tests.Unit
[UsesVerify]
[ExpectationPath("Prompts/Text")]
public sealed class TextPromptTests
{
[UsesVerify]
[ExpectationPath("Prompts/Text")]
public sealed class TextPromptTests
[Fact]
public void Should_Return_Entered_Text()
{
[Fact]
public void Should_Return_Entered_Text()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Hello World");
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Hello World");
// When
var result = console.Prompt(new TextPrompt<string>("Enter text:"));
// When
var result = console.Prompt(new TextPrompt<string>("Enter text:"));
// Then
result.ShouldBe("Hello World");
}
[Fact]
[Expectation("ConversionError")]
public Task Should_Return_Validation_Error_If_Value_Cannot_Be_Converted()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("ninety-nine");
console.Input.PushTextWithEnter("99");
// When
console.Prompt(new TextPrompt<int>("Age?"));
// Then
return Verifier.Verify(console.Lines);
}
[Fact]
[Expectation("DefaultValue")]
public Task Should_Chose_Default_Value_If_Nothing_Is_Entered()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("InvalidChoice")]
public Task Should_Return_Error_If_An_Invalid_Choice_Is_Made()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Apple");
console.Input.PushTextWithEnter("Banana");
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AcceptChoice")]
public Task Should_Accept_Choice_In_List()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Orange");
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AutoComplete_Empty")]
public Task Should_Auto_Complete_To_First_Choice_If_Pressing_Tab_On_Empty_String()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AutoComplete_BestMatch")]
public Task Should_Auto_Complete_To_Best_Match()
{
// Given
var console = new TestConsole();
console.Input.PushText("Band");
console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Bandana")
.AddChoice("Orange"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AutoComplete_NextChoice")]
public Task Should_Auto_Complete_To_Next_Choice_When_Pressing_Tab_On_A_Match()
{
// Given
var console = new TestConsole();
console.Input.PushText("Apple");
console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Apple")
.AddChoice("Banana")
.AddChoice("Orange"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("CustomValidation")]
public Task Should_Return_Error_If_Custom_Validation_Fails()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("22");
console.Input.PushTextWithEnter("102");
console.Input.PushTextWithEnter("ABC");
console.Input.PushTextWithEnter("99");
// When
console.Prompt(
new TextPrompt<int>("Guess number:")
.ValidationErrorMessage("Invalid input")
.Validate(age =>
{
if (age < 99)
{
return ValidationResult.Error("Too low");
}
else if (age > 99)
{
return ValidationResult.Error("Too high");
}
return ValidationResult.Success();
}));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("CustomConverter")]
public Task Should_Use_Custom_Converter()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Banana");
// When
var result = console.Prompt(
new TextPrompt<(int, string)>("Favorite fruit?")
.AddChoice((1, "Apple"))
.AddChoice((2, "Banana"))
.WithConverter(testData => testData.Item2));
// Then
result.Item1.ShouldBe(2);
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("SecretDefaultValue")]
public Task Should_Chose_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.Secret()
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("NoSuffix")]
[GitHubIssue(413)]
public Task Should_Not_Append_Questionmark_Or_Colon_If_No_Choices_Are_Set()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Orange");
// When
console.Prompt(
new TextPrompt<string>("Enter command$"));
// Then
return Verifier.Verify(console.Output);
}
// Then
result.ShouldBe("Hello World");
}
}
[Fact]
[Expectation("ConversionError")]
public Task Should_Return_Validation_Error_If_Value_Cannot_Be_Converted()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("ninety-nine");
console.Input.PushTextWithEnter("99");
// When
console.Prompt(new TextPrompt<int>("Age?"));
// Then
return Verifier.Verify(console.Lines);
}
[Fact]
[Expectation("DefaultValue")]
public Task Should_Chose_Default_Value_If_Nothing_Is_Entered()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("InvalidChoice")]
public Task Should_Return_Error_If_An_Invalid_Choice_Is_Made()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Apple");
console.Input.PushTextWithEnter("Banana");
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AcceptChoice")]
public Task Should_Accept_Choice_In_List()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Orange");
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AutoComplete_Empty")]
public Task Should_Auto_Complete_To_First_Choice_If_Pressing_Tab_On_Empty_String()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange")
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AutoComplete_BestMatch")]
public Task Should_Auto_Complete_To_Best_Match()
{
// Given
var console = new TestConsole();
console.Input.PushText("Band");
console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Bandana")
.AddChoice("Orange"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("AutoComplete_NextChoice")]
public Task Should_Auto_Complete_To_Next_Choice_When_Pressing_Tab_On_A_Match()
{
// Given
var console = new TestConsole();
console.Input.PushText("Apple");
console.Input.PushKey(ConsoleKey.Tab);
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Apple")
.AddChoice("Banana")
.AddChoice("Orange"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("CustomValidation")]
public Task Should_Return_Error_If_Custom_Validation_Fails()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("22");
console.Input.PushTextWithEnter("102");
console.Input.PushTextWithEnter("ABC");
console.Input.PushTextWithEnter("99");
// When
console.Prompt(
new TextPrompt<int>("Guess number:")
.ValidationErrorMessage("Invalid input")
.Validate(age =>
{
if (age < 99)
{
return ValidationResult.Error("Too low");
}
else if (age > 99)
{
return ValidationResult.Error("Too high");
}
return ValidationResult.Success();
}));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("CustomConverter")]
public Task Should_Use_Custom_Converter()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Banana");
// When
var result = console.Prompt(
new TextPrompt<(int, string)>("Favorite fruit?")
.AddChoice((1, "Apple"))
.AddChoice((2, "Banana"))
.WithConverter(testData => testData.Item2));
// Then
result.Item1.ShouldBe(2);
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("SecretDefaultValue")]
public Task Should_Chose_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret()
{
// Given
var console = new TestConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.Secret()
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("NoSuffix")]
[GitHubIssue(413)]
public Task Should_Not_Append_Questionmark_Or_Colon_If_No_Choices_Are_Set()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Orange");
// When
console.Prompt(
new TextPrompt<string>("Enter command$"));
// Then
return Verifier.Verify(console.Output);
}
}