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"); }
/// <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!"); }
/// <summary> /// A boolean expression using one variable. /// returns always a boolean value result. /// /// The execution finish successfully. /// </summary> public static void A_Gt_15_ToManyBracket() { string expr = "(A > 15))"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression ParseResult parseResult = evaluator.Parse(expr); if (parseResult.HasError) { Console.WriteLine("The expr '" + expr + "' has errors, nb=" + parseResult.ListError.Count); ExprError error = parseResult.ListError[0]; Console.WriteLine("Error code: " + error.Code); Console.WriteLine("Pos in the expr: " + error.TokenPosition); Console.WriteLine("Wrong token in the expr: " + error.TokenValue); return; } Console.WriteLine("The expr " + expr + " parse finished sucessfully!"); }