Skip to content

Commit

Permalink
#23: BDD: Fixed error when scenario line contains bracket { or }
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Feb 1, 2024
1 parent 7f607c1 commit 4213195
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,18 @@ string GenerateScenarioOutlineAsXunitTheory()

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

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

string Quote(string line)
=> line
.Replace("\"", "\\\"")
.Replace("{", "{{")
.Replace("}", "}}");
}

private void GenerateSteps(StringBuilder code, List<Step> steps)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ 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}>",
$" When the two numbers are added",
$" Then the result should be <{result}>",
$" Given the first number is <{{firstNo}}>",
$" And the second number is <{{second}}>",
$" When the two {{numbers}} are added",
$" Then the result should be <{{result}}>",
$" Examples:",
$" | {firstNo} | {second} | {result} |"
$" | {{firstNo}} | {{second}} | {{result}} |"
);

Background().CalculatorBackground();

Given().TheFirstNumberIs(firstNo); // Given the first number is <first no>
And().TheSecondNumberIs(second); // And the second number is <second>
When().TheTwoNumbersAreAdded(); // When the two numbers are added
When().TheTwoNumbersAreAdded(); // When the two {numbers} are added
Then().TheResultShouldBe(result); // Then the result should be <result>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Feature: Calculator
Scenario Outline: Add many numbers
Given the first number is <first no>
And the second number is <second>
When the two numbers are added
When the two {numbers} are added
Then the result should be <result>
Examples:
| first no | second | result |
Expand All @@ -48,7 +48,7 @@ Feature: Calculator
Then the result should be 25

@Subtract
Scenario Template: : Subtract two numbers
Scenario Template: Subtract two numbers
Given the first number is <first>
And the second number is <second>
When the two numbers are subtracted
Expand Down

0 comments on commit 4213195

Please sign in to comment.