public static void AddProcedure(ref List <string> fileContent, string procedureNameToCall, bool addException) { TestALCodeunit testCodeunit = (TestALCodeunit) new ALTestCodeunitReader().Read(fileContent); if (!testCodeunit.Methods.Exists(method => method.Name.ToLower() == procedureNameToCall.ToLower())) { int lineToInsertProcedure = FindLineToInsertProcedure(testCodeunit, fileContent); if (lineToInsertProcedure != 0) { List <string> methodContentLinesOnly = new List <string>(); if (addException) { methodContentLinesOnly.Add(string.Format("Error('Procedure {0} not yet implemented.');", procedureNameToCall)); } List <string> methodContentLines = Library.ALMethodHelper.WriteMethodBody(methodContentLinesOnly); string methodContent = String.Join("\r\n", methodContentLines); ALMethod alMethod = new ALMethod { IsLocal = true, Name = procedureNameToCall, MethodBody = new ALMethodBody { Content = methodContent, ContentLines = methodContentLines }, Content = methodContent, ReturnTypeDefinition = new ALReturnTypeDefinition() }; fileContent.Insert(lineToInsertProcedure, Library.ALMethodHelper.Write(alMethod)); } } }
public static string[] GetDefaultTestCodeunitText(string elementValue) { string newProcedureName = "NewTestProcedure"; TestALMethod testalMethod = new TestALMethod { MethodKind = ALMethodKind.Method, Name = newProcedureName, Scenario = new TestScenario { ID = 1, Name = "New Test Procedure", Feature = new TestFeature { Name = elementValue } }, TestMethod = true }; testalMethod.Scenario.Feature.Scenarios.Add(testalMethod.Scenario); testalMethod.Attributes.Add(new ALAttribute { Name = "Test" }); string codeunitName = elementValue.Contains(' ') ? '"' + elementValue + '"' : elementValue; TestALCodeunit testALCodeunit = new TestALCodeunit { Type = ALObjectParser.ALObjectType.codeunit, Id = 0, Name = codeunitName }; testALCodeunit.Methods.Add(testalMethod); testALCodeunit.Features.Add(testalMethod.Scenario.Feature); testALCodeunit.Properties.Add(new ALProperty { Name = "Subtype", Value = "Test" }); ALTestCodeunitWriter aLTestCodeunitWriter = new ALTestCodeunitWriter(); return(aLTestCodeunitWriter.Write(testALCodeunit).Split("\r\n")); }
private void InitializeSaveChanges(MessageUpdate msg, Configurations config, out TestALMethod testALMethod, out int scenarioLine, out int elementLine, bool elementOfNewValue, out List <string> fileContent, out ScenarioElementType scenarioElementType, out string oldProcedureNameOfElement, out string newProcedureNameOfElement) { string scenarioName = msg.Scenario; string fsPath = msg.FsPath; string elementOldValue = msg.OldValue; string elementNewValue = msg.NewValue; TypeChanged typeChanged = msg.Type; ALTestCodeunitReader alTestCodeunitReader = new ALTestCodeunitReader(); TestALCodeunit testCodeunit = (TestALCodeunit)alTestCodeunitReader.ReadSingle(fsPath); testALMethod = testCodeunit.Methods.First(m => (m.Scenario != null) && (m.Scenario.Name == scenarioName)); scenarioElementType = Library.ALMethodHelper.ToEnum <ScenarioElementType>(typeChanged.ToString().ToUpper()); if (elementOldValue == null) { oldProcedureNameOfElement = ""; } else { oldProcedureNameOfElement = Library.ALMethodHelper.GetProcedurename(scenarioElementType, elementOldValue, config.prefixGiven, config.prefixWhen, config.prefixThen); } if (elementNewValue == null) { newProcedureNameOfElement = ""; } else { newProcedureNameOfElement = Library.ALMethodHelper.GetProcedurename(scenarioElementType, elementNewValue, config.prefixGiven, config.prefixWhen, config.prefixThen); } fileContent = File.ReadAllLines(fsPath).ToList(); elementLine = 0; scenarioLine = Library.Helpers.ALTestCodeunitHelper.FindScenarioLine(testALMethod.Scenario.Name, fileContent); if (scenarioLine == 0) { return; } if (elementOfNewValue) { elementLine = Library.Helpers.ALTestCodeunitHelper.FindElementLine(fileContent, scenarioElementType, elementNewValue, scenarioLine); } else { elementLine = Library.Helpers.ALTestCodeunitHelper.FindElementLine(fileContent, scenarioElementType, elementOldValue, scenarioLine); } if (elementLine == 0) { return; } }
private static int FindLineToInsertProcedure(TestALCodeunit testCodeunit, List <string> fileContent) { int lineToInsertProcedure = 0; IEnumerable <TestALMethod> helperFunctions = testCodeunit.Methods.Where(m => m.Attributes.Count == 0); if (helperFunctions.Count() == 0) { helperFunctions = testCodeunit.Methods.Where(m => m.Attributes.Where(a => a.Name.ToLower() == "test").Count() > 0); } if (helperFunctions.Count() > 0) { string lastProcedureName = helperFunctions.Last().Name; int lineOfDeclarationOfLastProcedure = 0; for (int i = 0; i < fileContent.Count; i++) { if (Regex.IsMatch(fileContent[i], @"\s*(local )?" + lastProcedureName, RegexOptions.IgnoreCase)) { lineOfDeclarationOfLastProcedure = i; break; } } if (lineOfDeclarationOfLastProcedure == 0) { return(0); } for (int i = lineOfDeclarationOfLastProcedure; i < fileContent.Count; i++) { if (Regex.IsMatch(fileContent[i], @"\s{4}end;", RegexOptions.IgnoreCase)) { lineToInsertProcedure = i + 1; } } } return(lineToInsertProcedure); }
public static void DeleteElementWithProcedureCall(ref List <string> fileContent, string procedureNameOfElement, int elementLine) { TestALCodeunit codeunit = (TestALCodeunit) new ALTestCodeunitReader().Read(fileContent); TestALMethod method = ALTestCodeunitHelper.GetMethodAtLine(codeunit, elementLine); string commentLineText = fileContent[elementLine]; ITestScenarioElement element = method.Scenario.Elements.First(element => element.LineText.Trim() == commentLineText.Trim()); int deleteRange = 1; for (int line = elementLine + 1; line < fileContent.Count; line++) { if (Regex.IsMatch(fileContent[line], @"\s*//\s*\[.+\].*")) { deleteRange = line - elementLine; break; } if (Regex.IsMatch(fileContent[line], "^ end;", RegexOptions.IgnoreCase)) { deleteRange = line - elementLine; break; } } fileContent.RemoveRange(elementLine, deleteRange); }
public static TestALMethod GetMethodAtLine(TestALCodeunit codeunit, int line) { return(codeunit.Methods.First(method => method.MethodRange.Start.Value <= line && line <= method.MethodRange.End.Value)); }