From 049aa7dff82650472f77edd75cb5780b961ab553 Mon Sep 17 00:00:00 2001 From: prashelke Date: Thu, 11 Jul 2024 20:06:08 +0530 Subject: [PATCH] D39652_D39651_D39645 Mock Data Defect Fixed --- .../GingerCoreNET/RosLynLib/CodeProcessor.cs | 86 ++++++++++++----- .../ValueExpressionLib/ValueExpression.cs | 93 +++++++++++-------- .../RosLynTestLib/GlobalsTest.cs | 11 ++- 3 files changed, 125 insertions(+), 65 deletions(-) diff --git a/Ginger/GingerCoreNET/RosLynLib/CodeProcessor.cs b/Ginger/GingerCoreNET/RosLynLib/CodeProcessor.cs index e2ddda76d7..78bc229c8f 100644 --- a/Ginger/GingerCoreNET/RosLynLib/CodeProcessor.cs +++ b/Ginger/GingerCoreNET/RosLynLib/CodeProcessor.cs @@ -19,14 +19,17 @@ limitations under the License. using Amdocs.Ginger.Common; using Amdocs.Ginger.CoreNET.RosLynLib; using Amdocs.Ginger.Repository; +using DocumentFormat.OpenXml.Office2010.ExcelAc; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; +using OfficeOpenXml.Drawing.Slicer.Style; using System; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Reflection.Metadata; +using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -65,13 +68,19 @@ public static string GetResult(string Expression) "((?'Close-Open'})[^{}]*)+" + ")*" + "(?(Open)(?!))"; - pattern = "{CS({.*}|[^{}]*)*}"; + Regex CsExppattern = new Regex("{CS Exp({.*}|[^{}]*)*}", RegexOptions.Compiled); Pattern = new Regex(pattern); Regex Clean = new Regex("{CS(\\s)*Exp(\\s)*="); + MatchCollection PatternMatchlist = CsExppattern.Matches(Expression); + if (PatternMatchlist == null || PatternMatchlist.Count == 0) + { + Reporter.ToLog(eLogLevel.DEBUG, Expression + System.Environment.NewLine + " not a valid c# expression to evaluate"); + return string.Empty; + } - foreach (Match M in Pattern.Matches(Expression)) + foreach (Match M in PatternMatchlist) { string csharpError = ""; string match = M.Value; @@ -82,9 +91,7 @@ public static string GetResult(string Expression) string evalresult = GetEvaluteResult(exp, out csharpError); Expression = Expression.Replace(match, evalresult); } - return Expression; - } public static string GetEvaluteResult(string expression, out string error) @@ -105,6 +112,15 @@ public static string GetEvaluteResult(string expression, out string error) { evalresult = result.ToString().ToLower(); } + else if(result.GetType() == typeof(string)) + { + evalresult = result.ToString(); + } + else if(result.GetType() == typeof(string[])) + { + evalresult = string.Join(",", (string[])result); + evalresult = $"[{evalresult}]"; + } else { evalresult = result.ToString(); @@ -254,26 +270,40 @@ public static object ExecuteNew(string code) /// public static string GetBogusDataGenerateresult(string Expression) { - if (!Expression.Contains(@"{MockDataExp")) + try { + if (!Expression.Contains(@"{MockDataExp")) + { + return Expression; + } + Pattern = new Regex("^\\{MockDataExp Fun=.*\\}$", RegexOptions.Compiled, new TimeSpan(0, 0, 5)); + Regex Clean = new Regex("{MockDataExp(\\s)*Fun(\\s)*=", RegexOptions.Compiled); + MatchCollection PatternMatchlist = Pattern.Matches(Expression); + if (PatternMatchlist == null || PatternMatchlist.Count == 0) + { + Reporter.ToLog(eLogLevel.DEBUG, Expression + System.Environment.NewLine + " not a valid c# expression to evaluate"); + return string.Empty; + } + + foreach (Match M in PatternMatchlist) + { + string Error = ""; + string match = M.Value; + string exp = match; + exp = exp.Replace(Clean.Match(exp).Value, ""); + exp = exp.Remove(exp.Length - 1); + string evalresult = GetBogusExpressionEvaluteResult(exp, out Error); + Expression = Expression.Replace(match, evalresult); + } return Expression; } - const string pattern = "{MockDataExp({.*}|[^{}]*)*}"; - Pattern = new Regex(pattern, RegexOptions.Compiled); - Regex Clean = new Regex("{MockDataExp(\\s)*Fun(\\s)*=", RegexOptions.Compiled); - - foreach (Match M in Pattern.Matches(Expression)) + catch(RegexMatchTimeoutException ex) { - string Error = ""; - string match = M.Value; - string exp = match; - exp = exp.Replace(Clean.Match(exp).Value, ""); - exp = exp.Remove(exp.Length - 1); - string evalresult = GetBogusExpressionEvaluteResult(exp, out Error); - Expression = Expression.Replace(match, evalresult); + Reporter.ToLog(eLogLevel.ERROR, "Timeout Exception", ex); + return string.Empty; } - return Expression; } + /// /// The GetBogusExpressionEvaluteResult function is responsible for evaluating and generating data using Bogus library expressions based on the provided expression. /// It handles different types of expressions related to data generation and returns the evaluated result as a string. @@ -285,9 +315,9 @@ public static string GetBogusDataGenerateresult(string Expression) /// /// /// - public static string GetBogusExpressionEvaluteResult(string expression, out string error) + public static string GetBogusExpressionEvaluteResult(string expression, out string evalresult) { - error = string.Empty; + evalresult = string.Empty; try { Assembly BogusAssembly = Assembly.Load("Bogus"); @@ -335,13 +365,25 @@ public static string GetBogusExpressionEvaluteResult(string expression, out stri } object result = CSharpScript.EvaluateAsync(expression, ScriptOptions.Default.WithReferences(BogusAssembly)).Result; - return result.ToString(); + //c# generate True/False for bool.tostring which fails in subsequent expressions + if (result == null) + { + Reporter.ToLog(eLogLevel.DEBUG, $"{expression} evaluation returned null value"); + } + else if (result.GetType() == typeof(int[])) + { + evalresult = string.Join(",", (int[])result); + } + else + { + evalresult = result.ToString(); + } } catch (Exception e) { Reporter.ToLog(eLogLevel.DEBUG, expression + System.Environment.NewLine + " not a valid Bogus data generate expression to evaluate", e); } - return error; + return evalresult; } } } diff --git a/Ginger/GingerCoreNET/ValueExpressionLib/ValueExpression.cs b/Ginger/GingerCoreNET/ValueExpressionLib/ValueExpression.cs index aee5ebcf57..38d59706b4 100644 --- a/Ginger/GingerCoreNET/ValueExpressionLib/ValueExpression.cs +++ b/Ginger/GingerCoreNET/ValueExpressionLib/ValueExpression.cs @@ -30,11 +30,13 @@ limitations under the License. using GingerCore.GeneralLib; using GingerCore.Variables; using GingerCoreNET.RosLynLib; +using SikuliStandard.sikuli_REST; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; +using System.Net; using System.Reflection; using System.Text.RegularExpressions; @@ -108,6 +110,8 @@ public class ValueExpression : IValueExpression private static Regex rxe = new Regex(@"{RegEx" + rxVare + ".*}", RegexOptions.Compiled | RegexOptions.Singleline); private static Regex rNestedfunc = new Regex("{Function(\\s)*Fun(\\s)*=(\\s)*([a-zA-Z]|\\d)*\\(([^()])*\\)}", RegexOptions.Compiled); + private static Regex MockDataExpPattern = new Regex("^\\{MockDataExp Fun=.*\\}$", RegexOptions.Compiled, new TimeSpan(0, 0, 5)); + private static Regex CsExppattern = new Regex("{CS Exp({.*}|[^{}]*)*}", RegexOptions.Compiled); // Enable setting value simply by assigned string, // so no need to create new VE class everywhere in code @@ -1728,50 +1732,59 @@ private void ReplaceVarWithValue(string p, string[] a) private bool ContainsFormula(string mValueCalculated) { - if (rxGlobalParamPattern.IsMatch(mValueCalculated)) - { - return true; - } - else if (rxEnvParamPattern.IsMatch(mValueCalculated)) - { - return true; - } - else if (rxEnvUrlPattern.IsMatch(mValueCalculated)) - { - return true; - } - else if (rxe.IsMatch(mValueCalculated)) - { - return true; - } - else if (VBSRegex.IsMatch(mValueCalculated)) - { - return true; - } - else if (rNestedfunc.IsMatch(mValueCalculated)) - { - return true; - } - else if (rxDSPattern.IsMatch(mValueCalculated)) - { - return true; - } - else if (rxFDPattern.IsMatch(mValueCalculated)) - { - return true; - } - else if (rxExecutionJsonDataPattern.IsMatch(mValueCalculated)) + try { - return true; + if (rxGlobalParamPattern.IsMatch(mValueCalculated)) + { + return true; + } + else if (rxEnvParamPattern.IsMatch(mValueCalculated)) + { + return true; + } + else if (rxEnvUrlPattern.IsMatch(mValueCalculated)) + { + return true; + } + else if (rxe.IsMatch(mValueCalculated)) + { + return true; + } + else if (VBSRegex.IsMatch(mValueCalculated)) + { + return true; + } + else if (rNestedfunc.IsMatch(mValueCalculated)) + { + return true; + } + else if (rxDSPattern.IsMatch(mValueCalculated)) + { + return true; + } + else if (rxFDPattern.IsMatch(mValueCalculated)) + { + return true; + } + else if (rxExecutionJsonDataPattern.IsMatch(mValueCalculated)) + { + return true; + } + else if (CsExppattern.IsMatch(mValueCalculated)) + { + return true; + } + else if (MockDataExpPattern.IsMatch(mValueCalculated)) + { + return true; + } } - else if (mValueCalculated.Contains(@"{CS")) + catch (RegexMatchTimeoutException ex) { - return true; - } - else if(mValueCalculated.Contains(@"{MockDataExp")) - { - return true; + Reporter.ToLog(eLogLevel.ERROR, "Timeout Exception", ex); + return false; } + return false; } diff --git a/Ginger/GingerCoreNETUnitTest/RosLynTestLib/GlobalsTest.cs b/Ginger/GingerCoreNETUnitTest/RosLynTestLib/GlobalsTest.cs index a1204c2920..748cc6d55a 100644 --- a/Ginger/GingerCoreNETUnitTest/RosLynTestLib/GlobalsTest.cs +++ b/Ginger/GingerCoreNETUnitTest/RosLynTestLib/GlobalsTest.cs @@ -20,7 +20,12 @@ limitations under the License. using GingerCoreNET.RosLynLib; using GingerTestHelper; using Microsoft.VisualStudio.TestTools.UnitTesting; +using NPOI.SS.Formula.Functions; +using OpenTracing.Tag; using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; namespace GingerCoreNETUnitTest.RosLynTestLib { @@ -300,7 +305,7 @@ public void TestBogusData_RandomizerNumber_IsNotnullAndEmpty() string Expression = "{MockDataExp Fun=Randomizer().Number(1,10);}"; string error = string.Empty; string output = CodeProcessor.GetBogusDataGenerateresult(Expression); - Assert.IsTrue(output != null && !output.Equals(string.Empty)); + Assert.IsTrue(output != null && !output.Equals(string.Empty) && Regex.IsMatch(output, @"\d")); } [TestMethod] @@ -309,7 +314,7 @@ public void TestBogusData_RandomizerDigits_IsNotnullAndEmpty() string Expression = "{MockDataExp Fun=Randomizer().Digits(3,0,9);}"; string error = string.Empty; string output = CodeProcessor.GetBogusDataGenerateresult(Expression); - Assert.IsTrue(output != null && !output.Equals(string.Empty)); + Assert.IsTrue(output != null && !output.Equals(string.Empty) && output.Split(",").Select(i => i.Trim()).All(x=> Regex.IsMatch(x, @"\d"))); } [TestMethod] @@ -318,7 +323,7 @@ public void TestBogusData_RandomizerDecimal_IsNotnullAndEmpty() string Expression = "{MockDataExp Fun=Randomizer().Decimal();}"; string error = string.Empty; string output = CodeProcessor.GetBogusDataGenerateresult(Expression); - Assert.IsTrue(output != null && !output.Equals(string.Empty)); + Assert.IsTrue(output != null && !output.Equals(string.Empty) && Regex.IsMatch(output, "^[+-]?(\\d*\\.)?\\d+$")); } [TestMethod]