Skip to content

Commit 456453f

Browse files
committed
Merge pull request #162 from JakeGinnivan/FluentCustomStepTitles
Allowed custom step title via attribute on Fluent
2 parents 7cdd716 + 66de5ff commit 456453f

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@ public void MethodCallInStepTitle()
1616
var story = this
1717
.Given(_ => GivenWeMutateSomeState())
1818
.When(_ => something.Sub.SomethingHappens())
19+
.And(_ => something.Sub.SomethingWithDifferentTitle())
1920
.Then(_ => ThenTitleHas(AMethodCall()))
21+
.And(_ => something.Sub.SomethingWithArg("foo"))
22+
.And(_ => something.Sub.SomethingWithArg2("foo"))
23+
.And(_ => something.Sub.SomethingWithArg3("foo"))
2024
.BDDfy();
2125

22-
story.Scenarios.Single().Steps.ElementAt(2).Title.ShouldBe("Then title has Mutated state");
26+
story.Scenarios.Single().Steps.ElementAt(2).Title.ShouldBe("And different title");
27+
story.Scenarios.Single().Steps.ElementAt(3).Title.ShouldBe("Then title has Mutated state");
28+
story.Scenarios.Single().Steps.ElementAt(4).Title.ShouldBe("And with arg foo");
29+
story.Scenarios.Single().Steps.ElementAt(5).Title.ShouldBe("And with arg");
30+
story.Scenarios.Single().Steps.ElementAt(6).Title.ShouldBe("And with foo arg");
2331
}
2432

2533
public class FooClass
@@ -38,6 +46,26 @@ public void SomethingHappens()
3846
{
3947

4048
}
49+
50+
[StepTitle("Different title")]
51+
public void SomethingWithDifferentTitle()
52+
{
53+
}
54+
55+
[StepTitle("With arg")]
56+
public void SomethingWithArg(string arg)
57+
{
58+
}
59+
60+
[StepTitle("With arg", false)]
61+
public void SomethingWithArg2(string arg)
62+
{
63+
}
64+
65+
[StepTitle("With {0} arg", false)]
66+
public void SomethingWithArg3(string arg)
67+
{
68+
}
4169
}
4270

4371
private string AMethodCall()

TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,17 @@ private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepT
151151
{
152152

153153
var flatInputArray = inputArguments.Select(o => o.Value).FlattenArrays();
154-
var stepTitle = AppendPrefix(Configurator.Scanners.Humanize(methodInfo.Name), stepPrefix);
154+
var name = methodInfo.Name;
155+
var stepTitleAttribute = methodInfo.GetCustomAttributes(typeof(StepTitleAttribute), true).SingleOrDefault();
156+
if (stepTitleAttribute != null)
157+
{
158+
var titleAttribute = ((StepTitleAttribute)stepTitleAttribute);
159+
name = string.Format(titleAttribute.StepTitle, flatInputArray);
160+
if (titleAttribute.IncludeInputsInStepTitle != null)
161+
includeInputsInStepTitle = titleAttribute.IncludeInputsInStepTitle.Value;
162+
}
163+
164+
var stepTitle = AppendPrefix(Configurator.Scanners.Humanize(name), stepPrefix);
155165

156166
if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray);
157167
else if (includeInputsInStepTitle)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace TestStack.BDDfy
4+
{
5+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
6+
public class StepTitleAttribute : Attribute
7+
{
8+
public StepTitleAttribute(string stepTitle)
9+
{
10+
StepTitle = stepTitle;
11+
}
12+
13+
public StepTitleAttribute(string stepTitle, bool includeInputsInStepTitle)
14+
{
15+
IncludeInputsInStepTitle = includeInputsInStepTitle;
16+
StepTitle = stepTitle;
17+
}
18+
19+
public string StepTitle { get; private set; }
20+
21+
public bool? IncludeInputsInStepTitle { get; private set; }
22+
}
23+
}

TestStack.BDDfy/TestStack.BDDfy.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
<Compile Include="Scenario.cs" />
131131
<Compile Include="Engine.cs" />
132132
<Compile Include="StepTitle.cs" />
133+
<Compile Include="StepTitleAttribute.cs" />
133134
<Compile Include="Story.cs" />
134135
<Compile Include="StoryAttribute.cs" />
135136
<Compile Include="Scanners\StoryMetadata.cs" />

0 commit comments

Comments
 (0)