public void fct_BO_12_BC_ok() { string expr = "fct(12)"; List <ExprToken> listTokens = TestCommon.AddTokens("fct", "(", "12", ")"); // decoder, renvoie un arbre de node ExprTokensParser parser = new ExprTokensParser(); // the default list: =, <, >, >=, <=, <> ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(config); // decode the list of tokens ParseResult result = parser.Parse(expr, listTokens); // finished with no error Assert.AreEqual(0, result.ListError.Count, "The tokens should be decoded with success"); // get the root expression ExprFunctionCall rootExpr = result.RootExpr as ExprFunctionCall; Assert.IsNotNull(rootExpr, "The root node type should be ExprFunctionCall"); Assert.AreEqual("fct", rootExpr.FunctionName, "The function name should be: fct"); Assert.AreEqual(1, rootExpr.ListExprParameters.Count, "The function call should have one parameter"); // check the parameter: its a final operand: name and type!! ExprFinalOperand const12 = rootExpr.ListExprParameters[0] as ExprFinalOperand; Assert.AreEqual("12", const12.Operand, "The parameter name should be: 12"); Assert.IsTrue(const12.ContentType == OperandType.ValueInt, "The parameter type should be: int"); }
public void Group_Gr_Eq() { //>, = string expr = ">="; List <ExprToken> listTokens = TestCommon.AddTokens(">", "="); List <string> listSpecialOperators = new List <string>(); listSpecialOperators.Add("<>"); listSpecialOperators.Add(">="); listSpecialOperators.Add("<="); ExprScanner parser = new ExprScanner(); ExpressionEvalConfig evalConfig = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(evalConfig); List <ExprToken> listTokensGrp = parser.GroupTokens(expr, listTokens); Assert.AreEqual(1, listTokensGrp.Count, expr + " should contains 1 token"); Assert.AreEqual(">=", listTokensGrp[0].Value, expr + " should contains 1 '>='"); Assert.AreEqual(0, listTokensGrp[0].Position, expr + " should contains 1 '>='"); }
public void fct_OP_CP_ok() { string expr = "fct()"; List <ExprToken> listTokens = TestCommon.AddTokens("fct", "(", ")"); // decoder, renvoie un arbre de node ExprTokensParser parser = new ExprTokensParser(); // the default list: =, <, >, >=, <=, <> ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(config); // decode the list of tokens ParseResult result = parser.Parse(expr, listTokens); // finished with no error Assert.AreEqual(0, result.ListError.Count, "The tokens should be decoded with success"); // get the root expression ExprFunctionCall rootExpr = result.RootExpr as ExprFunctionCall; Assert.IsNotNull(rootExpr, "The root node type should be ExprFunctionCall"); Assert.AreEqual("fct", rootExpr.FunctionName, "The function name should be: fct"); Assert.AreEqual(0, rootExpr.ListExprParameters.Count, "The function call should have any parameter"); }
/// <summary> /// Build the list of operators depending on the language/culture. /// boolean and comparison operators. /// Build also the list of special large operators (2 char large). /// </summary> /// <param name="lang"></param> /// <param name="dictOperators"></param> /// <param name="listSpecial2CharOperators"></param> public bool BuildOperators(ExpressionEvalConfig exprEvalConfig) { if (exprEvalConfig == null) { return(false); } exprEvalConfig.DictComparisonOperators.Clear(); //----build list of comparison operators exprEvalConfig.DictComparisonOperators.Add("=", OperatorComparisonCode.Equals); exprEvalConfig.DictComparisonOperators.Add("<>", OperatorComparisonCode.Different); exprEvalConfig.DictComparisonOperators.Add(">", OperatorComparisonCode.Greater); exprEvalConfig.DictComparisonOperators.Add(">=", OperatorComparisonCode.GreaterEquals); exprEvalConfig.DictComparisonOperators.Add("<", OperatorComparisonCode.Less); exprEvalConfig.DictComparisonOperators.Add("<=", OperatorComparisonCode.LessEquals); //----build list of calculation operators exprEvalConfig.DictCalculationOperators.Add("+", OperatorCalculationCode.Plus); exprEvalConfig.DictCalculationOperators.Add("-", OperatorCalculationCode.Minus); exprEvalConfig.DictCalculationOperators.Add("*", OperatorCalculationCode.Multiplication); exprEvalConfig.DictCalculationOperators.Add("/", OperatorCalculationCode.Division); //---build list of special 2 char operators (needed for grouping/compacting tokens) exprEvalConfig.ListSpecial2CharOperators.Add("<>"); exprEvalConfig.ListSpecial2CharOperators.Add(">="); exprEvalConfig.ListSpecial2CharOperators.Add("<="); //----build binary operators if (exprEvalConfig.Lang == Language.Fr) { exprEvalConfig.DictLogicalOperators.Add("et", OperatorLogicalCode.And); exprEvalConfig.DictLogicalOperators.Add("ou", OperatorLogicalCode.Or); exprEvalConfig.DictLogicalOperators.Add("oux", OperatorLogicalCode.Xor); exprEvalConfig.DictLogicalOperators.Add("non", OperatorLogicalCode.Not); // define true and false boolean const value exprEvalConfig.SetBoolConstValue(true, "vrai"); exprEvalConfig.SetBoolConstValue(false, "faux"); return(true); } // par défaut, en anglais exprEvalConfig.DictLogicalOperators.Add("and", OperatorLogicalCode.And); exprEvalConfig.DictLogicalOperators.Add("or", OperatorLogicalCode.Or); exprEvalConfig.DictLogicalOperators.Add("xor", OperatorLogicalCode.Xor); exprEvalConfig.DictLogicalOperators.Add("not", OperatorLogicalCode.Not); // define true and false boolean const value exprEvalConfig.SetBoolConstValue(true, "true"); exprEvalConfig.SetBoolConstValue(false, "false"); return(true); }
public static ExpressionEvalConfig BuildDefaultConfig() { // build and set operators ExprOperatorBuilder operatorsBuilder = new ExprOperatorBuilder(); ExpressionEvalConfig exprEvalConfig = new ExpressionEvalConfig(); exprEvalConfig.SetLang(Language.En); exprEvalConfig.SetDecimalAndFunctionSeparators(DecimalAndFunctionSeparators.Standard); operatorsBuilder.BuildOperators(exprEvalConfig); return(exprEvalConfig); }
public static void BuildDefaultConfig(ExprScanner scanner) { // configure ExpressionEvalConfig exprEvalConfig = new ExpressionEvalConfig(); exprEvalConfig.SetLang(Language.En); exprEvalConfig.SetStringTagCode(StringTagCode.DoubleQuote); ExprOperatorBuilder operatorsBuilder = new ExprOperatorBuilder(); operatorsBuilder.BuildOperators(exprEvalConfig); //ExpressionEvalConfig evalConfig = new ExpressionEvalConfig(); //scanner.SetListSpecial2CharOperators(exprEvalConfig.ListSpecial2CharOperators); scanner.SetConfiguration(exprEvalConfig); }
public void DontGroup_Gr_Ls() { //>, = string expr = ">="; List <ExprToken> listTokens = TestCommon.AddTokens(">", "<"); ExpressionEvalConfig exprOperators = TestCommon.BuildDefaultConfig(); ExpressionEvalConfig evalConfig = new ExpressionEvalConfig(); ExprScanner parser = new ExprScanner(); parser.SetConfiguration(evalConfig); List <ExprToken> listTokensGrp = parser.GroupTokens(expr, listTokens); Assert.AreEqual(2, listTokens.Count, expr + " should contains 2 tokens"); }
public void fct_OP_a_CP_And_b_ok() { string expr = "fct(a) And b"; List <ExprToken> listTokens = TestCommon.AddTokens("fct", "(", "a", ")", "And"); TestCommon.AddTokens(listTokens, "b"); // decoder, renvoie un arbre de node ExprTokensParser parser = new ExprTokensParser(); // the default list: =, <, >, >=, <=, <> ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(config); // decode the list of tokens ParseResult result = parser.Parse(expr, listTokens); // finished with no error Assert.AreEqual(0, result.ListError.Count, "The tokens should be decoded with success"); // get the root expression ExprLogical exprLogical = result.RootExpr as ExprLogical; Assert.IsNotNull(exprLogical, "The root node type should be Logical"); //----check the left part of the comparison: the function call ExprFunctionCall exprFunction = exprLogical.ExprLeft as ExprFunctionCall; Assert.IsNotNull(exprFunction, "The root node type should be ExprFunctionCall"); Assert.AreEqual("fct", exprFunction.FunctionName, "The function name should be: fct"); Assert.AreEqual(1, exprFunction.ListExprParameters.Count, "The function call should have one parameter"); // check the parameter: its a final operand ExprFinalOperand paraFunction = exprFunction.ListExprParameters[0] as ExprFinalOperand; Assert.AreEqual("a", paraFunction.Operand, "The parameter name should be: a"); Assert.IsTrue(paraFunction.ContentType == OperandType.ObjectName, "The parameter type should be: ObjectName"); //----check the right part of the root node ExprFinalOperand operandRight = exprLogical.ExprRight as ExprFinalOperand; Assert.IsNotNull(operandRight, "The left root node type should be BoolBinExprOperand"); Assert.AreEqual(operandRight.Operand, "b", "The left operand should be A"); }
public void A_STR_s_c() { ExprScanner scanner = new ExprScanner(); // todo: parametriser stringtag: Quote ou DoubleQuote ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); config.SetStringTagCode(StringTagCode.Quote); scanner.SetConfiguration(config); string expr = "a 's c"; List <ExprToken> listTokens = scanner.SplitExpr(expr); Assert.AreEqual(2, listTokens.Count, expr + " should contains 2 tokens"); Assert.AreEqual("a", listTokens[0].Value); Assert.AreEqual("'s c", listTokens[1].Value); }
public void A_STR_s_s_s_STR_c() { ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); config.SetStringTagCode(StringTagCode.Quote); ExprScanner scanner = new ExprScanner(); scanner.SetConfiguration(config); string expr = "a 's s s' c"; List <ExprToken> listTokens = scanner.SplitExpr(expr); Assert.AreEqual(3, listTokens.Count, expr + " should contains 3 tokens"); Assert.AreEqual("a", listTokens[0].Value); Assert.AreEqual("'s s s'", listTokens[1].Value); Assert.AreEqual("c", listTokens[2].Value); }
public void fct_OP_a_Eq_b_CP_ok() { string expr = "fct(a=b)"; List <ExprToken> listTokens = TestCommon.AddTokens("fct", "(", "a", "=", "b", ")"); // decoder, renvoie un arbre de node ExprTokensParser parser = new ExprTokensParser(); ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(config); // decode the list of tokens ParseResult result = parser.Parse(expr, listTokens); // finished with no error Assert.AreEqual(0, result.ListError.Count, "The tokens should be decoded with success"); // get the root expression ExprFunctionCall rootExpr = result.RootExpr as ExprFunctionCall; Assert.IsNotNull(rootExpr, "The root node type should be ExprFunctionCall"); Assert.AreEqual("fct", rootExpr.FunctionName, "The function name should be: fct"); Assert.AreEqual(1, rootExpr.ListExprParameters.Count, "The function call should have one parameter"); // check the parameter: its a comparison expression ExprComparison exprComparison = rootExpr.ListExprParameters[0] as ExprComparison; Assert.AreEqual(OperatorComparisonCode.Equals, exprComparison.Operator, "The operator name should be: ="); // check the left part: its a final operand: a ExprFinalOperand operandLeft = exprComparison.ExprLeft as ExprFinalOperand; Assert.AreEqual("a", operandLeft.Operand, "should be: a"); Assert.IsTrue(operandLeft.ContentType == OperandType.ObjectName, "The type should be: ObjectName"); // check the right part: its a final operand: a ExprFinalOperand operandRight = exprComparison.ExprRight as ExprFinalOperand; Assert.AreEqual("b", operandRight.Operand, "should be: b"); Assert.IsTrue(operandLeft.ContentType == OperandType.ObjectName, "The type should be: ObjectName"); }
public void Group_Bra_A_Gr_Eq_B_Bra() { //>, = string expr = ">="; List <ExprToken> listTokens = TestCommon.AddTokens("(", "A", ">"); TestCommon.AddTokens(listTokens, "=", "B", ")"); ExprScanner parser = new ExprScanner(); ExpressionEvalConfig evalConfig = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(evalConfig); List <ExprToken> listTokensGrp = parser.GroupTokens(expr, listTokens); Assert.AreEqual(5, listTokensGrp.Count, expr + " should contains 5 tokens"); Assert.AreEqual(">=", listTokensGrp[2].Value, expr + " should contains '>='"); }
public void fct_OP_a_sep_b_CP_ok() { string expr = "fct(a,b)"; List <ExprToken> listTokens = TestCommon.AddTokens("fct", "(", "a", ",", "b"); TestCommon.AddTokens(listTokens, ")"); // decoder, renvoie un arbre de node ExprTokensParser parser = new ExprTokensParser(); // the default list: =, <, >, >=, <=, <> ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(config); // decode the list of tokens ParseResult result = parser.Parse(expr, listTokens); // finished with no error Assert.AreEqual(0, result.ListError.Count, "The tokens should be decoded with success"); // get the root expression ExprFunctionCall rootExpr = result.RootExpr as ExprFunctionCall; Assert.IsNotNull(rootExpr, "The root node type should be ExprFunctionCall"); Assert.AreEqual("fct", rootExpr.FunctionName, "The function name should be: fct"); Assert.AreEqual(2, rootExpr.ListExprParameters.Count, "The function call should have one parameter"); // check the parameter: its a final operand: name and type!! ExprFinalOperand paraFunction = rootExpr.ListExprParameters[0] as ExprFinalOperand; Assert.AreEqual("a", paraFunction.Operand, "The parameter name should be: a"); Assert.IsTrue(paraFunction.ContentType == OperandType.ObjectName, "The parameter type should be: ObjectName"); // check the parameter 2: its a final operand: name and type!! ExprFinalOperand paraFunction2 = rootExpr.ListExprParameters[1] as ExprFinalOperand; Assert.AreEqual("b", paraFunction2.Operand, "The parameter name should be: b"); Assert.IsTrue(paraFunction2.ContentType == OperandType.ObjectName, "The parameter type should be: ObjectName"); }
public void fct_OP_a_Eq_b_CP_ok() { string expr = "fct(not a)"; List <ExprToken> listTokens = TestCommon.AddTokens("fct", "(", "not", "a", ")"); // decoder, renvoie un arbre de node ExprTokensParser parser = new ExprTokensParser(); ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(config); // decode the list of tokens ParseResult result = parser.Parse(expr, listTokens); // finished with no error Assert.AreEqual(0, result.ListError.Count, "The tokens should be decoded with success"); // get the root expression ExprFunctionCall rootExpr = result.RootExpr as ExprFunctionCall; Assert.IsNotNull(rootExpr, "The root node type should be ExprFunctionCall"); Assert.AreEqual("fct", rootExpr.FunctionName, "The function name should be: fct"); Assert.AreEqual(1, rootExpr.ListExprParameters.Count, "The function call should have one parameter"); // check the parameter: its a logical Not expression ExprLogicalNot exprLogicalNot = rootExpr.ListExprParameters[0] as ExprLogicalNot; Assert.IsNotNull(exprLogicalNot, "The funct param should a Logical Not expression"); // check the left part: its a final operand: a ExprFinalOperand paramFunc = exprLogicalNot.ExprBase as ExprFinalOperand; Assert.AreEqual("a", paramFunc.Operand, "should be: a"); Assert.IsTrue(paramFunc.ContentType == OperandType.ObjectName, "The type should be: ObjectName"); }
public void a_Gt_fct_OP_CP_ok() { string expr = "a > fct()"; List <ExprToken> listTokens = TestCommon.AddTokens("a", ">", "fct", "(", ")"); // decoder, renvoie un arbre de node ExprTokensParser parser = new ExprTokensParser(); // the default list: =, <, >, >=, <=, <> ExpressionEvalConfig config = TestCommon.BuildDefaultConfig(); parser.SetConfiguration(config); // decode the list of tokens ParseResult result = parser.Parse(expr, listTokens); // finished with no error Assert.AreEqual(0, result.ListError.Count, "The tokens should be decoded with success"); // get the root expression ExprComparison exprComparison = result.RootExpr as ExprComparison; Assert.IsNotNull(exprComparison, "The root node type should be Comparison"); //----check the left part of the root node ExprFinalOperand operandLeft = exprComparison.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 comparison: the function call ExprFunctionCall exprFunction = exprComparison.ExprRight as ExprFunctionCall; Assert.IsNotNull(exprFunction, "The root node type should be ExprFunctionCall"); Assert.AreEqual("fct", exprFunction.FunctionName, "The function name should be: fct"); Assert.AreEqual(0, exprFunction.ListExprParameters.Count, "The function call should have any parameter"); }