예제 #1
0
 public void GivenSyntaxError_WhenLoadLibrary_ThenExceptionThrown()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         Assert.Throws <ActiveScriptException>(
             () => engine.LoadLibrary("var !x = 1;")
             );
     }
 }
예제 #2
0
 public void GivenLibraryLoaded_WhenCallFunctionPassingInArgument_ThenResultReturned()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         engine.LoadLibrary("function test(input) { return input * 2; }");
         var result = engine.CallFunction <int>("test", 10);
         result.ShouldEqual(20);
     }
 }
예제 #3
0
 public void GivenGlobalAddedToEngine_WhenScriptUsesGlobalFunction_ThenResultReturned()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         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.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.LoadLibrary(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  x = 1;\n}).call(this);\n");
     }
 }
예제 #6
0
 public void GivenCoffeeScriptCompilerLoaded_WhenCompileInvalidCoffeeScript_ThenExceptionThrown()
 {
     using (var engine = new IEJavaScriptEngine())
     {
         engine.LoadLibrary(Properties.Resources.coffeescript);
         engine.LoadLibrary("function compile(code) { return CoffeeScript.compile(code); }");
         var exception = Assert.Throws <ActiveScriptException>(
             () => engine.CallFunction <string>("compile", "x = [1..")
             );
         exception.Message.ShouldEqual("unclosed [ on line 1");
     }
 }
예제 #7
0
        public void GivenGlobalAddedToEngine_WhenScriptUsesGlobalFunctionWithCallback_ThenCallbackInvoked()
        {
            using (var engine = new IEJavaScriptEngine())
            {
                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");
            }
        }