public static void It(string message, Action tests) { try { StaticHelper.Logs.Clear(); Assert.message = "It " + message; tests(); RuntimeExecution.ContinueUntilEnd(); } finally { Assert.message = null; } }
public void ObjectHosting() { It("should return different instances on different execution thread", () => { TestObject obj = new TestObject(); RuntimeObject v0 = RuntimeRealm.Current.ResolveRuntimeObject(obj); RuntimeExecution.CreateWorkerThread(parentRealm => { Assume.That(parentRealm, Is.Not.EqualTo(RuntimeRealm.Current), "realm should be different"); RuntimeObject v1 = RuntimeRealm.Current.ResolveRuntimeObject(obj); That(v0 != v1, "returned object should be different"); That(v0.Realm, Is.EqualTo(parentRealm)); That(v1.Realm, Is.EqualTo(RuntimeRealm.Current)); That(v0.GetPrototypeOf() != v1.GetPrototypeOf(), "the prototype of returned object should be different"); That(v0.GetPrototypeOf().Realm, Is.EqualTo(parentRealm)); That(v1.GetPrototypeOf().Realm, Is.EqualTo(RuntimeRealm.Current)); That(v0.GetPrototypeOf().GetPrototypeOf() != v1.GetPrototypeOf().GetPrototypeOf(), "all prototype ancestors of returned object should be different"); That(v0["MyMethod"], Is.TypeOf("function")); That(v0["MyMethod"], Is.Not.EqualTo(v1["MyMethod"]), "property value of object type should be different"); }, true).Thread.Join(); }); It("should return different instances on different realm of the same execution thread", () => { TestObject obj = new TestObject(); RuntimeObject v0 = RuntimeRealm.Current.ResolveRuntimeObject(obj); RuntimeRealm parentRealm = RuntimeRealm.Current; RuntimeRealm other = new RuntimeRealm(); other.Enqueue(() => { Assume.That(RuntimeRealm.Current, Is.Not.EqualTo(parentRealm), "realm should be different"); Assume.That(RuntimeRealm.Current, Is.EqualTo(other)); RuntimeObject v1 = RuntimeRealm.Current.ResolveRuntimeObject(obj); That(v0 != v1, "returned object should be different"); That(v0.Realm, Is.EqualTo(parentRealm)); That(v1.Realm, Is.EqualTo(RuntimeRealm.Current)); That(v0.GetPrototypeOf() != v1.GetPrototypeOf(), "the prototype of returned object should be different"); That(v0.GetPrototypeOf().Realm, Is.EqualTo(parentRealm)); That(v1.GetPrototypeOf().Realm, Is.EqualTo(RuntimeRealm.Current)); That(v0.GetPrototypeOf().GetPrototypeOf() != v1.GetPrototypeOf().GetPrototypeOf(), "all prototype ancestors of returned object should be different"); That(v0["MyMethod"], Is.TypeOf("function")); That(v0["MyMethod"], Is.Not.EqualTo(v1["MyMethod"]), "property value of object type should be different"); }); RuntimeExecution.ContinueUntilEnd(); }); It("should expose native properties as getters and setters on protoype", () => { TestObject obj = new TestObject(); EcmaValue value = new EcmaValue(obj); value["Value"] = 1; That(value["Value"], Is.EqualTo(1)); That(obj.Value, Is.EqualTo(1), "Value should be reflected on native object"); That(() => value["ReadOnlyValue"] = 2, Throws.Nothing, "Setting a readonly property does not throw exception"); That(value["ReadOnlyValue"], Is.EqualTo(1), "Value should not be changed"); RuntimeObject testObjectPrototype = value.ToObject().GetPrototypeOf(); That(testObjectPrototype.GetOwnProperty("Value"), Is.Not.EqualTo(EcmaValue.Undefined), "Property should be defined on the reflected prototype object"); That(testObjectPrototype.GetOwnProperty("Value").Get, Is.Not.EqualTo(EcmaValue.Undefined), "Property should be defined as getter/setter"); That(testObjectPrototype.GetOwnProperty("ReadOnlyValue").Set, Is.Undefined, "Readonly property should have no setter"); }); It("should expose IList as an Array exotic object", () => { EcmaValue list = new EcmaValue(new List <object> { 1, 2, 3, new EcmaObject() }); That(list["length"], Is.EqualTo(4)); That(list.ToString(), Is.EqualTo("1,2,3,[object Object]")); That(Global.Json.Invoke("stringify", list), Is.EqualTo("[1,2,3,{}]")); }); It("should expose IDictionary as an ordinary object for valid EcmaPropertyKey", () => { Hashtable ht = new Hashtable(); EcmaValue obj = new EcmaValue(ht); obj["prop"] = 1; That(obj["prop"], Is.EqualTo(1)); That(Global.Json.Invoke("stringify", obj), Is.EqualTo("{\"prop\":1}")); }); }