using System; using System.Collections.Immutable; using System.Threading.Tasks; using NUnit.Framework; using Shamir.Abstractions; // TODO: Restructure test project(s) namespace Shamir.Console.Tests { public class CommandTreeTests { [SetUp] public void Setup() { tree = new DefaultCommandTree( "root", "top level", ImmutableArray.Create( new DefaultCommandTree( "bar", "child command tree", ImmutableArray.Empty, ImmutableArray.Create( new SimpleTextCommand("one", "bar/1 command"), new SimpleTextCommand("two", "bar/2 command") ))), ImmutableArray.Create( new SimpleTextCommand("foo", "foo command") )); } ICommandTree tree; [Test] public void ConstructsHelpTextWithNoArgs() { var command = tree.FindCommand(Array.Empty()); Assert.That(command, Is.InstanceOf()); 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()); var helpText = ((DefaultHelpTextCommand)command).GetHelpText(); Assert.That(helpText, Is.EqualTo(@" Group root bar : child command tree Commands: one : bar/1 command two : bar/2 command ".TrimStart())); } [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) { var command = tree.FindCommand(ImmutableStack.Empty, args); 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 args) { } public ValueTask ExecuteAsync(IServiceProvider serviceProvider) => ValueTask.FromResult(0); } } }