Skip to content

Commit

Permalink
Merge pull request #9 from Servan42/develop
Browse files Browse the repository at this point in the history
Version 1.2.0
  • Loading branch information
Servan42 committed Jan 18, 2024
2 parents 7b77dcd + ef75efb commit 5f3f69e
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 98 deletions.
36 changes: 32 additions & 4 deletions GherkinSimpleParser.Converter/CSVConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,53 @@ public List<string> ExportAsCSV(string separator, GherkinObject gherkinObj)

if (gherkinObj.Background.Givens.Count > 0)
{
string givenBackground = string.Join(separator, gherkinObj.Background.Givens.Prepend("GENERAL PREREQUISITES:"));
string givenBackground = $"GENERAL PREREQUISITES:{separator}{BuildInstructionBatchLine(separator, gherkinObj.Background.Givens)}";
csv.Add($";{givenBackground};;");
}

int testCount = 1;
foreach (var scenario in gherkinObj.Scenarios)
{
csv.Add($"{testCount};{scenario.Name};;");
csv.Add($";{string.Join(separator, scenario.Givens)};{scenario.When};{string.Join(separator, scenario.Thens)}");
string givenCol = BuildInstructionBatchLine(separator, scenario.Givens);
string whenCol = scenario.When;
string thenCol = BuildInstructionBatchLine(separator, scenario.Thens);
csv.Add($";{givenCol};{whenCol};{thenCol}");
testCount++;
}

return csv;
}

private string BuildInstructionBatchLine(string separator, List<Instruction> instructions)
{
var flattened = new List<string>();
foreach(var instruction in instructions)
{
var sb = new StringBuilder();
sb.Append(instruction.MainLine);
if(instruction.DocStrings.Count > 0)
{
sb.Append(" \"").Append(string.Join(' ', instruction.DocStrings)).Append('"');
}
flattened.Add(sb.ToString());
}
return string.Join(separator, flattened);
}

[Obsolete("Deprecated: Will be removed in future versions. Use/modify the ExcelConverter to your needs instead.")]
public List<string> ExportAsCSVWithExcelFormulaWrap_FR(GherkinObject gherkinObj)
{
return ExportAsCSVWithExcelFormulaWrap("\" & CAR(10) & \"", gherkinObj);
}

[Obsolete("Deprecated: Will be removed in future versions. Use/modify the ExcelConverter to your needs instead.")]
public List<string> ExportAsCSVWithExcelFormulaWrap_EN(GherkinObject gherkinObj)
{
return ExportAsCSVWithExcelFormulaWrap("\" & CHAR(10) & \"", gherkinObj);
}

[Obsolete("Deprecated: Will be removed in future versions. Use/modify the ExcelConverter to your needs instead.")]
private List<string> ExportAsCSVWithExcelFormulaWrap(string separator, GherkinObject gherkinObj)
{
var csv = new List<string>
Expand All @@ -51,15 +73,21 @@ private List<string> ExportAsCSVWithExcelFormulaWrap(string separator, GherkinOb

if (gherkinObj.Background.Givens.Count > 0)
{
string givenBackground = string.Join(separator, gherkinObj.Background.Givens.Prepend("GENERAL PREREQUISITES:").Select(g => g.Replace("\"", "\"\"")));
string givenBackground = string.Join(separator,
gherkinObj.Background.Givens
.Prepend(new Instruction("GENERAL PREREQUISITES:"))
.Select(g => g.MainLine.Replace("\"", "\"\"")));
csv.Add($";=\"{givenBackground}\";;");
}

int testCount = 1;
foreach (var scenario in gherkinObj.Scenarios)
{
csv.Add($"{testCount};{scenario.Name};;");
csv.Add($";=\"{string.Join(separator, scenario.Givens.Select(g => g.Replace("\"", "\"\"")))}\";{scenario.When};=\"{string.Join(separator, scenario.Thens.Select(g => g.Replace("\"", "\"\"")))}\"");
string givenCol = string.Join(separator, scenario.Givens.Select(g => g.MainLine.Replace("\"", "\"\"")));
string whenCol = scenario.When;
string thenCol = string.Join(separator, scenario.Thens.Select(g => g.MainLine.Replace("\"", "\"\"")));
csv.Add($";=\"{givenCol}\";{whenCol};=\"{thenCol}\"");
testCount++;
}

