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

fix #32 initial commandexectiontype change at runtime #33

Merged
merged 1 commit into from
Oct 5, 2024
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
58 changes: 57 additions & 1 deletion src/InterAppConnector/CommandOutput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json;
using System.Collections;
using System.Text.Json;
using InterAppConnector.DataModels;
using InterAppConnector.Enumerations;
using InterAppConnector.Exceptions;
Expand Down Expand Up @@ -28,6 +29,61 @@ public class CommandOutput
internal static event StatusEventHandler WarningMessageEmitted;
internal static event StatusEventHandler ErrorMessageEmitted;

/// <summary>
/// Check if the method in event <paramref name="eventToAttach"/> is already added in <paramref name="targetEvent"/> invocation list.
/// </summary>
/// <param name="targetEvent">The target event</param>
/// <param name="eventToAttach">The event to add</param>
/// <returns><see langword="true"/> if the event is already attached, otherwise <see langword="false"/></returns>
public static bool IsEventAlreadyAttached(Delegate targetEvent, Delegate eventToAttach)
{
bool isEventFound = false;
if (targetEvent != null && eventToAttach != null)
{
/*
* It's important to compare the parameters used by targetEvent and eventToAdd.
* If the parameters and the name are the same, it is possible to say that
* it is using the same delegate that is already added. The comparison between arrays should be done
* via IStructuralEquatable because this method check if the structure of the array is equal, and not
* the reference
*/
IStructuralEquatable eventToCompare = eventToAttach.Method.GetParameters();

isEventFound = (from item in targetEvent.GetInvocationList()
where item.Method.Name == eventToAttach.Method.Name
&& eventToCompare.Equals(item.Method.GetParameters(), StructuralComparisons.StructuralEqualityComparer)
select item).Any();
}
return isEventFound;
}

public static bool IsSuccessEventAlreadyAttached(Delegate eventToFind)
{
return IsEventAlreadyAttached(SuccessMessageEmitted, eventToFind);
}

public static bool IsWarningEventAlreadyAttached(Delegate eventToFind)
{
return IsEventAlreadyAttached(WarningMessageEmitted, eventToFind);
}
public static bool IsErrorEventAlreadyAttached(Delegate eventToFind)
{
return IsEventAlreadyAttached(ErrorMessageEmitted, eventToFind);
}

public static bool IsInfoEventAlreadyAttached(Delegate eventToFind)
{
return IsEventAlreadyAttached(InfoMessageEmitted, eventToFind);
}

internal static void ClearEvents()
{
SuccessMessageEmitted = null!;
WarningMessageEmitted = null!;
ErrorMessageEmitted = null!;
InfoMessageEmitted = null!;
}

