Skip to content

Commit

Permalink
Refactor JavaScript list and array test cases
Browse files Browse the repository at this point in the history
The JavaScript list and array test cases code has been restructured and moved into JavaScriptListsAndArrays directory. As a consequence, the previous Tests.cs file has been deleted. Additional test cases have been added to cover more scenarios, such as handling magic numbers in the code.
  • Loading branch information
sfmskywalker committed Jan 31, 2024
1 parent a53c792 commit 1712e41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 60 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Elsa.Extensions;
using Elsa.JavaScript.Contracts;
using Elsa.Testing.Shared;
using Jint;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -16,12 +17,14 @@ 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.")]
Expand All @@ -31,10 +34,40 @@ public async Task Test1()

dynamicObject.List = new List<string> { "a", "b", "c" };
var script = "getObj().List.filter(x => x === 'b').length === 1";
var context = new ExpressionExecutionContext(_services, new MemoryRegister());
context.SetVariable("obj", (object)dynamicObject);
var result = await _evaluator.EvaluateAsync(script, typeof(bool), context);
_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);
}
}

0 comments on commit 1712e41

Please sign in to comment.