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"); }
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"); }
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 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 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); }
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"); }
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"); }
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"); }
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"); }
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 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"); }
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"); }
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"); }
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"); }
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"); }
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 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"); }
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 Exec_aBool_plus_5_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 = "a + 5"; ParseResult parseResult = evaluator.Parse(expr); evaluator.DefineVarBool("a", true); ExecResult execResult = evaluator.Exec(); Assert.IsTrue(execResult.HasError, "The exec of the expression should failed"); Assert.AreEqual(ErrorCode.ExprCalculationOperandTypeUnExcepted, execResult.ListError[0].Code, "the error should be ParsedExpressionMissing"); }
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"); }
public void Exec_2_Plus_3_Mul_4_Plus_5_Ret_19_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 = "2+3*4+5"; ParseResult parseResult = evaluator.Parse(expr); ExecResult execResult = evaluator.Exec(); Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success"); // check the final result value ExprExecValueInt valueInt = execResult.ExprExec as ExprExecValueInt; Assert.IsNotNull(valueInt, "The result value should be an int"); Assert.AreEqual(19, valueInt.Value, "The result value should be: 19"); }
public void Exec_26dot5_Plus_4_Ret_30dot5_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 = "26.5+4"; ParseResult parseResult = evaluator.Parse(expr); ExecResult execResult = evaluator.Exec(); Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success"); // check the final result value ExprExecValueDouble valueDouble = execResult.ExprExec as ExprExecValueDouble; Assert.IsNotNull(valueDouble, "The result value should be a bool"); Assert.AreEqual(30.5, valueDouble.Value, "The result value should be: 30.5"); }
public void Expr_Minus_6_Ret_Minus_6_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 = "-6"; //-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 ExprFinalOperand rootExpr = parseResult.RootExpr as ExprFinalOperand; Assert.IsNotNull(rootExpr, "The root node type should be a ExprFinalOperand"); Assert.AreEqual(-6, rootExpr.ValueInt, "The left operand should be -6"); }
public void ExprIsA() { 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"; ParseResult parseResult = evaluator.Parse(expr); Assert.IsFalse(parseResult.HasError, "the expression process should finish successfully"); // check the syntax tree // check the root node ExprFinalOperand rootBinExprOperand = parseResult.RootExpr as ExprFinalOperand; Assert.IsNotNull(rootBinExprOperand, "The root node type should be a ExprFinalOperand"); Assert.AreEqual(rootBinExprOperand.Operand, "A", "The left operand should be A"); }
public void Exec_Minus_4_Minus_6_Ret_Minus_10_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-6"; ParseResult parseResult = evaluator.Parse(expr); ExecResult execResult = evaluator.Exec(); Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success"); // check the final result value ExprExecValueInt valueInt = execResult.ExprExec as ExprExecValueInt; Assert.IsNotNull(valueInt, "The result value should be a bool"); Assert.AreEqual(-10, valueInt.Value, "The result value should be: -10"); }
public void Exec_2_mul_4_div_3_Ret_Double_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 = "2*4/3"; ParseResult parseResult = evaluator.Parse(expr); ExecResult execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The exec of the expression should finish with success"); // check the final result value ExprExecValueDouble valueDouble = execResult.ExprExec as ExprExecValueDouble; Assert.IsNotNull(valueDouble, "The result value should be a bool"); Assert.IsTrue(valueDouble.Value > 2.666, "The result value should be greater than 2.666"); Assert.IsTrue(valueDouble.Value < 2.667, "The result value should be lesser than 2.667"); }
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 functionCall_paramTypeWrong_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.SetStringTagCode(StringTagCode.Quote); string expr = "fct('bonjour')"; evaluator.Parse(expr); // link function body to function call evaluator.AttachFunction("Fct", FctInt); //====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.FunctionCallParamTypeWrong, execResult.ListError[0].Code, "The exec of the expression should finish with error"); }
public void Exec_a_gt_OP_b_Plus_5_CP_retFalse_ok() { ExpressionEval evaluator = new ExpressionEval(); evaluator.SetLang(Language.En); string expr = "a > (b+5)"; ParseResult parseResult = evaluator.Parse(expr); evaluator.DefineVarInt("a", 2); evaluator.DefineVarInt("b", 10); ExecResult execResult = evaluator.Exec(); Assert.IsFalse(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(false, valueBool.Value, "The result value should be: false"); }
public void fct_OP_not_12_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 12)"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables and functions // link function body to function call evaluator.AttachFunction("Fct", Fct_RetNot); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.AreEqual(true, execResult.HasError, "The exec of the expression should finish with success"); Assert.AreEqual(ErrorCode.ExprLogicalNotOperator_InnerOperandBoolTypeExpected, execResult.ListError[0].Code, "The exec of the expression should finish with error: ExprLogicalNotOperator_InnerOperandBoolTypeExpected"); }