Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Resolve #16 by adding defined check for EnumConverter #17

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions VCF.Core/Registry/CommandRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,25 @@ static void HandleAfterExecute(ICommandContext ctx, CommandMetadata command)
return CommandResult.InternalError;
}
}
// Default Converter
else
{
// default convertet
var builtinConverter = TypeDescriptor.GetConverter(param.ParameterType);
var defaultConverter = TypeDescriptor.GetConverter(param.ParameterType);
try
{
commandArgs[i + 1] = builtinConverter.ConvertFromInvariantString(arg);
var val = defaultConverter.ConvertFromInvariantString(arg);

// ensure enums are valid for #16
if (defaultConverter is EnumConverter)
{
if (!Enum.IsDefined(param.ParameterType, val))
{
ctx.Reply($"<color=red>[error]</color> Invalid value {val} for {param.ParameterType.Name}");
return CommandResult.UsageError;
}
}

commandArgs[i + 1] = val;
}
catch (Exception e)
{
Expand Down
8 changes: 8 additions & 0 deletions VCF.Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public void CanCallWithEnum()
Assert.That(CommandRegistry.Handle(AnyCtx, ".horse color Brown"), Is.EqualTo(CommandResult.Success));
Assert.That(CommandRegistry.Handle(AnyCtx, ".horse color Purple"), Is.EqualTo(CommandResult.UsageError));
}

[Test]
public void CanCallWithEnumValues()
{
Assert.That(CommandRegistry.Handle(AnyCtx, ".horse color 1"), Is.EqualTo(CommandResult.Success));
Assert.That(CommandRegistry.Handle(AnyCtx, ".horse color 2"), Is.EqualTo(CommandResult.Success));
Assert.That(CommandRegistry.Handle(AnyCtx, ".horse color 4"), Is.EqualTo(CommandResult.UsageError));
}

[Test]
public void CommandsCanBeSubstrings()
Expand Down