Skip to content

Commit

Permalink
#23: Added ability to use Verify.Net with the BDD framework
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed Mar 2, 2023
1 parent b671f23 commit aa783ae
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 10 deletions.
37 changes: 33 additions & 4 deletions Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ public static void Generate<TBehaviour>(
[CallerFilePath] string callerFilePath = ""
)
where TBehaviour : IFeature
{
var code = feature.Generate(from, withMoreover, include, exclude, callerFilePath);
var destinationFilePath = Path.Combine(Path.GetDirectoryName(callerFilePath), to);
File.WriteAllText(destinationFilePath, code.ToString());
}

public static string Generate<TBehaviour>(
this TBehaviour feature,
string from,
bool withMoreover = false,
string[]? include = null,
string[]? exclude = null,
[CallerFilePath] string callerFilePath = ""
)
where TBehaviour : IFeature
{
StringBuilder code = new StringBuilder();
string className = feature.GetType().Name;
Expand All @@ -27,14 +42,15 @@ public static void Generate<TBehaviour>(
code.AppendLine();
code.AppendLine($"namespace {feature.GetType().Namespace};");
code.AppendLine();
code.AppendLine(
$"[GeneratedCode(\"{typeof(FeatureGenerator).Assembly.FullName}\", \"{typeof(FeatureGenerator).Assembly.GetName().Version.ToString()}\")]");
code.AppendLine($"[GeneratedCode(\"{typeof(FeatureGenerator).Assembly.FullName}\", \"{typeof(FeatureGenerator).Assembly.GetName().Version.ToString()}\")]");
code.AppendLine($"public partial class {className}");
code.AppendLine("{");
// code.AppendLine(" [Fact]");
// code.AppendLine(" public void Generate()");
// code.AppendLine($" => Behaviours<{featureClass}>.Generate({nameof(from)}: \"{from}\", this);");

// TODO: Support tags above the feature

string? scenarioMethod = null;
bool includeScenario = ResetInclude();
List<string>? tags = null;
Expand Down Expand Up @@ -77,6 +93,10 @@ public static void Generate<TBehaviour>(

if (includeScenario == false)
continue;

// TODO: add Rule handling

// TODO: Add Background handling

var scenario = Regex.Match(line, "\\s*Scenario\\: (.*)");
if (scenario.Success)
Expand Down Expand Up @@ -123,8 +143,7 @@ public static void Generate<TBehaviour>(
// code.AppendLine(" }");
code.AppendLine("}");

var destinationFilePath = Path.Combine(Path.GetDirectoryName(callerFilePath), to);
File.WriteAllText(destinationFilePath, code.ToString());
return code.ToString();

string? CloseScenario()
{
Expand Down Expand Up @@ -161,4 +180,14 @@ private static string ToMethod(string sentence)
m = Regex.Replace(m, "[^A-Za-z0-9_]", "");
return m;
}

// private static string[]? ReadTagsFrom(string line)
// {
// if (line.TrimStart().StartsWith("@") == false)
// return null;
//
// return Regex.Match(line, "\\@\\w+")
// .Groups.Values.Select(g => g.Value)
// .ToArray();
// }
}
16 changes: 16 additions & 0 deletions Behaviours/Synergy.Behaviours.Tests/!Init.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.CompilerServices;
using DiffEngine;

namespace Synergy.Behaviours.Tests;

public static class Init
{
[ModuleInitializer]
public static void Initialize()
{
// if (Repair.Mode)
// VerifierSettings.AutoVerify();

DiffTools.UseOrder(DiffTool.Rider, DiffTool.VisualStudioCode, DiffTool.VisualStudio, DiffTool.WinMerge);
}
}
16 changes: 12 additions & 4 deletions Behaviours/Synergy.Behaviours.Tests/Calculator.Behaviours.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using Synergy.Behaviours.Testing;
using Xunit;

namespace Synergy.Behaviours.Tests;

[UsesVerify]
public partial class CalculatorFeature : Feature<CalculatorFeature>
{
[Fact]
public void GenerateFeature()
=> this.Generate(
public async Task GenerateFeature()
{
var code = this.Generate(
from: "Calculator.feature",
to: "Calculator.Feature.cs",
include: new[] { "@Add" }
//exclude: new[] { "@Divide" }
);

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

private int _firstNumber;

private CalculatorFeature TheFirstNumberIs50()
Expand Down Expand Up @@ -48,4 +51,9 @@ private CalculatorFeature VerifyAddTwoNumbers()
{
return this;
}

private CalculatorFeature TwoNumbers()
{
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <auto-generated />
// <auto-generated />
using System.CodeDom.Compiler;

namespace Synergy.Behaviours.Tests;
Expand All @@ -18,6 +18,17 @@ public void AddTwoNumbers() // Scenario: Add two numbers
.Then().TheResultShouldBe120() // Then the result should be 120
;

[Xunit.Fact]
// @Add
public void AddTwoNumbersInDifferentWay() // Scenario: Add two numbers in different way
=>
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
;




Expand Down
9 changes: 9 additions & 0 deletions Behaviours/Synergy.Behaviours.Tests/Calculator.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@tag
Feature: Calculator
![Calculator](https://specflow.org/wp-content/uploads/2020/09/calculator.png)
Simple calculator for performing calculations on **two** numbers
Expand All @@ -12,6 +13,14 @@ Feature: Calculator
When the two numbers are added
Then the result should be 120

@Add
Scenario: Add two numbers in different way
Given Two numbers:
* the first number is 50
* the second number is 70
When the two numbers are added
Then the result should be 120

@Subtract
Scenario: Subtract two numbers
Given the first number is 50
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Verify.Xunit" Version="19.10.0" />
<PackageReference Include="xunit" Version="2.4.2" />
</ItemGroup>

Expand All @@ -19,7 +20,7 @@

<ItemGroup>
<Compile Update="Calculator.Behaviours.cs">
<DependentUpon>Calculator.Feature.cs</DependentUpon>
<DependentUpon>Calculator.Feature.verified.cs</DependentUpon>
</Compile>
</ItemGroup>

Expand Down

0 comments on commit aa783ae

Please sign in to comment.