Coverage Report - org.jbehave.core.parsers.RegexStoryParser
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexStoryParser
98%
116/118
97%
43/44
1.844
 
 1  
 package org.jbehave.core.parsers;
 2  
 
 3  
 import java.io.File;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 import java.util.regex.Matcher;
 7  
 import java.util.regex.Pattern;
 8  
 
 9  
 import org.apache.commons.lang.StringUtils;
 10  
 import org.jbehave.core.configuration.Keywords;
 11  
 import org.jbehave.core.i18n.LocalizedKeywords;
 12  
 import org.jbehave.core.model.Description;
 13  
 import org.jbehave.core.model.ExamplesTable;
 14  
 import org.jbehave.core.model.ExamplesTableFactory;
 15  
 import org.jbehave.core.model.GivenStories;
 16  
 import org.jbehave.core.model.Meta;
 17  
 import org.jbehave.core.model.Narrative;
 18  
 import org.jbehave.core.model.Scenario;
 19  
 import org.jbehave.core.model.Story;
 20  
 
 21  
 import static java.util.regex.Pattern.DOTALL;
 22  
 import static java.util.regex.Pattern.compile;
 23  
 import static org.apache.commons.lang.StringUtils.removeStart;
 24  
 
 25  
 /**
 26  
  * Pattern-based story parser, which uses the keywords provided to parse the
 27  
  * textual story into a {@link Story}.
 28  
  */
 29  
 public class RegexStoryParser implements StoryParser {
 30  
 
 31  
     private static final String NONE = "";
 32  
     private final Keywords keywords;
 33  
     private final ExamplesTableFactory tableFactory;
 34  
 
 35  
     public RegexStoryParser() {
 36  34
         this(new LocalizedKeywords());
 37  34
     }
 38  
 
 39  
     public RegexStoryParser(Keywords keywords) {
 40  691
         this(keywords, new ExamplesTableFactory());
 41  691
     }
 42  
 
 43  
     public RegexStoryParser(ExamplesTableFactory tableFactory) {
 44  0
         this(new LocalizedKeywords(), tableFactory);
 45  0
     }
 46  
 
 47  691
     public RegexStoryParser(Keywords keywords, ExamplesTableFactory tableFactory) {
 48  691
         this.keywords = keywords;
 49  691
         this.tableFactory = tableFactory;
 50  691
     }
 51  
 
 52  
     public Story parseStory(String storyAsText) {
 53  4
         return parseStory(storyAsText, null);
 54  
     }
 55  
 
 56  
     public Story parseStory(String storyAsText, String storyPath) {
 57  27
         Description description = parseDescriptionFrom(storyAsText);
 58  27
         Meta meta = parseStoryMetaFrom(storyAsText);
 59  27
         Narrative narrative = parseNarrativeFrom(storyAsText);
 60  27
         GivenStories givenStories = parseGivenStories(storyAsText);
 61  27
         List<Scenario> scenarios = parseScenariosFrom(storyAsText);
 62  27
         Story story = new Story(storyPath, description, meta, narrative, givenStories, scenarios);
 63  27
         if (storyPath != null) {
 64  20
             story.namedAs(new File(storyPath).getName());
 65  
         }
 66  27
         return story;
 67  
     }
 68  
 
 69  
     private Description parseDescriptionFrom(String storyAsText) {
 70  27
         Matcher findingDescription = patternToPullDescriptionIntoGroupOne().matcher(storyAsText);
 71  27
         if (findingDescription.matches()) {
 72  17
             return new Description(findingDescription.group(1).trim());
 73  
         }
 74  10
         return Description.EMPTY;
 75  
     }
 76  
 
 77  
     private Meta parseStoryMetaFrom(String storyAsText) {
 78  27
         Matcher findingMeta = patternToPullStoryMetaIntoGroupOne().matcher(preScenarioText(storyAsText));
 79  27
         if (findingMeta.matches()) {
 80  4
             String meta = findingMeta.group(1).trim();
 81  4
             return Meta.createMeta(meta, keywords);
 82  
         }
 83  23
         return Meta.EMPTY;
 84  
     }
 85  
 
 86  
     private String preScenarioText(String storyAsText) {
 87  27
         String[] split = storyAsText.split(keywords.scenario());
 88  27
         return split.length > 0 ? split[0] : storyAsText;
 89  
     }
 90  
 
 91  
     private Narrative parseNarrativeFrom(String storyAsText) {
 92  27
         Matcher findingNarrative = patternToPullNarrativeIntoGroupOne().matcher(storyAsText);
 93  27
         if (findingNarrative.matches()) {
 94  3
             String narrative = findingNarrative.group(1).trim();
 95  3
             return createNarrative(narrative);
 96  
         }
 97  24
         return Narrative.EMPTY;
 98  
     }
 99  
 
 100  
     private Narrative createNarrative(String narrative) {
 101  3
         Pattern findElements = patternToPullNarrativeElementsIntoGroups();
 102  3
         Matcher findingElements = findElements.matcher(narrative);
 103  3
         if (findingElements.matches()) {
 104  2
             String inOrderTo = findingElements.group(1).trim();
 105  2
             String asA = findingElements.group(2).trim();
 106  2
             String iWantTo = findingElements.group(3).trim();
 107  2
             return new Narrative(inOrderTo, asA, iWantTo);
 108  
         }
 109  1
         return Narrative.EMPTY;
 110  
     }
 111  
     
 112  
     private GivenStories parseGivenStories(String storyAsText) {
 113  27
         String scenarioKeyword = keywords.scenario();
 114  
         // use text before scenario keyword, if found
 115  27
         String beforeScenario = "";
 116  27
         if (StringUtils.contains(storyAsText, scenarioKeyword)) {
 117  16
             beforeScenario = StringUtils.substringBefore(storyAsText, scenarioKeyword);
 118  
         }
 119  27
         Matcher findingGivenStories = patternToPullStoryGivenStoriesIntoGroupOne().matcher(beforeScenario);
 120  27
         String givenStories = findingGivenStories.find() ? findingGivenStories.group(1).trim() : NONE;
 121  27
         return new GivenStories(givenStories);
 122  
     }
 123  
 
 124  
     private List<Scenario> parseScenariosFrom(String storyAsText) {
 125  27
         List<Scenario> parsed = new ArrayList<Scenario>();
 126  27
         for (String scenarioAsText : splitScenarios(storyAsText)) {
 127  128
             parsed.add(parseScenario(scenarioAsText));
 128  
         }
 129  27
         return parsed;
 130  
     }
 131  
 
 132  
     private List<String> splitScenarios(String storyAsText) {
 133  27
         List<String> scenarios = new ArrayList<String>();
 134  27
         String scenarioKeyword = keywords.scenario();
 135  
 
 136  
         // use text after scenario keyword, if found
 137  27
         if (StringUtils.contains(storyAsText, scenarioKeyword)) {
 138  16
             storyAsText = StringUtils.substringAfter(storyAsText, scenarioKeyword);
 139  
         }
 140  
 
 141  157
         for (String scenarioAsText : storyAsText.split(scenarioKeyword)) {
 142  130
             if (scenarioAsText.trim().length() > 0) {
 143  128
                 scenarios.add(scenarioKeyword + "\n" + scenarioAsText);
 144  
             }
 145  
         }
 146  
         
 147  27
         return scenarios;
 148  
     }
 149  
 
 150  
     private Scenario parseScenario(String scenarioAsText) {
 151  128
         String title = findScenarioTitle(scenarioAsText);
 152  128
         String scenarioWithoutKeyword = removeStart(scenarioAsText, keywords.scenario()).trim();
 153  128
         String scenarioWithoutTitle = removeStart(scenarioWithoutKeyword, title);
 154  128
         if ( !scenarioWithoutTitle.startsWith("\n") ){ // always ensure scenario starts with newline
 155  16
             scenarioWithoutTitle = "\n" + scenarioWithoutTitle;
 156  
         }
 157  128
         Meta meta = findScenarioMeta(scenarioWithoutTitle);
 158  128
         ExamplesTable examplesTable = findExamplesTable(scenarioWithoutTitle);
 159  128
         GivenStories givenStories = findScenarioGivenStories(scenarioWithoutTitle);
 160  128
         if (givenStories.requireParameters()) {
 161  1
             givenStories.useExamplesTable(examplesTable);
 162  
         }
 163  128
         List<String> steps = findSteps(scenarioWithoutTitle);
 164  128
         return new Scenario(title, meta, givenStories, examplesTable, steps);
 165  
     }
 166  
 
 167  
     private String findScenarioTitle(String scenarioAsText) {
 168  128
         Matcher findingTitle = patternToPullScenarioTitleIntoGroupOne().matcher(scenarioAsText);
 169  128
         return findingTitle.find() ? findingTitle.group(1).trim() : NONE;
 170  
     }
 171  
 
 172  
     private Meta findScenarioMeta(String scenarioAsText) {
 173  128
         Matcher findingMeta = patternToPullScenarioMetaIntoGroupOne().matcher(scenarioAsText);
 174  128
         if (findingMeta.matches()) {
 175  5
             String meta = findingMeta.group(1).trim();
 176  5
             return Meta.createMeta(meta, keywords);
 177  
         }
 178  123
         return Meta.EMPTY;
 179  
     }
 180  
 
 181  
     private ExamplesTable findExamplesTable(String scenarioAsText) {
 182  128
         Matcher findingTable = patternToPullExamplesTableIntoGroupOne().matcher(scenarioAsText);
 183  128
         String tableInput = findingTable.find() ? findingTable.group(1).trim() : NONE;
 184  128
         return tableFactory.createExamplesTable(tableInput);
 185  
     }
 186  
 
 187  
     private GivenStories findScenarioGivenStories(String scenarioAsText) {
 188  128
         Matcher findingGivenStories = patternToPullScenarioGivenStoriesIntoGroupOne().matcher(scenarioAsText);
 189  128
         String givenStories = findingGivenStories.find() ? findingGivenStories.group(1).trim() : NONE;
 190  128
         return new GivenStories(givenStories);
 191  
     }
 192  
 
 193  
     private List<String> findSteps(String scenarioAsText) {
 194  128
         Matcher matcher = patternToPullStepsIntoGroupOne().matcher(scenarioAsText);
 195  128
         List<String> steps = new ArrayList<String>();
 196  128
         int startAt = 0;
 197  15195
         while (matcher.find(startAt)) {
 198  15067
             steps.add(StringUtils.substringAfter(matcher.group(1), "\n"));
 199  15067
             startAt = matcher.start(4);
 200  
         }
 201  128
         return steps;
 202  
     }
 203  
 
 204  
     // Regex Patterns
 205  
 
 206  
     private Pattern patternToPullDescriptionIntoGroupOne() {
 207  27
         String metaOrNarrativeOrScenario = concatenateWithOr(keywords.meta(), keywords.narrative(), keywords.scenario());
 208  27
         return compile("(.*?)(" + metaOrNarrativeOrScenario + ").*", DOTALL);
 209  
     }
 210  
 
 211  
     private Pattern patternToPullStoryMetaIntoGroupOne() {
 212  27
         return compile(".*" + keywords.meta() + "(.*?)\\s*(\\Z|" + keywords.narrative() + "|" +  keywords.givenStories() + ").*", DOTALL);
 213  
     }
 214  
 
 215  
     private Pattern patternToPullNarrativeIntoGroupOne() {
 216  27
         return compile(".*" + keywords.narrative() + "(.*?)\\s*(" + keywords.givenStories() + "|" + keywords.scenario() + ").*", DOTALL);
 217  
     }
 218  
 
 219  
     private Pattern patternToPullNarrativeElementsIntoGroups() {
 220  3
         return compile(".*" + keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo()
 221  
                 + "(.*)", DOTALL);
 222  
     }
 223  
 
 224  
     private Pattern patternToPullScenarioTitleIntoGroupOne() {
 225  128
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 226  128
         return compile(keywords.scenario() + "((.)*?)\\s*(" + keywords.meta() + "|" + startingWords + ").*", DOTALL);
 227  
     }
 228  
 
 229  
     private Pattern patternToPullScenarioMetaIntoGroupOne() {
 230  128
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 231  128
         return compile(".*" + keywords.meta() + "(.*?)\\s*(" + keywords.givenStories() + "|" + startingWords + ").*",
 232  
                 DOTALL);
 233  
     }
 234  
 
 235  
     private Pattern patternToPullStoryGivenStoriesIntoGroupOne() {
 236  27
         return compile(".*" + keywords.givenStories() + "\\s*(.*)", DOTALL);
 237  
     }
 238  
 
 239  
     private Pattern patternToPullScenarioGivenStoriesIntoGroupOne() {
 240  128
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 241  128
         return compile("\\n" + keywords.givenStories() + "((.|\\n)*?)\\s*(" + startingWords + ").*", DOTALL);
 242  
     }
 243  
 
 244  
     private Pattern patternToPullStepsIntoGroupOne() {
 245  128
         String initialStartingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 246  128
         String followingStartingWords = concatenateWithOr("\\n", "\\s", keywords.startingWords());
 247  128
         return compile(
 248  
                 "((" + initialStartingWords + ")\\s(.)*?)\\s*(\\Z|" + followingStartingWords + "|\\n"
 249  
                         + keywords.examplesTable() + ")", DOTALL);
 250  
     }
 251  
 
 252  
     private Pattern patternToPullExamplesTableIntoGroupOne() {
 253  128
         return compile("\\n" + keywords.examplesTable() + "\\s*(.*)", DOTALL);
 254  
     }
 255  
 
 256  
     private String concatenateWithOr(String... keywords) {
 257  27
         return concatenateWithOr(null, null, keywords);
 258  
     }
 259  
 
 260  
     private String concatenateWithOr(String beforeKeyword, String afterKeyword, String[] keywords) {
 261  667
         StringBuilder builder = new StringBuilder();
 262  667
         String before = beforeKeyword != null ? beforeKeyword : NONE;
 263  667
         String after = afterKeyword != null ? afterKeyword : NONE;
 264  3948
         for (String keyword : keywords) {
 265  3281
             builder.append(before).append(keyword).append(after).append("|");
 266  
         }
 267  667
         return StringUtils.chomp(builder.toString(), "|"); // chop off the last
 268  
                                                            // "|"
 269  
     }
 270  
 
 271  
 }