Skip to content

Commit 4c23ca6

Browse files
author
Gurpreet Singh
committed
removed .NET 4 related code
removed conditionally compiled code
1 parent 16f5557 commit 4c23ca6

File tree

6 files changed

+27
-99
lines changed

6 files changed

+27
-99
lines changed

src/TestStack.BDDfy/Configuration/Scanners.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using TestStack.BDDfy.Scanners;
34

45
namespace TestStack.BDDfy.Configuration
56
{
@@ -11,7 +12,7 @@ public class Scanners
1112
private readonly StepScannerFactory _methodNameStepScanner = new(() => new DefaultMethodNameStepScanner());
1213
public StepScannerFactory DefaultMethodNameStepScanner { get { return _methodNameStepScanner; } }
1314

14-
private readonly List<Func<IStepScanner>> _addedStepScanners = new();
15+
private readonly List<Func<IStepScanner>> _addedStepScanners = [];
1516
public Scanners Add(Func<IStepScanner> stepScannerFactory)
1617
{
1718
_addedStepScanners.Add(stepScannerFactory);

src/TestStack.BDDfy/Processors/InconclusiveException.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,5 @@ public InconclusiveException(string message) : base(message)
1515
public InconclusiveException(string message, Exception innerException) : base(message, innerException)
1616
{
1717
}
18-
#if NET40
19-
20-
protected InconclusiveException(SerializationInfo info, StreamingContext context) : base(info, context)
21-
{
22-
}
23-
#endif
2418
}
2519
}

src/TestStack.BDDfy/Scanners/ScenarioScanners/FluentScenarioScanner.cs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,25 @@
22
using System.Linq;
33
using TestStack.BDDfy.Configuration;
44

