Skip to content

Commit 784447c

Browse files
RikvanSpreuwelrik van spreuwel
andauthored
StepDefinitionSampler: do not convert options list parameter to [string] (#55)
Co-authored-by: rik van spreuwel <r.spreuwel@ellips.com>
1 parent c469828 commit 784447c

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
## Improvements:
44

5+
* Step previews: do not convert options list parameter, e.g. '(option1|option2|option3)', to [string]
6+
57
## Bug fixes:
68

79
* Fix: Error message box when creating feature file with space in its name (#50)
810

9-
*Contributors of this release (in alphabetical order):* @gasparnagy
11+
*Contributors of this release (in alphabetical order):* @gasparnagy, @RikvanSpreuwel
1012

1113
# v2024.7.204 - 2024-11-20
1214

Reqnroll.VisualStudio/Editor/Completions/StepDefinitionSampler.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System;
2-
using System.Linq;
3-
41
namespace Reqnroll.VisualStudio.Editor.Completions;
52

63
public class StepDefinitionSampler
@@ -23,16 +20,29 @@ public string GetStepDefinitionSample(ProjectStepDefinitionBinding stepDefinitio
2320
completionTextBuilder.Append(GetUnescapedText(analyzedStepDefinitionExpression.Parts[i]));
2421
if (i < analyzedStepDefinitionExpression.Parts.Length - 1)
2522
{
26-
completionTextBuilder.Append("[");
27-
completionTextBuilder.Append(GetPlaceHolderText(stepDefinitionBinding, i / 2));
28-
completionTextBuilder.Append("]");
23+
if (ParameterIsListOfOptions(GetUnescapedText(analyzedStepDefinitionExpression.Parts[i + 1])))
24+
completionTextBuilder.Append(GetUnescapedText(analyzedStepDefinitionExpression.Parts[i + 1]));
25+
else
26+
{
27+
completionTextBuilder.Append("[");
28+
completionTextBuilder.Append(GetPlaceHolderText(stepDefinitionBinding, i / 2));
29+
completionTextBuilder.Append("]");
30+
}
2931
}
3032
}
3133

3234
return completionTextBuilder.ToString();
3335
}
3436

35-
private string GetUnescapedText(AnalyzedStepDefinitionExpressionPart part)
37+
38+
private bool ParameterIsListOfOptions(string parameter)
39+
{
40+
var regex = new Regex(@"^\(\s*[a-zA-Z0-9 ]+(?:\s*\|\s*[a-zA-Z0-9 ]+)*\s*\)$");
41+
return regex.IsMatch(parameter);
42+
}
43+
44+
45+
private string GetUnescapedText(AnalyzedStepDefinitionExpressionPart part)
3646
{
3747
if (part is AnalyzedStepDefinitionExpressionSimpleTextPart simpleTextPart)
3848
return simpleTextPart.UnescapedText;

Tests/Reqnroll.VisualStudio.Tests/Editor/Completions/StepDefinitionSamplerTests.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#nullable disable
2-
using System;
3-
using System.Linq;
42
using Reqnroll.VisualStudio.Editor.Completions;
53

64
namespace Reqnroll.VisualStudio.Tests.Editor.Completions;
@@ -9,7 +7,7 @@ public class StepDefinitionSamplerTests
97
{
108
private ProjectStepDefinitionBinding CreateStepDefinitionBinding(string regex, params string[] parameterTypes)
119
{
12-
parameterTypes = parameterTypes ?? new string[0];
10+
parameterTypes ??= Array.Empty<string>();
1311
return new ProjectStepDefinitionBinding(ScenarioBlock.Given, new Regex("^" + regex + "$"), null,
1412
new ProjectBindingImplementation("M1", parameterTypes, null));
1513
}
@@ -90,4 +88,20 @@ public void Falls_back_to_regex(string regex)
9088

9189
result.Should().Be(regex);
9290
}
91+
92+
[Theory]
93+
[InlineData("(.*) is entered into the (very basic|standard|scientific) calculator",
94+
"[int] is entered into the (very basic|standard|scientific) calculator",
95+
"System.Int32", "System.String")]
96+
[InlineData("(.*) is entered into the ( 1st| 2nd | 3 rd |4th) calculator and saved with name ([^']*)",
97+
"[int] is entered into the ( 1st| 2nd | 3 rd |4th) calculator and saved with name [string]",
98+
"System.Int32", "System.String", "System.String")]
99+
public void Does_not_replace_choice_parameters(string regex, string expectedResult, params string[] paramType)
100+
{
101+
var sut = new StepDefinitionSampler();
102+
103+
var result = sut.GetStepDefinitionSample(CreateStepDefinitionBinding(regex, paramType));
104+
105+
result.Should().Be(expectedResult);
106+
}
93107
}

0 commit comments

Comments
 (0)