/// <summary>
/// Return a positive message that an operation has been completed successfully
/// </summary>
Expand Down
62 changes: 54 additions & 8 deletions src/InterAppConnector/InterAppCommunication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,32 @@ public InterAppCommunication(CommandManager commandManager)
/// <param name="arguments">Arguments passed to the CLI</param>
public void ExecuteAsInteractiveCLI(string[] arguments, CommandOutputFormat commandOutputFormat = CommandOutputFormat.Text)
{
CommandExecutionType currentCommandExecutionType = _commandExecutionType;
CommandOutputFormat currentCommandOutputFormat = CommandOutputFormat;

_commandExecutionType = CommandExecutionType.Interactive;
CommandOutputFormat = commandOutputFormat;
CommandOutput.ErrorMessageEmitted += CommandOutput_ErrorMessageEmitted;
CommandOutput.SuccessMessageEmitted += CommandOutput_SuccessMessageEmitted;
CommandOutput.InfoMessageEmitted += CommandOutput_InfoMessageEmitted;
CommandOutput.WarningMessageEmitted += CommandOutput_WarningMessageEmitted;

if (!IsErrorEventAlreadyAttached(CommandOutput_ErrorMessageEmitted))
{
CommandOutput.ErrorMessageEmitted += CommandOutput_ErrorMessageEmitted;
}

if (!IsSuccessEventAlreadyAttached(CommandOutput_SuccessMessageEmitted))
{
CommandOutput.SuccessMessageEmitted += CommandOutput_SuccessMessageEmitted;
}

if (!IsInfoEventAlreadyAttached(CommandOutput_InfoMessageEmitted))
{
CommandOutput.InfoMessageEmitted += CommandOutput_InfoMessageEmitted;
}

if (!IsWarningEventAlreadyAttached(CommandOutput_WarningMessageEmitted))
{
CommandOutput.WarningMessageEmitted += CommandOutput_WarningMessageEmitted;
}

string selectedAction = "";
Argument argumentsParsed = Argument.Parse(arguments, "-");

Expand Down Expand Up @@ -115,6 +135,9 @@ public void ExecuteAsInteractiveCLI(string[] arguments, CommandOutputFormat comm
{
Console.Write(CommandUtil.DescribeCommands(_commandManager));
}

_commandExecutionType = currentCommandExecutionType;
CommandOutputFormat = currentCommandOutputFormat;
}

/// <summary>
Expand All @@ -129,13 +152,32 @@ public void ExecuteAsInteractiveCLI(string[] arguments, CommandOutputFormat comm
/// <returns>The raw string returned by the requested command</returns>
public string ExecuteAsBatch(string action, dynamic parameters, CommandOutputFormat commandOutputFormat = CommandOutputFormat.Json)
{
CommandExecutionType currentCommandExecutionType = _commandExecutionType;
CommandOutputFormat currentCommandOutputFormat = CommandOutputFormat;

string output = "";
_commandExecutionType = CommandExecutionType.Batch;
CommandOutputFormat = commandOutputFormat;
CommandOutput.ErrorMessageEmitted += CommandOutput_ErrorMessageEmitted;
CommandOutput.SuccessMessageEmitted += CommandOutput_SuccessMessageEmitted;
CommandOutput.InfoMessageEmitted += CommandOutput_InfoMessageEmitted;
CommandOutput.WarningMessageEmitted += CommandOutput_WarningMessageEmitted;

if (!IsErrorEventAlreadyAttached(CommandOutput_ErrorMessageEmitted))
{
CommandOutput.ErrorMessageEmitted += CommandOutput_ErrorMessageEmitted;
}

if (!IsSuccessEventAlreadyAttached(CommandOutput_SuccessMessageEmitted))
{
CommandOutput.SuccessMessageEmitted += CommandOutput_SuccessMessageEmitted;
}

if (!IsInfoEventAlreadyAttached(CommandOutput_InfoMessageEmitted))
{
CommandOutput.InfoMessageEmitted += CommandOutput_InfoMessageEmitted;
}

if (!IsWarningEventAlreadyAttached(CommandOutput_WarningMessageEmitted))
{
CommandOutput.WarningMessageEmitted += CommandOutput_WarningMessageEmitted;
}

Argument argumentsParsed = Argument.Parse(parameters, "-");
argumentsParsed.Action.AddRange(action.Split(" ", StringSplitOptions.RemoveEmptyEntries));
Expand Down Expand Up @@ -171,6 +213,10 @@ public string ExecuteAsBatch(string action, dynamic parameters, CommandOutputFor
output = Error("Cannot execute the selected action because this action does not exist." + Environment.NewLine + CommandUtil.DescribeCommands(_commandManager, false));
}
}

_commandExecutionType = currentCommandExecutionType;
CommandOutputFormat = currentCommandOutputFormat;

return output;
}

Expand Down
1 change: 0 additions & 1 deletion tests/InterAppConnector.Test.Library/CommandManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ public void SetArgument_WithAnotherValidatorCustomInputStringAndValueNotInRange_
[TestCase("ab")]
//
[TestCase("")]
[TestCase(" ")]
//
public void SetArgument_ParameterWithWrongCustomString_ReturnArgumentException(string value)
{
Expand Down
78 changes: 76 additions & 2 deletions tests/InterAppConnector.Test.Library/CommandOutputTest.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,91 @@
using InterAppConnector.DataModels;
using InterAppConnector.Enumerations;
using InterAppConnector.Exceptions;
using InterAppConnector.Interfaces;
using InterAppConnector.Test.Library.DataModels;
using Newtonsoft.Json;
using NUnit.Framework;
using System.Collections.Generic;
using System.Dynamic;

namespace InterAppConnector.Test.Library
{
public class CommandOutputTest
{
private delegate void TestDelegate();

private event TestDelegate TestEvent;

[Test]
public void IsEventAlreadyAttached_CompareWithTwoSameEvent_ShouldReturnTrue()
{
TestEvent += CommandOutputTest_TestEvent;
TestEvent += CommandOutputTest_TestEvent2;

TestEvent.Invoke();

Assert.That(CommandOutput.IsEventAlreadyAttached(TestEvent, CommandOutputTest_TestEvent2), Is.True);
}

[Test]
public void IsEventAlreadyAttached_CompareWithDifferentEventsWithTheSameName_ShouldReturnTrue()
{
TestEvent += CommandOutputTest_TestEvent;
TestEvent += CommandOutputTest_TestEvent2;
TestEvent += CommandOutputTest_TestEvent;
TestEvent += CommandOutputTest_TestEvent2;

TestEvent.Invoke();

Assert.That(CommandOutput.IsEventAlreadyAttached(TestEvent, CommandOutputTest_TestEvent2), Is.True);
}

[Test]
public void IsEventAlreadyAttached_CompareWitANullEvent_ShouldReturnFalse()
{
TestEvent += CommandOutputTest_TestEvent;
TestEvent += CommandOutputTest_TestEvent2;

TestEvent.Invoke();

Assert.That(CommandOutput.IsEventAlreadyAttached(TestEvent, null), Is.False);
}

[Test]
public void IsEventAlreadyAttached_CompareWitANullTarget_ShouldReturnFalse()
{
TestEvent += CommandOutputTest_TestEvent;
TestEvent += CommandOutputTest_TestEvent2;

TestEvent.Invoke();

Assert.That(CommandOutput.IsEventAlreadyAttached(null, CommandOutputTest_TestEvent2), Is.False);
}

[Test]
public void IsEventAlreadyAttached_CompareAllNull_ShouldReturnFalse()
{
TestEvent += CommandOutputTest_TestEvent;
TestEvent += CommandOutputTest_TestEvent2;

TestEvent.Invoke();

Assert.That(CommandOutput.IsEventAlreadyAttached(null, null), Is.False);
}

private void CommandOutputTest_TestEvent()
{
Assert.That(CommandOutput.IsEventAlreadyAttached(TestEvent, CommandOutputTest_TestEvent1), Is.False);
}

private void CommandOutputTest_TestEvent1()
{
throw new NotImplementedException();
}

private void CommandOutputTest_TestEvent2()
{
Assert.That(CommandOutput.IsEventAlreadyAttached(TestEvent, CommandOutputTest_TestEvent1), Is.False);
}

[Test]
public void Parse_ParseSuccessMessage_ReturnSuccessMessage()
{
Expand Down
Loading
Loading