forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Collections; | ||
|
||
namespace Elsa.JavaScript.Helpers; | ||
|
||
/// <summary> | ||
/// Contains helper methods for working with object arrays. | ||
/// </summary> | ||
public static class ObjectArrayHelper | ||
{ | ||
/// <summary> | ||
/// Determines if the specified object is an array-like CLR collection. | ||
/// </summary> | ||
public static bool DetermineIfObjectIsArrayLikeClrCollection(Type type) | ||
{ | ||
var isDictionary = typeof(IDictionary).IsAssignableFrom(type); | ||
|
||
if (isDictionary) | ||
return false; | ||
|
||
if (typeof(ICollection).IsAssignableFrom(type)) | ||
return true; | ||
|
||
foreach (var interfaceType in type.GetInterfaces()) | ||
{ | ||
if (!interfaceType.IsGenericType) | ||
{ | ||
continue; | ||
} | ||
|
||
if (interfaceType.GetGenericTypeDefinition() == typeof(IReadOnlyCollection<>) | ||
|| interfaceType.GetGenericTypeDefinition() == typeof(ICollection<>)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
test/integration/Elsa.IntegrationTests/Scenarios/JavaScriptListsAndArrays/Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Dynamic; | ||
using System.Threading.Tasks; | ||
using Elsa.Expressions.Models; | ||
using Elsa.Extensions; | ||
using Elsa.JavaScript.Contracts; | ||
using Elsa.Testing.Shared; | ||
using Jint; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Elsa.IntegrationTests.Scenarios.JavaScriptListsAndArrays; | ||
|
||
public class Tests | ||
{ | ||
private readonly IServiceProvider _services; | ||
private readonly IJavaScriptEvaluator _evaluator; | ||
private readonly ExpressionExecutionContext _expressionContext; | ||
|
||
public Tests(ITestOutputHelper testOutputHelper) | ||
{ | ||
var testOutputHelper1 = testOutputHelper ?? throw new ArgumentNullException(nameof(testOutputHelper)); | ||
_services = new TestApplicationBuilder(testOutputHelper1).Build(); | ||
_evaluator = _services.GetRequiredService<IJavaScriptEvaluator>(); | ||
_expressionContext = new ExpressionExecutionContext(_services, new MemoryRegister()); | ||
} | ||
|
||
[Fact(DisplayName = "Workflow inputs containing .NET lists on dynamic objects are converted to arrays for use in JavaScript.")] | ||
public async Task Test1() | ||
{ | ||
dynamic dynamicObject = new ExpandoObject(); | ||
|
||
dynamicObject.List = new List<string> { "a", "b", "c" }; | ||
var script = "getObj().List.filter(x => x === 'b').length === 1"; | ||
_expressionContext.SetVariable("obj", (object)dynamicObject); | ||
var result = await _evaluator.EvaluateAsync(script, typeof(bool), _expressionContext); | ||
|
||
Assert.True((bool)result!); | ||
} | ||
|
||
[Fact(DisplayName = "Can access list properties as arrays")] | ||
public async Task Test2() | ||
{ | ||
var person = new ExpandoObject(); | ||
var magicNumbers = new[]{42, 43, 44}; | ||
var order1 = new ExpandoObject(); | ||
|
||
order1.TryAdd("magicNumbers", magicNumbers); | ||
person.TryAdd("orders", new []{ order1 }); | ||
person.TryAdd("name", "John"); | ||
person.TryAdd("age", 12); | ||
|
||
var languages = new List<object> | ||
{ | ||
"English", | ||
"French" | ||
}; | ||
|
||
person.TryAdd("languages", languages); | ||
|
||
var obj = new ExpandoObject(); | ||
obj.TryAdd("persons", new List<object> { person }); | ||
|
||
var name = await _evaluator.EvaluateAsync("o.persons.filter(x => x.age == 12)[0].name", typeof(string), _expressionContext, configureEngine: engine => engine.SetValue("o", obj)); | ||
var language = await _evaluator.EvaluateAsync("o.persons[0].languages.filter(x => x == 'English')[0]", typeof(string), _expressionContext, configureEngine: engine => engine.SetValue("o", obj)); | ||
var magicNumber = await _evaluator.EvaluateAsync("o.persons[0].orders[0].magicNumbers[1]", typeof(int), _expressionContext, configureEngine: engine => engine.SetValue("o", obj)); | ||
Assert.Equal("John", name); | ||
Assert.Equal("English", language); | ||
Assert.Equal(43, magicNumber); | ||
} | ||
} |