From 7d77e501f6c6c526dc9dfbbbf0d722e6ad1fc3a8 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 23 Oct 2020 14:00:10 +0200 Subject: [PATCH] Fix While activity + add test --- .../Activities/ControlFlow/While/While.cs | 11 ++---- .../ControlFlow/While/WhileExtensions.cs | 14 ++++++++ .../WhileWorkflowTests.cs | 27 ++++++++++++++ .../Workflows/WhileWorkflow.cs | 36 +++++++++++++++++++ 4 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 src/core/Elsa.Core/Activities/ControlFlow/While/WhileExtensions.cs create mode 100644 test/integration/Elsa.Core.IntegrationTests/WhileWorkflowTests.cs create mode 100644 test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflow.cs diff --git a/src/core/Elsa.Core/Activities/ControlFlow/While/While.cs b/src/core/Elsa.Core/Activities/ControlFlow/While/While.cs index 2ed30555c..30794c3d4 100644 --- a/src/core/Elsa.Core/Activities/ControlFlow/While/While.cs +++ b/src/core/Elsa.Core/Activities/ControlFlow/While/While.cs @@ -15,13 +15,8 @@ namespace Elsa.Activities.ControlFlow )] public class While : Activity { - private readonly IExpressionEvaluator _expressionEvaluator; - - public While(IExpressionEvaluator expressionEvaluator) - { - _expressionEvaluator = expressionEvaluator; - } - + public const string IterateOutcome = "Iterate"; + [ActivityProperty(Hint = "The condition to evaluate.")] public bool Condition { get; set; } @@ -30,7 +25,7 @@ namespace Elsa.Activities.ControlFlow var loop = Condition; if (loop) - return Combine(Schedule(Id), Done(OutcomeNames.Iterate)); + return Combine(PostSchedule(Id), Outcome(IterateOutcome)); return Done(); } diff --git a/src/core/Elsa.Core/Activities/ControlFlow/While/WhileExtensions.cs b/src/core/Elsa.Core/Activities/ControlFlow/While/WhileExtensions.cs new file mode 100644 index 000000000..5ef8953d4 --- /dev/null +++ b/src/core/Elsa.Core/Activities/ControlFlow/While/WhileExtensions.cs @@ -0,0 +1,14 @@ +using System; +using Elsa.Builders; +using Elsa.Services.Models; + +// ReSharper disable once CheckNamespace +namespace Elsa.Activities.ControlFlow +{ + public static class WhileExtensions + { + public static ISetupActivity WithCondition(this ISetupActivity activity, Func value) => activity.Set(x => x.Condition, value); + public static ISetupActivity WithCondition(this ISetupActivity activity, Func value) => activity.Set(x => x.Condition, value); + public static ISetupActivity WithCondition(this ISetupActivity activity, bool value) => activity.Set(x => x.Condition, value); + } +} \ No newline at end of file diff --git a/test/integration/Elsa.Core.IntegrationTests/WhileWorkflowTests.cs b/test/integration/Elsa.Core.IntegrationTests/WhileWorkflowTests.cs new file mode 100644 index 000000000..6cbb3be84 --- /dev/null +++ b/test/integration/Elsa.Core.IntegrationTests/WhileWorkflowTests.cs @@ -0,0 +1,27 @@ +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 WhileWorkflowTests : WorkflowsUnitTestBase + { + public WhileWorkflowTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) + { + } + + [Fact(DisplayName = "Runs loop while condition is true.")] + public async Task Test01() + { + const int loopCount = 3; + var workflow = new WhileWorkflow(loopCount); + var workflowInstance = await WorkflowRunner.RunWorkflowAsync(workflow); + var iterationLogs = workflowInstance.ExecutionLog.Where(x => x.ActivityId == "WriteLoopCount").ToList(); + + Assert.Equal(loopCount, iterationLogs.Count); + } + } +} \ No newline at end of file diff --git a/test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflow.cs b/test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflow.cs new file mode 100644 index 000000000..5ba0b1ead --- /dev/null +++ b/test/integration/Elsa.Core.IntegrationTests/Workflows/WhileWorkflow.cs @@ -0,0 +1,36 @@ +using Elsa.Activities.Console; +using Elsa.Activities.ControlFlow; +using Elsa.Builders; +using Elsa.Services.Models; + +namespace Elsa.Core.IntegrationTests.Workflows +{ + public class WhileWorkflow : IWorkflow + { + private const string CounterVariableName = "Counter"; + private readonly int _loopCount; + + public WhileWorkflow(int loopCount) + { + _loopCount = loopCount; + } + + public void Build(IWorkflowBuilder workflow) + { + workflow + .Then( + @while => @while.WithCondition(context => GetCounter(context) < _loopCount), + @while => + { + @while + .When(While.IterateOutcome) + .WriteLine(context => $"Inside while loop. Counter = {context.GetVariable(CounterVariableName)}", id: "WriteLoopCount") + .SetVariable(CounterVariableName, context => GetCounter(context) + 1); + + }) + .Then(writeLine => writeLine.WithText("Done")).WithName("Done"); + } + + private int GetCounter(ActivityExecutionContext context) => context.GetVariable(CounterVariableName); + } +} \ No newline at end of file