feature: Add a way to invoke a command specifying optional values by name (#1123)

* Add NamedArgumentTypeAttribute

* Add NamedArgumentTypeReader

* Fix superflous empty line.

* Fix logic for quoted arguments

* Throw an exception with a tailored message.

* Add a catch to wrap parsing/input errors

* Trim potential excess whitespace

* Fix an off-by-one

* Support to read an IEnumerable property

* Add a doc

* Add assertion for the collection test
This commit is contained in:
Joe4evr
2018-11-07 00:36:44 +01:00
committed by Christopher F
parent 8ef5f8120f
commit 419c0a5b53
7 changed files with 369 additions and 9 deletions

View File

@@ -3,6 +3,7 @@
<OutputType>Exe</OutputType>
<RootNamespace>Discord</RootNamespace>
<TargetFramework>netcoreapp1.1</TargetFramework>
<DebugType>portable</DebugType>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81</PackageTargetFallback>
</PropertyGroup>
<ItemGroup>
@@ -23,8 +24,8 @@
<PackageReference Include="Akavache" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.runner.reporters" Version="2.3.1" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="xunit.runner.reporters" Version="2.4.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Discord.Commands;
using Xunit;
namespace Discord
{
public sealed class TypeReaderTests
{
[Fact]
public async Task TestNamedArgumentReader()
{
var commands = new CommandService();
var module = await commands.AddModuleAsync<TestModule>(null);
Assert.NotNull(module);
Assert.NotEmpty(module.Commands);
var cmd = module.Commands[0];
Assert.NotNull(cmd);
Assert.NotEmpty(cmd.Parameters);
var param = cmd.Parameters[0];
Assert.NotNull(param);
Assert.True(param.IsRemainder);
var result = await param.ParseAsync(null, "bar: hello foo: 42");
Assert.True(result.IsSuccess);
var m = result.BestMatch as ArgumentType;
Assert.NotNull(m);
Assert.Equal(expected: 42, actual: m.Foo);
Assert.Equal(expected: "hello", actual: m.Bar);
}
[Fact]
public async Task TestQuotedArgumentValue()
{
var commands = new CommandService();
var module = await commands.AddModuleAsync<TestModule>(null);
Assert.NotNull(module);
Assert.NotEmpty(module.Commands);
var cmd = module.Commands[0];
Assert.NotNull(cmd);
Assert.NotEmpty(cmd.Parameters);
var param = cmd.Parameters[0];
Assert.NotNull(param);
Assert.True(param.IsRemainder);
var result = await param.ParseAsync(null, "foo: 42 bar: 《hello》");
Assert.True(result.IsSuccess);
var m = result.BestMatch as ArgumentType;
Assert.NotNull(m);
Assert.Equal(expected: 42, actual: m.Foo);
Assert.Equal(expected: "hello", actual: m.Bar);
}
[Fact]
public async Task TestNonPatternInput()
{
var commands = new CommandService();
var module = await commands.AddModuleAsync<TestModule>(null);
Assert.NotNull(module);
Assert.NotEmpty(module.Commands);
var cmd = module.Commands[0];
Assert.NotNull(cmd);
Assert.NotEmpty(cmd.Parameters);
var param = cmd.Parameters[0];
Assert.NotNull(param);
Assert.True(param.IsRemainder);
var result = await param.ParseAsync(null, "foobar");
Assert.False(result.IsSuccess);
Assert.Equal(expected: CommandError.Exception, actual: result.Error);
}
[Fact]
public async Task TestMultiple()
{
var commands = new CommandService();
var module = await commands.AddModuleAsync<TestModule>(null);
Assert.NotNull(module);
Assert.NotEmpty(module.Commands);
var cmd = module.Commands[0];
Assert.NotNull(cmd);
Assert.NotEmpty(cmd.Parameters);
var param = cmd.Parameters[0];
Assert.NotNull(param);
Assert.True(param.IsRemainder);
var result = await param.ParseAsync(null, "manyints: \"1, 2, 3, 4, 5, 6, 7\"");
Assert.True(result.IsSuccess);
var m = result.BestMatch as ArgumentType;
Assert.NotNull(m);
Assert.Equal(expected: new int[] { 1, 2, 3, 4, 5, 6, 7 }, actual: m.ManyInts);
}
}
[NamedArgumentType]
public sealed class ArgumentType
{
public int Foo { get; set; }
[OverrideTypeReader(typeof(CustomTypeReader))]
public string Bar { get; set; }
public IEnumerable<int> ManyInts { get; set; }
}
public sealed class CustomTypeReader : TypeReader
{
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
=> Task.FromResult(TypeReaderResult.FromSuccess(input));
}
public sealed class TestModule : ModuleBase
{
[Command("test")]
public Task TestCommand(ArgumentType arg) => Task.Delay(0);
}
}