Beispiel #1
0
        public void TestHashIndexExpressions()
        {
            var tests = new ConditonalTestData[] {
                new ConditonalTestData {
                    input = @"{""foo"": 5}[""foo""]", expected = new Integer_AST(5)
                },
                new ConditonalTestData {
                    input = @"{""foo"": 5}[""bar""]", expected = Evaluator.null_AST
                },                                                                                                   //anonymous types could not handle the varying 'type' for expected field

                new ConditonalTestData {
                    input = @"let key = ""foo""; {""foo"": 5}[key]", expected = new Integer_AST(5)
                },                                                                                                                  //anonymous types could not handle the varying 'type' for expected field
                new ConditonalTestData {
                    input = @"{}[""foo""]", expected = Evaluator.null_AST
                },                                                                                         //anonymous types could not handle the varying 'type' for expected field

                new ConditonalTestData {
                    input = @"{5: 5}[5]", expected = new Integer_AST(5)
                },
                new ConditonalTestData {
                    input = @"{true: 5}[true]", expected = new Integer_AST(5)
                },
                new ConditonalTestData {
                    input = @"{false: 5}[false]", expected = new Integer_AST(5)
                },
            };

            foreach (var test in tests)
            {
                var evaluated = testEval(test.input);
                switch (test.expected)
                {
                case Integer_AST i:
                    testIntegerObject(evaluated, i);
                    break;

                case Null_AST null_:
                    testNullObject(evaluated, null_);
                    break;

                default:
                    break;
                }
            }
        }
Beispiel #2
0
        public void TestIfElseExpressions()
        {
            var tests = new ConditonalTestData[] {
                new ConditonalTestData {
                    input = "if (true) { 10 }", expected = new Integer_AST(10)
                },
                new ConditonalTestData {
                    input = "if (false) { 10 }", expected = Evaluator.null_AST
                },                                                                                              //anonymous types could not handle the varying 'type' for expected field
                new ConditonalTestData {
                    input = "if (1) { 10 }", expected = new Integer_AST(10)
                },
                new ConditonalTestData {
                    input = "if (1<2) { 10 }", expected = new Integer_AST(10)
                },
                new ConditonalTestData {
                    input = "if (1>2) { 10 }", expected = Evaluator.null_AST
                },
                new ConditonalTestData {
                    input = "if (1<2) { 10 } else {20}", expected = new Integer_AST(10)
                },
                new ConditonalTestData {
                    input = "if (1>2) { 10 } else {20}", expected = new Integer_AST(20)
                },
            };

            foreach (var test in tests)
            {
                var evaluated = testEval(test.input);
                if (test.expected is Integer_AST)
                {
                    testIntegerObject(evaluated, test.expected);
                }
                else
                {
                    testNullObject(evaluated, test.expected);
                }
            }
        }