Example #1
0
        public void StringInterpolationAllowsSlashes()
        {
            var context     = new Context();
            var code        = @"`This is a string such as http://www.google.com`";
            var stringValue = context.Eval(code);

            Assert.AreEqual("This is a string such as http://www.google.com", stringValue.Value);
        }
Example #2
0
        public void LastExpressionShouldKeepAliveInRootOfEval()
        {
            var context = new Context();

            var result = context.Eval("42");

            Assert.AreEqual(42, result.Value);
        }
Example #3
0
        public void StringAllowsSlashesDoubleQuoted()
        {
            var context     = new Context();
            var code        = @"""This is a string such as http://www.google.com""";
            var stringValue = context.Eval(code);

            Assert.AreEqual("This is a string such as http://www.google.com", stringValue.Value);
        }
Example #4
0
        public void StringInterpolationAllowsSubstititions()
        {
            var context     = new Context();
            var code        = @"var a=1234; `This is a string such as ${a}`";
            var stringValue = context.Eval(code);

            Assert.AreEqual("This is a string such as 1234", stringValue.Value);
        }
Example #5
0
        public void StringInterpolationAllowsStrings()
        {
            var context     = new Context();
            var code        = @"`This is a string`";
            var stringValue = context.Eval(code);

            Assert.AreEqual("This is a string", stringValue.Value);
        }
Example #6
0
        public void TwoArrayArgumentsShouldCauseException()
        {
            var testClass = new ClassWithTwoMethods();
            var context   = new Context();

            context.DefineVariable("test").Assign(testClass);

            context.Eval("test.Method1(1, [2], [3])");
        }
Example #7
0
        public void JSObjectMastHaveObjectTag()
        {
            var context = new Context();
            var code    = @"new Object().toString()";

            var stringValue = context.Eval(code);

            Assert.AreEqual("[object Object]", stringValue.Value);
        }
Example #8
0
        public void AliasForMember_ShouldProcessSymbolName()
        {
            var context = new Context();

            context.DefineConstructor(typeof(ClassWithAliasedMember));

            var name = context.Eval("ClassWithAliasedMember[Symbol.for('MySymbol')].name").Value.ToString();

            Assert.AreEqual("MySymbol", name);
        }
Example #9
0
        public void OverloadedMethods_2()
        {
            var context  = new Context();
            var instance = new Class();

            context.DefineVariable($"{nameof(instance)}").Assign(JSValue.Marshal(instance));
            var result = context.Eval($"{nameof(instance)}.{nameof(instance.Method)}(1, 2)");

            Assert.AreEqual(2, result.Value);
        }
Example #10
0
        public void AliasForMember_ShouldChangeNameOfFunction()
        {
            var context = new Context();

            context.DefineConstructor(typeof(ClassWithAliasedMember));

            var name = context.Eval("ClassWithAliasedMember.NameForJsSide.name").Value.ToString();

            Assert.AreEqual("NameForJsSide", name);
        }
Example #11
0
        public void CreateInstanceOfGenericType()
        {
            var context = new Context();

            context.DefineConstructor(typeof(List <>));

            var result = context.Eval("new (List(Number))()").Value;

            Assert.AreSame(typeof(List <Number>), result.GetType());
        }
Example #12
0
        public void ToStringShouldUseNameOfTypeIfToStringTagNotDefined()
        {
            var context = new Context();

            context.DefineVariable("test").Assign(new ExternTypeWithoutTag());
            var code = @"test.toString()";

            var stringValue = context.Eval(code);

            Assert.AreEqual($"[object {typeof(ExternTypeWithoutTag).Name}]", stringValue.Value);
        }
Example #13
0
        public void ToStringTagShouldWorkForExternTypes()
        {
            var context = new Context();

            context.DefineVariable("test").Assign(new ExternTypeWithTag());
            var code = @"test.toString()";

            var stringValue = context.Eval(code);

            Assert.AreEqual("[object Tag]", stringValue.Value);
        }
Example #14
0
        public void ToStringTagShouldBeSymbol()
        {
            var context = new Context();

            context.DefineVariable("test").Assign(new ExternTypeWithTag());
            var code = @"test[Symbol.toStringTag]";

            var stringValue = context.Eval(code);

            Assert.AreEqual("Tag", stringValue.Value);
        }
        public void TryToAddFunctionIntoListOfDelegates_Wrap()
        {
            var context = new Context();
            var list    = new List <Func <string, string> >();

            context.DefineVariable("list").Assign(JSValue.Wrap(list));

            context.Eval("list.Add(x => 'hi ' + x)");

            Assert.AreEqual("hi Test", list[0]("Test"));
        }
Example #16
0
        public void WriteInsideWithoShouldNotCreateNewField()
        {
            dynamic obj     = new ExpandoObject();
            var     context = new Context();

            context.DefineVariable("obj").Assign(JSValue.Marshal(obj));

            var value = context.Eval("with(obj) field = 'value'");

            Assert.IsFalse((obj as IDictionary <string, object>).ContainsKey("field"));
        }
        public void TryToAddFunctionIntoListOfDelegates_Marshal()
        {
            var context = new Context();
            var list    = new List <Func <string, string> >();

            context.DefineVariable("list").Assign(JSValue.Marshal(list));

            context.Eval("list.push(x => 'hi ' + x)"); // IList marshaled as NativeList with array-like interface

            Assert.AreEqual("hi Test", list[0]("Test"));
        }
