Skip to content

Commit a40075d

Browse files
committed
Merge pull request #134 from JakeGinnivan/LocalVariableExamplePlaceholders
Can now declare a local variable inside the test method, and BDDfy will ...
2 parents 1bea54e + fedae8a commit a40075d

19 files changed

Lines changed: 164 additions & 86 deletions

TestStack.BDDfy.Tests/Processors/TestRunnerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ public class TestRunnerTests
1414
[Test]
1515
public void InitialisesScenarioWithExampleBeforeRunning()
1616
{
17-
const int ExpectedValue = 1;
17+
const int expectedValue = 1;
1818
int actualValue = 0;
1919
var exampleTable = new ExampleTable("ExampleValue")
2020
{
21-
ExpectedValue
21+
expectedValue
2222
}.Single();
2323

2424
var sut = new TestRunner();
2525
Action<object> action = o => actualValue = ExampleValue;
2626
var steps = new List<Step> { new Step(action, new StepTitle("A Step"), true, ExecutionOrder.Initialize, true) };
2727

28-
var scenarioWithExample = new Scenario("id", this, steps, "Scenario Text", exampleTable);
28+
var scenarioWithExample = new Scenario("id", this, steps, "Scenario Text", exampleTable, new List<StepArgument>());
2929
var story = new Story(new StoryMetadata(typeof(TestRunnerTests), new StoryNarrativeAttribute()),
3030
new[] { scenarioWithExample });
3131

3232
sut.Process(story);
3333

34-
Assert.AreEqual(ExpectedValue, actualValue);
34+
Assert.AreEqual(expectedValue, actualValue);
3535
}
3636
}
3737
}

