< Summary

Information
Class: Elsa.Workflows.Activities.While
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/While.cs
Line coverage
83%
Covered lines: 31
Uncovered lines: 6
Coverable lines: 37
Total lines: 107
Line coverage: 83.7%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
True(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%210%
.ctor(...)100%11100%
.ctor(...)100%210%
.ctor(...)100%210%
get_Condition()100%11100%
get_Body()100%11100%
ExecuteAsync()100%11100%
OnBodyCompleted()100%11100%
HandleIterationAsync()100%66100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/While.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Elsa.Expressions.Models;
 3using Elsa.Extensions;
 4using Elsa.Workflows.Attributes;
 5using Elsa.Workflows.Behaviors;
 6using Elsa.Workflows.Models;
 7using Elsa.Workflows.Options;
 8using Elsa.Workflows.UIHints;
 9using JetBrains.Annotations;
 10
 11namespace Elsa.Workflows.Activities;
 12
 13/// <summary>
 14/// Execute an activity while a given condition evaluates to true.
 15/// </summary>
 16[Activity("Elsa", "Looping", "Execute an activity while a given condition evaluates to true.")]
 17[PublicAPI]
 18public class While : Activity
 19{
 20    /// <summary>
 21    /// Creates a <see cref="While"/> activity that loops forever.
 22    /// </summary>
 123    public static While True(IActivity body) => new(body)
 124    {
 125        Condition = new(true)
 126    };
 27
 28    /// <inheritdoc />
 12629    public While([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 30    {
 12631        Behaviors.Add<BreakBehavior>(this);
 12632    }
 33
 34    /// <inheritdoc />
 12635    public While(IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : 
 36    {
 12637        Body = body;
 12638    }
 39
 40    /// <inheritdoc />
 12341    public While(Input<bool> condition, IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumbe
 42    {
 12343        Condition = condition;
 12344    }
 45
 46    /// <inheritdoc />
 47    public While(Func<ExpressionExecutionContext, ValueTask<bool>> condition, IActivity? body = null, [CallerFilePath] s
 048        : this(new Input<bool>(condition), body, source, line)
 49    {
 050    }
 51
 52    /// <inheritdoc />
 53    public While(Func<ExpressionExecutionContext, bool> condition, IActivity? body = null, [CallerFilePath] string? sour
 11254        : this(new Input<bool>(condition), body, source, line)
 55    {
 11256    }
 57
 58    /// <inheritdoc />
 59    public While(Func<ValueTask<bool>> condition, IActivity? body = null, [CallerFilePath] string? source = null, [Calle
 060        : this(new Input<bool>(condition), body, source, line)
 61    {
 062    }
 63
 64    /// <inheritdoc />
 65    public While(Func<bool> condition, IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumber
 066        : this(new Input<bool>(condition), body, source, line)
 67    {
 068    }
 69
 70    /// <summary>
 71    /// The condition to evaluate.
 72    /// </summary>
 73    [Input(AutoEvaluate = false, UIHint = InputUIHints.SingleLine)]
 61374    public Input<bool> Condition { get; set; } = new(false);
 75
 76    /// <summary>
 77    /// The <see cref="IActivity"/> to execute on every iteration.
 78    /// </summary>
 79    [Port]
 62780    public IActivity? Body { get; set; }
 81
 82    /// <inheritdoc />
 3183    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) => await HandleIterationAsync(cont
 84
 85    private async ValueTask OnBodyCompleted(ActivityCompletedContext context)
 86    {
 687        await HandleIterationAsync(context.TargetContext, context.ChildContext);
 688    }
 89
 90    private async ValueTask HandleIterationAsync(ActivityExecutionContext context, ActivityExecutionContext? completedCh
 91    {
 3792        var isBreaking = context.GetIsBreaking();
 3793        var loop = !isBreaking && await context.EvaluateInputPropertyAsync<While, bool>(x => x.Condition);
 94
 3695        if (loop)
 96        {
 2897            var options = new ScheduleWorkOptions
 2898            {
 2899                CompletionCallback = OnBodyCompleted,
 28100                SchedulingActivityExecutionId = completedChildContext?.Id
 28101            };
 28102            await context.ScheduleActivityAsync(Body, options);
 103        }
 104        else
 8105            await context.CompleteActivityAsync();
 36106    }
 107}