5-
namespace TestStack.BDDfy
6-
{
7-
public class FluentScenarioScanner(List<Step> steps, string title): IScenarioScanner
8-
{
9-
private readonly string _title = title;
10-
private readonly List<Step> _steps = steps;
11-
12-
public IEnumerable<Scenario> Scan(ITestContext testContext)
13-
{
14-
var scenarioText = _title ?? GetTitleFromMethodNameInStackTrace(testContext.TestObject);
15-
if (testContext.Examples != null)
16-
{
17-
var scenarioId = Configurator.IdGenerator.GetScenarioId();
18-
return testContext.Examples.Select(example =>
19-
new Scenario(scenarioId, testContext.TestObject, CloneSteps(_steps), scenarioText, example, testContext.Tags));
20-
}
5+
namespace TestStack.BDDfy.Scanners.ScenarioScanners;
216

22-
return new[] { new Scenario(testContext.TestObject, _steps, scenarioText, testContext.Tags) };
23-
}
7+
internal class FluentScenarioScanner(List<Step> steps, string title): IScenarioScanner
8+
{
9+
private readonly string _title = title;
10+
private readonly List<Step> _steps = steps;
2411

25-
private List<Step> CloneSteps(IEnumerable<Step> steps)
12+
public IEnumerable<Scenario> Scan(ITestContext testContext)
13+
{
14+
var scenarioText = _title ?? testContext.TestObject.GetType().Name;
15+
if (testContext.Examples != null)
2616
{
27-
return steps.Select(step => new Step(step)).ToList();
17+
var scenarioId = Configurator.IdGenerator.GetScenarioId();
18+
return testContext.Examples.Select(example =>
19+
new Scenario(scenarioId, testContext.TestObject, CloneSteps(_steps), scenarioText, example, testContext.Tags));
2820
}
2921

30-
private static string GetTitleFromMethodNameInStackTrace(object testObject)
31-
{
32-
// ReSharper disable once JoinDeclarationAndInitializer
33-
#if STACKTRACE
34-
var trace = new System.Diagnostics.StackTrace();
35-
var frames = trace.GetFrames();
36-
37-
var initiatingFrame = frames?.LastOrDefault(s => s.GetMethod().DeclaringType == testObject.GetType());
38-
if (initiatingFrame == null)
39-
return null;
40-
41-
return Configurator.Humanizer.Humanize(initiatingFrame.GetMethod().Name);
42-
#else
43-
return null;
44-
#endif
45-
}
22+
return [new Scenario(testContext.TestObject, _steps, scenarioText, testContext.Tags)];
4623
}
24+
25+
private static List<Step> CloneSteps(IEnumerable<Step> steps) => [.. steps.Select(static step => new Step(step))];
4726
}

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

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,11 @@
44
using System.Linq.Expressions;
55
using System.Reflection;
66
using System.Threading.Tasks;
7-
using TestStack.BDDfy.Annotations;
87
using TestStack.BDDfy.Configuration;
8+
using TestStack.BDDfy.Scanners.ScenarioScanners;
99

1010
namespace TestStack.BDDfy
1111
{
12-
/// <summary>
13-
/// Provides an alternative method of implementing stories and scenarios.
14-
/// </summary>
15-
/// <remarks>
16-
/// Reflecting scanners run in a pipeline which means you can mix and match their
17-
/// usage in your scenario; however, when you use FluentStepScanner, BDDfy does not
18-
/// use other scanners which means method names and attributes are ignored for
19-
/// scanning methods. You are in full control of what steps you want
20-
/// run and in what order.
21-
/// </remarks>
22-
/// <typeparam name="TScenario"></typeparam>
23-
/// <example>
24-
/// <code>
25-
/// [Fact]
26-
/// public void AccountHasSufficientFund()
27-
/// {
28-
/// this.Given(s => s.GivenTheAccountBalanceIs(100), GivenTheAccountBalanceIsTitleTemplate)
29-
/// .And(s => s.AndTheCardIsValid())
30-
/// .And(s => s.AndTheMachineContains(100), AndTheMachineContainsEnoughMoneyTitleTemplate)
31-
/// .When(s => s.WhenTheAccountHolderRequests(20), WhenTheAccountHolderRequestsTitleTemplate)
32-
/// .Then(s => s.TheAtmShouldDispense(20), "Then the ATM should dispense $20")
33-
/// .And(s => s.AndTheAccountBalanceShouldBe(80), "And the account balance should be $80")
34-
/// .And(s => s.ThenCardIsRetained(false), AndTheCardShouldBeReturnedTitleTemplate)
35-
/// .BDDfy(storyCategory: "ATM");
36-
/// }
37-
/// </code>
38-
/// </example>
3912
internal class FluentScanner<TScenario> : IFluentScanner
4013
where TScenario : class
4114
{
@@ -48,7 +21,7 @@ internal FluentScanner(TScenario testObject)
4821
{
4922
_testObject = testObject;
5023
_testContext = TestContext.GetContext(_testObject);
51-
_fakeExecuteActionMethod = typeof(FluentScanner<TScenario>).GetMethod("ExecuteAction", BindingFlags.Instance | BindingFlags.NonPublic);
24+
_fakeExecuteActionMethod = typeof(FluentScanner<TScenario>).GetMethod(nameof(ExecuteAction), BindingFlags.Instance | BindingFlags.NonPublic);
5225
}
5326

5427
IScanner IFluentScanner.GetScanner(string scenarioTitle, Type explicitStoryType)
@@ -80,9 +53,10 @@ public void AddStep(Expression<Func<ExampleAction>> stepAction, bool reports, Ex
8053
AddStep(_ => compiledAction().Action(), expression, null, true, reports, executionOrder, asserts, stepPrefix);
8154
}
8255

83-
[UsedImplicitly]
8456
[StepTitle("")]
85-
private void ExecuteAction(ExampleAction action)
57+
#pragma warning disable CA1822 // Mark members as static
58+
private void ExecuteAction(ExampleAction _)
59+
#pragma warning restore CA1822 // Mark members as static
8660
{
8761

8862
}

src/TestStack.BDDfy/Scanners/StepScanners/StepTitleException.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,5 @@ public StepTitleException(string message) : base(message)
1515
public StepTitleException(string message, Exception innerException) : base(message, innerException)
1616
{
1717
}
18-
#if NET40
19-
20-
protected StepTitleException(SerializationInfo info, StreamingContext context)
21-
: base(info, context)
22-
{
23-
}
24-
#endif
2518
}
2619
}

src/TestStack.BDDfy/Scanners/StoryAttributeMetaDataScanner.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Linq;
33

4-
namespace TestStack.BDDfy
4+
namespace TestStack.BDDfy.Scanners
55
{
6-
public class StoryAttributeMetadataScanner : IStoryMetadataScanner
6+
internal class StoryAttributeMetadataScanner : IStoryMetadataScanner
77
{
88
public virtual StoryMetadata Scan(object testObject, Type explicitStoryType = null)
99
{
@@ -38,22 +38,9 @@ protected virtual Type GetCandidateStory(object testObject, Type explicitStoryTy
3838
if (explicitStoryType != null)
3939
return explicitStoryType;
4040

41-
#if STACKTRACE
42-
StackTrace stackTrace = new StackTrace();
43-
44-
var frames = stackTrace.GetFrames();
45-
if (frames == null)
46-
return null;
47-
var scenarioType = testObject.GetType();
48-
// This is assuming scenario and story live in the same assembly
49-
var firstFrame = frames.LastOrDefault(f => f.GetMethod().DeclaringType.Assembly() == scenarioType.Assembly());
50-
if (firstFrame == null)
51-
return null;
52-
53-
return firstFrame.GetMethod().DeclaringType;
54-
#else
55-
return null;
56-
#endif
41+
var testObjectType = testObject.GetType();
42+
var declaringType = testObjectType.DeclaringType;
43+
return declaringType ?? testObjectType;
5744
}
5845

5946
static StoryNarrativeAttribute GetStoryAttribute(Type candidateStoryType)

0 commit comments

Comments
 (0)