Skip to content

Commit

Permalink
fix #19 boolean arguments are not managed properly
Browse files Browse the repository at this point in the history
  • Loading branch information
gcramarossa committed Dec 14, 2023
1 parent 71686a8 commit 1c95626
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/InterAppConnector/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,17 @@ where method.GetCustomAttribute<CustomInputStringAttribute>() != null
}
else
{
throw new ArgumentException();
}
if (parameterType == typeof(bool))
{
_arguments[selectedCommand].Arguments[findArgument.Name].Value = Convert.ChangeType(item.Value, parameterType);
_parameterObject[selectedCommand].GetType().GetProperty(findArgument.OriginalPropertyName)!.SetValue(_parameterObject[selectedCommand], Convert.ChangeType(item.Value, parameterType));
_arguments[selectedCommand].Arguments[findArgument.Name].IsSetByUser = true;
}
else
{
throw new ArgumentException();
}
}
}
catch (Exception exc)
{
Expand Down
1 change: 0 additions & 1 deletion src/InterAppConnector/CommandOutput.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using InterAppConnector.DataModels;
using InterAppConnector.Enumerations;
using InterAppConnector.Exceptions;
Expand Down
2 changes: 0 additions & 2 deletions src/InterAppConnector/CommandUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using InterAppConnector.DataModels;
using InterAppConnector.Enumerations;
using InterAppConnector.Interfaces;
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Text;

Expand Down
4 changes: 0 additions & 4 deletions tests/InterAppConnector.Test.Library/CommandManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,6 @@ public void SetArgument_WithAnotherValidatorCustomInputStringAndValueNotInRange_

[TestCase("abcd123a")]
[TestCase("ab")]
//
[TestCase("")]
[TestCase("abcdd123")]
//
public void SetArgument_ParameterWithWrongCustomString_ReturnArgumentException(string value)
{
Argument arguments = Argument.Parse(new[] { "setargument", "-plate", value }, "-");
Expand Down
15 changes: 15 additions & 0 deletions tests/InterAppConnector.Test.Library/Commands/BooleanCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using InterAppConnector.Attributes;
using InterAppConnector.Interfaces;
using InterAppConnector.Test.Library.DataModels;

namespace InterAppConnector.Test.Library.Commands
{
[Command("booleancommand")]
public class BooleanCommand : ICommand<DataModelWithBoolean>
{
public string Main(DataModelWithBoolean arguments)
{
return CommandOutput.Ok(arguments);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace InterAppConnector.Test.Library.DataModels
{
public class DataModelWithBoolean
{
public bool MandatorySwitch { get; set; }
public int Number { get; set; }
}
}
40 changes: 40 additions & 0 deletions tests/InterAppConnector.Test.Library/InterAppCommunicationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ public void ExecuteAsBatch_ExecuteBatchTestCommandWithSuccessfulMessage_ReturnSu
Assert.That(result.MessageStatus, Is.EqualTo(CommandExecutionMessageType.Success));
}

[Test]
public void ExecuteAsBatch_ExecuteBatchTestCommandWithBooleanSet_ReturnSuccessfulMessage()
{
CommandManager manager = new CommandManager();
manager.AddCommand<BooleanCommand, DataModelWithBoolean>(true);
DataModelWithBoolean batchCommandDataModel = new DataModelWithBoolean
{
MandatorySwitch = true,
Number = 50
};
dynamic parameters = new ExpandoObject();
parameters.MandatorySwitch = batchCommandDataModel.MandatorySwitch;
parameters.Number = 50;

InterAppCommunication connector = new InterAppCommunication(manager);
CommandResult<DataModelWithBoolean> result = connector.ExecuteAsBatch<DataModelWithBoolean>("booleancommand", parameters);

Assert.That(result, Is.Not.Null);
Assert.That(result.MessageStatus, Is.EqualTo(CommandExecutionMessageType.Success));
Assert.That(result.Message.MandatorySwitch, Is.True);
Assert.That(result.Message.Number, Is.EqualTo(50));
}

[Test]
public void ExecuteAsBatch_ExecuteBatchTestCommandWithWarningMessage_ReturnWarningMessage()
{
Expand Down Expand Up @@ -320,6 +343,23 @@ public void ExecuteAsInteractiveCLI_WithMissingValueTypeArguments_ReturnMissingA
Assert.That(Environment.ExitCode, Is.EqualTo(3));
}

[Test]
public void ExecuteAsInteractiveCLI_WithBooleanValue_ReturnNoErrorMessage()
{
CommandManager command = new CommandManager();
command.AddCommand<BooleanCommand, DataModelWithBoolean>();
string[] arguments = "booleancommand -mandatoryswitch -number 50".Split(" ");

Action connectorAction = () =>
{
InterAppCommunication connector = new InterAppCommunication(command);
connector.ExecuteAsInteractiveCLI(arguments);
};

Assert.That(connectorAction, Throws.Nothing);
Assert.That(Environment.ExitCode, Is.EqualTo(0));
}

[Test]
public void ExecuteAsInteractiveCLI_WithMissingOptionalValueTypeArguments_ReturnValue()
{
Expand Down

0 comments on commit 1c95626

Please sign in to comment.