TestStack.BDDfy.Tests/Reporters/Html/ClassicReportBuilderTests.ShouldProduceExpectedHtml.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ body{margin:0;padding:0;padding-bottom:40px;max-width:100%;background-color:#fff
104104
<li class='step Failed Assertion canToggle' data-toggle-target='step-3-3' >
105105
<span>Then no money is dispensed [Exception Message: 'Boom']</span>
106106
<div class='step FailedException' id='step-3-3'>
107-
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 90</code>
107+
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 89</code>
108108
</div>
109109
</li>
110110
</ul>

TestStack.BDDfy.Tests/Reporters/Html/ClassicReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ body{margin:0;padding:0;padding-bottom:40px;max-width:100%;background-color:#fff
115115
With
116116
New lines</span>
117117
<div class='step' id='step-1-19'>
118-
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 113</code>
118+
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 112</code>
119119
</div>
120120
</td>
121121
</tr>

TestStack.BDDfy.Tests/Reporters/Html/MetroReportBuilderTests.ShouldProduceExpectedHtmlWithExamples.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abb
126126
With
127127
New lines</span>
128128
<div class='step FailedException' id='step-1-19'>
129-
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 113</code>
129+
<code> at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 112</code>
130130
</div>
131131
</td>
132132
</tr>

TestStack.BDDfy.Tests/Reporters/MarkDown/MarkDownReportBuilderTests.ShouldProduceExpectedMarkdownWithExamples.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#### Exceptions:
1818
1. Boom With New lines
19-
at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 113
19+
at TestStack.BDDfy.Tests.Reporters.ReportTestData.GetScenarios(Boolean includeFailingScenario, Boolean includeExamples) in ...\ReportTestData.cs:line 112
2020

2121
## Story: Happiness
2222
**As a person**

TestStack.BDDfy.Tests/Reporters/ReportTestData.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43

54
namespace TestStack.BDDfy.Tests.Reporters
65
{
76
using System.Linq;
87

98
public class ReportTestData
109
{
11-
private int _idCount = 0;
10+
private int _idCount;
1211

1312
public IEnumerable<Story> CreateTwoStoriesEachWithOneFailingScenarioAndOnePassingScenarioWithThreeStepsOfFiveMilliseconds()
1413
{
1514
var storyMetadata1 = new StoryMetadata(typeof(RegularAccountHolderStory), "As a person", "I want ice cream", "So that I can be happy", "Happiness");
1615
var storyMetadata2 = new StoryMetadata(typeof(GoldAccountHolderStory), "As an account holder", "I want to withdraw cash", "So that I can get money when the bank is closed", "Account holder withdraws cash");
17-
var stories = new List<Story>()
16+
var stories = new List<Story>
1817
{
1918
new Story(storyMetadata1, GetScenarios(false, false)),
2019
new Story(storyMetadata2, GetScenarios(true, false))
@@ -30,7 +29,7 @@ public IEnumerable<Story> CreateMixContainingEachTypeOfOutcome()
3029

3130
const StoryMetadata testThatReportWorksWithNoStory = null;
3231

33-
var stories = new List<Story>()
32+
var stories = new List<Story>
3433
{
3534
new Story(storyMetadata1, GetOneOfEachScenarioResult()),
3635
new Story(storyMetadata2, GetOneOfEachScenarioResult()),
@@ -48,7 +47,7 @@ public IEnumerable<Story> CreateMixContainingEachTypeOfOutcomeWithOneScenarioPer
4847

4948
const StoryMetadata testThatReportWorksWithNoStory = null;
5049

51-
var stories = new List<Story>()
50+
var stories = new List<Story>
5251
{
5352
new Story(storyMetadata1, new Scenario(typeof(HappyPathScenario), GetHappyExecutionSteps(), "Happy Path Scenario [for Happiness]")),
5453
new Story(storyMetadata1, new Scenario(typeof(SadPathScenario), GetFailingExecutionSteps(), "Sad Path Scenario [for Happiness]")),
@@ -119,8 +118,8 @@ private Scenario[] GetScenarios(bool includeFailingScenario, bool includeExample
119118
}
120119
return new List<Scenario>
121120
{
122-
new Scenario(exampleId, typeof(ExampleScenario), GetExampleExecutionSteps(), "Example Scenario", exampleTable.ElementAt(0)),
123-
new Scenario(exampleId, typeof(ExampleScenario), exampleExecutionSteps, "Example Scenario", exampleTable.ElementAt(1))
121+
new Scenario(exampleId, typeof(ExampleScenario), GetExampleExecutionSteps(), "Example Scenario", exampleTable.ElementAt(0), new List<StepArgument>()),
122+
new Scenario(exampleId, typeof(ExampleScenario), exampleExecutionSteps, "Example Scenario", exampleTable.ElementAt(1), new List<StepArgument>())
124123
}.ToArray();
125124
}
126125

@@ -133,7 +132,7 @@ private Scenario[] GetScenarios(bool includeFailingScenario, bool includeExample
133132

134133
private Scenario[] GetOneOfEachScenarioResult()
135134
{
136-
var scenarios = new List<Scenario>()
135+
var scenarios = new List<Scenario>
137136
{
138137
new Scenario(typeof(HappyPathScenario), GetHappyExecutionSteps(), "Happy Path Scenario"),
139138
new Scenario(typeof(SadPathScenario), GetSadExecutionSteps(), "Sad Path Scenario"),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void MethodWithInputs(ContainerType subContainer)
6868

6969
object[] GetArguments(Expression<Action<ClassUnderTest>> action, ClassUnderTest instance)
7070
{
71-
return action.ExtractArguments(instance).ToArray();
71+
return action.ExtractArguments(instance).Select(o => o.Value).ToArray();
7272
}
7373

7474
int _input1 = 1;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+

2+
Scenario: Inline
3+
Given int with value <Inline Variable>
4+
5+
Examples:
6+
| Inline Variable |
7+
| 1 |
8+
| 2 |
9+

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ public void FluentCanBeUsedWithExamples()
3232
Approvals.Verify(textReporter.ToString());
3333
}
3434

35+
private void GivenIntWithValue(int differentName)
36+
{
37+
differentName.ShouldBeOneOf(1, 2);
38+
}
39+
40+
[Test]
41+
[MethodImpl(MethodImplOptions.NoInlining)]
42+
public void Inline()
43+
{
44+
// ReSharper disable once ConvertToConstant.Local
45+
var inlineVariable = 0;
46+
var story = this
47+
.Given(_ => GivenIntWithValue(inlineVariable))
48+
.WithExamples(new ExampleTable("Inline Variable") { 1, 2 })
49+
.BDDfy();
50+
51+
var textReporter = new TextReporter();
52+
textReporter.Process(story);
53+
Approvals.Verify(textReporter.ToString());
54+
}
55+
3556
[Test]
3657
[MethodImpl(MethodImplOptions.NoInlining)]
3758
public void ExampleTypeMismatch()

TestStack.BDDfy/Processors/ScenarioExecutor.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ public void InitializeScenario()
1818
if (_scenario.Example == null)
1919
return;
2020

21+
var type = _scenario.TestObject.GetType();
22+
var memberInfos = type
23+
.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
24+
.Where(m => m is FieldInfo || m is PropertyInfo)
25+
.Where(m => !m.Name.EndsWith("BackingField"))
26+
.ToArray();
27+
28+
var possibleTargets = memberInfos
29+
.OfType<FieldInfo>()
30+
.Select(f => new StepArgument(f, _scenario.TestObject))
31+
.Union(memberInfos.OfType<PropertyInfo>().Select(m => new StepArgument(m, _scenario.TestObject)))
32+
.Union(_scenario.Arguments)
33+
.ToArray();
34+
2135
foreach (var cell in _scenario.Example.Values)
2236
{
23-
var type = _scenario.TestObject.GetType();
24-
var matchingMembers = type
25-
.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
26-
.Where(m => m is FieldInfo || m is PropertyInfo)
27-
.Where(m => !m.Name.EndsWith("BackingField"))
37+
var matchingMembers = possibleTargets
2838
.Where(n => cell.MatchesName(n.Name))
2939
.ToArray();
3040

@@ -33,13 +43,7 @@ public void InitializeScenario()
3343

3444
foreach (var matchingMember in matchingMembers)
3545
{
36-
var prop = matchingMember as PropertyInfo;
37-
if (prop != null)
38-
prop.SetValue(_scenario.TestObject, cell.GetValue(prop.PropertyType), null);
39-
40-
var field = matchingMember as FieldInfo;
41-
if (field != null)
42-
field.SetValue(_scenario.TestObject, cell.GetValue(field.FieldType));
46+
matchingMember.SetValue(cell.GetValue(matchingMember.ArgumentType));
4347
}
4448
}
4549
}

0 commit comments

Comments
 (0)