public void Double_Exposant_SepIsDot_false()
        {
            ExpressionEval evaluator = new ExpressionEval();

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

            //evaluator.SetDoubleDecimalSeparator(ExpressionEvalDef.DoubleDecimalSeparator.Dot);

            string      expr        = "(a = 12.45E3)";
            ParseResult parseResult = evaluator.Parse(expr);

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

            //====2/prepare the execution, provide all used variables: type and value
            //ExprExecResult execResult = evaluator.InitExec();

            evaluator.DefineVarDouble("a", 1345);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value
            Assert.IsFalse(execResult.ResultBool, "The result value should be false");
        }
        /// <summary>
        /// error, the variables types mismatch.
        /// Can't compare an integer and a boolean.
        ///
        /// ====The expression is: (a=b)
        /// The expr '(a=b)' has errors, nb = 1
        /// Error code: ExprComparisonOperandsTypeMismatchIntExpected
        /// Error param: Position= 3
        ///
        /// </summary>
        public static void A_Eq_B_TypeMismatch()
        {
            string expr = "(a=b)";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            // 1-decode the expression
            evaluator.Parse(expr);

            // 2-set variables
            evaluator.DefineVarInt("a", 12);
            evaluator.DefineVarBool("b", true);

            // 3-execute the expression
            ExecResult execResult = evaluator.Exec();

            // 4-check error
            if (execResult.HasError)
            {
                // display the error code
                Console.WriteLine("The expr '" + expr + "' has errors, nb=" + execResult.ListError.Count);
                ExprError error = execResult.ListError[0];
                Console.WriteLine("Error code: " + error.Code);

                // display the error parameter (the position of the wrong token)
                Console.WriteLine("Error param: " + error.ListErrorParam[0].Key + "= " + error.ListErrorParam[0].Value);

                return;
            }

            Console.WriteLine("The expr " + expr + " parse finished sucessfully!");
        }
        public void fct_OP_12_Sep_bonjour_8dot5_CP_retInt_21_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 = "fct(4, \"bonjour\", 8.5)";

            evaluator.Parse(expr);

            // link function body to function call
            var funcMapper = new Func3ParamsRetIntMapper <int, string, double>();

            funcMapper.SetFunction(FctP1Int_P2String_P3Double);
            evaluator.AttachFunction("Fct", funcMapper);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value (is ExprExecFunctionCallBool override ExprExecValueBool)
            ExprExecValueInt valueInt = execResult.ExprExec as ExprExecValueInt;

            Assert.IsNotNull(execResult, "The result value should be a bool");
            Assert.AreEqual(21, valueInt.Value, "The result value should be: 18");
        }
Ejemplo n.º 4
0
        public void fct_OP_CP_retBool_false_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        = "fct()";
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value
            //ExprExecResult execResult = evaluator.InitExec();

            // attach the function code FctFalse() to the function call: Fct()
            evaluator.AttachFunction("Fct", FctFalse);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value (is ExprExecFunctionCallBool override ExprExecValueBool)
            ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool;

            Assert.IsNotNull(execResult, "The result value should be a bool");
            Assert.AreEqual(false, valueBool.Value, "The result value should be: false");
        }
        /// <summary>
        /// Execute a function having 2 int parameters.
        /// </summary>
        public static void CallFunctionWith3IntParams_ret_10()
        {
            string expr = "fct(a,b,c)";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            //====1/decode the expression
            evaluator.Parse(expr);

            //====2/prepare the execution, attach function
            Console.WriteLine("Attach function code to Fct() and set value to param: a=2, b=3; c=5");
            evaluator.DefineVarInt("a", 2);
            evaluator.DefineVarInt("b", 3);
            evaluator.DefineVarInt("c", 5);

            // attach the 3 params function to the function call
            Func3ParamsRetIntMapper <int, int, int> func3ParamsRetIntMapper = new Func3ParamsRetIntMapper <int, int, int>();

            func3ParamsRetIntMapper.SetFunction(FctRetInt_Int_Int_Int);
            evaluator.AttachFunction("fct", func3ParamsRetIntMapper);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution has error? (should be false): " + execResult.HasError);
            Console.WriteLine("Execution Result is an int type?: " + execResult.IsResultInt);
            Console.WriteLine("Execution Result is (should be 10): " + execResult.ResultInt);
        }
