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
49 changes: 49 additions & 0 deletions TestStack.BDDfy.Tests/Stories/CanUseACustomStoryAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NUnit.Framework;

namespace TestStack.BDDfy.Tests.Stories
{
class InOrderToStoryAttribute : StoryNarrativeAttribute
{
// ReSharper disable InconsistentNaming
private const string As_a_prefix = "As a";
private const string In_order_to_prefix = "In order to";
private const string I_want_prefix = "I want";
// ReSharper restore InconsistentNaming

public string InOrderTo
{
get { return Narrative1; }
set { Narrative1 = CleanseProperty(value, In_order_to_prefix); }
}

public string AsA
{
get { return Narrative2; }
set { Narrative2 = CleanseProperty(value, As_a_prefix); }
}

public string IWant
{
get { return Narrative3; }
set { Narrative3 = CleanseProperty(value, I_want_prefix); }
}
}

[TestFixture]
[InOrderToStory(
InOrderTo = "do something",
AsA = "programmer",
IWant = "this to work")]
public class CanUseACustomStoryAttribute
{
[Test]
public void When_InOrderTo_is_specified_the_InOrderTo_syntax_is_used()
{
var story = new DummyScenario().BDDfy<CanUseACustomStoryAttribute>();

Assert.That(story.Metadata.Narrative1, Is.EqualTo("In order to do something"));
Assert.That(story.Metadata.Narrative2, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.Narrative3, Is.EqualTo("I want this to work"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ void andTheNarrativeIsReturnedAsExpected()
{
var expectedNarrative = (StoryAttribute)typeof(StoryAsScenario).GetCustomAttributes(typeof(StoryAttribute), false).First();
Assert.That(_story.Metadata, Is.Not.Null);
Assert.That(_story.Metadata.AsA, Is.EqualTo(expectedNarrative.AsA));
Assert.That(_story.Metadata.IWant, Is.EqualTo(expectedNarrative.IWant));
Assert.That(_story.Metadata.SoThat, Is.EqualTo(expectedNarrative.SoThat));
Assert.That(_story.Metadata.Narrative1, Is.EqualTo(expectedNarrative.AsA));
Assert.That(_story.Metadata.Narrative2, Is.EqualTo(expectedNarrative.IWant));
Assert.That(_story.Metadata.Narrative3, Is.EqualTo(expectedNarrative.SoThat));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public void Then_it_is_injected_by_BDDfy()
{
var story = new DummyScenario().BDDfy<WhenStoryAttibuteMissesAsATextInAsAProperty>();

Assert.That(story.Metadata.AsA, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.IWant, Is.EqualTo("I want the missing 'As a' to be added to story metadata"));
Assert.That(story.Metadata.SoThat, Is.EqualTo("So that I don't have to duplicate it on the string"));
Assert.That(story.Metadata.Narrative1, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.Narrative2, Is.EqualTo("I want the missing 'As a' to be added to story metadata"));
Assert.That(story.Metadata.Narrative3, Is.EqualTo("So that I don't have to duplicate it on the string"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public void Then_it_is_injected_by_BDDfy()
{
var story = new DummyScenario().BDDfy<WhenStoryAttibuteMissesDuplicateTextsInProperties>();

Assert.That(story.Metadata.AsA, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.IWant, Is.EqualTo("I want the missing texts to be added to story metadata"));
Assert.That(story.Metadata.SoThat, Is.EqualTo("So that I don't have to duplicate it on the string"));
Assert.That(story.Metadata.Narrative1, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.Narrative2, Is.EqualTo("I want the missing texts to be added to story metadata"));
Assert.That(story.Metadata.Narrative3, Is.EqualTo("So that I don't have to duplicate it on the string"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public void Then_it_is_injected_by_BDDfy()
{
var story = new DummyScenario().BDDfy<WhenStoryAttibuteMissesIWantTextInIWantProperty>();

Assert.That(story.Metadata.AsA, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.IWant, Is.EqualTo("I want the missing 'I want' to be added to story metadata"));
Assert.That(story.Metadata.SoThat, Is.EqualTo("So that I don't have to duplicate it on the string"));
Assert.That(story.Metadata.Narrative1, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.Narrative2, Is.EqualTo("I want the missing 'I want' to be added to story metadata"));
Assert.That(story.Metadata.Narrative3, Is.EqualTo("So that I don't have to duplicate it on the string"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public void Then_it_is_injected_by_BDDfy()
{
var story = new DummyScenario().BDDfy<WhenStoryAttibuteMissesSoThatTextInSoThatProperty>();

Assert.That(story.Metadata.AsA, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.IWant, Is.EqualTo("I want the missing 'So that' to be added to story metadata"));
Assert.That(story.Metadata.SoThat, Is.EqualTo("So that I don't have to duplicate it on the string"));
Assert.That(story.Metadata.Narrative1, Is.EqualTo("As a programmer"));
Assert.That(story.Metadata.Narrative2, Is.EqualTo("I want the missing 'So that' to be added to story metadata"));
Assert.That(story.Metadata.Narrative3, Is.EqualTo("So that I don't have to duplicate it on the string"));
}
}
}
1 change: 1 addition & 0 deletions TestStack.BDDfy.Tests/TestStack.BDDfy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Compile Include="Scanner\WhenCombinationOfExecutableAttributeAndMethodNamingConventionIsUsed.cs" />
<Compile Include="Scanner\WhenStepsAreDefinedInABaseClass.cs" />
<Compile Include="Scanner\WhenStepsReturnTheirText.cs" />
<Compile Include="Stories\CanUseACustomStoryAttribute.cs" />
<Compile Include="Stories\WhenStoryAttibuteMissesDuplicateTextsInProperties.cs" />
<Compile Include="Stories\WhenStoryAttibuteMissesSoThatTextInSoThatProperty.cs" />
<Compile Include="Stories\WhenStoryAttibuteMissesIWantTextInIWantProperty.cs" />
Expand Down
6 changes: 3 additions & 3 deletions TestStack.BDDfy/Reporters/ConsoleReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ private static void ReportStoryHeader(Story story)
return;

Console.WriteLine("Story: " + story.Metadata.Title);
Console.WriteLine("\t" + story.Metadata.AsA);
Console.WriteLine("\t" + story.Metadata.IWant);
Console.WriteLine("\t" + story.Metadata.SoThat);
Console.WriteLine("\t" + story.Metadata.Narrative1);
Console.WriteLine("\t" + story.Metadata.Narrative2);
Console.WriteLine("\t" + story.Metadata.Narrative3);
}

static string PrefixWithSpaceIfRequired(Step step)
Expand Down
8 changes: 4 additions & 4 deletions TestStack.BDDfy/Reporters/Html/HtmlReportBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ private void AddStoryMetadataAndNarrative(Story story)
AddLine(string.Format("<div class='storyTitle'>{0}</div>", story.Metadata.Title));
}

if (story.Metadata != null && !string.IsNullOrEmpty(story.Metadata.AsA))
if (story.Metadata != null && !string.IsNullOrEmpty(story.Metadata.Narrative1))
{
using (OpenTag("<ul class='storyNarrative'>", HtmlTag.ul))
{
AddLine(string.Format("<li>{0}</li>", story.Metadata.AsA));
AddLine(string.Format("<li>{0}</li>", story.Metadata.IWant));
AddLine(string.Format("<li>{0}</li>", story.Metadata.SoThat));
AddLine(string.Format("<li>{0}</li>", story.Metadata.Narrative1));
AddLine(string.Format("<li>{0}</li>", story.Metadata.Narrative2));
AddLine(string.Format("<li>{0}</li>", story.Metadata.Narrative3));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions TestStack.BDDfy/Reporters/MarkDown/MarkDownReportBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public string CreateReport(FileReportModel model)
if (story.Metadata != null)
{
report.AppendLine(string.Format("## Story: {0}", story.Metadata.Title));
report.AppendLine(string.Format(" **{0}** ", story.Metadata.AsA));
report.AppendLine(string.Format(" **{0}** ", story.Metadata.IWant));
report.AppendLine(string.Format(" **{0}** ", story.Metadata.SoThat));
report.AppendLine(string.Format(" **{0}** ", story.Metadata.Narrative1));
report.AppendLine(string.Format(" **{0}** ", story.Metadata.Narrative2));
report.AppendLine(string.Format(" **{0}** ", story.Metadata.Narrative3));
}

report.AppendLine(); // separator
Expand Down
4 changes: 2 additions & 2 deletions TestStack.BDDfy/Scanners/StoryAttributeMetaDataScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ protected virtual Type GetCandidateStory(object testObject, Type explicitStoryTy
return firstFrame.GetMethod().DeclaringType;
}

static StoryAttribute GetStoryAttribute(Type candidateStoryType)
static StoryNarrativeAttribute GetStoryAttribute(Type candidateStoryType)
{
return (StoryAttribute)candidateStoryType.GetCustomAttributes(typeof(StoryAttribute), true).FirstOrDefault();
return (StoryNarrativeAttribute)candidateStoryType.GetCustomAttributes(typeof(StoryNarrativeAttribute), true).FirstOrDefault();
}
}
}
48 changes: 9 additions & 39 deletions TestStack.BDDfy/Scanners/StoryMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,28 @@
using System;
using System.Text;

namespace TestStack.BDDfy
{
public class StoryMetadata
{
// ReSharper disable InconsistentNaming
private const string I_want_prefix = "I want";
private const string So_that_prefix = "So that";
private const string As_a_prefix = "As a";
// ReSharper restore InconsistentNaming

public StoryMetadata(Type storyType, StoryAttribute storyAttribute)
public StoryMetadata(Type storyType, StoryNarrativeAttribute narrative)
: this(storyType, narrative.Narrative1, narrative.Narrative2, narrative.Narrative3, narrative.Title)
{
var title = storyAttribute.Title;
if (string.IsNullOrEmpty(title))
title = NetToString.Convert(storyType.Name);

Type = storyType;
Title = title;

AsA = CleanseProperty(storyAttribute.AsA, As_a_prefix);
IWant= CleanseProperty(storyAttribute.IWant, I_want_prefix);
SoThat = CleanseProperty(storyAttribute.SoThat, So_that_prefix);
}

public StoryMetadata(Type storyType, string asA, string iWant, string soThat, string storyTitle = null)
public StoryMetadata(Type storyType, string narrative1, string narrative2, string narrative3, string storyTitle = null)
{
Title = storyTitle ?? NetToString.Convert(storyType.Name);
Type = storyType;

AsA = CleanseProperty(asA, As_a_prefix);
IWant = CleanseProperty(iWant, I_want_prefix);
SoThat = CleanseProperty(soThat, So_that_prefix);
}

string CleanseProperty(string text, string prefix)
{
var property = new StringBuilder();

if (string.IsNullOrWhiteSpace(text))
return null;

if (!text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
property.AppendFormat("{0} ", prefix);

property.Append(text);
return property.ToString();
Narrative1 = narrative1;
Narrative2 = narrative2;
Narrative3 = narrative3;
}

public Type Type { get; private set; }
public string Title { get; private set; }
public string AsA { get; private set; }
public string IWant { get; private set; }
public string SoThat { get; private set; }
public string Narrative1 { get; private set; }
public string Narrative2 { get; private set; }
public string Narrative3 { get; private set; }
}
}
29 changes: 24 additions & 5 deletions TestStack.BDDfy/StoryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@
namespace TestStack.BDDfy
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class StoryAttribute : Attribute
public class StoryAttribute : StoryNarrativeAttribute
{
public string Title { get; set; }
public string AsA { get; set; }
public string IWant { get; set; }
public string SoThat { get; set; }
// ReSharper disable InconsistentNaming
private const string I_want_prefix = "I want";
private const string So_that_prefix = "So that";
private const string As_a_prefix = "As a";
// ReSharper restore InconsistentNaming

public string AsA
{
get { return Narrative1; }
set { Narrative1 = CleanseProperty(value, As_a_prefix); }
}

public string IWant
{
get { return Narrative2; }
set { Narrative2 = CleanseProperty(value, I_want_prefix); }
}

public string SoThat
{
get { return Narrative3; }
set { Narrative3 = CleanseProperty(value, So_that_prefix); }
}
}
}
28 changes: 28 additions & 0 deletions TestStack.BDDfy/StoryNarrativeAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Text;

namespace TestStack.BDDfy
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class StoryNarrativeAttribute : Attribute
{
public string Title { get; set; }
public string Narrative1 { get; set; }
public string Narrative2 { get; set; }
public string Narrative3 { get; set; }

protected string CleanseProperty(string text, string prefix)
{
var property = new StringBuilder();

if (string.IsNullOrWhiteSpace(text))
return null;

if (!text.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
property.AppendFormat("{0} ", prefix);

property.Append(text);
return property.ToString();
}
}
}
1 change: 1 addition & 0 deletions TestStack.BDDfy/TestStack.BDDfy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
<Compile Include="Scanners\StepScanners\StepScannerExtensions.cs" />
<Compile Include="Scanners\StepScanners\StepTitleException.cs" />
<Compile Include="Scanners\StoryAttributeMetadataScanner.cs" />
<Compile Include="StoryNarrativeAttribute.cs" />
</ItemGroup>
<ItemGroup>
<None Include="BDDfy.snk" />
Expand Down