Ejemplo n.º 1
0
            public void VisitBooleanConstantExpr(bool value)
            {
                var target = new EvaluateVisitor();

                var expression = new ConstantExpr(value);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual(value.ToString(), actual.ToString());
            }
Ejemplo n.º 2
0
            public void EqualsTest(object a, object b, bool expected)
            {
                var target = new EvaluateVisitor();

                var expr = new EqualsExpr(new ConstantExpr(a), new ConstantExpr(b));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToBoolean());
            }
Ejemplo n.º 3
0
            public void NegationExpressionTest(object value, object expected)
            {
                var target = new EvaluateVisitor();

                var expr = new NegationExpr(new ConstantExpr(value));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToObject());
            }
Ejemplo n.º 4
0
            public void AndOrTest()
            {
                var target = new EvaluateVisitor();

                var expr = new AndExpr(new ConstantExpr(true), new OrExpr(new ConstantExpr(true), new ConstantExpr(false)));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(true, actual.ToBoolean());
            }
Ejemplo n.º 5
0
            public void VisitFloatConstantExpr()
            {
                var target = new EvaluateVisitor();

                var expression = new ConstantExpr(12.34);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual("12.34", actual.ToString());
            }
Ejemplo n.º 6
0
            public void VisitConditionMustBeBooleanType(object condition)
            {
                var target = new EvaluateVisitor();

                var conditionExpression = new ConstantExpr(condition);

                var expr = new ConditionalExpr(conditionExpression, new ConstantExpr(0), new ConstantExpr(0));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 7
0
            public void FunctionCallWithEmptyScopeTest()
            {
                var target = new EvaluateVisitor();

                var functionName           = RandomGenerator.String();
                var calledFunctionNameExpr = new IdentifierExpr(functionName);

                var expr = new FunctionCallExpr(calledFunctionNameExpr, new Expression[0]);

                target.Visit(expr, _scope);
            }
Ejemplo n.º 8
0
            public void VisitBooleanNotExpr(bool a, bool expected)
            {
                var target = new EvaluateVisitor();

                var aExpression = new ConstantExpr(a);
                var expr        = new NotExpr(aExpression);

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToBoolean());
            }
Ejemplo n.º 9
0
            public void VisitDivExpr()
            {
                var target = new EvaluateVisitor();

                var lhs        = new ConstantExpr(10);
                var rhs        = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual("5", actual.ToString());
            }
Ejemplo n.º 10
0
            public void FunctionCallOnNonFunctionTest()
            {
                var target = new EvaluateVisitor();

                var functionNameExpr = new IdentifierExpr(RandomGenerator.String());

                var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionNameExpr.Name, Value.FromObject(RandomGenerator.String()));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 11
0
            public void VisitConditionalExpr(bool condition, int thenValue, int elseValue, int expected)
            {
                var target = new EvaluateVisitor();

                var conditionExpression = new ConstantExpr(condition);
                var thenExpression      = new ConstantExpr(thenValue);
                var elseExpression      = new ConstantExpr(elseValue);

                var expr = new ConditionalExpr(conditionExpression, thenExpression, elseExpression);

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToObject());
            }
