Пример #1
0
 public void GivenLibraryLoaded_WhenCallFunctionPassingInArgument_ThenResultReturned()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         engine.Initialize();
         engine.LoadLibrary("function test(input) { return input * 2; }");
         var result = engine.CallFunction <int>("test", 10);
         result.ShouldEqual(20);
     }
 }
Пример #2
0
 public void GivenSyntaxError_WhenLoadLibrary_ThenExceptionThrown()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         engine.Initialize();
         Assert.Throws <ActiveScriptException>(
             () => engine.LoadLibrary("var !x = 1;")
             );
     }
 }
Пример #3
0
 public void GivenGlobalAddedToEngine_WhenScriptUsesGlobalFunction_ThenResultReturned()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         engine.Initialize();
         engine.AddGlobalValue("Test", new GlobalData());
         engine.LoadLibrary("function go() { return Test.hello('John'); }");
         var result = engine.CallFunction <string>("go");
         result.ShouldEqual("Hello, John");
     }
 }
Пример #4
0
 public void GivenScriptThatThrows_WhenExecuted_ThenExceptionThrown()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         engine.Initialize();
         engine.LoadLibrary("function fail() { return this.x.y; }");
         Assert.Throws <ActiveScriptException>(
             () => engine.CallFunction <object>("fail")
             );
     }
 }
Пример #5
0
 public void GivenCoffeeScriptCompilerLoaded_WhenCompileCalled_ThenJavaScriptReturned()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         engine.Initialize();
         engine.LoadLibrary(Scripts.Properties.Resources.coffeescript);
         engine.LoadLibrary("function compile(code) { return CoffeeScript.compile(code); }");
         var js = engine.CallFunction <string>("compile", "x = 1");
         js.ShouldEqual("(function() {\n  var x;\n\n  x = 1;\n\n}).call(this);\n");
     }
 }
Пример #6
0
        public void GivenGlobalAddedToEngine_WhenScriptUsesGlobalFunctionWithCallback_ThenCallbackInvoked()
        {
            using (var engine = new IEJavaScriptEngine())
            {
                engine.Initialize();
                engine.AddGlobalValue("Test", new GlobalData());
                engine.LoadLibrary(@"
function go() { 
    var result;
    Test.process(function(x) {
        result = x;
    });
    return result;
}");

                var result = engine.CallFunction <string>("go");
                result.ShouldEqual("done");
            }
        }