Expand Down
64 changes: 37 additions & 27 deletions GherkinSimpleParser.Converter/ExcelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,18 @@ private void AppendSheetsToExcelFile(List<GherkinObject> featuresAndScenarios, E
}
}

private void SetColor(ExcelWorksheet ws, string range, Color color)
{
ws.Cells[range].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[range].Style.Fill.BackgroundColor.SetColor(color);
}

private void InsertSheetHeader(GherkinObject featureAndScenarios, ExcelWorksheet ws)
{
ws.Cells["B2"].Value = featureAndScenarios.FeatureName;
ws.Cells["B2:E2"].Merge = true;
ws.Cells["B2:E2"].Style.Font.Bold = true;
ws.Cells["B2:E2"].Style.Font.Size = 20;
ws.Cells["B2:E2"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
ws.Cells["B2:E2"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.AlignCenter("B2:E2", true, true);

ws.Cells["A4:F4"].Style.Font.Bold = true;
ws.Cells["A4:F4"].Style.Font.Size = 12;
ws.Cells["A4:F4"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
ws.Cells["A4:F4"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
SetColor(ws, "A4:F4", Color.FromArgb(255, 68, 114, 196));
ws.AlignCenter("A4:F4", true, true);
ws.SetColor("A4:F4", Color.FromArgb(255, 68, 114, 196));
ws.Cells["A4"].Value = "Number";
ws.Cells["B4"].Value = "GIVEN";
ws.Cells["C4"].Value = "WHEN";
Expand All @@ -76,39 +68,57 @@ private void InsertGherkinObjectInSheet(GherkinObject featureAndScenarios, Excel
{
var generalPrerequisite = new StringBuilder();
generalPrerequisite.AppendLine("GENERAL PREREQUISITES:");
foreach (var prerequisite in featureAndScenarios.Background.Givens)
{
generalPrerequisite.Append("- ");
generalPrerequisite.AppendLine(prerequisite);
}
generalPrerequisite.AppendLine(BuildInstructionBatch(featureAndScenarios.Background.Givens));

ws.Cells[$"B{lineToFillId}"].Value = generalPrerequisite.ToString();
ws.Cells[$"B{lineToFillId}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
SetColor(ws, $"A{lineToFillId}:F{lineToFillId}", Color.FromArgb(255, 180, 198, 231));
ws.AlignCenter($"B{lineToFillId}", true, false);
ws.SetColor($"A{lineToFillId}:F{lineToFillId}", Color.FromArgb(255, 180, 198, 231));
lineToFillId++;
}

int testId = 1;
foreach (var scenario in featureAndScenarios.Scenarios)
{
ws.Cells[$"A{lineToFillId}"].Value = testId.ToString();
ws.Cells[$"A{lineToFillId}:F{lineToFillId}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.Cells[$"A{lineToFillId}"].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
ws.Cells[$"A{lineToFillId}"].Value = testId;
ws.AlignCenter($"A{lineToFillId}:F{lineToFillId}", true, false);
ws.AlignCenter($"A{lineToFillId}", false, true);

ws.Cells[$"B{lineToFillId}"].Value = scenario.Name;
SetColor(ws, $"A{lineToFillId}:F{lineToFillId}", Color.FromArgb(255, 180, 198, 231));
ws.SetColor($"A{lineToFillId}:F{lineToFillId}", Color.FromArgb(255, 180, 198, 231));
lineToFillId++;

string givens = string.Join('\n', scenario.Givens.Select(x => "- " + x));
ws.Cells[$"B{lineToFillId}"].Value = givens;
ws.Cells[$"B{lineToFillId}"].Value = BuildInstructionBatch(scenario.Givens);
ws.Cells[$"C{lineToFillId}"].Value = scenario.When;
string thens = string.Join('\n', scenario.Thens.Select(x => "- " + x));
ws.Cells[$"D{lineToFillId}"].Value = thens;
ws.Cells[$"A{lineToFillId}:F{lineToFillId}"].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
ws.Cells[$"D{lineToFillId}"].Value = BuildInstructionBatch(scenario.Thens);
ws.AlignCenter($"A{lineToFillId}:F{lineToFillId}", true, false);

lineToFillId++;
testId++;
}
}

private string BuildInstructionBatch(List<Instruction> instructions)
{
StringBuilder sb = new();
foreach (var instruction in instructions)
{
sb.Append("- ").AppendLine(instruction.MainLine);
if (instruction.DocStrings.Count > 0)
{
sb.AppendLine("\"");
foreach (var docstring in instruction.DocStrings)
sb.AppendLine(docstring);
sb.AppendLine("\"");
}
}
return RemoveLastNewLine(sb.ToString());
}

private string RemoveLastNewLine(string s)
{
if (s.Length < 2)
return s;
return s.Substring(0, s.Length - 2);
}
}
}
45 changes: 45 additions & 0 deletions GherkinSimpleParser.Converter/ExcelExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using OfficeOpenXml.Style;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GherkinSimpleParser.Converter
{
public static class ExcelExtensions
{
public static void SetGridBorder(this ExcelWorksheet ws, ExcelBorderStyle borderStyle, int startRow, int endRow, int startCol, int endCol)
{
for (int row = startRow; row <= endRow; row++)
{
for (int col = startCol; col <= endCol; col++)
{
GetCellAt(ws, row, col).Style.Border.BorderAround(borderStyle);
}
}
}

public static ExcelRange GetCellAt(this ExcelWorksheet ws, int row, int col)
{
char colChar = (char)('A' - 1 + col);
return ws.Cells[$"{colChar}{row}"];
}

public static void SetColor(this ExcelWorksheet ws, string range, Color color)
{
ws.Cells[range].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[range].Style.Fill.BackgroundColor.SetColor(color);
}

public static void AlignCenter(this ExcelWorksheet ws, string range, bool vertical, bool horizontal)
{
if (horizontal)
ws.Cells[range].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
if (vertical)
ws.Cells[range].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>1.2.0</Version>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<Title>Converter to easily write Gherkin data into files</Title>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
57 changes: 42 additions & 15 deletions GherkinSimpleParser.Tests/ExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,42 @@ public void Setup()
FeatureName = "feature name",
Background = new Background
{
Givens = new List<string>
Givens = new List<Instruction>
{
"Prerequisite_0.1",
"Prere\"q\"uisite_0.2"
new("Prerequisite_0.1"),
new("Prere\"q\"uisite_0.2")
}
},
Scenarios = new List<Scenario>
{
new Scenario
{
Name = "Test Case 1",
Givens = new List<string>
Givens = new List<Instruction>
{
"Prerequisite_1.1",
"Prere\"q\"uisite_1.2"
new("Prerequisite_1.1"),
new("Prere\"q\"uisite_1.2")
},
When = "Action_1",
Thens = new List<string>
Thens = new List<Instruction>
{
"Result_1.1",
"Resu\"l\"t_1.2"
new("Result_1.1"),
new("Resu\"l\"t_1.2")
},
},
new Scenario
{
Name = "Test Case 2",
Givens = new List<string>
Givens = new List<Instruction>
{
"Prerequisite_2.1",
"Prere\"q\"uisite_2.2"
new("Prerequisite_2.1"),
new("Prere\"q\"uisite_2.2")
},
When = "Action_2",
Thens = new List<string>
Thens = new List<Instruction>
{
"Result_2.1",
"Resu\"l\"t_2.2"
new("Result_2.1"),
new("Resu\"l\"t_2.2")
}
}
}
Expand Down Expand Up @@ -87,6 +87,33 @@ public void Should_export_as_CSV()
csvResult);
}

[Test]
public void Should_export_as_CSV_withdocstrings()
{
// Given
var converter = new CSVConverter();
gherkinObject.Scenarios.First().Givens.First().DocStrings = new List<string>
{
"docstrings1",
"docstrings2",
};

// When
var csvResult = converter.ExportAsCSV("|", gherkinObject);

// Then
CollectionAssert.AreEqual(new List<string>()
{
"Number;GIVEN;WHEN;THEN",
";GENERAL PREREQUISITES:|Prerequisite_0.1|Prere\"q\"uisite_0.2;;",
"1;Test Case 1;;",
";Prerequisite_1.1 \"docstrings1 docstrings2\"|Prere\"q\"uisite_1.2;Action_1;Result_1.1|Resu\"l\"t_1.2",
"2;Test Case 2;;",
";Prerequisite_2.1|Prere\"q\"uisite_2.2;Action_2;Result_2.1|Resu\"l\"t_2.2"
},
csvResult);
}

[Test]
public void Should_export_as_CSV_excel_formula_wrap_french()
{
Expand Down
Loading

0 comments on commit 5f3f69e

Please sign in to comment.