Fix While activity + add test
This commit is contained in:
parent
bc4924e57b
commit
7d77e501f6
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<While> WithCondition(this ISetupActivity<While> activity, Func<ActivityExecutionContext, bool> value) => activity.Set(x => x.Condition, value);
|
||||
public static ISetupActivity<While> WithCondition(this ISetupActivity<While> activity, Func<bool> value) => activity.Set(x => x.Condition, value);
|
||||
public static ISetupActivity<While> WithCondition(this ISetupActivity<While> activity, bool value) => activity.Set(x => x.Condition, value);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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 => @while.WithCondition(context => GetCounter(context) < _loopCount),
|
||||
@while =>
|
||||
{
|
||||
@while
|
||||
.When(While.IterateOutcome)
|
||||
.WriteLine(context => $"Inside while loop. Counter = {context.GetVariable<int>(CounterVariableName)}", id: "WriteLoopCount")
|
||||
.SetVariable(CounterVariableName, context => GetCounter(context) + 1);
|
||||
|
||||
})
|
||||
.Then<WriteLine>(writeLine => writeLine.WithText("Done")).WithName("Done");
|
||||
}
|
||||
|
||||
private int GetCounter(ActivityExecutionContext context) => context.GetVariable<int>(CounterVariableName);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue