Skip to content

Commit

Permalink
#23: Synergy.Behaviours: Improved generation of scenario description …
Browse files Browse the repository at this point in the history
…inside XUnit test.

{, } and " signs were not escaped properly inside generated string
  • Loading branch information
MarcinCelej committed May 16, 2024
1 parent a41a16a commit 058a16d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,46 @@ string GenerateScenarioOutlineAsXunitTheory()

private static void GenerateCurrentScenario(StringBuilder code, Scenario scenario)
{
var currentScenario = string.Join($"\",{Environment.NewLine} $\"", scenario.Lines.Select(line => Quote(line)));
var currentScenario = string.Join($"\",{Environment.NewLine} $\"", LinesOf(scenario));

code.AppendLine($" CurrentScenario(");
code.AppendLine($" $\"{currentScenario}\"");
code.AppendLine($" );");
code.AppendLine();
}

private static List<string> LinesOf(Scenario scenario)
{
var lines = new List<string>(1 + scenario.Steps.Count);
lines.Add(Quote(scenario.Line.Text));
if (scenario is ScenarioOutline)
{
var examples = ((ScenarioOutline)scenario).Examples;
var arguments = examples.Header.Values;

foreach (var line in scenario.Steps.Select(step=> step.Line.Text))
{
var tweakedLine = Quote(line);

foreach (var argument in arguments)
{
var argumentName = Sentence.ToArgument(argument);
tweakedLine = tweakedLine.Replace($"<{argument}>", "<{" + argumentName + "}>");
}

lines.Add(tweakedLine);
}

lines.Add(examples.Line.Text);
lines.Add(" | {" + string.Join("} | {", arguments.Select(argument => Sentence.ToArgument(argument))) + "} |");
}
else
{
lines.AddRange(scenario.Steps.Select(step => Quote(step.Line.Text)));
}

return lines;

string Quote(string line)
=> line
.Replace("\"", "\\\"")
Expand Down
13 changes: 1 addition & 12 deletions Behaviours/Synergy.Behaviours.Testing/Gherkin/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public record Scenario(
Line Line
)
{
public static string[] Keywords = { "Scenario", "Example" };
public static readonly string[] Keywords = { "Scenario", "Example" };

public bool IsTagged(string tag, params string[] tags)
{
Expand All @@ -20,15 +20,4 @@ public bool IsTagged(string tag, params string[] tags)

private bool IsTagged(string tag)
=> this.Tags.Any(t => t.TrimStart('@').Equals(tag.TrimStart('@'), StringComparison.InvariantCultureIgnoreCase));

internal virtual List<string> Lines
{
get
{
var lines = new List<string>(1 + this.Steps.Count);
lines.Add(this.Line.Text);
lines.AddRange(this.Steps.Select(step => step.Line.Text));
return lines;
}
}
}
27 changes: 1 addition & 26 deletions Behaviours/Synergy.Behaviours.Testing/Gherkin/ScenarioOutline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,5 @@ Line Line
Line
)
{
public new static string[] Keywords = { "Scenario Outline", "Scenario Template" };

/// <inheritdoc />
internal override List<string> Lines
{
get
{
var lines = new List<string>();

foreach (var line in base.Lines)
{
var tweakedLine = line;
foreach (var argument in this.Examples.Header.Values)
{
var argumentName = Sentence.ToArgument(argument);
tweakedLine = tweakedLine.Replace($"<{argument}>", "<{" + argumentName + "}>");
}

lines.Add(tweakedLine);
}

lines.Add(this.Examples.Line.Text);
lines.Add(" | {" + string.Join("} | {", this.Examples.Header.Values.Select(argument => Sentence.ToArgument(argument))) + "} |");
return lines;
}
}
public new static readonly string[] Keywords = { "Scenario Outline", "Scenario Template" };
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ public void AddManyNumbers(string firstNo, string second, string result) // Scen
{
CurrentScenario(
$" Scenario Outline: Add many numbers",
$" Given the first number is <{{firstNo}}>",
$" And the second number is <{{second}}>",
$" Given the first number is <{firstNo}>",
$" And the second number is <{second}>",
$" When the two {{numbers}} are added",
$" Then the result should be <{{result}}>",
$" Then the result should be <{result}>",
$" Examples:",
$" | {{firstNo}} | {{second}} | {{result}} |"
$" | {firstNo} | {second} | {result} |"
);

Background().CalculatorBackground();
Expand Down

0 comments on commit 058a16d

Please sign in to comment.