Ejemplo n.º 6
0
        public void fct_OP_not_OP_a_CP_CP_retBool_true_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        = "fct(not (a))";
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables and functions
            evaluator.DefineVarBool("a", true);

            // link function body to function call
            evaluator.AttachFunction("Fct", Fct_RetNot);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value (is ExprExecFunctionCallBool override ExprExecValueBool)
            ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool;

            Assert.IsNotNull(execResult, "The result value should be a bool");
            Assert.AreEqual(true, valueBool.Value, "The result value should be: true");
        }
Ejemplo n.º 7
0
        public static void a_plus_b_mul_c_ret_14()
        {
            string expr = "a+b*c";

            Console.WriteLine("\n====The expression is: " + expr);
            Console.WriteLine("The component Apply operator priority: a+(b*c)");

            ExpressionEval evaluator = new ExpressionEval();

            //====1/decode the expression
            evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            Console.WriteLine("Define variables: a:=2, b:=3, c:= 4");
            evaluator.DefineVarInt("a", 2);
            evaluator.DefineVarInt("b", 3);
            evaluator.DefineVarInt("c", 4);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result is an int (true)?: " + execResult.IsResultInt);
            Console.WriteLine("Execution Result (should be 14): " + execResult.ResultInt);
        }
        /// <summary>
        /// Define a variable but the syntax name is wrong: has space.
        /// </summary>
        public static void DefineVarSyntaxName()
        {
            string expr = "Not(A)";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            //====1/decode the expression
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            Console.WriteLine("Define variables: A=12");
            evaluator.DefineVarBool("a", false);
            Console.WriteLine("Define wrong variable name: 'a b c'  -> don't stop the execution of the evaluation of the expression!");
            evaluator.DefineVarInt("a b c", 12);

            List <ExprError> listConfigError = evaluator.GetListErrorExprConfig();

            Console.WriteLine("DefineVar failed, err (VarNameSyntaxWrong): " + listConfigError[0].Code);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result (true): " + execResult.ResultBool);
        }
Ejemplo n.º 9
0
        public void fct_OP_CP_retBool_paramMissing_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        = "fct()";
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value
            //ExprExecResult execResult = evaluator.InitExec();

            // link function body to function call
            evaluator.AttachFunction("fct", FctRetBool_P1Bool);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.IsTrue(execResult.HasError, "The exec of the expression should finish with error");

            Assert.AreEqual(ErrorCode.FunctionCallParamCountWrong, execResult.ListError[0].Code, "The exec of the expression should finish with success");
            Assert.AreEqual("FunctionCallName", execResult.ListError[0].ListErrorParam[0].Key, "The exec of the expression should finish with success");
            Assert.AreEqual("fct", execResult.ListError[0].ListErrorParam[0].Value, "The exec of the expression should finish with success");
        }
Ejemplo n.º 10
0
        public void Double_and_OneVarTypeWrong_err()
        {
            ExpressionEval evaluator = new ExpressionEval();

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

            //evaluator.SetDoubleDecimalSeparator(ExpressionEvalDef.DoubleDecimalSeparator.Dot);

            string      expr        = "a and b";
            ParseResult parseResult = evaluator.Parse(expr);

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

            //====2/prepare the execution, provide all used variables: type and value
            evaluator.DefineVarInt("a", 12);
            evaluator.DefineVarBool("b", true);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.IsTrue(execResult.HasError, "The exec of the expression should finish with error");

            // todo: pas bon!! corriger
            Assert.AreEqual(ErrorCode.ExprLogicalOperandTypeNotAllowed, execResult.ListError[0].Code, "The exec of the expression should finish with success");
        }
        public void Double_a_xor_b_false_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);

            //evaluator.SetDoubleDecimalSeparator(ExpressionEvalDef.DoubleDecimalSeparator.Dot);

            string      expr        = "a xor b";
            ParseResult parseResult = evaluator.Parse(expr);

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

            //====2/prepare the execution, provide all used variables: type and value
            evaluator.DefineVarBool("a", false);
            evaluator.DefineVarBool("b", true);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value
            Assert.IsTrue(execResult.IsResultBool, "The result should be a bool");
            Assert.IsTrue(execResult.ResultBool, "The result value should be true");
        }
Ejemplo n.º 12
0
        /// <summary>
        /// A dev!!
        /// </summary>
        static void TestExprLogicalSeveralOperands()
        {
            string expr = "(A and B or C)";

            ExpressionEval evaluator = new ExpressionEval();

            //====1/décode l'expression booléenne
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            //ExprExecResult execResult = evaluator.InitExec();

            evaluator.DefineVarBool("a", true);
            evaluator.DefineVarBool("b", true);
            evaluator.DefineVarBool("c", false);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            //====4/get the result
            if (execResult.HasError)
            {
                Console.WriteLine("Execution Result: error!");
            }

            if (execResult.IsResultBool)
            {
                Console.WriteLine("Execution Result: " + execResult.ResultBool);
            }
        }
Ejemplo n.º 13
0
        public void Exec_A_Diff_B_Bool_True_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<>B)";
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value
            //ExprExecResult execResult = evaluator.InitExec();

            evaluator.DefineVarBool("a", false);
            evaluator.DefineVarBool("b", true);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value
            ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool;

            Assert.IsNotNull(valueBool, "The result value should be a bool");
            Assert.AreEqual(true, valueBool.Value, "The result value should be: true");
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Get variables used in the expression.
        ///
        /// The execution will displays:
        /// Var #1, Name=a
        /// Var #2, Name=b
        /// </summary>
        public static void GetVariableOfExpression()
        {
            string expr = "a=b";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            //====1/decode the expression
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            // scan all variables found in the expression (found the variable named 'a')
            int i = 0;

            foreach (ExprVarUsed exprVar in parseResult.ListExprVarUsed)
            {
                i++;
                Console.WriteLine("Var #" + i + ", Name=" + exprVar.Name);
            }

            Console.WriteLine("Define variables: a:=12, b:=12");
            evaluator.DefineVarInt("a", 12);
            evaluator.DefineVarInt("b", 12);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result (should be true): " + execResult.ResultBool);
        }
        public void Exec_Not_OP_a_CP_VarTypeWrong()
        {
            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        = "Not(A)";
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value
            //ExprExecResult execResult = evaluator.InitExec();

            // define the var a as an int in place of a bool!!!
            evaluator.DefineVarInt("a", 12);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(true, execResult.HasError, "The exec of the expression should finish with success");

            // get the error
            ExprError error = execResult.ListError[0];

            Assert.AreEqual(ErrorType.Exec, error.ErrorType, "The errorType should be: Exec");
            Assert.AreEqual(ErrorCode.ExprLogicalNotOperator_InnerOperandBoolTypeExpected, error.Code, "The errCode should be: VariableNotCreated");
            ErrorParam errParam = error.ListErrorParam[0];

            Assert.AreEqual("Token", errParam.Key, "The errParam key should be: VarName");
            Assert.AreEqual("A", errParam.Value, "The errParam value should be: a");
            ErrorParam errParam2 = error.ListErrorParam[1];

            Assert.AreEqual("Position", errParam2.Key, "The errParam key should be: VarName");
            Assert.AreEqual("4", errParam2.Value, "The errParam value should be: a");
        }
Ejemplo n.º 16
0
        public void Bra_A_Eq_Minus_12_Bra()
        {
            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=-12)";

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

            Assert.IsFalse(parseResult.HasError, "the expression process should return success");

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

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

            // check the left part of the root node
            ExprFinalOperand operandLeft = rootBinExpr.ExprLeft as ExprFinalOperand;

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

            // check the right part of the root node
            ExprFinalOperand operandRight = rootBinExpr.ExprRight as ExprFinalOperand;

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

            // check the root node operator
            Assert.AreEqual(rootBinExpr.Operator, OperatorComparisonCode.Equals, "The root operator should be =");
        }
        public static void Not_OP_A_CP_A_Int_Err()
        {
            string expr = "Not(A)";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            //====1/decode the expression
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            Console.WriteLine("Define variables: A=12");
            evaluator.DefineVarInt("a", 12);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            if (execResult.HasError)
            {
                // error: VariableNotCreated
                Console.WriteLine("Execution Result failed, err: " + execResult.ListError[0].Code);

                // Key: VarName, Value: a
                Console.WriteLine("Execution Result failed, ParamKey: " + execResult.ListError[0].ListErrorParam[0].Key + ", ParamValue: " + execResult.ListError[0].ListErrorParam[0].Value);
                return;
            }

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result: " + execResult.ResultBool);
        }
        /// <summary>
        /// Execute a function call two times with different values.
        ///
        /// fct(a)  a:=8  ->return false
        /// fct(a)  a:=12  ->return true
        ///
        /// </summary>
        public static void CallFunctionWithIntParam_TwoTimes()
        {
            string expr = "fct(a)";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            //====1/decode the expression
            evaluator.Parse(expr);

            //====2/prepare the execution, attach function
            Console.WriteLine("Attach function code to Fct() and set value to param: a=8");
            evaluator.DefineVarInt("a", 8);
            evaluator.AttachFunction("fct", FctRetBool_Int);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result (should return false): " + execResult.ResultBool);

            //============================================================
            //====2/prepare the execution, attach function
            Console.WriteLine("Set value to param: a=12");
            evaluator.DefineVarInt("a", 12);
            evaluator.AttachFunction("fct", FctRetBool_Int);

            //====3/Execute the expression
            execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result (should return true): " + execResult.ResultBool);
        }
