Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced the name of the global variable holding the input data in the Javascript expression evaluator #93

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static bool DetermineIfObjectIsArrayLikeClrCollection(Type type)
})
;
}); ;
jsEngine.SetValue("input", data);
jsEngine.SetValue("$", data);
if (args != null) foreach (var arg in args) jsEngine.SetValue(arg.Key, arg.Value);
var result = jsEngine.Evaluate(expression).UnwrapIfPromise().ToObject();
if (expectedType == typeof(object)) return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
input.foo + " " + input.bar
$.foo + " " + $.bar
Original file line number Diff line number Diff line change
@@ -1 +1 @@
`${input.foo.bar} is a greeting`
`${$.foo.bar} is a greeting`
Original file line number Diff line number Diff line change
@@ -1 +1 @@
`${input.foo} is "bar"`
`${$.foo} is "bar"`
Original file line number Diff line number Diff line change
@@ -1 +1 @@
input.say.replace('${greeting}', input.someGreeting)
$.say.replace('${greeting}', $.someGreeting)
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task Evaluate_PrimitiveOutput_ShouldWork()
{
//arrange
var value = 42;
var expression = "input.value";
var expression = "$.value";
var data = new { value };

//act
Expand All @@ -64,11 +64,25 @@ public async Task Evaluate_PrimitiveOutput_ShouldWork()
}

[Fact]
public async Task Evaluate_ComplexTypeOutput_ShouldWork()
public async Task Evaluate_PrimitiveInput_ShouldWork()
{
//arrange
var expression = "$";
var data = 42;

//act
var result = (int)(await this.ExpressionEvaluator.EvaluateAsync<double>(expression, data));

//assert
result.Should().Be(data);
}

[Fact]
public async Task Evaluate_ObjectTypeOutput_ShouldWork()
{
//arrange
var value = 42;
var expression = "${ input }";
var expression = "$";
var data = new { value };

//act
Expand All @@ -84,7 +98,7 @@ public async Task Evaluate_ComplexTypeInput_ShouldWork()
//arrange
var bar = "bar";
var baz = new { foo = "bar" };
var obj = new { foo = "${ input.bar }", bar = "foo", baz };
var obj = new { foo = "${ $.bar }", bar = "foo", baz };
var data = new { bar };
var expectedResult = new { foo = bar, bar = "foo", baz = baz.ToExpandoObject() };

Expand All @@ -100,7 +114,7 @@ public async Task Evaluate_LargeData_ShouldWork()
{
//arrange
var data = JsonSerializer.Default.Deserialize<List<object>>(File.ReadAllText(Path.Combine("Assets", "dogs.json")))!;
var expression = "input.filter(i => i.category?.name === CONST.category)[0]";
var expression = "$.filter(i => i.category?.name === CONST.category)[0]";
var args = new Dictionary<string, object>() { { "CONST", new { category = "Pugal" } } };

//act
Expand Down Expand Up @@ -130,10 +144,9 @@ public async Task Evaluate_LargeExpression_ShouldWork()
//arrange
var data = new { };
var expression = File.ReadAllText(Path.Combine("Assets", "pets.expression.js.txt"));
var args = new Dictionary<string, object>() { { "CONST", new { category = "Pugal" } } };

//act
dynamic? result = await this.ExpressionEvaluator.EvaluateAsync(expression, data, args);
dynamic? result = await this.ExpressionEvaluator.EvaluateAsync(expression, data);

//assert
Assert.NotEmpty(result?.pets);
Expand All @@ -145,7 +158,7 @@ public async Task Evaluate_EscapedJsonInput_ShouldWork()
//arrange
var json = File.ReadAllText(Path.Combine("Assets", "inputWithEscapedJson.json"));
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<ExpandoObject>(json)!;
var expression = "input._user";
var expression = "$._user";

//act
dynamic? result = await this.ExpressionEvaluator.EvaluateAsync(expression, data);
Expand Down
Loading