< Summary

Information
Class: Elsa.Expressions.JavaScript.Activities.RunJavaScript
Assembly: Elsa.Expressions.JavaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Activities/RunJavaScript/RunJavaScript.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 85
Line coverage: 100%
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
.ctor(...)100%11100%
.ctor(...)100%11100%
get_Script()100%11100%
get_PossibleOutcomes()100%11100%
ExecuteAsync()100%44100%
ConfigureEngine(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Activities/RunJavaScript/RunJavaScript.cs

#LineLine coverage
 1using System.Runtime.CompilerServices;
 2using Elsa.Expressions.Models;
 3using Elsa.Extensions;
 4using Elsa.Expressions.JavaScript.Contracts;
 5using Elsa.Workflows;
 6using Elsa.Workflows.Attributes;
 7using Elsa.Workflows.Models;
 8using Elsa.Workflows.UIHints;
 9using Jint;
 10
 11// ReSharper disable once CheckNamespace
 12namespace Elsa.Expressions.JavaScript.Activities;
 13
 14/// <summary>
 15/// Executes JavaScript code.
 16/// </summary>
 17[Activity("Elsa", "Scripting", "Executes JavaScript code", DisplayName = "Run JavaScript")]
 18public class RunJavaScript : CodeActivity<object?>
 19{
 20    /// <inheritdoc />
 8821    public RunJavaScript([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, lin
 22    {
 8823    }
 24
 25    /// <inheritdoc />
 5426    public RunJavaScript(string script, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : t
 27    {
 5428        Script = new(script);
 5429    }
 30
 31    /// <summary>
 32    /// The script to run.
 33    /// </summary>
 34    [Input(
 35        Description = "The script to run.",
 36        DefaultSyntax = "JavaScript",
 37        UIHint = InputUIHints.CodeEditor,
 38        UIHandler = typeof(RunJavaScriptOptionsProvider)
 39    )]
 39840    public Input<string> Script { get; set; } = new("");
 41
 42    /// <summary>
 43    /// A list of possible outcomes. Use "setOutcome()" to set the outcome. Use "setOutcomes" to set multiple outcomes.
 44    /// </summary>
 45    [Input(Description = "A list of possible outcomes.", UIHint = InputUIHints.DynamicOutcomes)]
 21946    public Input<ICollection<string>> PossibleOutcomes { get; set; } = null!;
 47
 48    /// <inheritdoc />
 49    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
 50    {
 2151        var script = context.Get(Script);
 52
 53        // If no script was specified, there's nothing to do.
 2154        if (string.IsNullOrWhiteSpace(script))
 255            return;
 56
 57        // Get a JavaScript evaluator.
 1958        var javaScriptEvaluator = context.GetRequiredService<IJavaScriptEvaluator>();
 59
 60        // Run the script.
 1961        var result = await javaScriptEvaluator.EvaluateAsync(
 1962            script,
 1963            typeof(object),
 1964            context.ExpressionExecutionContext,
 1965            ExpressionEvaluatorOptions.Empty,
 1966            engine => ConfigureEngine(engine, context),
 1967            context.CancellationToken);
 68
 69        // Set the result as output, if any.
 1670        if (result is not null)
 1171            context.Set(Result, result);
 72
 73        // Get the outcome or outcomes set by the script, if any. If not set, use "Done".
 2974        var outcomes = context.TransientProperties.GetValueOrDefault("Outcomes", () => new[] { "Done" })!;
 75
 76        // Complete the activity with the outcome.
 1677        await context.CompleteActivityWithOutcomesAsync(outcomes);
 1878    }
 79
 80    private static void ConfigureEngine(Engine engine, ActivityExecutionContext context)
 81    {
 2182        engine.SetValue("setOutcome", (Action<string>)(value => context.TransientProperties["Outcomes"] = new[] { value 
 2083        engine.SetValue("setOutcomes", (Action<string[]>)(value => context.TransientProperties["Outcomes"] = value));
 1984    }
 85}