Shamir/tests/console.tests/CommandTreeTests.cs

91 lines
2.9 KiB
C#
Raw Normal View History

2021-09-25 13:04:18 +00:00
using System;
using System.Collections.Immutable;
using System.Threading.Tasks;
using NUnit.Framework;
namespace Shamir.Console.Tests
{
public class CommandTreeTests
{
[SetUp]
public void Setup()
{
tree = new DefaultCommandTree(
"root",
"top level",
ImmutableArray.Create<ICommandTree>(
new DefaultCommandTree(
"bar",
"child command tree",
ImmutableArray<ICommandTree>.Empty,
ImmutableArray.Create<ICommand>(
new SimpleTextCommand("one", "bar/1 command"),
new SimpleTextCommand("two", "bar/2 command")
))),
ImmutableArray.Create<ICommand>(
new SimpleTextCommand("foo", "foo command")
));
}
ICommandTree tree;
[Test]
public void ConstructsHelpTextWithNoArgs()
{
var command = tree.FindCommand(Array.Empty<string>());
Assert.That(command, Is.InstanceOf<DefaultHelpTextCommand>());
var helpText = ((DefaultHelpTextCommand)command).GetHelpText();
Assert.That(helpText, Is.EqualTo(@"
Group
root : top level
Subgroups:
bar : child command tree
Commands:
foo : foo command
".TrimStart()));
}
[Test]
public void ConstructsHelpTextForSubTree()
{
var command = tree.FindCommand(new[] { "bar" });
Assert.That(command, Is.InstanceOf<DefaultHelpTextCommand>());
var helpText = ((DefaultHelpTextCommand)command).GetHelpText();
Assert.That(helpText, Is.EqualTo(@"
Group
root bar : child command tree
Commands:
one : bar/1 command
2021-09-25 13:22:16 +00:00
two : bar/2 command
".TrimStart()));
2021-09-25 13:04:18 +00:00
}
[TestCase((object)new string[] { "foo" }, ExpectedResult = "foo command")]
[TestCase((object)new string[] { "bar", "one" }, ExpectedResult = "bar/1 command")]
[TestCase((object)new string[] { "bar", "two" }, ExpectedResult = "bar/2 command")]
public string FindsCommand(string[] args)
{
2021-09-25 13:22:16 +00:00
var command = tree.FindCommand(ImmutableStack<ICommandTree>.Empty, args);
2021-09-25 13:04:18 +00:00
return command?.Description;
}
class SimpleTextCommand : ICommand
{
public SimpleTextCommand(string name, string description)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Description = description ?? throw new ArgumentNullException(nameof(description));
}
public string Name { get; }
public string Description { get; }
public void Initialize(ReadOnlySpan<string> args) { }
public ValueTask<int> ExecuteAsync(IServiceProvider serviceProvider) => ValueTask.FromResult(0);
2021-09-25 13:04:18 +00:00
}
}
2021-09-25 12:17:53 +00:00
}