Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/TestStack.BDDfy.Tests/HumanizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ public void ReportsIllegalExampleStepNames(string stepName, string expectedStepT
exception.ShouldBeOfType<ArgumentException>();
}

[Theory]
[InlineData("my__step_method", "my step method")]
[InlineData("Method__Name", "Method name")]
[InlineData("my__step__method", "My <step> method")]
[InlineData("Given__value__is_set", "Given <value> is set")]
[InlineData("a__b", "A b")]
[InlineData("step____name", "Step name")]
public void DoubleUnderscoreWithoutClosingPair_DoesNotCauseStackOverflow(string stepName, string expectedStepTitle)
{
Humanizer.Humanize(stepName).ShouldBe(expectedStepTitle);
}

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Linq;
using Shouldly;
using Xunit;

namespace TestStack.BDDfy.Tests.Scanner.ReflectiveScanner
{
/// <summary>
/// Regression test for https://github.com/TestStack/TestStack.BDDfy/issues/201
/// A double underscore in a step method name should not cause a stack overflow.
/// </summary>
public class WhenStepMethodNameHasDoubleUnderscores
{
private class StoryWithDoubleUnderscoreMethodNames
{
public bool GivenExecuted { get; private set; }
public bool WhenExecuted { get; private set; }
public bool ThenExecuted { get; private set; }

public void Given_a__setup_step() => GivenExecuted = true;
public void When_something__happens() => WhenExecuted = true;
public void Then_the__result_is_correct() => ThenExecuted = true;
}

private class StoryWithPascalCaseDoubleUnderscore
{
public bool Executed { get; private set; }

public void GivenSome__Precondition() => Executed = true;
public void WhenAction__IsPerformed() { }
public void ThenOutcome__IsExpected() { }
}

[Fact]
public void DoesNotThrowForSnakeCaseWithDoubleUnderscore()
{
var story = new StoryWithDoubleUnderscoreMethodNames();

var engine = story.LazyBDDfy();
var exception = Record.Exception(() => engine.Run());

exception.ShouldBeNull();
}

[Fact]
public void AllStepsAreExecuted()
{
var story = new StoryWithDoubleUnderscoreMethodNames();
story.BDDfy();

story.GivenExecuted.ShouldBeTrue();
story.WhenExecuted.ShouldBeTrue();
story.ThenExecuted.ShouldBeTrue();
}

[Fact]
public void StepTitlesAreGeneratedCorrectly_SnakeCase()
{
var story = new StoryWithDoubleUnderscoreMethodNames();
var result = story.BDDfy();

var steps = result.Scenarios.First().Steps.Select(s => s.Title).ToList();

steps.ShouldContain("Given a setup step");
steps.ShouldContain("When something happens");
steps.ShouldContain("Then the result is correct");
}

[Fact]
public void DoesNotThrowForPascalCaseWithDoubleUnderscore()
{
var story = new StoryWithPascalCaseDoubleUnderscore();

var engine = story.LazyBDDfy();
var exception = Record.Exception(() => engine.Run());

exception.ShouldBeNull();
}

[Fact]
public void StepTitlesAreGeneratedCorrectly_PascalCase()
{
var story = new StoryWithPascalCaseDoubleUnderscore();
var result = story.BDDfy();

var steps = result.Scenarios.First().Steps.Select(s => s.Title).ToList();

steps.ShouldContain("Given some precondition");
steps.ShouldContain("When action is performed");
steps.ShouldContain("Then outcome is expected");
}
}
}
Loading