Пример #1
0
        public void CompileFailTest()
        {
            var expression = "1 / 0";
            var pResult = Parser.ParseAssignment(expression);
            Assert.AreEqual(expression, pResult.ExpressionString);
            Assert.AreEqual(expression, pResult.WholeCode);
            Assert.IsFalse(pResult.IsDeclaring);

            var cResult = RexCompileEngine.Compile(pResult);
            Assert.IsNotEmpty(cResult.Errors);
            CollectionAssert.Contains(cResult.Errors, "Division by constant zero\r");
        }
Пример #2
0
        public void ClassSetup()
        {
            if (Parser == null)
                Parser = new RexParser();

            RexHelper.Variables.Clear();
            var expression = "1+1";
            var pResult = Parser.ParseAssignment(expression);
            var cResult = RexCompileEngine.Compile(pResult);
            var output = Execute(cResult);
            Assert.AreEqual(2, output.Value);

            RexHelper.Variables.Clear();
            RexHelper.ClearOutput();
        }
Пример #3
0
        public void SimpleAssigmentTest()
        {
            var expression = "x = 1 + 1";
            var pResult = Parser.ParseAssignment(expression);
            Assert.AreEqual("1 + 1", pResult.ExpressionString);
            Assert.AreEqual(expression, pResult.WholeCode);
            Assert.IsTrue(pResult.IsDeclaring);

            var cResult = RexCompileEngine.Compile(pResult);
            Assert.AreEqual(pResult, cResult.Parse);
            Assert.IsNotNull(cResult.Assembly);

            var output = Execute(cResult);
            Assert.AreEqual(2, output.Value);
            Assert.AreEqual(2, RexHelper.Variables["x"].VarValue);
        }
Пример #4
0
        public void SimpleExpressionTest()
        {
            var expression = "1+1";
            var pResult = Parser.ParseAssignment(expression);
            Assert.AreEqual(expression, pResult.ExpressionString);
            Assert.AreEqual(expression, pResult.WholeCode);
            Assert.IsFalse(pResult.IsDeclaring);

            var cResult = RexCompileEngine.Compile(pResult);
            Assert.AreEqual(pResult, cResult.Parse);
            Assert.IsNotNull(cResult.Assembly);

            var output = Execute(cResult);
            Assert.AreEqual(2, output.Value);

            output = CompileAndRun("null");
            Assert.IsNull(output.Value);
        }
Пример #5
0
        public void AdvancedAssigmentTest()
        {
            var expr = "new Func<int, bool>(i => i < 5)";
            var expression = "x = " + expr;
            var pResult = Parser.ParseAssignment(expression);
            Assert.AreEqual(expr, pResult.ExpressionString);
            Assert.AreEqual(expression, pResult.WholeCode);
            Assert.IsTrue(pResult.IsDeclaring);

            var cResult = RexCompileEngine.Compile(pResult);
            Assert.AreEqual(pResult, cResult.Parse);
            Assert.IsNotNull(cResult.Assembly);

            var output = Execute(cResult);
            Assert.IsInstanceOf<Func<int, bool>>(output.Value);
            var func = RexHelper.Variables["x"].VarValue as Func<int, bool>;
            Assert.IsTrue(func(1));
            Assert.IsFalse(func(6));
        }
Пример #6
0
        public void LinqAssigmentTest()
        {
            var expr = "new[] { 1, 2, 3 }.Select(i => i)";
            var expression = "x = " + expr;
            var pResult = Parser.ParseAssignment(expression);
            Assert.AreEqual(expr, pResult.ExpressionString);
            Assert.AreEqual(expression, pResult.WholeCode);
            Assert.IsTrue(pResult.IsDeclaring);
            var cResult = RexCompileEngine.Compile(pResult);
            Assert.AreEqual(pResult, cResult.Parse);
            Assert.IsNotNull(cResult.Assembly);

            var output = Execute(cResult);
            Assert.IsInstanceOf<IEnumerable<int>>(output.Value);
            var list = RexHelper.Variables["x"].VarValue as IEnumerable<int>;
            Assert.AreEqual(new[] { 1, 2, 3 }, list);

            Assert.AreEqual(CompileAndRun("1+1").Value, 2);
        }
Пример #7
0
 public static DummyOutput CompileAndRun(string code, out Dictionary<MessageType, List<string>> messages)
 {
     var pResult = new RexParser().ParseAssignment(code);
     var cResult = RexCompileEngine.Compile(pResult);
     return RexHelper.Execute<DummyOutput>(cResult, out messages);
 }