< Summary

Information
Class: Elsa.Workflows.Activities.Complete
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Complete.cs
Line coverage
30%
Covered lines: 10
Uncovered lines: 23
Coverable lines: 33
Total lines: 105
Line coverage: 30.3%
Branch coverage
25%
Covered branches: 4
Total branches: 16
Branch coverage: 25%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
get_Outcomes()100%11100%
ExecuteAsync()100%11100%
InterpretOutcomes()25%1881612.5%

File(s)

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

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using System.Text.Json;
 3using Elsa.Expressions.Models;
 4using Elsa.Extensions;
 5using Elsa.Workflows.Activities.Flowchart.Models;
 6using Elsa.Workflows.Attributes;
 7using Elsa.Workflows.Models;
 8using Elsa.Workflows.Signals;
 9using Elsa.Workflows.UIHints;
 10using JetBrains.Annotations;
 11
 12namespace Elsa.Workflows.Activities;
 13
 14/// <summary>
 15/// Signals the current composite activity to complete itself as a whole.
 16/// </summary>
 17[Activity("Elsa", "Composition", "Signals the current composite activity to complete itself as a whole.")]
 18[PublicAPI]
 19public class Complete : Activity, ITerminalNode
 20{
 21    /// <inheritdoc />
 2022    public Complete([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 23    {
 2024    }
 25
 26    /// <inheritdoc />
 27    public Complete(IEnumerable<string> outcomes, [CallerFilePath] string? source = null, [CallerLineNumber] int? line =
 028        : this(new Input<object>(outcomes.ToList()), source, line)
 29    {
 030    }
 31
 32    /// <inheritdoc />
 33    public Complete(Func<ExpressionExecutionContext, ICollection<string>> outcomes, [CallerFilePath] string? source = nu
 034        : this(new Input<object>(outcomes), source, line)
 35    {
 036    }
 37
 38    /// <inheritdoc />
 39    public Complete(Func<ExpressionExecutionContext, string> outcome, [CallerFilePath] string? source = null, [CallerLin
 040        : this(context => [outcome(context)], source, line)
 41    {
 042    }
 43
 44    /// <inheritdoc />
 045    public Complete(Input<object> outcomes, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null)
 46    {
 047        Outcomes = outcomes;
 048    }
 49
 50    /// <summary>
 51    /// The outcome or set of outcomes to complete this activity with.
 52    /// </summary>
 53    [Input(
 54        Description = "The outcome or set of outcomes to complete this activity with.",
 55        UIHint = InputUIHints.OutcomePicker,
 56        DefaultSyntax = "Object"
 57    )]
 4358    public Input<object> Outcomes { get; set; } = null!;
 59
 60    /// <inheritdoc />
 61    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
 62    {
 363        var outcomesValue = Outcomes.GetOrDefault(context);
 364        var outcomes = InterpretOutcomes(outcomesValue).ToArray();
 65
 366        await context.SendSignalAsync(new CompleteCompositeSignal(new Outcomes(outcomes)));
 367        await context.CompleteActivityAsync();
 368    }
 69
 70    private static IEnumerable<string> InterpretOutcomes(object? outcomesValue)
 71    {
 72        switch (outcomesValue)
 73        {
 74            case string singleOutcome:
 075                yield return singleOutcome;
 076                break;
 77            case IEnumerable<string> outcomeStrings:
 078                foreach (var outcome in outcomeStrings)
 079                    yield return outcome;
 080                break;
 81            case IEnumerable<object> outcomeObjects:
 82            {
 083                foreach (var outcome in outcomeObjects)
 084                    yield return outcome.ToString()!;
 085                break;
 86            }
 87            case JsonElement jsonElement:
 88            {
 089                if (jsonElement.ValueKind == JsonValueKind.Array)
 90                {
 091                    var outcomeArray = jsonElement.EnumerateArray().ToList();
 092                    foreach (var element in outcomeArray)
 093                        yield return element.ToString();
 94                }
 95                else
 096                    yield return jsonElement.ToString();
 97
 098                break;
 99            }
 100            default:
 3101                yield return "Done";
 102                break;
 103        }
 3104    }
 105}