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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ bin/
obj/
*.user
*.suo
*.DotSettings
*~
*.swp
*.orig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace TestStack.BDDfy.Tests
{
[TestFixture]
public class HumanizerTests
public class NetToStringTests
{
[Test]
public void PascalCaseInputStringIsTurnedIntoSentence()
{
Assert.That(
Configurator.Scanners.Humanize("PascalCaseInputStringIsTurnedIntoSentence"),
Configurator.Scanners.Humanize("PascalCaseInputStringIsTurnedIntoSentence"),
Is.EqualTo("Pascal case input string is turned into sentence"));
}

Expand All @@ -37,32 +37,37 @@ public void WhenInputStringEndWithANumber_ThenNumberIsDealtWithLikeAWord()
public void UnderscoredInputStringIsTurnedIntoSentence()
{
Assert.That(
Configurator.Scanners.Humanize("Underscored_input_string_is_turned_into_sentence"),
Configurator.Scanners.Humanize("Underscored_input_string_is_turned_into_sentence"),
Is.EqualTo("Underscored input string is turned into sentence"));
}

[Test]
public void UnderscoredInputStringPreservesCasing()
{
Assert.That(
Configurator.Scanners.Humanize("Underscored_input_String_is_turned_INTO_sentence"),
Configurator.Scanners.Humanize("Underscored_input_String_is_turned_INTO_sentence"),
Is.EqualTo("Underscored input String is turned INTO sentence"));
}

[Test]
public void OneLetterWordInTheBeginningOfStringIsTurnedIntoAWord()
{
Assert.That(
Configurator.Scanners.Humanize("XIsFirstPlayer"),
Is.EqualTo("X is first player"));
Assert.That(Configurator.Scanners.Humanize("XIsFirstPlayer"), Is.EqualTo("X is first player"));
}

[Test]
public void CanDealWithExampleStepNames()
[TestCase("GivenThereAre__start__Cucumbers", "Given there are <start> cucumbers")]
[TestCase("Given_there_are__start__cucumbers", "Given there are <start> cucumbers")]
[TestCase("GivenMethodTaking__ExampleInt__", "Given method taking <example int>")]
[TestCase("Given_method_taking__ExampleInt__", "Given method taking <ExampleInt>")]
[TestCase("__starting__with_example", "<starting> with example")]
[TestCase("__starting__WithExample", "<starting> with example")]
[TestCase("WhenMethod__takes____two__parameters", "When method <takes> <two> parameters")]
[TestCase("When_method__takes____two__parameters", "When method <takes> <two> parameters")]
[TestCase("When_method_takes__one__and__two__parameters", "When method takes <one> and <two> parameters")]
[TestCase("WhenMethodTakes__one__and__two__parameters", "When method takes <one> and <two> parameters")]
public void CanDealWithExampleStepNames(string stepName, string expectedStepTitle)
{
NetToString.Convert("GivenThereAre__start__Cucumbers").ShouldBe("Given there are <start> cucumbers");
NetToString.Convert("Given_there_are__start__cucumbers").ShouldBe("Given there are <start> cucumbers");
NetToString.Convert("GivenMethodTaking__ExampleInt__").ShouldBe("Given method taking <example int>");
NetToString.Convert(stepName).ShouldBe(expectedStepTitle, Case.Sensitive);
}
}
}
2 changes: 1 addition & 1 deletion TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<Compile Include="Scanner\FluentScanner\WhenStepsAreScannedUsingFluentScanner.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="HumanizerTests.cs" />
<Compile Include="NetToStringTests.cs" />
<Compile Include="Reporters\Diagnostics\DiagnosticsReportBuilderTests.cs" />
<Compile Include="Reporters\Diagnostics\WhenBuildingReportDiagnostics.cs" />
<Compile Include="Reporters\Diagnostics\DiagnosticsReporterTests.cs" />
Expand Down
59 changes: 33 additions & 26 deletions TestStack.BDDfy/NetToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,59 @@ static string FromPascalCase(string name)
new List<char>(),
(list, currentChar) =>
{
if (currentChar == ' ')
{
list.Add(currentChar);
return list;
}

if(list.Count == 0)
{
if (currentChar == ' ' || list.Count == 0)
list.Add(currentChar);
return list;
}

var lastCharacterInTheList = list[list.Count - 1];
if (lastCharacterInTheList != ' ')
else
{
if(char.IsDigit(lastCharacterInTheList))
{
if (char.IsLetter(currentChar))
list.Add(' ');
}
else if (!char.IsLower(currentChar))
if(ShouldAddSpace(list[list.Count - 1], currentChar))
list.Add(' ');
list.Add(char.ToLower(currentChar));
}

list.Add(char.ToLower(currentChar));

return list;
});

var result = new string(chars.ToArray());
return result.Replace(" i ", " I "); // I is an exception
}

