Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configure title function with configurator #279

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/TestStack.BDDfy.Tests/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
6 changes: 3 additions & 3 deletions src/TestStack.BDDfy.Tests/Exceptions/ExceptionThrowingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace TestStack.BDDfy.Tests.Exceptions
{
public class ExceptionThrowingTest<T> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ private void ExecuteUsingReflectingScanners()

private void ExecuteUsingFluentScanners()
{

Should.Throw<Exception>(() => Sut.Execute(ThrowingMethods.Given, true));
}

Expand Down
125 changes: 122 additions & 3 deletions src/TestStack.BDDfy.Tests/Scanner/FluentScanner/StepTitleTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using System.Linq;
using System;
using System.Linq;

using Shouldly;
using TestStack.BDDfy.Configuration;
using Xunit;

namespace TestStack.BDDfy.Tests.Scanner.FluentScanner
{
public class StepTitleTests
public class StepTitleTests:IDisposable
{
public StepTitleTests()
{
Configurator.Scanners.SetDefaultStepTitleCreatorFunction(null);
}
private string _mutatedState;

[Fact]
Expand All @@ -29,6 +36,113 @@ 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);

Configurator.Scanners.SetDefaultStepTitleCreatorFunction((a, b, c, d, e) => new StepTitle("hello"));
new FluentStepBuilder<FooClass>(something);
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("hello");
story.Scenarios.Single().Steps.ElementAt(3).Title.ShouldBe("hello");
story.Scenarios.Single().Steps.ElementAt(4).Title.ShouldBe("hello");
story.Scenarios.Single().Steps.ElementAt(5).Title.ShouldBe("hello");
story.Scenarios.Single().Steps.ElementAt(6).Title.ShouldBe("hello");
}

[Fact]
public void TitleFunctionCanBeOverridenAndUseParameters()
{
GivenWeMutateSomeState();

FooClass something = new FooClass();
var context = TestContext.GetContext(something);

Configurator.Scanners.SetDefaultStepTitleCreatorFunction((a, b, c, d, e) => new StepTitle(e + " " + c.Name + " " + string.Join(",", d.Select(arg => arg.Value).ToArray())));
new FluentStepBuilder<FooClass>(something);
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("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");
}
[Fact]
public void TitleFunctionCanBeOverridenWithinTestAndUseParameters()
{
GivenWeMutateSomeState();

FooClass something = new FooClass();
var context = TestContext.GetContext(something);
Configurator.Scanners.SetDefaultStepTitleCreatorFunction((a, b, c, d, e) => new StepTitle(e + " " + c.Name + " " + string.Join(",", d.Select(arg => arg.Value).ToArray())));
new FluentStepBuilder<FooClass>(something);
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("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");
}

[Fact]
public void TitleFunctionCanBeOverridenFromThestartWithinTestAndUseParameters()
{
GivenWeMutateSomeState();

FooClass something = new FooClass();
var context = TestContext.GetContext(something);
new FluentStepBuilder<FooClass>(something);
var story = something
.SetStepTitleFunction((a, b, c, d, e) => new StepTitle("hello"))
.Given(_ => GivenWeMutateSomeState())
.When(_ => something.Sub.SomethingHappens())
.BDDfy();

story.Scenarios.Single().Steps.ElementAt(0).Title.ShouldBe("hello");
story.Scenarios.Single().Steps.ElementAt(1).Title.ShouldBe("hello");


var story2 = something
.Given(_ => GivenWeMutateSomeState())
.BDDfy();

story2.Scenarios.Single().Steps.ElementAt(0).Title.ShouldBe("Given we mutate some state");

}

public class FooClass
{
public FooClass()
Expand All @@ -43,7 +157,6 @@ public class BarClass
{
public void SomethingHappens()
{

}

[StepTitle("Different title")]
Expand Down Expand Up @@ -81,5 +194,11 @@ private void ThenTitleHas(string result)
{
result.ShouldBe(_mutatedState);
}

public void Dispose()
{
Configurator.Scanners.SetDefaultStepTitleCreatorFunction(null);

}
}
}
13 changes: 10 additions & 3 deletions src/TestStack.BDDfy/BDDfyExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using TestStack.BDDfy.Configuration;

namespace TestStack.BDDfy
Expand Down Expand Up @@ -52,7 +53,9 @@ public static Engine LazyBDDfy<TStory>(this object testObject, string scenarioTi
/// <returns></returns>
public static Story BDDfy(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
{
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller)).Run();
var currentStory= InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller)).Run();
Configurator.Scanners.SetCustomStepTitleCreatorFunction(null);
return currentStory;
}

