예제 #1
0
        public void Expr_4_Mul_5_Plus_1_ok()
        {
            ExpressionEval evaluator = new ExpressionEval();

            // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT.
            evaluator.SetLang(Language.En);

            string expr = "4*5-1";

            //-1---parse the string, return a syntax tree
            ParseResult parseResult = evaluator.Parse(expr);

            Assert.IsFalse(parseResult.HasError, "the expression process should finish successfully");

            // check the root node
            ExprCalculation rootExprCalc = parseResult.RootExpr as ExprCalculation;

            Assert.IsNotNull(rootExprCalc, "The root node type should be an ExprCalculation");

            // the calc expr should contains 3 operands and 2 operators
            Assert.AreEqual(3, rootExprCalc.ListExprOperand.Count, "The ExprCalculation should contains 3 operands");
            Assert.AreEqual(2, rootExprCalc.ListOperator.Count, "The ExprCalculation should contains 2 operators");

            // check (just) the fisrt operator: */Mul
            ExprOperatorCalculation operatorTwo = rootExprCalc.ListOperator[0] as ExprOperatorCalculation;

            Assert.IsNotNull(operatorTwo, "The left root node type should be a ExprFinalOperand");
            Assert.AreEqual(OperatorCalculationCode.Multiplication, operatorTwo.Operator, "The Second operator should be: *");

            // check (just) the last operand: 1
            ExprFinalOperand operandThree = rootExprCalc.ListExprOperand[2] as ExprFinalOperand;

            Assert.IsNotNull(operandThree, "The left root node type should be a ExprFinalOperand");
            Assert.AreEqual(1, operandThree.ValueInt, "The left operand should be 11");
        }
예제 #2
0
        public void Expr_OP_a_Minus_2_CP_Eq_7_ok()
        {
            ExpressionEval evaluator = new ExpressionEval();

            // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT.
            evaluator.SetLang(Language.En);

            string expr = "(a-2) = 7";

            //-1---parse the string, return a syntax tree
            ParseResult parseResult = evaluator.Parse(expr);

            Assert.IsFalse(parseResult.HasError, "the expression process should finish successfully");

            // check the root node
            ExprComparison rootExpr = parseResult.RootExpr as ExprComparison;

            Assert.IsNotNull(rootExpr, "The root node type should be an ExprComparison");
            Assert.AreEqual(OperatorComparisonCode.Equals, rootExpr.Operator, "The operator qqhould: plus");

            //----left part (a-2)
            // check the left part
            ExprCalculation operandLeft = rootExpr.ExprLeft as ExprCalculation;

            Assert.IsNotNull(operandLeft, "The left root node type should be an ExprCalculation");
            Assert.AreEqual(OperatorCalculationCode.Minus, operandLeft.ListOperator[0].Operator, "The operator should: minus");

            // check the right part
            ExprFinalOperand operandLeftRight = operandLeft.ListExprOperand[0] as ExprFinalOperand;

            Assert.IsNotNull(operandLeftRight, "The left root node type should be a ExprFinalOperand");
            Assert.AreEqual("a", operandLeftRight.Operand, "The left operand should be: a");

            // check the right part of the root node
            ExprFinalOperand operandRightRight = operandLeft.ListExprOperand[1] as ExprFinalOperand;

            Assert.IsNotNull(operandRightRight, "The left root node type should be a ExprFinalOperand");
            Assert.AreEqual(2, operandRightRight.ValueInt, "The left operand should be 2");

            //----right part: 7
            // check the left part
            ExprFinalOperand operandRight = rootExpr.ExprRight as ExprFinalOperand;

            Assert.IsNotNull(operandRight, "The left root node type should be a ExprFinalOperand");
            Assert.AreEqual(7, operandRight.ValueInt, "The left operand should be 7");
        }
예제 #3
0
        public void Expr_12_Plus_5_Ret_17_ok()
        {
            ExpressionEval evaluator = new ExpressionEval();

            // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT.
            evaluator.SetLang(Language.En);

            string expr = "12+5";

            //-1---parse the string, return a syntax tree
            ParseResult parseResult = evaluator.Parse(expr);

            Assert.IsFalse(parseResult.HasError, "the expression process should finish successfully");

            // check the root node
            ExprCalculation rootExprCalc = parseResult.RootExpr as ExprCalculation;

            Assert.IsNotNull(rootExprCalc, "The root node type should be an ExprCalculation");

            // has two operands
            Assert.AreEqual(2, rootExprCalc.ListExprOperand.Count, "should have 2 operands");
            Assert.AreEqual(1, rootExprCalc.ListOperator.Count, "should have 1 operator");

            // check the left part of the root node
            //ExprFinalOperand operandLeft = rootExprCalc.ExprLeft as ExprFinalOperand;
            ExprFinalOperand operandLeft = rootExprCalc.ListExprOperand[0] as ExprFinalOperand;

            Assert.IsNotNull(operandLeft, "The left root node type should be a ExprFinalOperand");
            Assert.AreEqual(12, operandLeft.ValueInt, "The left operand should be 12");

            // check the right part of the root node
            //ExprFinalOperand operandRight = rootExprCalc.ExprRight as ExprFinalOperand;
            ExprFinalOperand operandRight = rootExprCalc.ListExprOperand[1] as ExprFinalOperand;

            Assert.IsNotNull(operandRight, "The left root node type should be a ExprFinalOperand");
            Assert.AreEqual(5, operandRight.ValueInt, "The left operand should be 5");

            // check the root node operator
            Assert.AreEqual(rootExprCalc.ListOperator[0].Operator, OperatorCalculationCode.Plus, "The root operator should be Plus");
        }
        public void a_Plus_12()
        {
            string           expr       = "a+12";
            List <ExprToken> listTokens = TestCommon.AddTokens("a", "+", "12");

            // decoder, renvoie un arbre de node
            ExprTokensParser parser = new ExprTokensParser();

            // the default list: =, <, >, >=, <=, <>
            var dictOperators = TestCommon.BuildDefaultConfig();

            parser.SetConfiguration(dictOperators);

            // decode the list of tokens
            ParseResult result = parser.Parse(expr, listTokens);

            //----check the root node
            ExprCalculation rootExpr = result.RootExpr as ExprCalculation;

            Assert.IsNotNull(rootExpr, "The root node type should be BoolBinExpr");

            //----check the left part of the root node
            //ExprFinalOperand operandLeft = rootExpr.ExprLeft as ExprFinalOperand;
            ExprFinalOperand operandLeft = rootExpr.ListExprOperand[0] as ExprFinalOperand;

            Assert.IsNotNull(operandLeft, "The left root node type should be a final operand");
            Assert.AreEqual(operandLeft.Operand, "a", "The left operand should be A");

            //----check the right part of the root node
            //ExprFinalOperand operandRight = rootExpr.ExprRight as ExprFinalOperand;
            ExprFinalOperand operandRight = rootExpr.ListExprOperand[1] as ExprFinalOperand;

            Assert.IsNotNull(operandRight, "The left root node type should be a Final operand");
            Assert.AreEqual(operandRight.Operand, "12", "The left operand should be 12");

            // check the root node operator
            Assert.AreEqual(rootExpr.ListOperator[0].Operator, OperatorCalculationCode.Plus, "The root operator should be =");
        }