Skip to content

Commit

Permalink
#23: Added static Sentence class for sentence conversion operations
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcinCelej committed May 12, 2023
1 parent 24abd7d commit b6dea6c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
24 changes: 8 additions & 16 deletions Behaviours/Synergy.Behaviours.Testing/FeatureGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static string Generate<TBehaviour>(
{
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(" {");
Expand Down Expand Up @@ -208,42 +208,42 @@ public static string Generate<TBehaviour>(
// 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;
}
}
Expand Down Expand Up @@ -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)
// {
Expand Down
2 changes: 1 addition & 1 deletion Behaviours/Synergy.Behaviours.Testing/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Synergy.Behaviours.Testing;
public record Scenario(string Title, ReadOnlyCollection<string> Tags)
{
public string Method
=> FeatureGenerator.ToMethod(this.Title);
=> Sentence.ToMethod(this.Title);

public bool IsTagged(string tag, params string[] tags)
{
Expand Down
28 changes: 28 additions & 0 deletions Behaviours/Synergy.Behaviours.Testing/Sentence.cs
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b6dea6c

Please sign in to comment.