From aa783aeefb1d241babc1b3052356dfefc83b9427 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Thu, 2 Mar 2023 07:48:45 +0100 Subject: [PATCH] #23: Added ability to use Verify.Net with the BDD framework --- .../FeatureGenerator.cs | 37 +++++++++++++++++-- Behaviours/Synergy.Behaviours.Tests/!Init.cs | 16 ++++++++ .../Calculator.Behaviours.cs | 16 ++++++-- ...ture.cs => Calculator.Feature.verified.cs} | 13 ++++++- .../Calculator.feature | 9 +++++ .../Synergy.Behaviours.Tests.csproj | 3 +- 6 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 Behaviours/Synergy.Behaviours.Tests/!Init.cs rename Behaviours/Synergy.Behaviours.Tests/{Calculator.Feature.cs => Calculator.Feature.verified.cs} (55%) diff --git a/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs b/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs index 32529e5..023c613 100644 --- a/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs +++ b/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs @@ -16,6 +16,21 @@ public static void Generate( [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( + 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; @@ -27,14 +42,15 @@ public static void Generate( 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? tags = null; @@ -77,6 +93,10 @@ public static void Generate( if (includeScenario == false) continue; + + // TODO: add Rule handling + + // TODO: Add Background handling var scenario = Regex.Match(line, "\\s*Scenario\\: (.*)"); if (scenario.Success) @@ -123,8 +143,7 @@ public static void Generate( // code.AppendLine(" }"); code.AppendLine("}"); - var destinationFilePath = Path.Combine(Path.GetDirectoryName(callerFilePath), to); - File.WriteAllText(destinationFilePath, code.ToString()); + return code.ToString(); string? CloseScenario() { @@ -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(); + // } } \ No newline at end of file diff --git a/Behaviours/Synergy.Behaviours.Tests/!Init.cs b/Behaviours/Synergy.Behaviours.Tests/!Init.cs new file mode 100644 index 0000000..954bfd9 --- /dev/null +++ b/Behaviours/Synergy.Behaviours.Tests/!Init.cs @@ -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); + } +} \ No newline at end of file diff --git a/Behaviours/Synergy.Behaviours.Tests/Calculator.Behaviours.cs b/Behaviours/Synergy.Behaviours.Tests/Calculator.Behaviours.cs index b5fa7f7..a292d2b 100644 --- a/Behaviours/Synergy.Behaviours.Tests/Calculator.Behaviours.cs +++ b/Behaviours/Synergy.Behaviours.Tests/Calculator.Behaviours.cs @@ -1,19 +1,22 @@ using Synergy.Behaviours.Testing; -using Xunit; namespace Synergy.Behaviours.Tests; +[UsesVerify] public partial class CalculatorFeature : Feature { [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() @@ -48,4 +51,9 @@ private CalculatorFeature VerifyAddTwoNumbers() { return this; } + + private CalculatorFeature TwoNumbers() + { + return this; + } } \ No newline at end of file diff --git a/Behaviours/Synergy.Behaviours.Tests/Calculator.Feature.cs b/Behaviours/Synergy.Behaviours.Tests/Calculator.Feature.verified.cs similarity index 55% rename from Behaviours/Synergy.Behaviours.Tests/Calculator.Feature.cs rename to Behaviours/Synergy.Behaviours.Tests/Calculator.Feature.verified.cs index a4d2a05..3967957 100644 --- a/Behaviours/Synergy.Behaviours.Tests/Calculator.Feature.cs +++ b/Behaviours/Synergy.Behaviours.Tests/Calculator.Feature.verified.cs @@ -1,4 +1,4 @@ -// +// using System.CodeDom.Compiler; namespace Synergy.Behaviours.Tests; @@ -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 + ; + diff --git a/Behaviours/Synergy.Behaviours.Tests/Calculator.feature b/Behaviours/Synergy.Behaviours.Tests/Calculator.feature index a0b32b7..3c6904f 100644 --- a/Behaviours/Synergy.Behaviours.Tests/Calculator.feature +++ b/Behaviours/Synergy.Behaviours.Tests/Calculator.feature @@ -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 @@ -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 diff --git a/Behaviours/Synergy.Behaviours.Tests/Synergy.Behaviours.Tests.csproj b/Behaviours/Synergy.Behaviours.Tests/Synergy.Behaviours.Tests.csproj index ca4df66..3fa1488 100644 --- a/Behaviours/Synergy.Behaviours.Tests/Synergy.Behaviours.Tests.csproj +++ b/Behaviours/Synergy.Behaviours.Tests/Synergy.Behaviours.Tests.csproj @@ -10,6 +10,7 @@ + @@ -19,7 +20,7 @@ - Calculator.Feature.cs + Calculator.Feature.verified.cs