Add project files.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Testing.Shared;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Elsa.Workflows.IntegrationTests.Scenarios.Composites;
|
||||
|
||||
public class Tests
|
||||
{
|
||||
private readonly IWorkflowRunner _workflowRunner;
|
||||
private readonly CapturingTextWriter _capturingTextWriter = new();
|
||||
private readonly IWorkflowBuilderFactory _workflowBuilderFactory;
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public Tests(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
_services = new TestApplicationBuilder(testOutputHelper)
|
||||
.WithCapturingTextWriter(_capturingTextWriter)
|
||||
.ConfigureElsa(elsa => elsa.UseJavaScript())
|
||||
.Build();
|
||||
_workflowBuilderFactory = _services.GetRequiredService<IWorkflowBuilderFactory>();
|
||||
_workflowRunner = _services.GetRequiredService<IWorkflowRunner>();
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "The main workflow can capture the result of the composite activity")]
|
||||
public async Task Test1()
|
||||
{
|
||||
await _services.PopulateRegistriesAsync();
|
||||
var workflow = await _workflowBuilderFactory.CreateBuilder().BuildWorkflowAsync<SumWorkflow>();
|
||||
|
||||
// Start workflow.
|
||||
await _workflowRunner.RunAsync(workflow);
|
||||
|
||||
// Verify expected output.
|
||||
var lines = _capturingTextWriter.Lines.ToList();
|
||||
Assert.Equal(new[] { "Sum: 3" }, lines);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using Elsa.Extensions;
|
||||
using Elsa.JavaScript.Activities;
|
||||
using Elsa.Workflows.Activities;
|
||||
using Elsa.Workflows.Memory;
|
||||
using Elsa.Workflows.Models;
|
||||
|
||||
namespace Elsa.Workflows.IntegrationTests.Scenarios.Composites;
|
||||
|
||||
/// <summary>
|
||||
/// A composite activity that sums two inputs and returns the result.
|
||||
/// </summary>
|
||||
public class Sum : Composite<int>
|
||||
{
|
||||
private readonly RunJavaScript _runJavaScript;
|
||||
|
||||
public Input<int> A { get; set; } = null!;
|
||||
public Input<int> B { get; set; } = null!;
|
||||
|
||||
public Sum()
|
||||
{
|
||||
_runJavaScript = new()
|
||||
{
|
||||
Script = new("getA() + getB();"),
|
||||
};
|
||||
|
||||
Root = new Sequence
|
||||
{
|
||||
Activities =
|
||||
{
|
||||
_runJavaScript
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected override void ConfigureActivities(ActivityExecutionContext context)
|
||||
{
|
||||
// Copy input into variables so that JS activity can access them.
|
||||
var a = A.Get(context);
|
||||
var b = B.Get(context);
|
||||
Variables.Add(new Variable<int>("A", a));
|
||||
Variables.Add(new Variable<int>("B", b));
|
||||
|
||||
// If the call site set a result variable, assign it to the JavaScript activity's result.
|
||||
if (Result != null)
|
||||
_runJavaScript.Result = new(Result.MemoryBlockReference)!;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A workflow that uses the <see cref="Sum"/> composite activity.
|
||||
/// </summary>
|
||||
public class SumWorkflow : WorkflowBase
|
||||
{
|
||||
protected override void Build(IWorkflowBuilder workflow)
|
||||
{
|
||||
var sum = workflow.WithVariable<int>("Sum", 0);
|
||||
|
||||
workflow.Root = new Sequence
|
||||
{
|
||||
Activities =
|
||||
{
|
||||
new Sum
|
||||
{
|
||||
A = new(1),
|
||||
B = new(2),
|
||||
Result = new(sum)
|
||||
},
|
||||
new WriteLine(context => $"Sum: {sum.Get(context)}")
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user