示例#1
0
        public void TestSetGetMemberInBody()
        {
            var engine = GetRuntime().GetEngine("Nova");
            var source =
                engine.CreateScriptSourceFromString("def test() { return @x + @y; }; def new() { @x = 12; @y = 10; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            var testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)),
                                          new List <NovaFunction> {
                scope.GetVariable("new")
            },
                                          new List <NovaFunction> {
                scope.GetVariable("test")
            });

            var nscope = engine.CreateScope();

            nscope.SetVariable("Test", testClass);

            var nsource = engine.CreateScriptSourceFromString("x = Test();  x.test;");

            Assert.That(nsource.Execute(nscope), Is.EqualTo(22));
        }
示例#2
0
        public void TestNovaObjectBinaryIronRuby()
        {
            var engine = GetRuntime().GetEngine("Nova");
            var source = engine.CreateScriptSourceFromString("def +(x) { return @x + x; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            dynamic testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)), new List <NovaFunction>(),
                                              new List <NovaFunction> {
                scope.GetVariable("+")
            });

            scope.RemoveVariable("+");
            scope.SetVariable("TestClass", testClass);

            var rubyengine = GetRuntime().GetEngine("IronRuby");
            var rubysource = rubyengine.CreateScriptSourceFromString("x = nova.TestClass; x.x = 17; x + 10;");

            var rubyscope = rubyengine.CreateScope();

            rubyscope.SetVariable("nova", scope);

            Assert.That(rubysource.Execute(rubyscope), Is.EqualTo(27));
        }
示例#3
0
        public void TestInvokeMemberPartialFunctionIVarTestCSharp()
        {
            var engine = GetRuntime().GetEngine("Nova");
            var source =
                engine.CreateScriptSourceFromString("def new(z) { @z = z; }; def test(x,y) { return x + y + @z; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            dynamic testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)),
                                              new List <NovaFunction> {
                scope.GetVariable("new")
            },
                                              new List <NovaFunction> {
                scope.GetVariable("test")
            });

            scope.RemoveVariable("new");
            scope.RemoveVariable("test");

            var x  = testClass(10);
            var px = x.test(10);

            Assert.That(px(17), Is.EqualTo(37));
        }
示例#4
0
        public void TestInvokeMemberSuperCSharp()
        {
            var engine  = GetRuntime().GetEngine("Nova");
            var source  = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");
            var source2 = engine.CreateScriptSourceFromString("def test(x) { super.test(x) + 15; };");

            var scope  = engine.CreateScope();
            var scope2 = engine.CreateScope();

            source.Execute(scope);
            source2.Execute(scope2);

            var testClassBase = new NovaClass("TestBase", global::Nova.Nova.Box(typeof(object)),
                                              new List <NovaFunction>(),
                                              new List <NovaFunction> {
                scope.GetVariable("test")
            });

            dynamic testClass = new NovaClass("Test", testClassBase, new List <NovaFunction>(),
                                              new List <NovaFunction> {
                scope2.GetVariable("test")
            });

            var x = testClass();

            Assert.That((int)x.test(10), Is.EqualTo(35));
        }
示例#5
0
        public void TestInvokeMemberIronRubyWithNewArg()
        {
            var engine = GetRuntime().GetEngine("Nova");
            var source =
                engine.CreateScriptSourceFromString("def new(z) { @y = z; }; def test(x) { return x + @y; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            dynamic testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)),
                                              new List <NovaFunction> {
                scope.GetVariable("new")
            },
                                              new List <NovaFunction> {
                scope.GetVariable("test")
            });

            scope.RemoveVariable("new");
            scope.RemoveVariable("test");
            scope.SetVariable("TestClass", testClass);

            var rubyengine = GetRuntime().GetEngine("IronRuby");
            var rubysource = rubyengine.CreateScriptSourceFromString("x = nova.TestClass(22); x.test(17);");

            var rubyscope = rubyengine.CreateScope();

            rubyscope.SetVariable("nova", scope);

            Assert.That(rubysource.Execute(rubyscope), Is.EqualTo(39));
        }
示例#6
0
        public void TestInvokeMemberMethodTable()
        {
            var engine  = GetRuntime().GetEngine("Nova");
            var source  = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");
            var source2 = engine.CreateScriptSourceFromString("def test(x,y) { return x + y; };");

            var scope = engine.CreateScope();

            source.Execute(scope);
            var scope2 = engine.CreateScope();

            source2.Execute(scope2);

            var testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)), new List <NovaFunction>(),
                                          new List <NovaFunction> {
                scope.GetVariable("test"),
                scope2.GetVariable("test")
            });

            var nscope = engine.CreateScope();

            nscope.SetVariable("Test", testClass);

            var nsource = engine.CreateScriptSourceFromString("x = Test();  x.test(17) + x.test(5,10);");

            Assert.That(nsource.Execute(nscope), Is.EqualTo(42));
        }
示例#7
0
        public void TestCustomOp()
        {
            var engine = GetRuntime().GetEngine("Nova");
            var source =
                engine.CreateScriptSourceFromString(
                    "def new(x) { @x = x; }; def <==>(other) { return ((@x + other.x) / (other.x/2)); };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            var testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)),
                                          new List <NovaFunction> {
                scope.GetVariable("new")
            },
                                          new List <NovaFunction> {
                scope.GetVariable("<==>")
            });

            scope.SetVariable("Test", testClass);
            scope.RemoveVariable("new");
            scope.RemoveVariable("<==>");
            source = engine.CreateScriptSourceFromString("x = Test(8); y = Test(8); x <==> y;");
            Assert.That(source.Execute(scope), Is.EqualTo(4));
        }
