11using System ;
22using System . Diagnostics ;
33using System . Linq ;
4+ using System . Text ;
45
56namespace TestStack . BDDfy
67{
78 public class StoryAttributeMetadataScanner : IStoryMetadataScanner
89 {
10+ // ReSharper disable InconsistentNaming
11+ private const string I_want_prefix = "I want" ;
12+ private const string So_that_prefix = "So that" ;
13+ private const string As_a_prefix = "As a" ;
14+ // ReSharper restore InconsistentNaming
15+
916 public virtual StoryMetadata Scan ( object testObject , Type explicitStoryType = null )
1017 {
1118 return GetStoryMetadata ( testObject , explicitStoryType ) ?? GetStoryMetadataFromScenario ( testObject ) ;
@@ -18,7 +25,7 @@ static StoryMetadata GetStoryMetadataFromScenario(object testObject)
1825 if ( storyAttribute == null )
1926 return null ;
2027
21- return new StoryMetadata ( scenarioType , storyAttribute ) ;
28+ return CreateStoryMetadata ( scenarioType , storyAttribute ) ;
2229 }
2330
2431 StoryMetadata GetStoryMetadata ( object testObject , Type explicityStoryType )
@@ -31,7 +38,34 @@ StoryMetadata GetStoryMetadata(object testObject, Type explicityStoryType)
3138 if ( storyAttribute == null )
3239 return null ;
3340
34- return new StoryMetadata ( candidateStoryType , storyAttribute ) ;
41+ return CreateStoryMetadata ( candidateStoryType , storyAttribute ) ;
42+ }
43+
44+ static StoryMetadata CreateStoryMetadata ( Type storyType , StoryAttribute storyAttribute )
45+ {
46+ var title = storyAttribute . Title ;
47+ if ( string . IsNullOrEmpty ( title ) )
48+ title = NetToString . Convert ( storyType . Name ) ;
49+
50+ var narrative1 = CleanseProperty ( storyAttribute . AsA , As_a_prefix ) ;
51+ var narrative2 = CleanseProperty ( storyAttribute . IWant , I_want_prefix ) ;
52+ var narrative3 = CleanseProperty ( storyAttribute . SoThat , So_that_prefix ) ;
53+
54+ return new StoryMetadata ( storyType , narrative1 , narrative2 , narrative3 , title ) ;
55+ }
56+
57+ static string CleanseProperty ( string text , string prefix )
58+ {
59+ var property = new StringBuilder ( ) ;
60+
61+ if ( string . IsNullOrWhiteSpace ( text ) )
62+ return null ;
63+
64+ if ( ! text . StartsWith ( prefix , StringComparison . OrdinalIgnoreCase ) )
65+ property . AppendFormat ( "{0} " , prefix ) ;
66+
67+ property . Append ( text ) ;
68+ return property . ToString ( ) ;
3569 }
3670
3771 protected virtual Type GetCandidateStory ( object testObject , Type explicitStoryType )
0 commit comments