Ejemplo n.º 19
0
        public static void OP_A_Ou_b_CP_true()
        {
            string expr = "(a Ou b)";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            // set the tool in french
            evaluator.SetLang(Language.Fr);

            //====1/decode the expression
            evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            Console.WriteLine("Define variables: a:=false, b=true");
            evaluator.DefineVarBool("a", false);
            evaluator.DefineVarBool("b", true);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result: " + execResult.ResultBool);
        }
Ejemplo n.º 20
0
        public void fct_OP_a_CP_retDouble_P1String_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        = "fct(a)";
            ParseResult parseResult = evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value
            //ExprExecResult execResult = evaluator.InitExec();

            // 3 letters -> return 3x1.5=4.5
            evaluator.DefineVarString("a", "abc");

            // link function body to function call
            evaluator.AttachFunction("Fct", FctDouble_String);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value (is ExprExecFunctionCallBool override ExprExecValueBool)
            ExprExecValueDouble valueDouble = execResult.ExprExec as ExprExecValueDouble;

            Assert.IsNotNull(valueDouble, "The result value should be a double");
            Assert.AreEqual(4.5, valueDouble.Value, "The result value should be: true");
        }
        public void fct_OP_false_Sep_false_CP_retBool_true_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 = "fct(true, true, true)";

            evaluator.Parse(expr);

            // link function body to function call
            var funcMapper = new Func3ParamsRetBoolMapper <bool, bool, bool>();

            funcMapper.SetFunction(Fct3ParamsBool);
            evaluator.AttachFunction("Fct", funcMapper);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value (is ExprExecFunctionCallBool override ExprExecValueBool)
            ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool;

            Assert.IsNotNull(execResult, "The result value should be a bool");
            Assert.AreEqual(true, valueBool.Value, "The result value should be: true");
        }
Ejemplo n.º 22
0
        public void fct_3Params_FunctionCall_OneMissing_err()
        {
            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 = "fct(true, true)";

            evaluator.Parse(expr);

            // link function body to function call
            Func3ParamsRetBoolMapper <bool, bool, bool> func3ParamsRetBoolMapper = new Pierlam.ExpressionEval.Func3ParamsRetBoolMapper <bool, bool, bool>();

            func3ParamsRetBoolMapper.SetFunction(Fct3ParamsBool);
            evaluator.AttachFunction("fct", func3ParamsRetBoolMapper);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.IsTrue(execResult.HasError, "The exec of the expression should finish with error");

            Assert.AreEqual(ErrorCode.FunctionCallParamCountWrong, execResult.ListError[0].Code, "The exec of the expression should finish with success");
            Assert.AreEqual("FunctionCallName", execResult.ListError[0].ListErrorParam[0].Key, "The exec of the expression should finish with success");
            Assert.AreEqual("fct", execResult.ListError[0].ListErrorParam[0].Value, "The exec of the expression should finish with success");
        }
        public void fct_OP_10_Sep_bonjour_CP_retBool_true_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);
            evaluator.SetStringTagCode(StringTagCode.Quote);

            string expr = "fct(10, 'bonjour')";

            evaluator.Parse(expr);

            // link function body to function call
            evaluator.AttachFunction("Fct", FctP1Int_P2String);

            //====3/execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success");

            // check the final result value (is ExprExecFunctionCallBool override ExprExecValueBool)
            ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool;

            Assert.IsNotNull(execResult, "The result value should be a bool");
            Assert.AreEqual(true, valueBool.Value, "The result value should be: true");
        }
        /// <summary>
        /// Expression: (A=B)
        /// A boolean expression using two variables.
        /// returns always a boolean value result.
        ///
        /// Particularity:
        /// Execute the same expression 2 times.
        /// The first time, both variables A and B are defined as integer.
        /// The second time, A and B are then defined as boolean.
        ///
        /// The execution finish successfully.
        /// </summary>
        public static void A_Eq_B_Exec2Times()
        {
            string expr = "(A = B)";

            Console.WriteLine("\n====The expression is: " + expr);

            ExpressionEval evaluator = new ExpressionEval();

            //====1/decode the expression
            evaluator.Parse(expr);

            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            Console.WriteLine("Define variables: A=15; B=15 ");
            evaluator.DefineVarInt("a", 15);
            evaluator.DefineVarInt("b", 15);

            //====3/Execute the expression
            ExecResult execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result: " + execResult.ResultBool);

            //======================================================
            //====2/prepare the execution, provide all used variables: type and value, remove the previous result
            Console.WriteLine("\nExecute again the same provided expression but changes variables types and values:");
            Console.WriteLine("Define variables: A=false; B=false");
            evaluator.DefineVarBool("a", false);
            evaluator.DefineVarBool("b", false);

            //====3/execute l'expression booléenne
            execResult = evaluator.Exec();

            //====4/get the result, its a bool value
            Console.WriteLine("Execution Result: " + execResult.ResultBool);
        }
