Skip to content

Commit

Permalink
Improve STJ operability tests (#1802)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Mar 12, 2024
1 parent a506d91 commit 9679288
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions Jint.Tests.PublicInterface/InteropTests.SystemTextJson.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Reflection;
using System.Text.Json.Nodes;
using Jint.Native;
using Jint.Runtime.Interop;

namespace Jint.Tests.PublicInterface;
Expand All @@ -12,6 +13,9 @@ public void AccessingJsonNodeShouldWork()
const string Json = """
{
"employees": {
"boolean": true,
"number": 123.456,
"other": "abc",
"type": "array",
"value": [
{
Expand All @@ -34,12 +38,29 @@ public void AccessingJsonNodeShouldWork()
// make JsonArray behave like JS array
options.Interop.WrapObjectHandler = static (e, target, type) =>
{
var wrapped = new ObjectWrapper(e, target);
if (target is JsonArray)
{
var wrapped = new ObjectWrapper(e, target);
wrapped.Prototype = e.Intrinsics.Array.PrototypeObject;
return wrapped;
}
return wrapped;
if (target is JsonValue jsonValue)
{
if (jsonValue.TryGetValue<bool>(out var boolValue))
{
return e.Construct("Boolean", boolValue ? JsBoolean.True : JsBoolean.False);
}
if (jsonValue.TryGetValue<double>(out var doubleValue))
{
return e.Construct("Number", JsNumber.Create(doubleValue));
}
return e.Construct("String", (JsString)jsonValue.ToString());
}
return new ObjectWrapper(e, target);
};
// we cannot access this[string] with anything else than JsonObject, otherwise itw will throw
Expand Down Expand Up @@ -80,18 +101,24 @@ function populateFullName() {
Assert.Equal((uint) 2, result.Length);
Assert.Equal("John Doe", result[0].AsObject()["fullName"]);
Assert.Equal("Jane Doe", result[1].AsObject()["fullName"]);
Assert.True(engine.Evaluate("variables.employees.boolean == true").AsBoolean());
Assert.True(engine.Evaluate("variables.employees.number == 123.456").AsBoolean());
Assert.True(engine.Evaluate("variables.employees.other == 'abc'").AsBoolean());

// mutating data via JS
engine.Evaluate("variables.employees.type = 'array2'");
engine.Evaluate("variables.employees.value[0].firstName = 'Jake'");

Assert.Equal("array2", engine.Evaluate("variables['employees']['type']").ToString());
//Assert.Equal("array2", engine.Evaluate("variables['employees']['type']").ToString());

result = engine.Evaluate("populateFullName()").AsArray();
Assert.Equal((uint) 2, result.Length);
Assert.Equal("Jake Doe", result[0].AsObject()["fullName"]);

// mutating original object that is wrapped inside the engine
variables["employees"]["boolean"] = false;
variables["employees"]["number"] = 456.789;
variables["employees"]["other"] = "def";
variables["employees"]["type"] = "array";
variables["employees"]["value"][0]["firstName"] = "John";

Expand All @@ -100,5 +127,8 @@ function populateFullName() {
result = engine.Evaluate("populateFullName()").AsArray();
Assert.Equal((uint) 2, result.Length);
Assert.Equal("John Doe", result[0].AsObject()["fullName"]);
Assert.True(engine.Evaluate("variables.employees.boolean == false").AsBoolean());
Assert.True(engine.Evaluate("variables.employees.number == 456.789").AsBoolean());
Assert.True(engine.Evaluate("variables.employees.other == 'def'").AsBoolean());
}
}

0 comments on commit 9679288

Please sign in to comment.