示例#8
0
        public void TestBoxExtensionIronRuby()
        {
            NovaClass strb      = global::Nova.Nova.Box(typeof(StringBuilder));
            var       engine    = GetRuntime().GetEngine("Nova");
            var       defsource =
                engine.CreateScriptSourceFromString(
                    "def AppendFromNova() { self.Append('Appending from Nova extension method!'); };");
            var defscope = engine.CreateScope();

            defsource.Execute(defscope);
            NovaClass.AddMethod(strb.InstanceMethods, defscope.GetVariable("AppendFromNova"));

            var scope = engine.CreateScope();

            scope.SetVariable("StringBuilder", strb);
            var str        = new StringBuilder("Hello world! ");
            var strx       = global::Nova.Nova.Box(str, new NovaScope(scope));
            var rubyengine = GetRuntime().GetEngine("IronRuby");
            var rubysource =
                rubyengine.CreateScriptSourceFromString("strx.AppendFromNova(); strx.ToString();");

            var rubyscope = rubyengine.CreateScope();

            rubyscope.SetVariable("strx", strx);
            Assert.That((string)rubysource.Execute(rubyscope),
                        Is.EqualTo("Hello world! Appending from Nova extension method!"));
        }
示例#9
0
        public void TestSingleton()
        {
            var expect = new NovaArray {
                null, 5
            };
            var engine = GetRuntime().GetEngine("Nova");
            var source = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            var testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)), new List <NovaFunction>(),
                                          new List <NovaFunction> {
                scope.GetVariable("test")
            });

            var nscope = engine.CreateScope();

            nscope.SetVariable("Test", testClass);

            var nsource =
                engine.CreateScriptSourceFromString(
                    "x = Test();  y = Test(); def x.single(y) { return y - 5; }; [y.single(10),x.single(10)];");

            Assert.That(nsource.Execute(nscope), Is.EqualTo(expect));
        }
示例#10
0
        public void TestNovaExportAttribute()
        {
            NovaClass klass  = global::Nova.Nova.Box(typeof(ExportHelper));
            var       engine = GetRuntime().GetEngine("Nova");
            var       source = engine.CreateScriptSourceFromString("x = Export(); x.test_me(13);");
            var       scope  = engine.CreateScope();

            scope.SetVariable(klass.Name, klass);
            Assert.That(source.Execute(scope), Is.EqualTo(40));
        }
示例#11
0
        public void TestCSharpContructorMap()
        {
            NovaClass strb      = global::Nova.Nova.Box(typeof(StringBuilder));
            var       engine    = GetRuntime().GetEngine("Nova");
            var       defsource =
                engine.CreateScriptSourceFromString(
                    "str = StringBuilder('Hello '); str.Append('from Nova!'); str.ToString();");
            var defscope = engine.CreateScope();

            defscope.SetVariable("StringBuilder", strb);
            Assert.That(defsource.Execute(defscope), Is.EqualTo("Hello from Nova!"));
        }
示例#12
0
        public void TestNovaDoNotExportAttribute()
        {
            NovaClass klass  = global::Nova.Nova.Box(typeof(ExportHelper));
            var       engine = GetRuntime().GetEngine("Nova");
            var       source = engine.CreateScriptSourceFromString("x = Export(); x.ShouldNotExport(10);");
            var       scope  = engine.CreateScope();

            scope.SetVariable(klass.Name, klass);

            object actual = source.Execute(scope);

            Assert.That(actual, Is.Null);
        }
示例#13
0
        public void TestNovaObjectBinaryCSharp()
        {
            var engine = GetRuntime().GetEngine("Nova");
            var source = engine.CreateScriptSourceFromString("def +(x) { return @x + x; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            dynamic testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)), new List <NovaFunction>(),
                                              new List <NovaFunction> {
                scope.GetVariable("+")
            });

            var x = testClass();

            x.x = 17;
            Assert.AreEqual(27, x + 10);
        }
示例#14
0
        public void TestNovaObjectBinary()
        {
            var engine = GetRuntime().GetEngine("Nova");
            var source = engine.CreateScriptSourceFromString("def +(x) { return @x + x; };");

            var scope = engine.CreateScope();

            source.Execute(scope);

            var testClass = new NovaClass("Test", global::Nova.Nova.Box(typeof(object)), new List <NovaFunction>(),
                                          new List <NovaFunction> {
                scope.GetVariable("+")
            });

            var nscope = engine.CreateScope();

            nscope.SetVariable("Test", testClass);

            var nsource = engine.CreateScriptSourceFromString("x = Test();  x.x = 17; x + 10;");

            Assert.That(nsource.Execute(nscope), Is.EqualTo(27));
        }
示例#15
0
        public void TestCSharpClassExtend()
        {
            NovaClass strb      = global::Nova.Nova.Box(typeof(StringBuilder));
            var       engine    = GetRuntime().GetEngine("Nova");
            var       defsource =
                engine.CreateScriptSourceFromString(
                    "def AppendFromNova() { self.Append('Appending from Nova extension method!'); };");
            var defscope = engine.CreateScope();

            defsource.Execute(defscope);
            NovaClass.AddMethod(strb.InstanceMethods, defscope.GetVariable("AppendFromNova"));

            var source =
                engine.CreateScriptSourceFromString(
                    "str.AppendFormat('{0} world! ','hello'); str.AppendFromNova(); str.ToString();");

            var scope = engine.CreateScope();

            scope.SetVariable("str", new StringBuilder());
            scope.SetVariable("StringBuilder", strb);
            Assert.That(source.Execute(scope),
                        Is.EqualTo("hello world! Appending from Nova extension method!"));
        }
示例#16
0
 public static dynamic Box(Type type)
 {
     return(NovaClass.BoxClass(type));
 }