Ejemplo n.º 25
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");
        }
Ejemplo n.º 26
0
        public void ExprIsBlank()
        {
            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 = "   ";

            ParseResult decodeResult = evaluator.Parse(expr);

            Assert.IsTrue(decodeResult.HasError, "the expression process should fail");
        }
        public void Double_SepIsComma_err()
        {
            ExpressionEval evaluator = new ExpressionEval();

            //evaluator.SetDoubleAndFunctionCallParameterSeparator(ExpressionEvalDef.DoubleAndFunctionCallParameterSeparator.ExcelLike);

            string      expr        = "12,45";
            ParseResult parseResult = evaluator.Parse(expr);

            Assert.IsTrue(parseResult.HasError, "the parse should failed");

            // todo: pour l'instant!! car traitement des appels de fct pas encore traité!
            Assert.AreEqual(ErrorCode.WrongExpression, parseResult.ListError[0].Code, "the parse should failed: ValueNumberBadFormed");
        }
Ejemplo n.º 28
0
        public void ParseOk_InitExec_Exec_ok()
        {
            ExpressionEval evaluator = new ExpressionEval();

            string      expr        = "12";
            ParseResult parseResult = evaluator.Parse(expr);

            Assert.IsFalse(parseResult.HasError, "The parse of the expression should finish with success");

            //====execute l'expression booléenne
            ExecResult execResult = execResult = evaluator.Exec();

            Assert.IsFalse(execResult.HasError, "The init exec of the expression should failed");
        }
Ejemplo n.º 29
0
        static ParseResult ParseExpr(ExpressionEval evaluator, string expr)
        {
            // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT.
            evaluator.SetLang(Language.En);

            // configurer le format des opérandes
            // todo:  constante, var..
            // todo: configurer les valeurs spéciales: true et false (vrai et faux)

            Console.WriteLine("eval expr: " + expr);

            //====1/décode l'expression booléenne
            ParseResult parseResult = evaluator.Parse(expr);

            if (parseResult.HasError)
            {
                Console.WriteLine("ERR, Une ou plusieurs erreurs ont été générées lors du parsing de l'expression " + expr + ", nb erreur(s): " + parseResult.ListError.Count);

                // affiche les erreurs
                int i = 0;
                foreach (ExprError error in parseResult.ListError)
                {
                    Console.WriteLine("erreur N°" + i + ": " + error.Code.ToString());
                }
                return(null);
            }

            // displays all variables present in the expression
            int j = 0;

            foreach (ExprVarUsed exprVar in parseResult.ListExprVarUsed)
            {
                j++;
                Console.WriteLine("Var #" + j + "Name=" + exprVar.Name);
            }

            // display all the tokens found in the expression
            foreach (ExprToken token in parseResult.ListExprToken)
            {
                Console.WriteLine(token.Position + ", " + token.Value);
            }
            Console.WriteLine("Decode expr: OK, pas d'erreur.");


            //====2/prepare the execution, provide all used variables: type and value
            //return evaluator.InitExec(parseResult);

            return(parseResult);
        }
        public void ParseOk__DefVarMissing_InitExec_Exec_ok()
        {
            ExpressionEval evaluator = new ExpressionEval();

            string      expr        = "a";
            ParseResult parseResult = evaluator.Parse(expr);

            Assert.IsFalse(parseResult.HasError, "The parse of the expression should finish with success");

            //====execute l'expression booléenne
            ExecResult execResult = evaluator.Exec();

            Assert.IsTrue(execResult.HasError, "The init exec of the expression should failed");
            Assert.AreEqual(ErrorCode.VariableNotDefined, execResult.ListError[0].Code, "the error should be VariableNotCreated");
        }