Ejemplo n.º 12
0
            public void FunctionCallWithNoReturnStatementTest()
            {
                var target = new EvaluateVisitor();

                var functionName       = RandomGenerator.String();
                var functionNameExpr   = new IdentifierExpr(functionName);
                var returnValue        = RandomGenerator.String();
                var functionDefinition = new FunctionDefinitionExpr(functionNameExpr, new VarDefinitionStmt[0], new ScopeBlockStmt(new[] { new NoOpStatement() }), new IdentifierExpr("String"));

                var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 13
0
            public void StatementBlockCallsAllStatementsTest()
            {
                var target = new EvaluateVisitor();

                var statement1 = new Mock <Statement>();
                var statement2 = new Mock <Statement>();
                var statement3 = new Mock <Statement>();

                var stmt = new ScopeBlockStmt(new[] { statement1.Object, statement2.Object, statement3.Object });

                target.Visit(stmt, _scope);

                statement1.Verify(s => s.Accept(target, It.IsAny <Scope>()), Times.Once);
                statement2.Verify(s => s.Accept(target, It.IsAny <Scope>()), Times.Once);
                statement3.Verify(s => s.Accept(target, It.IsAny <Scope>()), Times.Once);
            }
Ejemplo n.º 14
0
            public void VisitMathExpressionUsingFloatingPointDivisionTree()
            {
                var target = new EvaluateVisitor();

                var one   = new ConstantExpr(1.0);
                var two   = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four  = new ConstantExpr(4);
                var five  = new ConstantExpr(5);
                var six   = new ConstantExpr(6);

                var expr = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual("0.75", actual.ToString());
            }
Ejemplo n.º 15
0
            public void LambdaCallTest()
            {
                var target = new EvaluateVisitor();

                var functionName       = RandomGenerator.String();
                var functionNameExpr   = new IdentifierExpr(functionName);
                var returnValue        = RandomGenerator.String();
                var functionDefinition = new LambdaDefinitionExpr(functionNameExpr, new VarDefinitionStmt[0], new ConstantExpr(returnValue), new IdentifierExpr("String"));

                var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(returnValue, actual.ToObject());
            }
Ejemplo n.º 16
0
            public void StatementBlockAllStatementsAreExecutedInOrderTest()
            {
                var target = new EvaluateVisitor();

                var statement1 = new Mock <Statement>();
                var statement2 = new Mock <Statement>();
                var statement3 = new Mock <Statement>();

                int invocations     = 0;
                var invocationOrder = new int[3];

                statement1.Setup(c => c.Accept(It.IsAny <IExpressionVisitor <Value, Scope> >(), It.IsAny <Scope>()))
                .Returns <IExpressionVisitor <Value, Scope>, Scope>(
                    (v, s) =>
                {
                    invocationOrder[0] = invocations;
                    invocations++;
                    return(Value.Unit);;
                });
                statement2.Setup(c => c.Accept(It.IsAny <IExpressionVisitor <Value, Scope> >(), It.IsAny <Scope>()))
                .Returns <IExpressionVisitor <Value, Scope>, Scope>(
                    (v, s) =>
                {
                    invocationOrder[1] = invocations;
                    invocations++;
                    return(Value.Unit);;
                });
                statement3.Setup(c => c.Accept(It.IsAny <IExpressionVisitor <Value, Scope> >(), It.IsAny <Scope>()))
                .Returns <IExpressionVisitor <Value, Scope>, Scope>(
                    (v, s) =>
                {
                    invocationOrder[2] = invocations;
                    invocations++;
                    return(Value.Unit);;
                });

                var stmt = new ScopeBlockStmt(new[] { statement1.Object, statement2.Object, statement3.Object });

                target.Visit(stmt, _scope);

                for (int i = 0; i < invocationOrder.Length; i++)
                {
                    Assert.AreEqual(i, invocationOrder[i]);
                }
            }
Ejemplo n.º 17
0
            public void FunctionCallOnUndefinedFuncTest()
            {
                var target = new EvaluateVisitor();

                var functionName            = RandomGenerator.String();
                var definedFunctionNameExpr = new IdentifierExpr(functionName);
                var calledFunctionNameExpr  = new IdentifierExpr(functionName + "UNDEFINED");
                var returnValue             = RandomGenerator.String();

                var functionDefinition = new FunctionDefinitionExpr(definedFunctionNameExpr, new VarDefinitionStmt[0],
                                                                    new ScopeBlockStmt(new[] { new ReturnStmt(new ConstantExpr(returnValue)) }), new IdentifierExpr("String"));

                var expr = new FunctionCallExpr(calledFunctionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 18
0
            public void IfStatementWithNoElseTest(bool conditionValue)
            {
                var target = new EvaluateVisitor();

                var condition = new Mock <Expression>();

                condition.Setup(c => c.Accept(It.IsAny <IExpressionVisitor <Value, Scope> >(), It.IsAny <Scope>()))
                .Returns <IExpressionVisitor <Value, Scope>, Scope>((v, s) => new Value(conditionValue));

                var trueStmt = new Mock <Statement>();

                var expr = new IfStmt(condition.Object, trueStmt.Object, null);

                target.Visit(expr, _scope);

                condition.Verify(c => c.Accept(target, _scope), Times.Once);
                trueStmt.Verify(s => s.Accept(target, _scope), Times.Exactly(conditionValue ? 1 : 0));
            }
Ejemplo n.º 19
0
            public void DoWhileFalseLoopTest()
            {
                var target = new EvaluateVisitor();

                var condition = new Mock <Expression>();

                condition.Setup(c => c.Accept(It.IsAny <IExpressionVisitor <Value, Scope> >(), It.IsAny <Scope>()))
                .Returns <IExpressionVisitor <Value, Scope>, Scope>(
                    (v, s) =>
                {
                    return(new Value(false));
                });

                var statement = new Mock <Statement>();

                var expr = new DoWhileStmt(condition.Object, new ScopeBlockStmt(new[] { statement.Object }));

                target.Visit(expr, _scope);

                condition.Verify(c => c.Accept(target, _scope), Times.Exactly(1));
                statement.Verify(s => s.Accept(target, It.IsAny <Scope>()), Times.Once);
            }
Ejemplo n.º 20
0
            public void ReturnStatementVisitTest()
            {
                var target = new EvaluateVisitor();

                var returnValue = RandomGenerator.String();
                var returnStmt  = new ReturnStmt(new ConstantExpr(returnValue));

                try
                {
                    target.Visit(returnStmt, _scope);

                    Assert.Fail("No exception thrown");
                }
                catch (ReturnStatementException rse)
                {
                    Assert.AreEqual(returnValue, rse.Value.ToObject());
                }
                catch (Exception)
                {
                    Assert.Fail("Incorrect exception type caught");
                }
            }
Ejemplo n.º 21
0
            public void WhileLoopConditionIgnoredTest()
            {
                var target = new EvaluateVisitor();

                var conditionExecuted = false;
                var condition         = new Mock <Expression>();

                condition.Setup(c => c.Accept(It.IsAny <IExpressionVisitor <Value, Scope> >(), It.IsAny <Scope>()))
                .Returns <IExpressionVisitor <Value, Scope>, Scope>(
                    (v, s) =>
                {
                    var retVal        = new Value(!conditionExecuted);
                    conditionExecuted = true;
                    return(retVal);
                });

                var statement = new Mock <Statement>();

                statement.Setup(c => c.Accept(It.IsAny <IExpressionVisitor <Value, Scope> >(), It.IsAny <Scope>()))
                .Returns <IExpressionVisitor <Value, Scope>, Scope>(
                    (v, s) =>
                {
                    if (!conditionExecuted)
                    {
                        throw new Exception("Statement executed before condition evaluated.");
                    }
                    return(new Value(false));
                });

                var expr = new WhileStmt(condition.Object, new ScopeBlockStmt(new[] { statement.Object }));

                target.Visit(expr, _scope);

                condition.Verify(c => c.Accept(target, _scope), Times.Exactly(2));
                statement.Verify(s => s.Accept(target, It.IsAny <Scope>()), Times.Once);
            }
Ejemplo n.º 22
0
            public void AndOrTest()
            {
                var target = new EvaluateVisitor();

                var expr = new AndExpr(new ConstantExpr(true), new OrExpr(new ConstantExpr(true), new ConstantExpr(false)));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(true, actual.ToBoolean());
            }
Ejemplo n.º 23
0
            public void NegationExpressionTest(object value, object expected)
            {
                var target = new EvaluateVisitor();

                var expr = new NegationExpr(new ConstantExpr(value));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToObject());
            }
Ejemplo n.º 24
0
            public void FunctionCallCallsVisitOnAllParametersPassedTest()
            {
                var target = new EvaluateVisitor();

                var functionName = RandomGenerator.String();
                var functionNameExpr = new IdentifierExpr(functionName);

                var parameters = new[]
                {
                    new VarDefinitionStmt(new IdentifierExpr("A"), new IdentifierExpr("INT"), false, new ConstantExpr(1)),
                    new VarDefinitionStmt(new IdentifierExpr("B"), new IdentifierExpr("STRING"), false, new ConstantExpr("TEST")),
                    new VarDefinitionStmt(new IdentifierExpr("C"), new IdentifierExpr("BOOL"), false, new ConstantExpr(true))
                };

                var values = new Expression[]
                {
                    new ConstantExpr(1),
                    new ConstantExpr(RandomGenerator.String()),
                    new ConstantExpr(true)
                };

                var returnValue = values[1];

                var functionDefinition = new FunctionDefinitionExpr(functionNameExpr, parameters,
                    new ScopeBlockStmt(new[] { new ReturnStmt(returnValue) }), new IdentifierExpr("String"));

                var expr = new FunctionCallExpr(functionNameExpr, values);

                _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(((ConstantExpr)returnValue).Value, actual.ToObject());
            }
Ejemplo n.º 25
0
            public void IfStatementWithElseTest(bool conditionValue)
            {
                var target = new EvaluateVisitor();

                var condition = new Mock<Expression>();
                condition.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>((v, s) => new Value(conditionValue));

                var trueStmt = new Mock<Statement>();
                var falseStmt = new Mock<Statement>();

                var expr = new IfStmt(condition.Object, trueStmt.Object, falseStmt.Object);

                target.Visit(expr, _scope);

                condition.Verify(c => c.Accept(target, _scope), Times.Once);
                trueStmt.Verify(s => s.Accept(target, _scope), Times.Exactly(conditionValue ? 1 : 0));
                falseStmt.Verify(s => s.Accept(target, _scope), Times.Exactly(!conditionValue ? 1 : 0));
            }
Ejemplo n.º 26
0
            public void DoWhileFalseLoopTest()
            {
                var target = new EvaluateVisitor();

                var condition = new Mock<Expression>();
                condition.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>(
                        (v, s) =>
                        {
                            return new Value(false);
                        });

                var statement = new Mock<Statement>();

                var expr = new DoWhileStmt(condition.Object, new ScopeBlockStmt(new[] { statement.Object }));

                target.Visit(expr, _scope);

                condition.Verify(c => c.Accept(target, _scope), Times.Exactly(1));
                statement.Verify(s => s.Accept(target, It.IsAny<Scope>()), Times.Once);
            }
Ejemplo n.º 27
0
            public void StatementBlockAllStatementsAreExecutedInOrderTest()
            {
                var target = new EvaluateVisitor();

                var statement1 = new Mock<Statement>();
                var statement2 = new Mock<Statement>();
                var statement3 = new Mock<Statement>();

                int invocations = 0;
                var invocationOrder = new int[3];

                statement1.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>(
                        (v, s) =>
                        {
                            invocationOrder[0] = invocations;
                            invocations++;
                            return Value.Unit;;
                        });
                statement2.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>(
                        (v, s) =>
                        {
                            invocationOrder[1] = invocations;
                            invocations++;
                            return Value.Unit;;
                        });
                statement3.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>(
                        (v, s) =>
                        {
                            invocationOrder[2] = invocations;
                            invocations++;
                            return Value.Unit;;
                        });

                var stmt = new ScopeBlockStmt(new[] { statement1.Object, statement2.Object, statement3.Object });

                target.Visit(stmt, _scope);

                for (int i = 0; i < invocationOrder.Length; i++)
                {
                    Assert.AreEqual(i, invocationOrder[i]);
                }
            }
Ejemplo n.º 28
0
            public void FunctionCallWithNoReturnStatementTest()
            {
                var target = new EvaluateVisitor();

                var functionName = RandomGenerator.String();
                var functionNameExpr = new IdentifierExpr(functionName);
                var returnValue = RandomGenerator.String();
                var functionDefinition = new FunctionDefinitionExpr(functionNameExpr, new VarDefinitionStmt[0], new ScopeBlockStmt(new[] { new NoOpStatement()  }), new IdentifierExpr("String"));

                var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 29
0
            public void FunctionCallOnNonFunctionTest()
            {
                var target = new EvaluateVisitor();

                var functionNameExpr = new IdentifierExpr(RandomGenerator.String());

                var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionNameExpr.Name, Value.FromObject(RandomGenerator.String()));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 30
0
            public void EqualsTest(object a, object b, bool expected)
            {
                var target = new EvaluateVisitor();

                var expr = new EqualsExpr( new ConstantExpr(a), new ConstantExpr(b));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToBoolean());
            }
Ejemplo n.º 31
0
            public void StatementBlockCallsAllStatementsTest()
            {
                var target = new EvaluateVisitor();

                var statement1 = new Mock<Statement>();
                var statement2 = new Mock<Statement>();
                var statement3 = new Mock<Statement>();

                var stmt = new ScopeBlockStmt(new[] { statement1.Object, statement2.Object, statement3.Object });

                target.Visit(stmt, _scope);

                statement1.Verify(s => s.Accept(target, It.IsAny<Scope>()), Times.Once);
                statement2.Verify(s => s.Accept(target, It.IsAny<Scope>()), Times.Once);
                statement3.Verify(s => s.Accept(target, It.IsAny<Scope>()), Times.Once);
            }
Ejemplo n.º 32
0
            public void VisitDivExpr()
            {
                var target = new EvaluateVisitor();

                var lhs = new ConstantExpr(10);
                var rhs = new ConstantExpr(2);
                var expression = new DivExpr(lhs, rhs);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual("5", actual.ToString());
            }
Ejemplo n.º 33
0
            public void WhileLoopTest()
            {
                var target = new EvaluateVisitor();

                const int totalLoopIteration = 3;
                int loopIterations = 0;

                var condition = new Mock<Expression>();
                condition.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>(
                        (v, s) =>
                        {
                            loopIterations++;
                            return new Value(loopIterations <= totalLoopIteration);
                        });

                var statement = new Mock<Statement>();

                var expr = new WhileStmt(condition.Object, new ScopeBlockStmt(new[] { statement.Object }));

                target.Visit(expr, _scope);

                condition.Verify(c => c.Accept(target, _scope), Times.Exactly(totalLoopIteration + 1));
                statement.Verify( s => s.Accept(target, It.IsAny<Scope>()), Times.Exactly(totalLoopIteration) );
            }
Ejemplo n.º 34
0
            public void VisitMathExpressionUsingFloatingPointDivisionTree()
            {
                var target = new EvaluateVisitor();

                var one = new ConstantExpr(1.0);
                var two = new ConstantExpr(2);
                var three = new ConstantExpr(3);
                var four = new ConstantExpr(4);
                var five = new ConstantExpr(5);
                var six = new ConstantExpr(6);

                var expr = new DivExpr(new MultExpr(three, six), new MultExpr(new MinusExpr(five, one), new PlusExpr(four, two)));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual("0.75", actual.ToString());
            }
Ejemplo n.º 35
0
            public void DoWhileLoopConditionIgnoredTest()
            {
                var target = new EvaluateVisitor();

                var conditionExecuted = false;
                var condition = new Mock<Expression>();
                condition.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>(
                        (v, s) =>
                        {
                            conditionExecuted = true;
                            return new Value(false); ;
                        });

                var statement = new Mock<Statement>();
                statement.Setup(c => c.Accept(It.IsAny<IExpressionVisitor<Value, Scope>>(), It.IsAny<Scope>()))
                    .Returns<IExpressionVisitor<Value, Scope>, Scope>(
                        (v, s) =>
                        {
                            if (conditionExecuted)
                                throw new Exception("Statement executed after condition evaluated.");
                            return new Value(false);
                        });

                var expr = new DoWhileStmt(condition.Object, new ScopeBlockStmt(new[] { statement.Object }));

                target.Visit(expr, _scope);

                condition.Verify(c => c.Accept(target, _scope), Times.Once);
                statement.Verify(s => s.Accept(target, It.IsAny<Scope>()), Times.Once);
            }
Ejemplo n.º 36
0
            public void VisitBooleanNotExpr(bool a, bool expected)
            {
                var target = new EvaluateVisitor();

                var aExpression = new ConstantExpr(a);
                var expr = new NotExpr(aExpression);

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToBoolean());
            }
Ejemplo n.º 37
0
            public void ReturnStatementVisitTest()
            {
                var target = new EvaluateVisitor();

                var returnValue = RandomGenerator.String();
                var returnStmt = new ReturnStmt(new ConstantExpr(returnValue));

                try
                {
                    target.Visit(returnStmt, _scope);

                    Assert.Fail("No exception thrown");
                }
                catch (ReturnStatementException rse)
                {
                    Assert.AreEqual(returnValue, rse.Value.ToObject());
                }
                catch (Exception)
                {
                    Assert.Fail("Incorrect exception type caught");
                }
            }
Ejemplo n.º 38
0
            public void VisitConditionalExpr(bool condition, int thenValue, int elseValue, int expected)
            {
                var target = new EvaluateVisitor();

                var conditionExpression = new ConstantExpr(condition);
                var thenExpression = new ConstantExpr(thenValue);
                var elseExpression = new ConstantExpr(elseValue);

                var expr = new ConditionalExpr(conditionExpression, thenExpression, elseExpression);

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(expected, actual.ToObject());
            }
Ejemplo n.º 39
0
            public void FunctionCallOnUndefinedFuncTest()
            {
                var target = new EvaluateVisitor();

                var functionName = RandomGenerator.String();
                var definedFunctionNameExpr = new IdentifierExpr(functionName);
                var calledFunctionNameExpr = new IdentifierExpr(functionName + "UNDEFINED");
                var returnValue = RandomGenerator.String();

                var functionDefinition = new FunctionDefinitionExpr(definedFunctionNameExpr, new VarDefinitionStmt[0],
                    new ScopeBlockStmt(new[] { new ReturnStmt(new ConstantExpr(returnValue)) }), new IdentifierExpr("String"));

                var expr = new FunctionCallExpr(calledFunctionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 40
0
            public void VisitConditionMustBeBooleanType(object condition)
            {
                var target = new EvaluateVisitor();

                var conditionExpression = new ConstantExpr(condition);

                var expr = new ConditionalExpr(conditionExpression, new ConstantExpr(0), new ConstantExpr(0));

                target.Visit(expr, _scope);
            }
Ejemplo n.º 41
0
            public void FunctionCallWithEmptyScopeTest()
            {
                var target = new EvaluateVisitor();

                var functionName = RandomGenerator.String();
                var calledFunctionNameExpr = new IdentifierExpr(functionName);

                var expr = new FunctionCallExpr(calledFunctionNameExpr, new Expression[0]);

                target.Visit(expr, _scope);
            }
Ejemplo n.º 42
0
            public void VisitFloatConstantExpr()
            {
                var target = new EvaluateVisitor();

                var expression = new ConstantExpr(12.34);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual("12.34", actual.ToString());
            }
Ejemplo n.º 43
0
            public void LambdaCallTest()
            {
                var target = new EvaluateVisitor();

                var functionName = RandomGenerator.String();
                var functionNameExpr = new IdentifierExpr(functionName);
                var returnValue = RandomGenerator.String();
                var functionDefinition = new LambdaDefinitionExpr(functionNameExpr, new VarDefinitionStmt[0], new ConstantExpr(returnValue), new IdentifierExpr("String"));

                var expr = new FunctionCallExpr(functionNameExpr, new Expression[0]);

                _scope.DefineIdentifier(functionName, Value.FromObject(functionDefinition));

                var actual = target.Visit(expr, _scope);

                Assert.AreEqual(returnValue, actual.ToObject());
            }
Ejemplo n.º 44
0
            public void VisitBooleanConstantExpr(bool value)
            {
                var target = new EvaluateVisitor();

                var expression = new ConstantExpr(value);

                var actual = target.Visit(expression, _scope);

                Assert.AreEqual(value.ToString(), actual.ToString());
            }