Example #18
0
        public void OneSimpleArgumentShouldNotBeWrappedIntoArray()
        {
            var testClass = new ClassWithTwoMethods();
            var context   = new Context();

            context.DefineVariable("test").Assign(testClass);

            context.Eval("test.Method2(1, 2)");

            Assert.AreEqual(1, testClass.Arguments[0][0]);
            Assert.AreEqual(null, testClass.Arguments[0][1]);
        }
Example #19
0
        public void OneArrayArgumentShouldBeProcessedCorrectly()
        {
            var testClass = new ClassWithTwoMethods();
            var context   = new Context();

            context.DefineVariable("test").Assign(testClass);

            context.Eval("test.Method2(1, [2])");

            Assert.AreEqual(1, testClass.Arguments[0][0]);
            Assert.AreEqual(2, (testClass.Arguments[0][1] as int[])[0]);
        }
Example #20
0
        public void WritePropertiesOfDynamicobject()
        {
            dynamic obj     = new ExpandoObject();
            var     context = new Context();

            context.DefineVariable("obj").Assign(JSValue.Marshal(obj));

            var value = context.Eval("obj.field = 'value'");

            Assert.IsInstanceOfType(obj.field, typeof(string));
            Assert.AreEqual("value", obj.field);
        }
Example #21
0
        public void ReadPropertiesOfDynamicobject()
        {
            dynamic obj = new ExpandoObject();

            obj.field = "value";
            var context = new Context();

            context.DefineVariable("obj").Assign(JSValue.Marshal(obj));

            var value = context.Eval("obj.field");

            Assert.AreEqual(JSValueType.String, value.ValueType);
            Assert.AreEqual("value", value.Value);
        }
Example #22
0
        public void SimpleArgumentsShouldBeWrappedIntoArray()
        {
            var testClass = new ClassWithTwoMethods();
            var context   = new Context();

            context.DefineVariable("test").Assign(testClass);

            context.Eval("test.Method1(1, 2, 3, 4, 5)");

            Assert.AreEqual(1, testClass.Arguments[0][0]);
            Assert.AreEqual(2, (testClass.Arguments[0][1] as int[])[0]);
            Assert.AreEqual(3, (testClass.Arguments[0][1] as int[])[1]);
            Assert.AreEqual(4, (testClass.Arguments[0][1] as int[])[2]);
            Assert.AreEqual(5, (testClass.Arguments[0][1] as int[])[3]);
        }
Example #23
0
        public void CompletedTaskShouldBeReturnedAsResolvedPromise()
        {
            var context = new Context();

            context.DefineVariable("testAwaitable").Assign(JSValue.Marshal(new Func <string, Task <string> >((input) =>
            {
                return(Task.FromResult(input));
            })));

            context.Eval("function testAsync() { return testAwaitable('test'); }");

            var task = context.GetVariable("testAsync").As <Function>().Call(new Arguments()).As <Promise>().Task;

            Assert.AreEqual("test", task.GetAwaiter().GetResult().ToString());
        }
Example #24
0
        public void AliasForMember_ShouldChangePropertyName()
        {
            var context = new Context();

            context.DefineConstructor(typeof(ClassWithAliasedMember));

            var names = context.Eval("Object.getOwnPropertyNames(ClassWithAliasedMember)").Value as NiL.JS.BaseLibrary.Array;

            Assert.IsTrue((bool)NiL.JS.BaseLibrary.Array.includes(names, new Arguments {
                "NameForJsSide"
            }));
            Assert.IsFalse((bool)NiL.JS.BaseLibrary.Array.includes(names, new Arguments {
                "NameForDotNetSide"
            }));
        }
Example #25
0
        public async Task RejectedPromiseShouldBeReturnedAsFaultedTask()
        {
            var context = new Context();

            context.DefineVariable("testAwaitable").Assign(JSValue.Marshal(new Func <string, Task <string> >(async(input) =>
            {
                await Task.Delay(500);

                throw new Exception();
            })));

            context.Eval("let result = null; async function testAsync() { result = await testAwaitable('test'); }");

            var task = context.GetVariable("testAsync").As <Function>().Call(new Arguments()).As <Promise>().Task;
            await Assert.ThrowsExceptionAsync <AggregateException>(async() =>
            {
                await task;
            });
        }
Example #26
0
        public void AsyncFunctionWithNestedLiteralContextsShouldWorkAsExpected()
        {
            var script  = Script.Parse(@"
async function test() {
    const a = await Promise.resolve(123);
    if (true) {
        const b = await Promise.resolve(456);
        return b + a;
    }
}
");
            var context = new Context();

            script.Evaluate(context);

            var promiseValue = context.Eval("test()");
            var promise      = promiseValue.Value as Promise;

            Assert.AreEqual(579, promise.Task.GetAwaiter().GetResult());
        }
Example #27
0
        public async Task AsyncMethodShouldReturnValue()
        {
            // Arrange
            var request = "value";

            var script  = $@"
async function script() {{
    async function demo() {{
        let request = '{request}';
        let response = await fetch(request);
        check(response);
        return response;
    }}

    let result = await demo();
    check(result);
    return result;
}}
";
            var context = new Context();

            context
            .DefineVariable("fetch")
            .Assign(JSValue.Marshal(new Func <string, Task <string> >(FetchAsync)));

            context
            .DefineVariable("check")
            .Assign(JSValue.Marshal(new Action <string>((value) => { Assert.AreEqual(request, value); })));

            context.Eval(script);

            // Act
            var result = await context.GetVariable("script")
                         .As <Function>()
                         .Call(new Arguments())
                         .As <Promise>()
                         .Task;

            // Assert
            Assert.AreEqual(request, result.Value);
        }