Skip to content

Commit

Permalink
#23: Generating partial After{scenario method}() that is always prese…
Browse files Browse the repository at this point in the history
…nt but as it is partial does not need to be implemented
  • Loading branch information
MarcinCelej committed May 3, 2023
1 parent 59b3ab1 commit f262b16
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 36 deletions.
18 changes: 11 additions & 7 deletions Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ namespace Synergy.Behaviours.Testing;

public static class FeatureGenerator
{
// TODO: Marcin Celej [from: Marcin Celej on: 03-05-2023]: Add way to generate non-fluent implementation of the methods

public static void Generate<TBehaviour>(
this TBehaviour feature,
string from,
string to,
bool withMoreover = false,
string[]? include = null,
string[]? exclude = null,
[CallerFilePath] string callerFilePath = ""
)
{
var code = feature.Generate(from, withMoreover, include, exclude, callerFilePath);
var code = feature.Generate(from, include, exclude, callerFilePath);
var destinationFilePath = Path.Combine(Path.GetDirectoryName(callerFilePath), to);
File.WriteAllText(destinationFilePath, code.ToString());
}

public static string Generate<TBehaviour>(
this TBehaviour featureClass,
string from,
bool withMoreover = false,
string[]? include = null,
string[]? exclude = null,
[CallerFilePath] string callerFilePath = ""
Expand Down Expand Up @@ -238,10 +238,14 @@ void CloseScenario()
{
if (scenarioMethod != null)
{
if (withMoreover)
code.AppendLine($" .Moreover().After{scenarioMethod}();");
else
code.AppendLine($" ;");
code.AppendLine($" .Moreover().After{scenarioMethod}();");
code.AppendLine();
code.AppendLine(
$" partial void After{scenarioMethod}(" +
// "[CallerMemberName] string callerMemberName = \"\", " +
// "[CallerFilePath] string callerFilePath = \"\"" +
");"
);

code.AppendLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public void AddTwoNumbers() // Scenario: Add two numbers
.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
Expand All @@ -31,7 +33,9 @@ public void AddTwoNumbersInDifferentWay() // Scenario: Add two numbers in differ
.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();

// Rule: Subtracting numbers

Expand Down
25 changes: 13 additions & 12 deletions Behaviours/Synergy.Behaviours.Tests/Samples/Calculator.Steps.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Synergy.Behaviours.Testing;
using System.Runtime.CompilerServices;
using Synergy.Behaviours.Testing;

namespace Synergy.Behaviours.Tests.Samples;

Expand All @@ -16,35 +17,33 @@ public async Task GenerateFeature()

await Verifier
.Verify(code, "cs")
.UseFileName("Calculator.Feature");
.UseFileName("Calculator.Feature")
.AutoVerify();
}

private Calculator _calculator;
private CalculatorFeature UserOpenedCalculator()
{
this._calculator = new Calculator();
return this;
}

private int _firstNumber;

private CalculatorFeature TheFirstNumberIs50()
{
this._firstNumber = 50;
this._calculator.FirstNumber = 50;
return this;
}

private int _secondNumber;

private CalculatorFeature TheSecondNumberIs70()
{
this._secondNumber = 70;
this._calculator.SecondNumber = 70;
return this;
}

private int _result;
private CalculatorFeature TheTwoNumbersAreAdded()
{
var calculator = new Calculator { FirstNumber = this._firstNumber, SecondNumber = this._secondNumber };
this._result = calculator.Add();
this._result = this._calculator.Add();
return this;
}

Expand All @@ -54,9 +53,11 @@ private CalculatorFeature TheResultShouldBe120()
return this;
}

private CalculatorFeature VerifyAddTwoNumbers()
partial void AfterAddTwoNumbers()
{
return this;
Verifier.Verify(this._calculator)
.UseDirectory("Snapshots")
.GetAwaiter().GetResult();
}

private CalculatorFeature TwoNumbers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ Feature: Calculator

![Calculator](https://specflow.org/wp-content/uploads/2020/09/calculator.png)
Simple calculator for performing calculations on **two** numbers

Link to a feature: [Calculator](SpecFlowCalculator.Specs/Features/Calculator.feature)
***Further read***: **[Learn more about how to generate Living Documentation](https://docs.specflow.org/projects/specflow-livingdoc/en/latest/LivingDocGenerator/Generating-Documentation.html)**

Background:
Given User opened calculator
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions Behaviours/Synergy.Behaviours.Tests/Samples/New.Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ namespace Synergy.Behaviours.Tests.Samples;
public partial class NewFeature : Feature<NewFeature>
{
[Fact]
public async Task GenerateFeature()
public void GenerateFeature()
{
var code = this.Generate(
from: "New.feature"
this.Generate(
from: "New.feature",
to: "New.Feature.cs"
);

await Verifier
.Verify(code, "cs")
.UseFileName("New.Feature")
.AutoVerify();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
FirstNumber: 50,
SecondNumber: 70
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
<Compile Update="Samples\Calculator.Steps.cs">
<DependentUpon>Calculator.feature</DependentUpon>
</Compile>
<Compile Update="Samples\New.Feature.verified.cs">
<Compile Update="Samples\New.Steps.cs">
<DependentUpon>New.feature</DependentUpon>
</Compile>
<Compile Update="Samples\New.Steps.cs">
<Compile Update="Samples\New.Feature.cs">
<DependentUpon>New.feature</DependentUpon>
</Compile>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Technical Debt for Synergy.Contracts

Total: 0
Total: 2

## [FeatureGenerator.cs](../../Synergy.Behaviours.Testing/FeatureGenerator.cs)
- TODO: Marcin Celej [from: Marcin Celej on: 03-05-2023]: Add way to generate non-fluent implementation of the methods

## [New.feature](../Samples/New.feature)
- TODO: Provide scenarios here, check the sample down here

0 comments on commit f262b16

Please sign in to comment.