Add IfElse extensions + tests
This commit is contained in:
parent
a4d7713ae7
commit
f8e4cd1e6c
|
|
@ -11,25 +11,20 @@ namespace Elsa.Activities.ControlFlow
|
|||
DisplayName = "If/Else",
|
||||
Category = "Control Flow",
|
||||
Description = "Evaluate a Boolean expression and continue execution depending on the result.",
|
||||
RuntimeDescription =
|
||||
"x => !!x.state.expression ? `Evaluate <strong>${ x.state.expression.expression }</strong> and continue execution depending on the result.` : x.definition.description",
|
||||
Outcomes = new[] { OutcomeNames.True, OutcomeNames.False, OutcomeNames.Done }
|
||||
RuntimeDescription = "x => !!x.state.expression ? `Evaluate <strong>${ x.state.expression.expression }</strong> and continue execution depending on the result.` : x.definition.description",
|
||||
Outcomes = new[] { True, False, OutcomeNames.Done }
|
||||
)]
|
||||
public class IfElse : Activity
|
||||
{
|
||||
private readonly IExpressionEvaluator _expressionEvaluator;
|
||||
|
||||
public IfElse(IExpressionEvaluator expressionEvaluator)
|
||||
{
|
||||
_expressionEvaluator = expressionEvaluator;
|
||||
}
|
||||
|
||||
public const string True = "True";
|
||||
public const string False = "False";
|
||||
|
||||
[ActivityProperty(Hint = "The condition to evaluate.")]
|
||||
public bool Condition { get; set; }
|
||||
|
||||
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
|
||||
{
|
||||
var outcome = Condition ? OutcomeNames.True : OutcomeNames.False;
|
||||
var outcome = Condition ? True : False;
|
||||
return Outcomes(OutcomeNames.Done, outcome);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using Elsa.Builders;
|
||||
using Elsa.Services.Models;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace Elsa.Activities.ControlFlow
|
||||
{
|
||||
public static class IfElseBuilderExtensions
|
||||
{
|
||||
public static IActivityBuilder IfElse(this IBuilder builder, Action<ISetupActivity<IfElse>>? setup = default, string? name = default) => builder.Then(setup).WithName(name);
|
||||
public static IActivityBuilder IfElse(this IBuilder builder, Func<ActivityExecutionContext, bool> condition, string? name = default) => builder.IfElse(activity => activity.Set(x => x.Condition, condition), name);
|
||||
public static IActivityBuilder IfElse(this IBuilder builder, Func<bool> condition, string? name = default) => builder.IfElse(activity => activity.Set(x => x.Condition, condition), name);
|
||||
public static IActivityBuilder IfElse(this IBuilder builder, bool condition, string? name = default) => builder.IfElse(activity => activity.Set(x => x.Condition, condition), name);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using Elsa.Builders;
|
||||
using Elsa.Services.Models;
|
||||
|
||||
// ReSharper disable once CheckNamespace
|
||||
namespace Elsa.Activities.ControlFlow
|
||||
{
|
||||
public static class IfElseExtensions
|
||||
{
|
||||
public static ISetupActivity<IfElse> WithCondition(this ISetupActivity<IfElse> activity, Func<ActivityExecutionContext, bool> value) => activity.Set(x => x.Condition, value);
|
||||
public static ISetupActivity<IfElse> WithCondition(this ISetupActivity<IfElse> activity, Func<bool> value) => activity.Set(x => x.Condition, value);
|
||||
public static ISetupActivity<IfElse> WithCondition(this ISetupActivity<IfElse> activity, bool value) => activity.Set(x => x.Condition, value);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Elsa.Core.IntegrationTests.Workflows;
|
||||
using Elsa.Testing.Shared.Helpers;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Elsa.Core.IntegrationTests
|
||||
{
|
||||
public class IfElseWorkflowTests : WorkflowsUnitTestBase
|
||||
{
|
||||
public IfElseWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
|
||||
{
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "Runs IfElse twice")]
|
||||
public async Task Test01()
|
||||
{
|
||||
var workflowInstance = await WorkflowRunner.RunWorkflowAsync<IfElseWorkflow>();
|
||||
var iterationLogs = workflowInstance.ExecutionLog.Where(x => x.ActivityId == "IfElse").ToList();
|
||||
|
||||
Assert.Equal(2, iterationLogs.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
using Elsa.Activities.Console;
|
||||
using Elsa.Activities.ControlFlow;
|
||||
using Elsa.Builders;
|
||||
|
||||
namespace Elsa.Core.IntegrationTests.Workflows
|
||||
{
|
||||
public class IfElseWorkflow : IWorkflow
|
||||
{
|
||||
public void Build(IWorkflowBuilder workflow)
|
||||
{
|
||||
workflow
|
||||
.WriteLine("Start")
|
||||
.Then<IfElse>(
|
||||
ifElse => ifElse.WithCondition(context => context.GetVariable<bool>("Flag")),
|
||||
ifElse =>
|
||||
{
|
||||
ifElse
|
||||
.When(IfElse.False)
|
||||
.WriteLine("Flag is false. Setting it to true.")
|
||||
.SetVariable("Flag", true)
|
||||
.Then("IfElse");
|
||||
|
||||
ifElse
|
||||
.When(IfElse.True)
|
||||
.WriteLine("Flag is set to true.")
|
||||
.Then("Done");
|
||||
})
|
||||
.WithId("IfElse")
|
||||
.WithName("IfElse")
|
||||
.Add<WriteLine>(writeLine => writeLine.WithText("Done")).WithName("Done");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue