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"); }
/// <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); }
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); }
/// <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); } }
public void fct_OP_a_And__not_b_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(a and not b)"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables and functions evaluator.DefineVarBool("a", true); evaluator.DefineVarBool("b", 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_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"); }
/// <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); }
/// <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 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 static void Not_OP_A_and_b_CP_false() { string expr = "Not(A and 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=true, B=true"); evaluator.DefineVarBool("a", true); 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); }
static void OP_A_Eq_B_CP_Exec2Times() { string expr = "(A = B)"; 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.DefineVarInt("a", 15); evaluator.DefineVarInt("b", 15); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool; Console.WriteLine("Execution Result: " + valueBool.Value.ToString()); //====================================================== //====2/prepare the execution, provide all used variables: type and value, remove the previous result //execResult = evaluator.InitExec(); 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 valueBool = execResult.ExprExec as ExprExecValueBool; Console.WriteLine("Execution Result: " + valueBool.Value.ToString()); }
public void Exec_A_Gr_B_Bool_False_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>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", false); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.AreEqual(true, execResult.HasError, "The exec of the expression should finish with error"); Assert.AreEqual(ErrorCode.ExprComparisonOperatorNotAllowedForBoolType, execResult.ListError[0].Code, "Should failed"); }
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"); }
static void TestEpressionEval() { //string expr = "not A"; string expr = "A and B"; ExpressionEval evaluator = new ExpressionEval(); ParseResult parseResult = ParseExpr(evaluator, expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result evaluator.DefineVarInt("a", 12); evaluator.DefineVarBool("b", false); //evaluator.SetVariableValueInt("b", 12); Execute(evaluator, expr); }
public void DefineVarIsNull_err() { 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"); //====2/prepare the execution, provide all used variables: type and value //====2.1/Init var evaluator.DefineVarBool(null, true); //====3/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 ParsedExpressionMissing"); }
static void TestErrorDefinitionVarFunc() { //string expr = "not A"; string expr = "A and B"; ExpressionEval evaluator = new ExpressionEval(); ParseResult parseResult = ParseExpr(evaluator, expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result evaluator.DefineVarInt("a", 12); evaluator.DefineVarBool("b", false); //evaluator.SetVariableValueInt("b", 12); // check definition (var/func) errors List <ExprError> listError = evaluator.GetListErrorExprConfig(); Execute(evaluator, expr); }
public void ParseOk_InitExec_Exec_InitExec2_Exec2_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"); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); //Assert.IsFalse(execResult.HasError, "The parse of the expression should finish with success"); //=====2.1/Init var evaluator.DefineVarBool("a", true); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The init exec of the expression should failed"); Assert.IsTrue(execResult.IsResultBool, "The exec result be a bool"); Assert.IsTrue(execResult.ResultBool, "The exec result be true"); //====4/prepare the execution, provide all used variables: type and value //execResult = evaluator.InitExec(); Assert.IsFalse(execResult.HasError, "The parse of the expression should finish with success"); //=====2.1/Init var evaluator.DefineVarInt("a", 14); //====3/execute l'expression booléenne execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The init exec of the expression should failed"); Assert.IsTrue(execResult.IsResultInt, "The exec result be an int"); Assert.AreEqual(14, execResult.ResultInt, "The exec result be 14"); //====5/execute l'expression booléenne execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The init exec of the expression should failed"); }