/// <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); }
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_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_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_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_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_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 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> /// 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); }
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"); }
/// <summary> /// Use a function call in the expression. /// A function code is attached to the function call and executed. /// </summary> public static void Fct_OP_CP_true() { string expr = "fct()"; 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():"); evaluator.AttachFunction("fct", FctRetBool); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result: " + execResult.ResultBool); }
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 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"); }
public void fct_OP_CP_FuncCallNotExists_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()"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); // link the function code FctFalse() to the function call: Fct() evaluator.AttachFunction("FctDoesnotExist", FctFalse); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); // an error occurs Assert.IsTrue(execResult.HasError, "The exec of the expression should failed"); Assert.AreEqual(ErrorCode.FunctionCallNotLinked, execResult.ListError[0].Code, "The error code should be xx"); }
/// <summary> /// Execute a function having 2 int parameters. /// </summary> public static void CallFunctionWith2IntParams_ret_5() { string expr = "fct(a,b)"; 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", 2); evaluator.DefineVarInt("b", 3); evaluator.AttachFunction("fct", FctRetInt_Int_Int); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result is an int type?: " + execResult.IsResultInt); Console.WriteLine("Execution Result is (should be 5): " + execResult.ResultInt); }