public static readonly Func<string, string> Convert = name =>
private static bool ShouldAddSpace(char lastChar, char currentChar)
{
if (name.Contains("__"))
if (lastChar == ' ')
return false;

if (char.IsDigit(lastChar))
{
// hacking the crap out of it for now
name = Regex.Replace(name, "__(\\w+)__", " <$1> ");
return FromPascalCase(name).Replace("_", "").Replace(" >", ">").Replace("< ", "<").TrimEnd();
if (char.IsLetter(currentChar))
return true;

return false;
}

if (!char.IsLower(currentChar) && currentChar != '>' && lastChar != '<')
return true;

return false;
}

public static readonly Func<string, string> Convert = name =>
{
if (name.Contains("__"))
return ExampleTitle(name);

if (name.Contains("_"))
return FromUnderscoreSeparatedWords(name);

return FromPascalCase(name);
};

private static string ExampleTitle(string name)
{
name = Regex.Replace(name, "__([a-zA-Z]+)__", " <$1> ");

// for when there are two consequetive example placeholders in the word; e.g. Given__one____two__parameters
name = name.Replace(" ", " ");
return Convert(name).Trim();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static string GetTitleFromMethodNameInStackTrace(object testObject)
if (initiatingFrame == null)
return null;

return NetToString.Convert(initiatingFrame.GetMethod().Name);
return Configurator.Scanners.Humanize(initiatingFrame.GetMethod().Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public virtual IEnumerable<Scenario> Scan(ITestContext testContext)

static string GetScenarioText(Type scenarioType)
{
return NetToString.Convert(scenarioType.Name);
return Configurator.Scanners.Humanize(scenarioType.Name);
}

protected virtual IEnumerable<Step> ScanScenarioForSteps(ITestContext testContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using TestStack.BDDfy.Configuration;

namespace TestStack.BDDfy
{
Expand Down Expand Up @@ -33,7 +34,7 @@ public IEnumerable<Step> Scan(ITestContext testContext, MethodInfo candidateMeth

var stepTitle = new StepTitle(executableAttribute.StepTitle);
if(string.IsNullOrEmpty(stepTitle))
stepTitle = new StepTitle(NetToString.Convert(candidateMethod.Name));
stepTitle = new StepTitle(Configurator.Scanners.Humanize(candidateMethod.Name));

var stepAsserts = IsAssertingByAttribute(candidateMethod);

Expand Down Expand Up @@ -76,7 +77,7 @@ public IEnumerable<Step> Scan(ITestContext testContext, MethodInfo method, Examp

string stepTitle = executableAttribute.StepTitle;
if (string.IsNullOrEmpty(stepTitle))
stepTitle = NetToString.Convert(method.Name);
stepTitle = Configurator.Scanners.Humanize(method.Name);

var stepAsserts = IsAssertingByAttribute(method);
var methodParameters = method.GetParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Linq;
using System.Threading.Tasks;
using TestStack.BDDfy.Configuration;

namespace TestStack.BDDfy
{
Expand Down Expand Up @@ -136,7 +137,7 @@ private StepTitle CreateTitle(string stepTextTemplate, bool includeInputsInStepT
{

var flatInputArray = inputArguments.FlattenArrays();
var stepTitle = NetToString.Convert(methodInfo.Name);
var stepTitle = Configurator.Scanners.Humanize(methodInfo.Name);

if (!string.IsNullOrEmpty(stepTextTemplate)) stepTitle = string.Format(stepTextTemplate, flatInputArray);
else if (includeInputsInStepTitle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using TestStack.BDDfy.Configuration;

namespace TestStack.BDDfy
{
Expand Down Expand Up @@ -122,7 +123,7 @@ private string GetStepTitle(MethodInfo method, object testObject, RunStepWithArg

private string GetStepTitleFromMethodName(MethodInfo method, RunStepWithArgsAttribute argAttribute)
{
var methodName = _stepTextTransformer(NetToString.Convert(method.Name));
var methodName = _stepTextTransformer(Configurator.Scanners.Humanize(method.Name));
object[] inputs = null;

if (argAttribute != null && argAttribute.InputArguments != null)
Expand Down
3 changes: 2 additions & 1 deletion TestStack.BDDfy/Scanners/StoryMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using TestStack.BDDfy.Configuration;

namespace TestStack.BDDfy
{
Expand All @@ -11,7 +12,7 @@ public StoryMetadata(Type storyType, StoryNarrativeAttribute narrative)

public StoryMetadata(Type storyType, string narrative1, string narrative2, string narrative3, string title = null, string titlePrefix = null)
{
Title = title ?? NetToString.Convert(storyType.Name);
Title = title ?? Configurator.Scanners.Humanize(storyType.Name);
TitlePrefix = titlePrefix ?? "Story: ";
Type = storyType;

Expand Down
9 changes: 9 additions & 0 deletions TestStack.BDDfy/TestStack.BDDfy.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners_005CScenarioScanners/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners_005CStepScanners/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners_005CStepScanners_005CExamples/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners_005CStepScanners_005CExecutableAttribute/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners_005CStepScanners_005CExecutableAttribute_005CGwtAttributes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners_005CStepScanners_005CFluent/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=Scanners_005CStepScanners_005CMethodName/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>