コード例 #1
0
        public void ConfigTest()
        {
            string file = "config { foo { a = \"test\"; b = \"bar\"; } }";

            ScriptEngine engine = new ScriptEngine();

            ConfigBuilder.Structure s = new ConfigBuilder.Structure();

            s.AddElement(new ConfigBuilder.KeyValueTagBuilder("foo", false));

            engine.RegisterBuilder(new ConfigBuilder("config", s, NewConfigBuilt));

            engine.Build(file);

            if (_results == null)
            {
                Assert.Fail("Was not returned a configuration.");
            }

            Dictionary<string, List<Token>> foo = (Dictionary<string, List<Token>>)_results["foo"][0];

            Chunk c = new Chunk(foo["b"], new Script(engine) {UseEngineGlobals = false});

            object result = c.Evaluate();

            Assert.AreEqual("bar", (string) result);
        }
コード例 #2
0
        public void StringReplaceTest()
        {
            ScriptEngine engine = new ScriptEngine();

            string code = "$foo = \"bar\"; $result = $foo.Replace($foo, \"what\");";

            Script script = engine.Build(code);

            script.Execute();

            Assert.AreEqual((string)script.Globals["result"], "what");
        }
コード例 #3
0
        public void StaticMethodInvoke()
        {
            string code = "return StaticClass.StaticMethod();";
            ScriptEngine engine = new ScriptEngine();

            engine.SecurityPolicy.AddType("StaticClass", typeof (StaticClass));

            object result = engine.Build(code).Execute();
            Assert.IsNotNull(result);

            Assert.AreEqual(result, 9001);
        }
コード例 #4
0
        public void TypeCastTest()
        {
            string code = "$foo = new Foo(); return $foo.SomeMethod(5, 5.24);";

            ScriptEngine engine = new ScriptEngine();

            engine.SecurityPolicy.AddType("Foo", typeof (Foo2));

            object result = engine.Build(code).Execute();

            Assert.IsNotNull(result);

            Assert.AreEqual(result, 10.24);
        }
コード例 #5
0
        public void TestArrayAccess()
        {
            string code = "$c = new Class(); $a = $c[\"test\"]; $b = $c.Sub[\"this\"];";
            ScriptEngine engine = new ScriptEngine();
            engine.SecurityPolicy.AddType("Class", typeof(ArrayAccessTestClass));

            Script script = engine.Build(code);
            object result = script.Execute();

            Assert.IsNull(result);

            Assert.AreEqual(script.Globals["a"], "test");
            Assert.AreEqual(script.Globals["b"], "this");
        }
コード例 #6
0
ファイル: UsingTest.cs プロジェクト: iammitch/Slimterpreter
        public void SimpleUsingTest()
        {
            string code = "@n = new DisposableObject(); using (foo = @n) { @foo.SomeMethod(); } return @n.Disposed;";

            ScriptEngine engine = new ScriptEngine();
            engine.SecurityPolicy.AddType("DisposableObject", typeof (DisposableObject));

            Script script = engine.Build(code);

            object result = script.Execute();

            Assert.IsNotNull(result);

            Assert.AreEqual(result, true);
        }
コード例 #7
0
        public void TestCustomTypes()
        {
            string code = "return new Foo().SomeMethod();";

            ScriptEngine engine = new ScriptEngine();

            engine.SecurityPolicy.AddType("Foo", typeof(Foo));

            Script script = engine.Build(code);

            object result = script.Execute();

            Assert.IsNotNull(result);

            Assert.AreEqual(47, result);
        }
コード例 #8
0
        public void ParamsTesting()
        {
            ScriptEngine e = new ScriptEngine();

            e.SecurityPolicy.AddType("Class", typeof (SomeClass));

            string script = "@foo = new Class(); return @foo.Foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);";

            Script s = e.Build(script);

            object result = s.Execute();

            Assert.IsNotNull(result);

            SomeClass c = new SomeClass();
            Assert.AreEqual(c.Foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), result);
        }
コード例 #9
0
        public void ExternalTest()
        {
            string code = "$foo = 1; return $foo.Foo();";

            ScriptEngine engine = new ScriptEngine();

            engine.Globals["foo"] = new SomeClass();

            Script script = engine.Build(code);

            try
            {
                object result = script.Execute();
                Assert.Fail("Failed to prevent global variable $foo being replaced with different type.");
            }
            catch (InvalidCastException e)
            {
                // Test Passed
            }
        }
コード例 #10
0
        public void PrivateMethodTesting()
        {
            string code = "@c = new SomeClass(); return @c.PrivateMethod();";

            ScriptEngine engine = new ScriptEngine();
            engine.SecurityPolicy.AddType("SomeClass", typeof (SomeClass));

            Script script = engine.Build(code);

            // The expected outcome is that we get can exception.
            try
            {
                object result = script.Execute();
                Assert.Fail();
            }
            catch (Exception e)
            {

            }
        }
コード例 #11
0
        public void FunctionConfigTest()
        {
            string file = "config { script \"foo\" { return \"foo\"; } script \"bar\" { return 1 + 1; } }";

            ScriptEngine engine = new ScriptEngine();

            ConfigBuilder.Structure s = new ConfigBuilder.Structure();

            s.AddElement(new ConfigBuilder.ScriptTagBuilder());

            engine.RegisterBuilder(new ConfigBuilder("config", s, NewConfigBuilt));

            engine.Build(file);

            if (_results == null)
            {
                Assert.Fail("ScriptEngine failed to build config.");
            }

            ConfigBuilder.ScriptBlock block = (ConfigBuilder.ScriptBlock) _results["script"][0];

            object result = block.Code.Evaluate(true);

            Assert.AreEqual(result, "foo");

            block = (ConfigBuilder.ScriptBlock)_results["script"][1];

            result = block.Code.Evaluate(true);

            if (!(result is Number))
            {
                Assert.Fail("Code block did not return Slimterpreter.Number, returned " + result.GetType().Name + " instead.");
            }

            Assert.AreEqual(2, (int)(Number)result);

            System.Console.WriteLine(block);
        }