From cab27e64c02c98275bdbe378be49c94be18e7e2e Mon Sep 17 00:00:00 2001
From: paul
Date: Fri, 23 Aug 2019 11:45:22 +0200
Subject: [PATCH 1/7] xx
---
src/TestStack.BDDfy.Tests/AssemblyInfo.cs | 5 ++
.../Exceptions/ExceptionThrowingTest.cs | 6 +-
.../StepScanners/Fluent/FluentScanner.cs | 84 ++++++++++---------
3 files changed, 53 insertions(+), 42 deletions(-)
create mode 100644 src/TestStack.BDDfy.Tests/AssemblyInfo.cs
diff --git a/src/TestStack.BDDfy.Tests/AssemblyInfo.cs b/src/TestStack.BDDfy.Tests/AssemblyInfo.cs
new file mode 100644
index 0000000..75812dc
--- /dev/null
+++ b/src/TestStack.BDDfy.Tests/AssemblyInfo.cs
@@ -0,0 +1,5 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using Xunit;
+[assembly: CollectionBehavior(DisableTestParallelization = true)]
diff --git a/src/TestStack.BDDfy.Tests/Exceptions/ExceptionThrowingTest.cs b/src/TestStack.BDDfy.Tests/Exceptions/ExceptionThrowingTest.cs
index a9fb85a..05325e3 100644
--- a/src/TestStack.BDDfy.Tests/Exceptions/ExceptionThrowingTest.cs
+++ b/src/TestStack.BDDfy.Tests/Exceptions/ExceptionThrowingTest.cs
@@ -6,9 +6,9 @@ namespace TestStack.BDDfy.Tests.Exceptions
{
public class ExceptionThrowingTest where T : Exception, new()
{
- private static bool _givenShouldThrow;
- private static bool _whenShouldThrow;
- private static bool _thenShouldThrow;
+ private bool _givenShouldThrow;
+ private bool _whenShouldThrow;
+ private bool _thenShouldThrow;
Scenario _scenario;
void Given()
diff --git a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
index 92e5f39..52299ee 100644
--- a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
+++ b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
@@ -43,14 +43,20 @@ internal class FluentScanner : IFluentScanner
private readonly TScenario _testObject;
private readonly ITestContext _testContext;
private readonly MethodInfo _fakeExecuteActionMethod;
-
+ private Func _createTitle;
internal FluentScanner(TScenario testObject)
{
+ _createTitle = CreateTitle;
_testObject = testObject;
_testContext = TestContext.GetContext(_testObject);
_fakeExecuteActionMethod = typeof(FluentScanner).GetMethod("ExecuteAction", BindingFlags.Instance | BindingFlags.NonPublic);
}
+
+ public void SetCreateTitle(Func customCreateTitle)
+ {
+ _createTitle = customCreateTitle;
+ }
IScanner IFluentScanner.GetScanner(string scenarioTitle, Type explicitStoryType)
{
return new DefaultScanner(_testContext, new FluentScenarioScanner(_steps, scenarioTitle), explicitStoryType);
@@ -93,7 +99,7 @@ public void AddStep(Expression> stepAction, bool reports, Ex
[StepTitle("")]
private void ExecuteAction(ExampleAction action)
{
-
+
}
public void AddStep(Expression> stepAction, string stepTextTemplate, bool includeInputsInStepTitle, bool reports, ExecutionOrder executionOrder, bool asserts, string stepPrefix)
@@ -105,7 +111,7 @@ public void AddStep(Expression> stepAction, string stepTex
inputArguments = stepAction.ExtractArguments(_testObject).ToArray();
}
- var title = CreateTitle(stepTextTemplate, includeInputsInStepTitle, GetMethodInfo(stepAction), inputArguments, stepPrefix);
+ var title = _createTitle(stepTextTemplate, includeInputsInStepTitle, GetMethodInfo(stepAction), inputArguments, stepPrefix);
var args = inputArguments.Where(s => !string.IsNullOrEmpty(s.Name)).ToList();
_steps.Add(new Step(StepActionFactory.GetStepAction(action), title, FixAsserts(asserts, executionOrder), FixConsecutiveStep(executionOrder), reports, args));
}
@@ -126,7 +132,7 @@ private void AddStep(Action action, LambdaExpression stepAction, stri
inputArguments = stepAction.ExtractArguments(_testObject).ToArray();
}
- var title = CreateTitle(stepTextTemplate, includeInputsInStepTitle, GetMethodInfo(stepAction), inputArguments,
+ var title = _createTitle(stepTextTemplate, includeInputsInStepTitle, GetMethodInfo(stepAction), inputArguments,
stepPrefix);
var args = inputArguments.Where(s => !string.IsNullOrEmpty(s.Name)).ToList();
_steps.Add(new Step(StepActionFactory.GetStepAction(action), title, FixAsserts(asserts, executionOrder),
@@ -177,46 +183,46 @@ private ExecutionOrder FixConsecutiveStep(ExecutionOrder executionOrder)
private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepTitle, MethodInfo methodInfo, StepArgument[] inputArguments, string stepPrefix)
{
Func createTitle = () =>
- {
+ {
- var flatInputArray = inputArguments.Select(o => o.Value).FlattenArrays();
- var name = methodInfo.Name;
- var stepTitleAttribute = methodInfo.GetCustomAttributes(typeof(StepTitleAttribute), true).SingleOrDefault();
- if (stepTitleAttribute != null)
- {
- var titleAttribute = ((StepTitleAttribute)stepTitleAttribute);
- name = string.Format(titleAttribute.StepTitle, flatInputArray);
- if (titleAttribute.IncludeInputsInStepTitle != null)
- includeInputsInStepTitle = titleAttribute.IncludeInputsInStepTitle.Value;
- }
+ var flatInputArray = inputArguments.Select(o => o.Value).FlattenArrays();
+ var name = methodInfo.Name;
+ var stepTitleAttribute = methodInfo.GetCustomAttributes(typeof(StepTitleAttribute), true).SingleOrDefault();
+ if (stepTitleAttribute != null)
+ {
+ var titleAttribute = ((StepTitleAttribute)stepTitleAttribute);
+ name = string.Format(titleAttribute.StepTitle, flatInputArray);
+ if (titleAttribute.IncludeInputsInStepTitle != null)
+ includeInputsInStepTitle = titleAttribute.IncludeInputsInStepTitle.Value;
+ }
- var stepTitle = AppendPrefix(Configurator.Scanners.Humanize(name), stepPrefix);
+ var stepTitle = AppendPrefix(Configurator.Scanners.Humanize(name), stepPrefix);
- if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray);
- else if (includeInputsInStepTitle)
- {
- var parameters = methodInfo.GetParameters();
- var stringFlatInputs =
- inputArguments
- .Select((a, i) => new { ParameterName = parameters[i].Name, Value = a })
- .Select(i =>
+ if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray);
+ else if (includeInputsInStepTitle)
+ {
+ var parameters = methodInfo.GetParameters();
+ var stringFlatInputs =
+ inputArguments
+ .Select((a, i) => new { ParameterName = parameters[i].Name, Value = a })
+ .Select(i =>
+ {
+ if (_testContext.Examples != null)
{
- if (_testContext.Examples != null)
- {
- var matchingHeader = _testContext.Examples.Headers
- .SingleOrDefault(header => ExampleTable.HeaderMatches(header, i.ParameterName) ||
- ExampleTable.HeaderMatches(header, i.Value.Name));
- if (matchingHeader != null)
- return string.Format("<{0}>", matchingHeader);
- }
- return i.Value.Value.FlattenArray();
- })
- .ToArray();
- stepTitle = stepTitle + " " + string.Join(", ", stringFlatInputs);
- }
+ var matchingHeader = _testContext.Examples.Headers
+ .SingleOrDefault(header => ExampleTable.HeaderMatches(header, i.ParameterName) ||
+ ExampleTable.HeaderMatches(header, i.Value.Name));
+ if (matchingHeader != null)
+ return string.Format("<{0}>", matchingHeader);
+ }
+ return i.Value.Value.FlattenArray();
+ })
+ .ToArray();
+ stepTitle = stepTitle + " " + string.Join(", ", stringFlatInputs);
+ }
- return stepTitle.Trim();
- };
+ return stepTitle.Trim();
+ };
return new StepTitle(createTitle);
}
From 1cd33efa6220030d9e266874b0a4147612120fc4 Mon Sep 17 00:00:00 2001
From: paul
Date: Fri, 23 Aug 2019 15:46:31 +0200
Subject: [PATCH 2/7] fluent public and test added
---
.../Scanner/FluentScanner/StepTitleTests.cs | 55 ++++++++++++++++++-
.../StepScanners/Fluent/FluentScanner.cs | 13 +++--
.../StepScanners/Fluent/IFluentScanner.cs | 2 +
3 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs b/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs
index e566bee..d28d560 100644
--- a/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs
+++ b/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs
@@ -1,5 +1,7 @@
using System.Linq;
+
using Shouldly;
+
using Xunit;
namespace TestStack.BDDfy.Tests.Scanner.FluentScanner
@@ -29,6 +31,58 @@ public void MethodCallInStepTitle()
story.Scenarios.Single().Steps.ElementAt(6).Title.ShouldBe("And with foo arg");
}
+ [Fact]
+ public void TitleFunctionCanBeOverriden()
+ {
+ FooClass something = new FooClass();
+ var context = TestContext.GetContext(something);
+ context.FluentScanner = new TestStack.BDDfy.FluentScanner(something);
+ context.FluentScanner.SetCreateTitle((a, b, c, d, e) => new StepTitle("hallo"));
+ var story = something
+ .Given(_ => GivenWeMutateSomeState())
+ .When(_ => something.Sub.SomethingHappens())
+ .And(_ => something.Sub.SomethingWithDifferentTitle())
+ .Then(_ => ThenTitleHas(AMethodCall()))
+ .And(_ => something.Sub.SomethingWithArg("foo"))
+ .And(_ => something.Sub.SomethingWithArg2("foo"))
+ .And(_ => something.Sub.SomethingWithArg3("foo"))
+ .BDDfy();
+
+ story.Scenarios.Single().Steps.ElementAt(2).Title.ShouldBe("hallo");
+ story.Scenarios.Single().Steps.ElementAt(3).Title.ShouldBe("hallo");
+ story.Scenarios.Single().Steps.ElementAt(4).Title.ShouldBe("hallo");
+ story.Scenarios.Single().Steps.ElementAt(5).Title.ShouldBe("hallo");
+ story.Scenarios.Single().Steps.ElementAt(6).Title.ShouldBe("hallo");
+ }
+
+ [Fact]
+ public void TitleFunctionCanBeOverridenAndUseParameters()
+ {
+ GivenWeMutateSomeState();
+
+ FooClass something = new FooClass();
+ var context = TestContext.GetContext(something);
+ context.FluentScanner = new TestStack.BDDfy.FluentScanner(something);
+ context.FluentScanner.SetCreateTitle((a, b, c, d, e) => new StepTitle(e + " " + c.Name + " " + string.Join(",", d.Select(arg => arg.Value).ToArray())));
+ var story = something
+ .Given(_ => GivenWeMutateSomeState())
+ .When(_ => something.Sub.SomethingHappens())
+ .And(_ => something.Sub.SomethingWithDifferentTitle())
+ .Then(_ => ThenTitleHas("Mutated state"))
+ .And(_ => something.Sub.SomethingWithArg("foo"))
+ .And(_ => something.Sub.SomethingWithArg2("foo2"))
+ .And(_ => something.Sub.SomethingWithArg3("foo3"))
+ .BDDfy();
+
+ story.Scenarios.Single().Steps.ElementAt(0).Title.ShouldBe("Given GivenWeMutateSomeState ");
+ story.Scenarios.Single().Steps.ElementAt(1).Title.ShouldBe("When SomethingHappens ");
+ story.Scenarios.Single().Steps.ElementAt(2).Title.ShouldBe("And SomethingWithDifferentTitle ");
+ story.Scenarios.Single().Steps.ElementAt(3).Title.ShouldBe("Then ThenTitleHas Mutated state");
+ story.Scenarios.Single().Steps.ElementAt(4).Title.ShouldBe("And SomethingWithArg foo");
+ story.Scenarios.Single().Steps.ElementAt(5).Title.ShouldBe("And SomethingWithArg2 foo2");
+ story.Scenarios.Single().Steps.ElementAt(6).Title.ShouldBe("And SomethingWithArg3 foo3");
+ }
+
public class FooClass
{
public FooClass()
@@ -43,7 +97,6 @@ public class BarClass
{
public void SomethingHappens()
{
-
}
[StepTitle("Different title")]
diff --git a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
index 52299ee..5ae691d 100644
--- a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
+++ b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
@@ -36,7 +36,7 @@ namespace TestStack.BDDfy
/// }
///
///
- internal class FluentScanner : IFluentScanner
+ public class FluentScanner : IFluentScanner
where TScenario : class
{
private readonly List _steps = new List();
@@ -44,15 +44,18 @@ internal class FluentScanner : IFluentScanner
private readonly ITestContext _testContext;
private readonly MethodInfo _fakeExecuteActionMethod;
private Func _createTitle;
- internal FluentScanner(TScenario testObject)
- {
- _createTitle = CreateTitle;
+ public FluentScanner(TScenario testObject)
+ {
_testObject = testObject;
_testContext = TestContext.GetContext(_testObject);
_fakeExecuteActionMethod = typeof(FluentScanner).GetMethod("ExecuteAction", BindingFlags.Instance | BindingFlags.NonPublic);
+ _createTitle = CreateTitle;
}
-
+ ///
+ /// string stepTextTemplate, bool includeInputsInStepTitle, MethodInfo methodInfo, StepArgument[] inputArguments, string stepPrefix
+ ///
+ ///
public void SetCreateTitle(Func customCreateTitle)
{
_createTitle = customCreateTitle;
diff --git a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/IFluentScanner.cs b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/IFluentScanner.cs
index 06c021e..f2a5442 100644
--- a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/IFluentScanner.cs
+++ b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/IFluentScanner.cs
@@ -1,9 +1,11 @@
using System;
+using System.Reflection;
namespace TestStack.BDDfy
{
public interface IFluentScanner
{
IScanner GetScanner(string scenarioTitle, Type explicitStoryType);
+ void SetCreateTitle(Func customCreateTitle);
}
}
From 813be750a7f766a6309cfc35a088c7b454777077 Mon Sep 17 00:00:00 2001
From: paul
Date: Mon, 26 Aug 2019 14:03:15 +0200
Subject: [PATCH 3/7] alter title in time
---
.../Scanner/FluentScanner/StepTitleTests.cs | 2 +-
src/TestStack.BDDfy/Processors/TestRunner.cs | 6 +-
.../StepScanners/Fluent/FluentScanner.cs | 7 +-
src/TestStack.BDDfy/Step.cs | 67 ++++++++++++++++++-
4 files changed, 74 insertions(+), 8 deletions(-)
diff --git a/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs b/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs
index d28d560..32418e1 100644
--- a/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs
+++ b/src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs
@@ -68,7 +68,7 @@ public void TitleFunctionCanBeOverridenAndUseParameters()
.Given(_ => GivenWeMutateSomeState())
.When(_ => something.Sub.SomethingHappens())
.And(_ => something.Sub.SomethingWithDifferentTitle())
- .Then(_ => ThenTitleHas("Mutated state"))
+ .Then(_ => ThenTitleHas(AMethodCall()))
.And(_ => something.Sub.SomethingWithArg("foo"))
.And(_ => something.Sub.SomethingWithArg2("foo2"))
.And(_ => something.Sub.SomethingWithArg3("foo3"))
diff --git a/src/TestStack.BDDfy/Processors/TestRunner.cs b/src/TestStack.BDDfy/Processors/TestRunner.cs
index af0c267..f6798d9 100644
--- a/src/TestStack.BDDfy/Processors/TestRunner.cs
+++ b/src/TestStack.BDDfy/Processors/TestRunner.cs
@@ -1,5 +1,6 @@
namespace TestStack.BDDfy.Processors
{
+ using System;
using System.Linq;
public class TestRunner : IProcessor
@@ -25,10 +26,13 @@ public void Process(Story story)
var stepFailed = false;
foreach (var executionStep in scenario.Steps)
{
+ executionStep.ResetTitle();
+
if (stepFailed && ShouldExecuteStepWhenPreviousStepFailed(executionStep))
break;
- if (executor.ExecuteStep(executionStep) == Result.Passed)
+ if (executor.ExecuteStep(executionStep) == Result.Passed)
+
continue;
if (!executionStep.Asserts)
diff --git a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
index 5ae691d..72f4ebb 100644
--- a/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
+++ b/src/TestStack.BDDfy/Scanners/StepScanners/Fluent/FluentScanner.cs
@@ -98,13 +98,14 @@ public void AddStep(Expression> stepAction, bool reports, Ex
AddStep(_ => compiledAction().Action(), expression, null, true, reports, executionOrder, asserts, stepPrefix);
}
+
+
[UsedImplicitly]
[StepTitle("")]
private void ExecuteAction(ExampleAction action)
{
}
-
public void AddStep(Expression> stepAction, string stepTextTemplate, bool includeInputsInStepTitle, bool reports, ExecutionOrder executionOrder, bool asserts, string stepPrefix)
{
var action = stepAction.Compile();
@@ -116,7 +117,7 @@ public void AddStep(Expression> stepAction, string stepTex
var title = _createTitle(stepTextTemplate, includeInputsInStepTitle, GetMethodInfo(stepAction), inputArguments, stepPrefix);
var args = inputArguments.Where(s => !string.IsNullOrEmpty(s.Name)).ToList();
- _steps.Add(new Step(StepActionFactory.GetStepAction(action), title, FixAsserts(asserts, executionOrder), FixConsecutiveStep(executionOrder), reports, args));
+ _steps.Add(new Step(stepAction, StepActionFactory.GetStepAction(action), _createTitle, stepTextTemplate,stepPrefix, includeInputsInStepTitle, GetMethodInfo, _testObject,title, FixAsserts(asserts, executionOrder), FixConsecutiveStep(executionOrder), reports, args));
}
public void AddStep(Expression> stepAction, string stepTextTemplate, bool includeInputsInStepTitle, bool reports, ExecutionOrder executionOrder, bool asserts, string stepPrefix)
@@ -138,7 +139,7 @@ private void AddStep(Action action, LambdaExpression stepAction, stri
var title = _createTitle(stepTextTemplate, includeInputsInStepTitle, GetMethodInfo(stepAction), inputArguments,
stepPrefix);
var args = inputArguments.Where(s => !string.IsNullOrEmpty(s.Name)).ToList();
- _steps.Add(new Step(StepActionFactory.GetStepAction(action), title, FixAsserts(asserts, executionOrder),
+ _steps.Add(new Step(stepAction, stepTextTemplate, includeInputsInStepTitle, GetMethodInfo, stepPrefix,_createTitle, StepActionFactory.GetStepAction(action), title, FixAsserts(asserts, executionOrder),
FixConsecutiveStep(executionOrder), reports, args));
}
diff --git a/src/TestStack.BDDfy/Step.cs b/src/TestStack.BDDfy/Step.cs
index ac331ed..850add4 100644
--- a/src/TestStack.BDDfy/Step.cs
+++ b/src/TestStack.BDDfy/Step.cs
@@ -1,19 +1,31 @@
using System;
using System.Collections.Generic;
+using System.Linq.Expressions;
+using System.Reflection;
+using System.Threading.Tasks;
using TestStack.BDDfy.Configuration;
-
+using System.Linq;
namespace TestStack.BDDfy
{
public class Step
{
- private readonly StepTitle _title;
+ private StepTitle _title;
+ private LambdaExpression _stepAction;
+ private Func _createTitle;
+ private string _stepTextTemplate;
+ private string _stepPrefix;
+ private bool _includeInputsInStepTitle;
+ private Func _getMethodInfo;
+ private object _testObject;
+ private bool _reports;
+
public Step(
Func