Skip to content

Commit

Permalink
fix(Data.Expressions.JavaScript): Replaced the name of the global var…
Browse files Browse the repository at this point in the history
…iable holding the input data (#93)
  • Loading branch information
JBBianchi authored May 2, 2024
1 parent 4f93d17 commit 132eaef
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
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

0 comments on commit 132eaef

Please sign in to comment.