public static Engine LazyBDDfy(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
Expand All @@ -71,10 +74,13 @@ public static Engine LazyBDDfy(this object testObject, string scenarioTitle = nu
public static Story BDDfy<TStory>(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
where TStory : class
{
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller), typeof(TStory)).Run();
Story currentStory= InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller), typeof(TStory)).Run();
Configurator.Scanners.SetCustomStepTitleCreatorFunction(null);
return currentStory;
}

public static Engine LazyBDDfy<TStory>(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)

public static Engine LazyBDDfy<TStory>(this object testObject, string scenarioTitle = null, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
where TStory : class
{
return InternalLazyBDDfy(testObject, scenarioTitle ?? Configurator.Scanners.Humanize(caller), typeof(TStory));
Expand All @@ -95,6 +101,7 @@ static Engine InternalLazyBDDfy(
return new Engine(storyScanner);
}


static IScanner GetReflectiveScanner(ITestContext testContext, string scenarioTitle = null, Type explicitStoryType = null)
{
var stepScanners = Configurator.Scanners.GetStepScanners(testContext).ToArray();
Expand Down
16 changes: 16 additions & 0 deletions src/TestStack.BDDfy/Configuration/Scanners.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.Collections.Generic;
using System.Reflection;

namespace TestStack.BDDfy.Configuration
{
public class Scanners
{
public readonly StepScannerFactory _ = new StepScannerFactory(() => new ExecutableAttributeStepScanner());
private readonly StepScannerFactory _executableAttributeScanner = new StepScannerFactory(() => new ExecutableAttributeStepScanner());
public StepScannerFactory ExecutableAttributeScanner { get { return _executableAttributeScanner; } }

Expand All @@ -17,6 +19,17 @@ public Scanners Add(Func<IStepScanner> stepScannerFactory)
_addedStepScanners.Add(stepScannerFactory);
return this;
}
internal Scanners SetCustomStepTitleCreatorFunction( Func<string, bool, MethodInfo, StepArgument[], string, StepTitle> customStepTitleCreatorFunction)
{
CustomStepTitleCreatorFunction= customStepTitleCreatorFunction;
return this;
}

public Scanners SetDefaultStepTitleCreatorFunction(Func<string, bool, MethodInfo, StepArgument[], string, StepTitle> defaultStepTitleCreatorFunction)
{
DefaultStepTitleCreatorFunction = defaultStepTitleCreatorFunction;
return this;
}

public IEnumerable<IStepScanner> GetStepScanners(object objectUnderTest)
{
Expand All @@ -36,6 +49,9 @@ public IEnumerable<IStepScanner> GetStepScanners(object objectUnderTest)

public Func<IStoryMetadataScanner> StoryMetadataScanner = () => new StoryAttributeMetadataScanner();

internal Func<string, bool, MethodInfo, StepArgument[], string, StepTitle> CustomStepTitleCreatorFunction = null;
internal Func<string, bool, MethodInfo, StepArgument[], string, StepTitle> DefaultStepTitleCreatorFunction = null;

public Func<string, string> Humanize = NetToString.Convert;
}
}
8 changes: 5 additions & 3 deletions src/TestStack.BDDfy/Processors/TestRunner.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace TestStack.BDDfy.Processors
{
using System;
using System.Linq;

public class TestRunner : IProcessor
Expand All @@ -23,17 +24,18 @@ 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)
break;

stepFailed = true;
}
}
Expand Down
Loading