< Summary

Information
Class: Elsa.Workflows.Activities.Flowchart.Activities.FlowSwitch
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Flowchart/Activities/FlowSwitch.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 70
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Cases()100%11100%
get_Mode()100%11100%
ExecuteAsync()100%66100%
FindMatchingCasesAsync()100%44100%

File(s)

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

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Elsa.Expressions.Contracts;
 3using Elsa.Expressions.Models;
 4using Elsa.Workflows.Activities.Flowchart.Attributes;
 5using Elsa.Workflows.Activities.Flowchart.Models;
 6using Elsa.Workflows.Attributes;
 7using Elsa.Workflows.Models;
 8using Elsa.Workflows.UIHints;
 9using JetBrains.Annotations;
 10
 11namespace Elsa.Workflows.Activities.Flowchart.Activities;
 12
 13/// <summary>
 14/// Evaluates the specified case conditions and schedules the one that evaluates to <code>true</code>.
 15/// </summary>
 16[FlowNode("Default")]
 17[Activity("Elsa", "Branching", "Evaluate a set of case conditions and schedule the activity for a matching case.", Displ
 18[PublicAPI]
 19public class FlowSwitch : Activity
 20{
 21    /// <inheritdoc />
 522    public FlowSwitch([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 23    {
 524    }
 25
 26    /// <summary>
 27    /// The possible cases to evaluate.
 28    /// </summary>
 29    [Input(UIHint = "flow-switch-editor")]
 2030    public ICollection<FlowSwitchCase> Cases { get; set; } = new List<FlowSwitchCase>();
 31
 32    /// <summary>
 33    /// The switch mode determines whether the first match should be scheduled, or all matches.
 34    /// </summary>
 35    [Input(
 36        Description = "The switch mode determines whether the first match should be scheduled, or all matches.",
 37        UIHint = InputUIHints.DropDown
 38    )]
 2239    public Input<SwitchMode> Mode { get; set; } = new(SwitchMode.MatchFirst);
 40
 41    /// <inheritdoc />
 42    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
 43    {
 544        var matchingCases = (await FindMatchingCasesAsync(context.ExpressionExecutionContext)).ToList();
 545        var hasAnyMatches = matchingCases.Any();
 546        var mode = context.Get(Mode);
 547        var results = mode == SwitchMode.MatchFirst ? hasAnyMatches ? [matchingCases.First()] : Array.Empty<FlowSwitchCa
 948        var outcomes = hasAnyMatches ? results.Select(r => r.Label).ToArray() : ["Default"];
 49
 550        await context.CompleteActivityAsync(new Outcomes(outcomes));
 551    }
 52
 53    private async Task<IEnumerable<FlowSwitchCase>> FindMatchingCasesAsync(ExpressionExecutionContext context)
 54    {
 555        var matchingCases = new List<FlowSwitchCase>();
 556        var expressionEvaluator = context.GetRequiredService<IExpressionEvaluator>();
 57
 3058        foreach (var switchCase in Cases)
 59        {
 1060            var result = await expressionEvaluator.EvaluateAsync<bool?>(switchCase.Condition, context);
 61
 1062            if (result == true)
 63            {
 564                matchingCases.Add(switchCase);
 65            }
 1066        }
 67
 568        return matchingCases;
 569    }
 70}