From 41394c54ef33e2453f07405cfa86bbee1de52f20 Mon Sep 17 00:00:00 2001 From: deca Date: Thu, 27 Jul 2023 08:56:42 -0700 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A7=AA=20Reproduced=20in=20test=20for?= =?UTF-8?q?=20#16?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VCF.Tests/ParsingTests.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/VCF.Tests/ParsingTests.cs b/VCF.Tests/ParsingTests.cs index 196da6e..98e0894 100644 --- a/VCF.Tests/ParsingTests.cs +++ b/VCF.Tests/ParsingTests.cs @@ -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() From d715af8b8950c757174be7f37f0c93eb98ad5807 Mon Sep 17 00:00:00 2001 From: deca Date: Thu, 27 Jul 2023 09:27:24 -0700 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20Resolve=20#16=20by=20adding?= =?UTF-8?q?=20specific=20check=20for=20Enums?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VCF.Core/Registry/CommandRegistry.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/VCF.Core/Registry/CommandRegistry.cs b/VCF.Core/Registry/CommandRegistry.cs index 4d6960f..05961e8 100644 --- a/VCF.Core/Registry/CommandRegistry.cs +++ b/VCF.Core/Registry/CommandRegistry.cs @@ -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($"[error] Invalid value {val} for {param.ParameterType.Name}"); + return CommandResult.UsageError; + } + } + + commandArgs[i + 1] = val; } catch (Exception e) {