Skip to content

Commit

Permalink
#23: Fixed error in partial method call as last step of fluent method…
Browse files Browse the repository at this point in the history
…s chain - entire chain was ignored.
  • Loading branch information
MarcinCelej committed May 4, 2023
1 parent 79c4c23 commit 9d15ea6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
55 changes: 28 additions & 27 deletions Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void Generate<TBehaviour>(
{
var code = feature.Generate(from, include, exclude, callerFilePath);
var destinationFilePath = Path.Combine(Path.GetDirectoryName(callerFilePath), to);
File.WriteAllText(destinationFilePath, code.ToString());
File.WriteAllText(destinationFilePath, code);
}

public static string Generate<TBehaviour>(
Expand Down Expand Up @@ -124,8 +124,8 @@ public static string Generate<TBehaviour>(

backgroundMethod = FeatureGenerator.ToMethod(ruleName ?? featureName ?? "Feature") + "Background";
backgroundStarted = backgroundMethod;
code.AppendLine($" private {className} {backgroundMethod}() // {line.Trim()}");
code.Append($" => ");
code.AppendLine($" private void {backgroundMethod}() // {line.Trim()}");
code.AppendLine(" {");
continue;
}

Expand Down Expand Up @@ -178,65 +178,64 @@ public static string Generate<TBehaviour>(
if (tags != null)
code.AppendLine($" // {String.Join(" ", tags)}");
code.AppendLine($" public void {scenarioMethod}() // {line.Trim()}");
code.Append($" => {Background}()");
if (backgroundMethod == null)
code.AppendLine();
else
{
code.AppendLine($".{backgroundMethod}()");
}
code.AppendLine(" {");
var backgroundCall = "";
if (backgroundMethod != null)
backgroundCall = $".{backgroundMethod}()";
code.AppendLine($" {Background}(){backgroundCall};");
code.AppendLine();

continue;
}

var given = Regex.Match(line, "\\s*Given (.*)");
if (given.Success)
{
if (backgroundStarted == null)
{
code.Append($" .");
}
else
{
code.Append($" ");
}

code.AppendLine($"{Given}().{FeatureGenerator.ToMethod(given.Groups[1].Value)}() // {line.Trim()}");
// if (backgroundStarted == null)
// {
// code.Append($" ");
// }
// else
// {
// code.Append($" ");
// }

code.AppendLine($" {Given}().{FeatureGenerator.ToMethod(given.Groups[1].Value)}(); // {line.Trim()}");
continue;
}

var and = Regex.Match(line, "\\s*And (.*)");
if (and.Success)
{
code.AppendLine($" .{And}().{FeatureGenerator.ToMethod(and.Groups[1].Value)}() // {line.Trim()}");
code.AppendLine($" {And}().{FeatureGenerator.ToMethod(and.Groups[1].Value)}(); // {line.Trim()}");
continue;
}

var asterisk = Regex.Match(line, "\\s*\\* (.*)");
if (asterisk.Success)
{
code.AppendLine($" .{And}().{FeatureGenerator.ToMethod(asterisk.Groups[1].Value)}() // {line.Trim()}");
code.AppendLine($" {And}().{FeatureGenerator.ToMethod(asterisk.Groups[1].Value)}(); // {line.Trim()}");
continue;
}

var but = Regex.Match(line, "\\s*But (.*)");
if (but.Success)
{
code.AppendLine($" .{But}().{FeatureGenerator.ToMethod(but.Groups[1].Value)}() // {line.Trim()}");
code.AppendLine($" {But}().{FeatureGenerator.ToMethod(but.Groups[1].Value)}(); // {line.Trim()}");
continue;
}

var when = Regex.Match(line, "\\s*When (.*)");
if (when.Success)
{
code.AppendLine($" .{When}().{FeatureGenerator.ToMethod(when.Groups[1].Value)}() // {line.Trim()}");
code.AppendLine($" {When}().{FeatureGenerator.ToMethod(when.Groups[1].Value)}(); // {line.Trim()}");
continue;
}

var then = Regex.Match(line, "\\s*Then (.*)");
if (then.Success)
{
code.AppendLine($" .{Then}().{FeatureGenerator.ToMethod(then.Groups[1].Value)}() // {line.Trim()}");
code.AppendLine($" {Then}().{FeatureGenerator.ToMethod(then.Groups[1].Value)}(); // {line.Trim()}");
continue;
}
}
Expand All @@ -252,7 +251,9 @@ void CloseScenario()
{
if (scenarioMethod != null)
{
code.AppendLine($" .{Moreover}().After{scenarioMethod}();");
code.AppendLine();
code.AppendLine($" {Moreover}().After{scenarioMethod}();");
code.AppendLine(" }");
code.AppendLine();
code.AppendLine(
$" partial void After{scenarioMethod}(" +
Expand All @@ -271,7 +272,7 @@ void CloseBackground()
{
if (backgroundStarted != null)
{
code.AppendLine($" ;");
code.AppendLine(" }");
code.AppendLine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,43 @@ namespace Synergy.Behaviours.Tests.Samples;
[GeneratedCode("Synergy.Behaviours.Testing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "1.0.0.0")]
public partial class CalculatorFeature
{
private CalculatorFeature CalculatorBackground() // Background:
=> Given().UserOpenedCalculator() // Given User opened calculator
;
private void CalculatorBackground() // Background:
{
Given().UserOpenedCalculator(); // Given User opened calculator
}

// Rule: Adding numbers

[Xunit.Fact]
// @Add
public void AddTwoNumbers() // Scenario: Add two numbers
=> Background().CalculatorBackground()
.Given().TheFirstNumberIs50() // Given the first number is 50
.And().TheSecondNumberIs70() // And the second number is 70
.When().TheTwoNumbersAreAdded() // When the two numbers are added
.Then().TheResultShouldBe120() // Then the result should be 120
.Moreover().AfterAddTwoNumbers();
{
Background().CalculatorBackground();

Given().TheFirstNumberIs50(); // Given the first number is 50
And().TheSecondNumberIs70(); // And the second number is 70
When().TheTwoNumbersAreAdded(); // When the two numbers are added
Then().TheResultShouldBe120(); // Then the result should be 120

Moreover().AfterAddTwoNumbers();
}

partial void AfterAddTwoNumbers();

[Xunit.Fact]
// @Add
public void AddTwoNumbersInDifferentWay() // Scenario: Add two numbers in different way
=> Background().CalculatorBackground()
.Given().TwoNumbers() // Given Two numbers:
.And().TheFirstNumberIs50() // * the first number is 50
.And().TheSecondNumberIs70() // * the second number is 70
.When().TheTwoNumbersAreAdded() // When the two numbers are added
.Then().TheResultShouldBe120() // Then the result should be 120
.Moreover().AfterAddTwoNumbersInDifferentWay();
{
Background().CalculatorBackground();

Given().TwoNumbers(); // Given Two numbers:
And().TheFirstNumberIs50(); // * the first number is 50
And().TheSecondNumberIs70(); // * the second number is 70
When().TheTwoNumbersAreAdded(); // When the two numbers are added
Then().TheResultShouldBe120(); // Then the result should be 120

Moreover().AfterAddTwoNumbersInDifferentWay();
}

partial void AfterAddTwoNumbersInDifferentWay();

Expand Down

0 comments on commit 9d15ea6

Please sign in to comment.