Skip to content

Commit

Permalink
Convert to using file-scoped namespaces (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Jul 29, 2024
1 parent 2f49299 commit 9079aef
Show file tree
Hide file tree
Showing 253 changed files with 43,390 additions and 43,650 deletions.
231 changes: 115 additions & 116 deletions Jint.Tests.PublicInterface/InteropTests.Dynamic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,151 +2,150 @@
using Jint.Native;
using Jint.Native.Symbol;

namespace Jint.Tests.PublicInterface
namespace Jint.Tests.PublicInterface;

public partial class InteropTests
{
public partial class InteropTests
[Fact]
public void CanAccessExpandoObject()
{
[Fact]
public void CanAccessExpandoObject()
{
var engine = new Engine();
dynamic expando = new ExpandoObject();
expando.Name = "test";
engine.SetValue("expando", expando);
Assert.Equal("test", engine.Evaluate("expando.Name").ToString());
}

[Fact]
public void DebugView()
{
// allows displaying different local variables under debugger

var engine = new Engine();
var boolNet = true;
var boolJint = (JsBoolean) boolNet;
var doubleNet = 12.34;
var doubleJint = (JsNumber) doubleNet;
var integerNet = 42;
var integerJint = (JsNumber) integerNet;
var stringNet = "ABC";
var stringJint = (JsString) stringNet;
var arrayNet = new[] { 1, 2, 3 };
var arrayListNet = new List<int> { 1, 2, 3 };
var arrayJint = new JsArray(engine, arrayNet.Select(x => (JsNumber) x).ToArray());

var objectNet = new Person { Name = "name", Age = 12 };
var objectJint = new JsObject(engine);
objectJint["name"] = "name";
objectJint["age"] = 12;
objectJint[GlobalSymbolRegistry.ToStringTag] = "Object";

var dictionaryNet = new Dictionary<JsValue, JsValue>();
dictionaryNet["name"] = "name";
dictionaryNet["age"] = 12;
dictionaryNet[GlobalSymbolRegistry.ToStringTag] = "Object";
}
var engine = new Engine();
dynamic expando = new ExpandoObject();
expando.Name = "test";
engine.SetValue("expando", expando);
Assert.Equal("test", engine.Evaluate("expando.Name").ToString());
}

[Fact]
public void CanAccessMemberNamedItemThroughExpando()
{
var parent = (IDictionary<string, object>) new ExpandoObject();
var child = (IDictionary<string, object>) new ExpandoObject();
var values = (IDictionary<string, object>) new ExpandoObject();
[Fact]
public void DebugView()
{
// allows displaying different local variables under debugger

var engine = new Engine();
var boolNet = true;
var boolJint = (JsBoolean) boolNet;
var doubleNet = 12.34;
var doubleJint = (JsNumber) doubleNet;
var integerNet = 42;
var integerJint = (JsNumber) integerNet;
var stringNet = "ABC";
var stringJint = (JsString) stringNet;
var arrayNet = new[] { 1, 2, 3 };
var arrayListNet = new List<int> { 1, 2, 3 };
var arrayJint = new JsArray(engine, arrayNet.Select(x => (JsNumber) x).ToArray());

var objectNet = new Person { Name = "name", Age = 12 };
var objectJint = new JsObject(engine);
objectJint["name"] = "name";
objectJint["age"] = 12;
objectJint[GlobalSymbolRegistry.ToStringTag] = "Object";

var dictionaryNet = new Dictionary<JsValue, JsValue>();
dictionaryNet["name"] = "name";
dictionaryNet["age"] = 12;
dictionaryNet[GlobalSymbolRegistry.ToStringTag] = "Object";
}

parent["child"] = child;
child["item"] = values;
values["title"] = "abc";
[Fact]
public void CanAccessMemberNamedItemThroughExpando()
{
var parent = (IDictionary<string, object>) new ExpandoObject();
var child = (IDictionary<string, object>) new ExpandoObject();
var values = (IDictionary<string, object>) new ExpandoObject();

_engine.SetValue("parent", parent);
Assert.Equal("abc", _engine.Evaluate("parent.child.item.title"));
}
parent["child"] = child;
child["item"] = values;
values["title"] = "abc";

[Fact]
public void ShouldForOfOnExpandoObject()
{
dynamic o = new ExpandoObject();
o.a = 1;
o.b = 2;
_engine.SetValue("parent", parent);
Assert.Equal("abc", _engine.Evaluate("parent.child.item.title"));
}

_engine.SetValue("dynamic", o);
[Fact]
public void ShouldForOfOnExpandoObject()
{
dynamic o = new ExpandoObject();
o.a = 1;
o.b = 2;

var result = _engine.Evaluate("var l = ''; for (var x of dynamic) l += x; return l;").AsString();
_engine.SetValue("dynamic", o);

Assert.Equal("a,1b,2", result);
}
var result = _engine.Evaluate("var l = ''; for (var x of dynamic) l += x; return l;").AsString();

[Fact]
public void ShouldConvertObjectInstanceToExpando()
{
_engine.Evaluate("var o = {a: 1, b: 'foo'}");
var result = _engine.GetValue("o");
Assert.Equal("a,1b,2", result);
}

dynamic value = result.ToObject();
[Fact]
public void ShouldConvertObjectInstanceToExpando()
{
_engine.Evaluate("var o = {a: 1, b: 'foo'}");
var result = _engine.GetValue("o");

Assert.Equal(1, value.a);
Assert.Equal("foo", value.b);
dynamic value = result.ToObject();

var dic = (IDictionary<string, object>) result.ToObject();
Assert.Equal(1, value.a);
Assert.Equal("foo", value.b);

Assert.Equal(1d, dic["a"]);
Assert.Equal("foo", dic["b"]);
}
var dic = (IDictionary<string, object>) result.ToObject();

[Fact]
public void CanAccessDynamicObject()
{
var test = new DynamicClass();
var engine = new Engine();

engine.SetValue("test", test);
Assert.Equal(1d, dic["a"]);
Assert.Equal("foo", dic["b"]);
}

Assert.Equal("a", engine.Evaluate("test.a").AsString());
Assert.Equal("b", engine.Evaluate("test.b").AsString());
[Fact]
public void CanAccessDynamicObject()
{
var test = new DynamicClass();
var engine = new Engine();

engine.Evaluate("test.a = 5; test.b = 10; test.Name = 'Jint'");
engine.SetValue("test", test);

Assert.Equal(5, engine.Evaluate("test.a").AsNumber());
Assert.Equal(10, engine.Evaluate("test.b").AsNumber());
Assert.Equal("a", engine.Evaluate("test.a").AsString());
Assert.Equal("b", engine.Evaluate("test.b").AsString());

Assert.Equal("Jint", engine.Evaluate("test.Name").AsString());
Assert.True(engine.Evaluate("test.ContainsKey('a')").AsBoolean());
Assert.True(engine.Evaluate("test.ContainsKey('b')").AsBoolean());
Assert.False(engine.Evaluate("test.ContainsKey('c')").AsBoolean());
}
engine.Evaluate("test.a = 5; test.b = 10; test.Name = 'Jint'");

private class DynamicClass : DynamicObject
{
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
Assert.Equal(5, engine.Evaluate("test.a").AsNumber());
Assert.Equal(10, engine.Evaluate("test.b").AsNumber());

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = binder.Name;
if (_properties.TryGetValue(binder.Name, out var value))
{
result = value;
}
Assert.Equal("Jint", engine.Evaluate("test.Name").AsString());
Assert.True(engine.Evaluate("test.ContainsKey('a')").AsBoolean());
Assert.True(engine.Evaluate("test.ContainsKey('b')").AsBoolean());
Assert.False(engine.Evaluate("test.ContainsKey('c')").AsBoolean());
}

return true;
}
private class DynamicClass : DynamicObject
{
private readonly Dictionary<string, object> _properties = new Dictionary<string, object>();

public override bool TrySetMember(SetMemberBinder binder, object value)
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = binder.Name;
if (_properties.TryGetValue(binder.Name, out var value))
{
_properties[binder.Name] = value;
return true;
result = value;
}

public string Name { get; set; }
return true;
}

public bool ContainsKey(string key)
{
return _properties.ContainsKey(key);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_properties[binder.Name] = value;
return true;
}

private class Person
public string Name { get; set; }

public bool ContainsKey(string key)
{
public string Name { get; set; }
public int Age { get; set; }
return _properties.ContainsKey(key);
}
}
}

private class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Loading

0 comments on commit 9079aef

Please sign in to comment.