Beispiel #1
0
        public void RequireFunctionIsAvailable()
        {
            // Arrange
            var withoutBuiltinLibrary = new NodeSettings {
                UseBuiltinLibrary = false
            };
            var withBuiltinLibrary = new NodeSettings {
                UseBuiltinLibrary = true
            };

            const string input = @"typeof require !== 'undefined';";

            // Act
            bool output1 = false;
            bool output2 = false;

            using (var jsEngine = new NodeJsEngine(withoutBuiltinLibrary))
            {
                output1 = jsEngine.Evaluate <bool>(input);
            }

            using (var jsEngine = new NodeJsEngine(withBuiltinLibrary))
            {
                output2 = jsEngine.Evaluate <bool>(input);
            }

            // Assert
            Assert.False(output1);
            Assert.True(output2);
        }
Beispiel #2
0
        public void FileReadingIsCorrect()
        {
            // Arrange
            const string input        = @"let fs = require('fs');
fs.readFileSync('Files/link.txt', 'utf8')";
            const string targetOutput = "http://www.panopticoncentral.net/2015/09/09/the-two-faces-of-jsrt-in-windows-10/";

            // Act
            string output = string.Empty;

            using (var jsEngine = new NodeJsEngine(new NodeSettings {
                UseBuiltinLibrary = true
            }))
            {
                output = jsEngine.Evaluate <string>(input);
            }

            // Assert
            Assert.Equal(targetOutput, output);
        }
Beispiel #3
0
        private void ScriptThread(object obj)
        {
            workQueue.Enqueue(InitializeJsGlobals);
            NodeJsEngine.Run(Debugger.IsAttached ? new string[] { "--inspect", "hello.espr" } : new string[] { "hello.espr" },
            (eng, ctx) =>
            {
                _engine = eng;
                _context = ctx;

                JsTypeDefinition jstypedef = new JsTypeDefinition("LibEspressoClass");
                jstypedef.AddMember(new JsMethodDefinition("LoadMainSrcFile", args =>
                {
                    args.SetResult(@"
function MainLoop() {
    LibEspresso.Next();
    setImmediate(MainLoop);
}
MainLoop();");
                }));

                jstypedef.AddMember(new JsMethodDefinition("Log", args =>
                {
                    Console.WriteLine(args.GetArgAsObject(0));
                }));

                jstypedef.AddMember(new JsMethodDefinition("Next", args =>
                {
                    //call from js server
                    System.Action work;
                    if (workQueue.TryDequeue(out work))
                    {
                        work();
                    }
                }));
                _context.RegisterTypeDefinition(jstypedef);
                _context.SetVariableFromAny("LibEspresso", _context.CreateWrapper(new object(), jstypedef));
            });
        }
Beispiel #4
0
        public void MappingTimeoutErrorDuringExecutionOfCodeIsCorrect()
        {
            // Arrange
            const string input = @"while (true);";

            JsTimeoutException exception = null;

            // Act
            using (var jsEngine = new NodeJsEngine(
                       new NodeSettings
            {
                TimeoutInterval = TimeSpan.FromMilliseconds(30)
            }
                       ))
            {
                try
                {
                    jsEngine.Execute(input, "infinite-loop.js");
                }
                catch (JsTimeoutException e)
                {
                    exception = e;
                }
            }

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Timeout error", exception.Category);
            Assert.Equal("Script execution exceeded timeout.", exception.Description);
            Assert.Empty(exception.Type);
            Assert.Empty(exception.DocumentName);
            Assert.Equal(0, exception.LineNumber);
            Assert.Equal(0, exception.ColumnNumber);
            Assert.Empty(exception.SourceFragment);
            Assert.Empty(exception.CallStack);
        }