Esempio n. 1
0
        public void ConstructorTest()
        {
            var context = new CSharp.Context();

            CommandObject.Attach(context);
            context.Execute("var cmd = new Command()");
            var cmd = context.Execute("cmd") as ICommand;

            Assert.IsNotNull(cmd);
        }
Esempio n. 2
0
        public void AddChildTest()
        {
            var context = new CSharp.Context();

            CommandObject.Attach(context);
            context.Execute(@"
                var cmd = new Command();
                cmd.addChild(new Command());");
            var cmd = (ICommand)context.Execute("cmd");

            Assert.AreEqual(1, cmd.Children.Count);
        }
Esempio n. 3
0
        public void GetIdTest()
        {
            var context = new CSharp.Context();
            var id = "test";

            CommandObject.Attach(context);
            context.Execute("var cmd = new Command()");

            var cmd = context.GetGlobalAs<ICommand>("cmd");
            cmd.Id = id;

            var result = context.Execute<string>("cmd.getId()");

            Assert.AreEqual(result, id);
        }
Esempio n. 4
0
        public override object Run(string script, string fileName)
        {
            Trace.Assert(!String.IsNullOrWhiteSpace(script));
            Trace.Assert(ctx != null);

            return(ctx.Execute(script));
        }
 public void Execute(string script)
 {
     if (BeforeExecute != null)
     {
         BeforeExecute(this, EventArgs.Empty);
     }
     context.Execute(script);
 }
Esempio n. 6
0
        private static void CompileCoffeeScriptUsingIronJs(string coffeeCompiler, string input)
        {
            context = new CSharp.Context();

            //Create the JS object
            var robotConstructor = Utils.CreateConstructor <Func <FunctionObject, CommonObject, double, CommonObject> >(context.Environment, 1, Construct);
            var httpConstructor  = Utils.CreateConstructor <Func <FunctionObject, CommonObject, string, CommonObject> >(context.Environment, 1, ConstructHttp);

            //setup the prototype (methods) on teh JS object
            var robotPrototype = context.Environment.NewObject();

            robotPrototype.Prototype = context.Environment.Prototypes.Object;
            var respond = Utils.CreateFunction <Action <CommonObject, FunctionObject> >(context.Environment, 0, RobotObject.Respond);
            var hear    = Utils.CreateFunction <Action <CommonObject, FunctionObject> >(context.Environment, 0, RobotObject.Hear);
            var send    = Utils.CreateFunction <Action <CommonObject> >(context.Environment, 0, RobotObject.Send);
            var get     = Utils.CreateFunction <Func <FunctionObject, CommonObject, FunctionObject, string> >(context.Environment, 0, HttpObject.HttpGet);
            var random  = Utils.CreateFunction <Func <CommonObject, CommonObject> >(context.Environment, 1, RobotObject.Random);

            var httpPrototype = context.Environment.NewObject();

            httpPrototype.Prototype = context.Environment.Prototypes.Object;
            httpPrototype.Put("get", get);
            httpConstructor.Put("prototype", httpPrototype, DescriptorAttrs.Immutable);

            //attach the methods
            robotPrototype.Put("respond", respond);
            robotPrototype.Put("send", send);
            robotPrototype.Put("hear", hear);
            robotPrototype.Put("http", httpConstructor);
            robotPrototype.Put("random", random);

            //attach the prototype
            robotConstructor.Put("prototype", robotPrototype, DescriptorAttrs.Immutable);
            context.SetGlobal("robot", robotConstructor);
            context.Execute(@"var bot = new robot();");

            var result  = context.Execute("2 + 2");
            var result2 = context.Execute("bot.match");

            //Setup and compile CoffeeScript
            context.Execute(coffeeCompiler);
            context.Execute("var compile = function (src) { return CoffeeScript.compile(src, { bare: true }); };");

            //Attach basic ping script
            AttachScript(input);
        }
Esempio n. 7
0
        public static void AttachScript(string input)
        {
            var compile = context.GetGlobalAs <FunctionObject>("compile");
            var result  = compile.Call(context.Globals, input);
            var output  = TypeConverter.ToString(result);

            context.Execute(@"function module() { }" + output + @" module.exports(bot);");
        }
Esempio n. 8
0
        public void EditorsTest()
        {
            var mock = new MainWindowMock();
            var context = new CSharp.Context();
            WindowObject.AttachToContext(context, mock);

            bool result = (bool)context.Execute("!!window.editors");
            Assert.IsTrue(result);
        }
Esempio n. 9
0
        public void ExitTest()
        {
            var mock = new MainWindowMock();
            var context = new CSharp.Context();
            WindowObject.AttachToContext(context, mock);

            context.Execute("window.exit()");

            Assert.AreEqual(mock.ExitCalled, 1);
        }
Esempio n. 10
0
        public void AddCommandTest()
        {
            var mock = new MainWindowMock();
            var context = new CSharp.Context();
            WindowObject.AttachToContext(context, mock);
            CommandObject.Attach(context);

            context.Execute("window.addCommand(new Command())");

            Assert.AreEqual(mock.Commands.Count, 1);
        }
Esempio n. 11
0
        public void NewEditorTest()
        {
            var mock = new EditorProviderMock();
            var context = new CSharp.Context();
            var provider = new EditorProviderObject(
                context.Environment,
                context.Environment.Prototypes.Object, mock);
            context.SetGlobal("editors", provider);

            // Create the editor
            context.Execute("var edit = editors.newEditor()");
            var edit = context.GetGlobal("edit");
            Assert.IsFalse(edit.IsUndefined);
            Assert.IsNotNull(edit.Clr);
            Assert.AreEqual(1, mock.Editors.Count);

            // Make sure it is an editor
            bool result = (bool)context.Execute("('setText' in edit)");
            Assert.IsTrue(result);
        }
Esempio n. 12
0
        public void EventTest()
        {
            var context = new CSharp.Context();

            CommandObject.Attach(context);
            context.Execute(@"
                var cmd = new Command();
                var raised = false;
                var data = null;
                cmd.addHandler('test', function(d) {
                    raised = true;
                    data = d;
                });");
            var cmd = (ICommand)context.Execute("cmd");
            var data = "Testing";
            cmd.RaiseEvent("test", data);

            Assert.IsTrue((bool)context.Execute("raised"));
            Assert.AreEqual(data, context.Execute("data"));
        }
Esempio n. 13
0
        public void GetTitleTest()
        {
            var mock = new MainWindowMock();
            var context = new CSharp.Context();
            WindowObject.AttachToContext(context, mock);

            string title = "Test";
            mock.Title = title;
            string result = context.Execute("window.getTitle()").ToString();

            Assert.AreEqual(result, title);
        }
Esempio n. 14
0
        public void TestEvents()
        {
            var context = new CSharp.Context();
            EventObject.Attach(context);

            bool result = (bool)context.Execute(
                @"
            var e = new EventObject();
            var triggered = false;
            e.addHandler('test', function() {
            triggered = true;
            });
            e.trigger('test');
            triggered;
            ");
            Assert.IsTrue(result);
        }
Esempio n. 15
0
        public static CSharp.Context Initialize()
        {
            if (!_isInitialized)
            {
                lock (SyncLock)
                {
                    if (!_isInitialized)
                    {
                        Context.Execute(CommonJS);
                        Context.SetGlobal("read",
                                          IronJS.Native.Utils.CreateFunction <Func <string, string> >(Context.Environment, 1, ReadSource));

                        _isInitialized = true;
                    }
                }
            }

            return(Context);
        }
Esempio n. 16
0
 public void InitializeLibrary(string libraryCode)
 {
     _engine = new CSharp.Context();
     _engine.Execute(libraryCode);
 }
Esempio n. 17
0
        public void RemoveApplicationCommandTest()
        {
            var mock = new MainWindowMock();
            var context = new CSharp.Context();
            WindowObject.AttachToContext(context, mock);
            CommandObject.Attach(context);

            context.Execute(@"
                var c = new Command();
                window.addApplicationCommand(c);
                window.removeApplicationCommand(c);");

            Assert.AreEqual(mock.ApplicationCommands.Count, 0);
        }
Esempio n. 18
0
 public object Run(string script)
 {
     return(engine.Execute(script));
 }
Esempio n. 19
0
        public void GetTextTest()
        {
            var context = new CSharp.Context();
            var text = "MyText";

            CommandObject.Attach(context);
            context.Execute("var cmd = new Command()");
            var cmd = context.GetGlobalAs<ICommand>("cmd");
            cmd.Text = text;

            var result = context.Execute("cmd.getText()");

            Assert.AreEqual("MyText", (string)result);
        }
 public void InitializeLibrary(string libraryCode)
 {
     _engine = new CSharp.Context();
     _engine.Execute(libraryCode);
 }
Esempio n. 21
0
        public void SetIdTest()
        {
            var context = new CSharp.Context();
            var id = "test";

            CommandObject.Attach(context);
            context.Execute(string.Format(@"
                var cmd = new Command();
                cmd.setId('{0}');", id));

            var cmd = context.GetGlobalAs<ICommand>("cmd");
            Assert.AreEqual(id, cmd.Id);
        }
Esempio n. 22
0
        private static void CompileCoffeeScriptUsingIronJs(string coffeeCompiler, string input)
        {
            context = new CSharp.Context();

            //Create the JS object
            var robotConstructor = Utils.CreateConstructor<Func<FunctionObject, CommonObject, double, CommonObject>>(context.Environment, 1, Construct);
            var httpConstructor = Utils.CreateConstructor<Func<FunctionObject, CommonObject, string, CommonObject>>(context.Environment, 1, ConstructHttp);

            //setup the prototype (methods) on teh JS object
            var robotPrototype = context.Environment.NewObject();
            robotPrototype.Prototype = context.Environment.Prototypes.Object;
            var respond = Utils.CreateFunction<Action<CommonObject, FunctionObject>>(context.Environment, 0, RobotObject.Respond);
            var hear = Utils.CreateFunction<Action<CommonObject, FunctionObject>>(context.Environment, 0, RobotObject.Hear);
            var send = Utils.CreateFunction<Action<CommonObject>>(context.Environment, 0, RobotObject.Send);
            var get = Utils.CreateFunction<Func<FunctionObject, CommonObject, FunctionObject, string>>(context.Environment, 0, HttpObject.HttpGet);
            var random = Utils.CreateFunction<Func<CommonObject, CommonObject>>(context.Environment, 1, RobotObject.Random);

            var httpPrototype = context.Environment.NewObject();
            httpPrototype.Prototype = context.Environment.Prototypes.Object;
            httpPrototype.Put("get", get);
            httpConstructor.Put("prototype", httpPrototype, DescriptorAttrs.Immutable);

            //attach the methods
            robotPrototype.Put("respond", respond);
            robotPrototype.Put("send", send);
            robotPrototype.Put("hear", hear);
            robotPrototype.Put("http", httpConstructor);
            robotPrototype.Put("random", random);

            //attach the prototype
            robotConstructor.Put("prototype", robotPrototype, DescriptorAttrs.Immutable);
            context.SetGlobal("robot", robotConstructor);
            context.Execute(@"var bot = new robot();");

            var result = context.Execute("2 + 2");
            var result2 = context.Execute("bot.match");
            //Setup and compile CoffeeScript
            context.Execute(coffeeCompiler);
            context.Execute("var compile = function (src) { return CoffeeScript.compile(src, { bare: true }); };");

            //Attach basic ping script
            AttachScript(input);
        }
Esempio n. 23
0
        public void TextChangedTest()
        {
            var context = new CSharp.Context();

            CommandObject.Attach(context);
            context.Execute("var cmd = new Command()");

            var command = context.GetGlobalAs<ICommand>("cmd");
            bool fired = false;
            command.TextChanged += delegate { fired = true; };

            context.Execute("cmd.setText('test')");

            Assert.IsTrue(fired);
        }
Esempio n. 24
0
        public void SetTextTest()
        {
            var context = new CSharp.Context();
            var text = "MyText";

            CommandObject.Attach(context);
            context.Execute(string.Format(@"
                var cmd = new Command();
                cmd.setText('{0}');", text));
            var cmd = context.GetGlobalAs<ICommand>("cmd");

            Assert.AreEqual(text, cmd.Text);
        }
Esempio n. 25
0
        public void Render(ViewContext viewContext, System.IO.TextWriter writer)
        {
            var script = new StringBuilder();
            try
            {
                this.Template.RenderScript("render_page", script);

                if (this.MasterView != null)
                {
                    MasterView.Template.RenderScript("render_layout", script);
                }
                else
                {
                    script.AppendLine("function render_layout(){ render_page(); }");
                }

                var context = new CSharp.Context();
                object model = viewContext.ViewData.Model;
                if (model is DynamicCommonObject)
                {
                    model = ((DynamicCommonObject)model).CommonObject;
                }
                if (model != null)
                {
                    context.SetGlobal("model", model);
                }
                else
                {
                    script.AppendLine("var model = {};");
                }

                // Passing Response directly to IronJS doesn't work right now
                // Apparently, .NET interop needs to be built yet.
                //context.SetGlobal("response", viewContext.HttpContext.Response);
                //context.SetGlobal("server", viewContext.HttpContext.Server);
                //context.SetGlobal("viewContext", viewContext);

                context.SetGlobal("response_write", Utils.CreateFunction<Action<string>>(context.Environment, 1, (obj) =>
                {
                    writer.Write(obj);
                }));
                context.SetGlobal("server_htmlencode", Utils.CreateFunction<Func<string, string>>(context.Environment, 1, (obj) =>
                {
                    return viewContext.HttpContext.Server.HtmlEncode(obj);
                }));
                script.AppendLine(@"
            var response = {
            Write: function(s){
            response_write(s);
            }
            };
            var server = {
            htmlEncode: function(s){
            return server_htmlencode(s);
            }
            };
            ");

                var html = new HtmlHelper(viewContext, new ViewDataContainer(viewContext.ViewData));

                context.SetGlobal("html_actionlink1", Utils.CreateFunction<Func<string, string, MvcHtmlString>>(context.Environment, 2, (linkText, actionName) =>
                {
                    return html.ActionLink(linkText, actionName);
                }));
                context.SetGlobal("html_actionlink2", Utils.CreateFunction<Func<string, string, string, MvcHtmlString>>(context.Environment, 3, (linkText, actionName, controllerName) =>
                {
                    return html.ActionLink(linkText, actionName, new { controllerName = controllerName });
                }));
                context.SetGlobal("html_actionlink3", Utils.CreateFunction<Func<string, string, string, CommonObject, MvcHtmlString>>(context.Environment, 4, (linkText, actionName, controllerName, obj) =>
                {
                    var routeValues = obj.ToRouteValueDictionary();
                    if (!routeValues.ContainsKey("controllerName"))
                    {
                        routeValues["controllerName"] = controllerName;
                    }
                    return html.ActionLink(linkText, actionName, new { controllerName = controllerName });
                }));

                script.AppendLine(@"
            var html = {
            actionLink: function(){
            if (arguments.length == 2) {
            return html_actionlink1(arguments[0], arguments[1]);
            } else if (arguments.length == 3) {
            return html_actionlink2(arguments[0], arguments[1], arguments[2]);
            } else if (arguments.length == 4) {
            return html_actionlink3(arguments[0], arguments[1], arguments[2], arguments[3]);
            }
            }
            };
            ");

                script.AppendLine("render_layout();");

                context.Execute(script.ToString());
            }
            catch (Exception ex)
            {
                viewContext.HttpContext.Response.ClearContent();
                writer.Write(ex.ToString().Replace("\n", "<br/>"));
                writer.Write("<br/><hr/><br/>");
                writer.Write(script.Replace(" ", "&nbsp;").Replace(">", "&gt;").Replace("<", "&lt;").Replace("\n", "<br/>"));
            }
        }
Esempio n. 26
0
        public void SetTitleTest()
        {
            var mock = new MainWindowMock();
            var context = new CSharp.Context();
            WindowObject.AttachToContext(context, mock);

            context.Execute("window.setTitle(\"Test\")");

            Assert.AreEqual(mock.Title, "Test");
        }