/// <summary> /// Initialises the environment /// </summary> /// <returns></returns> static API.Context Initialise() { // Create new context API.Context context = new API.Context(); // Setting core global variables context.SetParameter("console", new API.Console()); context.SetParameter("phantom", new API.Phantom()); context.SetParameter("trifle", new API.Trifle()); context.SetParameter("window", new API.Window()); try { // Initialise host env context.RunScript(Resources.init, "init.js"); context.RunScript(Resources.trifle_Callback, "trifle.Callback.js"); context.RunScript(Resources.trifle_modules_WebPage, "trifle.modules.WebPage.js"); context.RunScript(Resources.trifle_modules_FileSystem, "trifle.modules.FileSystem.js"); context.RunScript(Resources.trifle_modules_System, "trifle.modules.System.js"); } catch (Exception ex) { API.Context.Handle(ex); } // Return context return context; }
/// <summary> /// Runs system tests /// </summary> static void Test() { Console.WriteLine(); Console.WriteLine("============================================"); Console.WriteLine("TrifleJS -- System Test"); Console.WriteLine("============================================"); Console.WriteLine(); // Initialize and start console read loop; using (Program.context = Initialise()) { try { // Load libs context.RunScript(Resources.jasmine, "lib/jasmine.js"); context.RunScript(Resources.jasmine_console, "lib/jasmine-console.js"); // Load Spec context.RunScript(Resources.spec_phantom, "spec/phantom.js"); context.RunScript(Resources.spec_webpage, "spec/webpage.js"); // Execute context.RunScript(Resources.run_jasmine, "run-jasmine.js"); // Keep running until told to stop // This is to make sure asynchronous code gets executed while (true) { API.Window.CheckTimers(); } } catch (Exception ex) { // Handle any exceptions API.Context.Handle(ex); } } }
/// <summary> /// Opens a javascript file and executes in host environment /// </summary> /// <param name="filename">Path to a javascript file</param> static void Open(string filename) { // Check file if (!File.Exists(filename)) { Console.Error.WriteLine(String.Format("File does not exist: {0}", filename)); return; } //Initialize a context using (Program.context = Initialise()) { // Set Library Path API.Phantom.LibraryPath = new FileInfo(filename).DirectoryName; try { // Run the script context.RunFile(filename); // Keep running until told to stop // This is to make sure asynchronous code gets executed while (true) { API.Window.CheckTimers(); } } catch (Exception ex) { // Handle any exceptions API.Context.Handle(ex); } } }
/// <summary> /// Run TrifleJS in interactive mode /// </summary> static void Interactive() { // Initialize and start console read loop; using (Program.context = Initialise()) { while (true) { Console.Write("triflejs> "); try { API.Console.log(context.Run(Console.ReadLine(), "REPL")); } catch (Exception ex) { API.Context.Handle(ex); } } } }
/// <summary> /// Runs unit tests /// </summary> static void UnitTest() { Console.WriteLine(); Console.WriteLine("============================================"); Console.WriteLine("TrifleJS -- Unit Tests"); Console.WriteLine("============================================"); Program.verbose = false; using (Program.context = Initialise()) { try { // Load libs context.RunScript(Resources.test_unit_tools, "test/unit/tools.js"); // Execute Specs context.RunScript(Resources.test_unit_spec_require, "test/unit/spec/require.js"); context.RunScript(Resources.test_unit_spec_webserver, "test/unit/spec/webserver.js"); //context.RunScript(Resources.test_unit_spec_fs, "test/unit/spec/fs.js"); //context.RunScript(Resources.test_unit_spec_webpage, "test/unit/spec/webpage.js"); // Finish context.RunScript(Resources.test_unit_finish, "test/unit/finish.js"); // Keep running until told to stop // This is to make sure asynchronous code gets executed while (true) { Program.DoEvents(); } } catch (Exception ex) { // Handle any exceptions API.Context.Handle(ex); } } }
/// <summary> /// Runs compatibility tests /// </summary> static void PhantomTest() { Console.WriteLine(); Console.WriteLine("============================================"); Console.WriteLine("TrifleJS -- Phantom Compatibility Test"); Console.WriteLine("============================================"); Console.WriteLine(); using (Program.context = Initialise()) { try { // Load libs context.RunScript(Resources.test_lib_jasmine, "test/lib/jasmine.js"); context.RunScript(Resources.test_lib_jasmine_console, "test/lib/jasmine-console.js"); context.RunScript(Resources.test_phantom_tools, "test/phantom/tools.js"); // Load Spec context.RunScript(Resources.test_phantom_spec_phantom, "test/phantom/phantom.js"); context.RunScript(Resources.test_phantom_spec_webserver, "test/phantom/webserver.js"); context.RunScript(Resources.test_phantom_spec_webserver, "test/phantom/webpage.js"); // Execute context.RunScript(Resources.test_run_jasmine, "test/phantom/run-jasmine.js"); // Keep running until told to stop // This is to make sure asynchronous code gets executed while (true) { Program.DoEvents(); } } catch (Exception ex) { // Handle any exceptions API.Context.Handle(ex); } } }