From b6dea6c80ccece3ef6902981ccc767d5c872c4a0 Mon Sep 17 00:00:00 2001 From: Marcin Celej Date: Fri, 12 May 2023 07:34:06 +0200 Subject: [PATCH] #23: Added static Sentence class for sentence conversion operations --- .../FeatureGenerator.cs | 24 ++++++---------- .../Synergy.Behaviours.Testing/Scenario.cs | 2 +- .../Synergy.Behaviours.Testing/Sentence.cs | 28 +++++++++++++++++++ .../Todos/Todos.Technical.Debt.verified.md | 5 +++- 4 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 Behaviours/Synergy.Behaviours.Testing/Sentence.cs diff --git a/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs b/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs index 5b4a7fb..a29f7f7 100644 --- a/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs +++ b/Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs @@ -128,7 +128,7 @@ public static string Generate( { CloseScenario(); - backgroundMethod = FeatureGenerator.ToMethod(ruleName ?? featureName ?? "Feature") + "Background"; + backgroundMethod = Sentence.ToMethod(ruleName ?? featureName ?? "Feature") + "Background"; backgroundStarted = backgroundMethod; code.AppendLine($" private void {backgroundMethod}() // {line.Trim()}"); code.AppendLine(" {"); @@ -208,42 +208,42 @@ public static string Generate( // code.Append($" "); // } - code.AppendLine($" {Given}().{FeatureGenerator.ToMethod(given.Groups[1].Value)}(); // {line.Trim()}"); + code.AppendLine($" {Given}().{Sentence.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}().{Sentence.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}().{Sentence.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}().{Sentence.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}().{Sentence.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}().{Sentence.ToMethod(then.Groups[1].Value)}(); // {line.Trim()}"); continue; } } @@ -327,15 +327,7 @@ private static string[] ReadAllLinesFrom(string gherkinPath) return gherkins; } - internal static string ToMethod(string sentence) - { - sentence = Regex.Replace(sentence, "[^A-Za-z0-9_]", " "); - var parts = sentence.Split(" "); - var m = string.Concat(parts.Where(p => !string.IsNullOrEmpty(p)) - .Select(p => p.Substring(0, 1) - .ToUpperInvariant() + p.Substring(1))); - return m; - } + // private static string[]? ReadTagsFrom(string line) // { diff --git a/Behaviours/Synergy.Behaviours.Testing/Scenario.cs b/Behaviours/Synergy.Behaviours.Testing/Scenario.cs index 361f90b..f19a426 100644 --- a/Behaviours/Synergy.Behaviours.Testing/Scenario.cs +++ b/Behaviours/Synergy.Behaviours.Testing/Scenario.cs @@ -5,7 +5,7 @@ namespace Synergy.Behaviours.Testing; public record Scenario(string Title, ReadOnlyCollection Tags) { public string Method - => FeatureGenerator.ToMethod(this.Title); + => Sentence.ToMethod(this.Title); public bool IsTagged(string tag, params string[] tags) { diff --git a/Behaviours/Synergy.Behaviours.Testing/Sentence.cs b/Behaviours/Synergy.Behaviours.Testing/Sentence.cs new file mode 100644 index 0000000..bc36b3c --- /dev/null +++ b/Behaviours/Synergy.Behaviours.Testing/Sentence.cs @@ -0,0 +1,28 @@ +using System.Text.RegularExpressions; + +namespace Synergy.Behaviours.Testing; + +// TODO: Marcin Celej [from: Marcin Celej on: 12-05-2023]: Add Public API verification test to see what is exposed from this library + +internal static class Sentence +{ + public static string ToMethod(string sentence) + { + sentence = Regex.Replace(sentence, "[^A-Za-z0-9_]", " "); + var parts = sentence.Split(" "); + var words = parts.Where(word => !string.IsNullOrWhiteSpace(word)) + .Select(word => word.Substring(0, 1) + .ToUpperInvariant() + word.Substring(1) + ); + var method = string.Concat(words); + return method; + } + + public static string FromMethod(string text) + => Regex.Replace( + text, + "([a-z])([A-Z])", + m => $"{m.Groups[1]} {m.Groups[2].Value.ToLowerInvariant()}" + ) + .Trim(); +} \ No newline at end of file diff --git a/Behaviours/Synergy.Behaviours.Tests/Todos/Todos.Technical.Debt.verified.md b/Behaviours/Synergy.Behaviours.Tests/Todos/Todos.Technical.Debt.verified.md index 7554098..de38343 100644 --- a/Behaviours/Synergy.Behaviours.Tests/Todos/Todos.Technical.Debt.verified.md +++ b/Behaviours/Synergy.Behaviours.Tests/Todos/Todos.Technical.Debt.verified.md @@ -1,7 +1,10 @@ # Technical Debt for Synergy.Contracts -Total: 2 +Total: 3 ## [FeatureGenerator.cs](../../Synergy.Behaviours.Testing/FeatureGenerator.cs) - TODO: Marcin Celej [from: Marcin Celej on: 10-05-2023]: Add include / exclude as functions - TODO: Marcin Celej [from: Marcin Celej on: 10-05-2023]: Support Scenario Outline along with Examples + +## [Sentence.cs](../../Synergy.Behaviours.Testing/Sentence.cs) +- TODO: Marcin Celej [from: Marcin Celej on: 12-05-2023]: Add Public API verification test to see what is exposed from this library