private static ExcelTestStruct findTablesInSheet(Excel.Worksheet wsCurrentTestSheet, SheetReport report) { Excel.ListObjects ListOfRanges = wsCurrentTestSheet.ListObjects; SortedList listActionTables = new SortedList(); SortedList listCheckTables = new SortedList(); SortedList listDescrTables = new SortedList(); foreach (Excel.ListObject obj in ListOfRanges) { int range = obj.Range.Row; String tableName = obj.Name; if (tableName.StartsWith(PR_TEST_TABLE_ACTION_PREFIX)) listActionTables.Add(range, tableName); else if (tableName.StartsWith(PR_TEST_TABLE_CHECK_PREFIX)) listCheckTables.Add(range, tableName); else if (tableName.StartsWith(PR_TEST_TABLE_DESCRIPTION_PREFIX)) listDescrTables.Add(range, tableName); else { report.add(new MessageReport("Unrecognised table type", obj.Range, String.Format("\"{0}\" is not an authorized name for table.", tableName), Criticity.Critical)); logger.Error(String.Format("\"{0}\" is not an authorized name for table.", tableName)); } } if(listActionTables.Count == 1 && listCheckTables.Count == 1 && listDescrTables.Count == 1) { ExcelTestStruct n = new ExcelTestStruct(); n.actionTableName = (string)listActionTables.GetByIndex(0); n.testTableName = (string)listCheckTables.GetByIndex(0); n.descrTableName = (string)listDescrTables.GetByIndex(0); return n; } else throw new NotImplementedException(String.Format("It is not currently possible to repair sheet \"{0}\"", wsCurrentTestSheet.Name)); }
public static CTestContainer parseTestsOfWorkbook(Excel.Sheets sheets, string filename) { logger.Info("Begin Analysis of selected sheets"); CTestContainer listOfTests = new CTestContainer(); WorkbookReport report = new WorkbookReport(filename); foreach (Excel.Worksheet wsCurrentTestSheet in sheets) { logger.Debug(String.Format("Processing sheet \"{0}\".", wsCurrentTestSheet.Name)); SheetReport sheetReport = new SheetReport(wsCurrentTestSheet.Name); try { ExcelTestStruct tableRefs = findTablesInSheet(wsCurrentTestSheet, sheetReport); CTest result = TestSheetParser.parseTest(wsCurrentTestSheet.Name, wsCurrentTestSheet, tableRefs, sheetReport); logger.Debug("Adding sheet to result list"); listOfTests.Add(result); } catch (Exception ex) { logger.Fatal("Sheet cannot be parsed : ", ex); sheetReport.add(new MessageReport("Parsing error", "Sheet", "Sheet was not analysed. Message is : "+ ex.Message, Criticity.Critical)); } report.add(sheetReport); } string URIFilename = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase) + Path.DirectorySeparatorChar + "Report.html"; Uri uri = new Uri(URIFilename); logger.Debug("Writing report in "+URIFilename); report.printReport(uri.LocalPath); if (report.NbrMessages > 0) { System.Diagnostics.Process.Start(URIFilename); } return listOfTests; }
public TestSheetParser(Excel.Worksheet sheet, string headerTableName, string actionsTableName, string checksTableName, SheetReport report) { this.report = report; this.sheet = sheet; if (!Regex.IsMatch(sheet.Name, PR_TEST_PREFIX + ".*")) throw new FormatException(String.Format("Sheet name doesn't comply with naming rules (begins with \"{0}\").", PR_TEST_PREFIX)); try { logger.Debug(String.Format("Trying to retrieve action table \"{0}\".", actionsTableName)); loActionsTable = sheet.ListObjects[actionsTableName]; actionTableName = String.Format("'{0}'!{1}", sheet.Name, actionsTableName); checkTableName = String.Format("'{0}'!{1}", sheet.Name, checksTableName); logger.Debug(String.Format("Extracting columns for action table.")); lcActionsTableColumns = loActionsTable.ListColumns; } catch (Exception ex) { logger.Error(String.Format("Action table \"{0}\" retrieval has failed.", actionsTableName), ex); throw new FormatException(String.Format("Action table \"{0}\" retrieval has failed.", actionsTableName)); } try { logger.Debug(String.Format("Trying to retrieve test table \"{0}\".", checksTableName)); loChecksTable = sheet.ListObjects[checksTableName]; logger.Debug(String.Format("Extracting columns for checks table.")); lcChecksTableColumns = loChecksTable.ListColumns; } catch (Exception ex) { logger.Error(String.Format("Check table \"{0}\" retrieval has failed.", checksTableName), ex); throw new FormatException(String.Format("Check table \"{0}\" retrieval has failed.", checksTableName)); } if(lcActionsTableColumns.Count != lcChecksTableColumns.Count) throw new FormatException(String.Format("Action ({0} columns) and check ({1} columns) tables has not same number of columns", lcActionsTableColumns.Count, lcChecksTableColumns.Count)); }
public static CTest parseTest(string title, Excel.Worksheet sheet, WorkbookParser.ExcelTestStruct tableRefs, SheetReport report) { logger.Info(String.Format("Beginning Analysis of sheet {0}, using arrays {1} and {2}", sheet.Name, tableRefs.actionTableName, tableRefs.testTableName)); TestSheetParser analyser = new TestSheetParser(sheet, tableRefs.descrTableName, tableRefs.actionTableName, tableRefs.testTableName, report); logger.Debug("Sheet passed validity tests successfully"); CTest test = null; test = analyser.parseAsTest(title); return test; }