Example #1
0
        public async Task TestScopeNode()
        {
            ScriptTree scriptTree = new ScriptTree("foo.bar;");
            Scope      scope      = new Scope();
            Scope      innerScope = new Scope();

            scope.Set("foo", innerScope);
            innerScope.Set("bar", 5);
            DynamicReturnValue value = await scriptTree.Evaluate(scope);

            Console.WriteLine($"result {value.GetValue<int>()}");
            Assert.Equal(5, value.GetValue <int>());

            innerScope.Set("bar", false);
            value = await scriptTree.Evaluate(scope);

            Assert.Equal(false, value.GetValue <bool>());

            scriptTree = new ScriptTree("foo.baz.nitch");
            Scope lastScope = new Scope();

            lastScope.Set("nitch", 101);
            innerScope.Set("baz", lastScope);
            value = await scriptTree.Evaluate(scope);

            Assert.Equal(101, value.GetValue <int>());
        }
Example #2
0
        public async Task TestForStatementNode()
        {
            Scope        scope      = new Scope();
            Scope        innerScope = new Scope();
            List <float> loopTest   = new List <float>();

            for (float i = 1.0f; i <= 9.0f; i++)
            {
                loopTest.Add(i);
            }
            scope.Set("loopTest", loopTest);
            scope.Set("test", innerScope);

            ScriptTree scriptTree = new ScriptTree(@"
                test.foo = 1.0;
                foo = 1.0;
                for (var of loopTest) {
                    test.foo += var;
                    foo = var;
                }
            ");

            await scriptTree.Evaluate(scope);

            Assert.Equal(46.0f, innerScope.Get("foo"));
            Assert.Equal(9.0f, scope.Get("foo"));
        }
Example #3
0
        public async Task TestStringLiteral()
        {
            ScriptTree         scriptTree = new ScriptTree("'testing'");
            DynamicReturnValue value      = await scriptTree.Evaluate(new Scope());

            Assert.Equal("testing", value.GetValue <string>());

            scriptTree = new ScriptTree("\"testing double quotes\"");
            value      = await scriptTree.Evaluate(new Scope());

            Assert.Equal("testing double quotes", value.GetValue <string>());
        }
Example #4
0
        public async Task TestBooleanLiteral()
        {
            ScriptTree         scriptTree = new ScriptTree("true");
            DynamicReturnValue value      = await scriptTree.Evaluate(new Scope());

            Assert.Equal(true, value.GetValue <bool>());

            scriptTree = new ScriptTree("false");
            value      = await scriptTree.Evaluate(new Scope());

            Assert.Equal(false, value.GetValue <bool>());
        }
Example #5
0
        public async Task TestNumberLiteral()
        {
            ScriptTree         scriptTree = new ScriptTree("5.52");
            DynamicReturnValue value      = await scriptTree.Evaluate(new Scope());

            Assert.Equal(5.52f, value.GetValue <float>());

            scriptTree = new ScriptTree("5");
            value      = await scriptTree.Evaluate(new Scope());

            Assert.Equal(5, value.GetValue <int>());
        }
Example #6
0
        public async Task TestAsyncFunctionCallNode()
        {
            Function   function   = new Function(typeof(FunctionTest), "MyTestAsyncFunction");
            ScriptTree scriptTree = new ScriptTree(@"test_function(words)");
            Scope      scope      = new Scope();
            string     words      = "pancakes are super tasty.";

            scope.Set("words", words);
            scope.Set("test_function", function);
            DynamicReturnValue r = await scriptTree.Evaluate(scope);

            Assert.Equal(words, r.GetValue <string>());
        }
Example #7
0
        public async Task TestFunctionMultipleArgumentsCallNode()
        {
            Function   function   = new Function(typeof(FunctionTest), "MyTestFunctionWithMultipleArguments");
            ScriptTree scriptTree = new ScriptTree(@"test_function(words, 'Cheese is also rather tasty.')");
            Scope      scope      = new Scope();
            string     words      = "pancakes are tasty.";

            scope.Set("words", words);
            scope.Set("test_function", function);
            DynamicReturnValue r = await scriptTree.Evaluate(scope);

            Assert.Equal($"{words} Cheese is also rather tasty.", r.GetValue <string>());
        }
Example #8
0
        public async Task TestArrayInitialization()
        {
            ScriptTree scriptTree = new ScriptTree("array = [0, 9, 200, 30];");
            Scope      scope      = new Scope();
            await scriptTree.Evaluate(scope);

            var array = scope.Get <List <int> >("array");

            Assert.Equal(4, array.Count);
            Assert.Equal(0, array[0]);
            Assert.Equal(9, array[1]);
            Assert.Equal(200, array[2]);
            Assert.Equal(30, array[3]);
        }
Example #9
0
        public async Task TestForStatementNodeWithInlineArray()
        {
            ScriptTree scriptTree = new ScriptTree(@"
                test = 0;
                for (var of [1, 2, 3, 4, 5, 6]) {
                    test += var;
                }
            ");

            Scope scope = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(21, scope.Get("test"));
        }
Example #10
0
        public async Task TestArithmeticNode()
        {
            ScriptTree scriptTree = new ScriptTree("foo = 150 + 5.0");
            Scope      scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(155.0f, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 + 5");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(155, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 - 5.0");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(145f, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 - 5");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(145, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 * 5.0");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(750f, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 * 5");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(750, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 / 5.0");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(30f, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 / 5");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(30, scope.Get("foo"));
        }
Example #11
0
        public async Task TestRootScopeNode()
        {
            ScriptTree scriptTree = new ScriptTree("foo");
            Scope      scope      = new Scope();

            scope.Set("foo", 5);
            DynamicReturnValue value = await scriptTree.Evaluate(scope);

            Assert.Equal(5, value.GetValue <int>());

            scriptTree = new ScriptTree("foo");
            scope.Set("foo", false);
            value = await scriptTree.Evaluate(scope);

            Assert.Equal(false, value.GetValue <bool>());
        }
Example #12
0
        public async Task TestIfStatementNode()
        {
            ScriptTree scriptTree = new ScriptTree(@"
            if (false) {
                result = false;
            } else if (1 > 2) {
                result = 'impossibru!';
            } else if (2 > 1) {
                result = true;
            } else {
                result = 'not right';
            }
            ");
            Scope      scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Console.WriteLine($"Result it Type {scope.GetType("result")}");
            Assert.Equal(true, scope.Get("result"));
        }
Example #13
0
        public async Task TestArithmeticOrderOfOperations()
        {
            ScriptTree scriptTree = new ScriptTree("foo = 1 + (5 + 1) * 2");
            Scope      scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(12.0f, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 150 + 5.0 / 5 + 1");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(152.0f, scope.Get("foo"));

            scriptTree = new ScriptTree("foo = 5 * 5 + 2 * 5");
            scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(35, scope.Get("foo"));
        }
Example #14
0
        public async Task TestScopeAssignmentNode()
        {
            ScriptTree scriptTree = new ScriptTree("foo = 150");
            Scope      scope      = new Scope();
            await scriptTree.Evaluate(scope);

            Assert.Equal(150, scope.Get("foo"));

            scriptTree = new ScriptTree("bar = 1.5;bar;");
            DynamicReturnValue value = await scriptTree.Evaluate(scope);

            Assert.Equal(1.5f, value.GetValue <float>());

            scope      = new Scope();
            scriptTree = new ScriptTree("foo.bar = 7.5;foo.bar;");
            Scope innerScope = new Scope();

            scope.Set("foo", innerScope);
            value = await scriptTree.Evaluate(scope);

            Assert.Equal(7.5f, value.GetValue <float>());
        }