public void MethodCall() { var engine = new ScriptEngine(); // Create a new object. dynamic obj = new DynamicMetaObjectWrapper((ObjectInstance)engine.Evaluate("({ f: function(a) { return a + 5; } })")); // Check calling the method works. Assert.AreEqual(8, obj.f(3)); Assert.AreEqual(9, obj.f(4, 5)); Assert.AreEqual(double.NaN, obj.f()); // Attempting to call an invalid function should throw an exception. TestUtils.ExpectException <Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(() => obj.g(3)); }
public void MethodCall() { var engine = new ScriptEngine(); // Create a new object. dynamic obj = new DynamicMetaObjectWrapper((ObjectInstance)engine.Evaluate("({ f: function(a) { return a + 5; } })")); // Check calling the method works. Assert.AreEqual(8, obj.f(3)); Assert.AreEqual(9, obj.f(4, 5)); Assert.AreEqual(double.NaN, obj.f()); // Attempting to call an invalid function should throw an exception. ExpectException<Microsoft.CSharp.RuntimeBinder.RuntimeBinderException>(() => obj.g(3)); }
public void PropertyAccess() { var engine = new ScriptEngine(); // Create a new object. dynamic obj = new DynamicMetaObjectWrapper((ObjectInstance)engine.Evaluate("({ one: 1, two: 2 })")); // Check property access works. Assert.AreEqual(1, obj.one); Assert.AreEqual(2, obj.two); // Check property setters work. obj.one = 5; Assert.AreEqual(5, obj.one); obj.three = 3; Assert.AreEqual(3, obj.three); // Undefined is returned if the property doesn't exist. Assert.AreEqual(Undefined.Value, obj.four); // Check indexer getters work. Assert.AreEqual(5, obj["one"]); Assert.AreEqual(2, obj["two"]); // Check indexer setters work. obj["one"] = 6; Assert.AreEqual(6, obj["one"]); obj["four"] = 7; Assert.AreEqual(7, obj["four"]); // Now check an array works dynamic array = new DynamicMetaObjectWrapper((ObjectInstance)engine.Evaluate("[9, 10]")); // Check indexer getters work. Assert.AreEqual(9, array[0]); Assert.AreEqual(10, array[1]); // Check indexer setters work. array[0] = 4; Assert.AreEqual(4, array[0]); array[2] = 5; Assert.AreEqual(5, array[2]); // Undefined is returned if the property doesn't exist. Assert.AreEqual(Undefined.Value, array[3]); }