< 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
81%
Covered lines: 26
Uncovered lines: 6
Coverable lines: 32
Total lines: 99
Line coverage: 81.2%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%44100%

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.UIHints;
 8using JetBrains.Annotations;
 9
 10namespace Elsa.Workflows.Activities;
 11
 12/// <summary>
 13/// Execute an activity while a given condition evaluates to true.
 14/// </summary>
 15[Activity("Elsa", "Looping", "Execute an activity while a given condition evaluates to true.")]
 16[PublicAPI]
 17public class While : Activity
 18{
 19    /// <summary>
 20    /// Creates a <see cref="While"/> activity that loops forever.
 21    /// </summary>
 122    public static While True(IActivity body) => new(body)
 123    {
 124        Condition = new(true)
 125    };
 26
 27    /// <inheritdoc />
 5028    public While([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 29    {
 5030        Behaviors.Add<BreakBehavior>(this);
 5031    }
 32
 33    /// <inheritdoc />
 5034    public While(IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : 
 35    {
 5036        Body = body;
 5037    }
 38
 39    /// <inheritdoc />
 4740    public While(Input<bool> condition, IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumbe
 41    {
 4742        Condition = condition;
 4743    }
 44
 45    /// <inheritdoc />
 46    public While(Func<ExpressionExecutionContext, ValueTask<bool>> condition, IActivity? body = null, [CallerFilePath] s
 047        : this(new Input<bool>(condition), body, source, line)
 48    {
 049    }
 50
 51    /// <inheritdoc />
 52    public While(Func<ExpressionExecutionContext, bool> condition, IActivity? body = null, [CallerFilePath] string? sour
 3653        : this(new Input<bool>(condition), body, source, line)
 54    {
 3655    }
 56
 57    /// <inheritdoc />
 58    public While(Func<ValueTask<bool>> condition, IActivity? body = null, [CallerFilePath] string? source = null, [Calle
 059        : this(new Input<bool>(condition), body, source, line)
 60    {
 061    }
 62
 63    /// <inheritdoc />
 64    public While(Func<bool> condition, IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumber
 065        : this(new Input<bool>(condition), body, source, line)
 66    {
 067    }
 68
 69    /// <summary>
 70    /// The condition to evaluate.
 71    /// </summary>
 72    [Input(AutoEvaluate = false, UIHint = InputUIHints.SingleLine)]
 25373    public Input<bool> Condition { get; set; } = new(false);
 74
 75    /// <summary>
 76    /// The <see cref="IActivity"/> to execute on every iteration.
 77    /// </summary>
 78    [Port]
 24579    public IActivity? Body { get; set; }
 80
 81    /// <inheritdoc />
 3182    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) => await HandleIterationAsync(cont
 83
 84    private async ValueTask OnBodyCompleted(ActivityCompletedContext context)
 85    {
 686        await HandleIterationAsync(context.TargetContext);
 687    }
 88
 89    private async ValueTask HandleIterationAsync(ActivityExecutionContext context)
 90    {
 3791        var isBreaking = context.GetIsBreaking();
 3792        var loop = !isBreaking && await context.EvaluateInputPropertyAsync<While, bool>(x => x.Condition);
 93
 3694        if (loop)
 2895            await context.ScheduleActivityAsync(Body, OnBodyCompleted);
 96        else
 897            await context.CompleteActivityAsync();
 3698    }
 99}