Coverage Report - org.jbehave.core.parsers.RegexStoryParser
 
Classes in this File Line Coverage Branch Coverage Complexity
RegexStoryParser
98%
111/113
97%
43/44
1.871
 
 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  33
         this(new LocalizedKeywords());
 37  33
     }
 38  
 
 39  
     public RegexStoryParser(Keywords keywords) {
 40  645
         this(keywords, new ExamplesTableFactory());
 41  645
     }
 42  
 
 43  
     public RegexStoryParser(ExamplesTableFactory tableFactory) {
 44  0
         this(new LocalizedKeywords(), tableFactory);
 45  0
     }
 46  
 
 47  645
     public RegexStoryParser(Keywords keywords, ExamplesTableFactory tableFactory) {
 48  645
         this.keywords = keywords;
 49  645
         this.tableFactory = tableFactory;
 50  645
     }
 51  
 
 52  
     public Story parseStory(String storyAsText) {
 53  3
         return parseStory(storyAsText, null);
 54  
     }
 55  
 
 56  
     public Story parseStory(String storyAsText, String storyPath) {
 57  24
         Description description = parseDescriptionFrom(storyAsText);
 58  24
         Meta meta = parseStoryMetaFrom(storyAsText);
 59  24
         Narrative narrative = parseNarrativeFrom(storyAsText);
 60  24
         List<Scenario> scenarios = parseScenariosFrom(storyAsText);
 61  24
         Story story = new Story(storyPath, description, meta, narrative, scenarios);
 62  24
         if (storyPath != null) {
 63  18
             story.namedAs(new File(storyPath).getName());
 64  
         }
 65  24
         return story;
 66  
     }
 67  
 
 68  
     private Description parseDescriptionFrom(String storyAsText) {
 69  24
         Matcher findingDescription = patternToPullDescriptionIntoGroupOne().matcher(storyAsText);
 70  24
         if (findingDescription.matches()) {
 71  14
             return new Description(findingDescription.group(1).trim());
 72  
         }
 73  10
         return Description.EMPTY;
 74  
     }
 75  
 
 76  
     private Meta parseStoryMetaFrom(String storyAsText) {
 77  24
         Matcher findingMeta = patternToPullStoryMetaIntoGroupOne().matcher(preScenarioText(storyAsText));
 78  24
         if (findingMeta.matches()) {
 79  3
             String meta = findingMeta.group(1).trim();
 80  3
             return createMeta(meta);
 81  
         }
 82  21
         return Meta.EMPTY;
 83  
     }
 84  
 
 85  
     private String preScenarioText(String storyAsText) {
 86  24
         String[] split = storyAsText.split(keywords.scenario());
 87  24
         return split.length > 0 ? split[0] : storyAsText;
 88  
     }
 89  
 
 90  
     private Meta createMeta(String meta) {
 91  7
         List<String> properties = new ArrayList<String>();
 92  24
         for (String property : meta.split(keywords.metaProperty())) {
 93  17
             if (StringUtils.isNotBlank(property)) {
 94  12
                 String beforeIgnorable = StringUtils.substringBefore(property,keywords.ignorable());
 95  12
                 if ( StringUtils.isNotBlank(beforeIgnorable)){
 96  10
                     properties.add(beforeIgnorable);
 97  
                 }
 98  
             }
 99  
         }
 100  7
         return new Meta(properties);
 101  
     }
 102  
 
 103  
     private Narrative parseNarrativeFrom(String storyAsText) {
 104  24
         Matcher findingNarrative = patternToPullNarrativeIntoGroupOne().matcher(storyAsText);
 105  24
         if (findingNarrative.matches()) {
 106  3
             String narrative = findingNarrative.group(1).trim();
 107  3
             return createNarrative(narrative);
 108  
         }
 109  21
         return Narrative.EMPTY;
 110  
     }
 111  
 
 112  
     private Narrative createNarrative(String narrative) {
 113  3
         Pattern findElements = patternToPullNarrativeElementsIntoGroups();
 114  3
         Matcher findingElements = findElements.matcher(narrative);
 115  3
         if (findingElements.matches()) {
 116  2
             String inOrderTo = findingElements.group(1).trim();
 117  2
             String asA = findingElements.group(2).trim();
 118  2
             String iWantTo = findingElements.group(3).trim();
 119  2
             return new Narrative(inOrderTo, asA, iWantTo);
 120  
         }
 121  1
         return Narrative.EMPTY;
 122  
     }
 123  
 
 124  
     private List<Scenario> parseScenariosFrom(String storyAsText) {
 125  24
         List<Scenario> parsed = new ArrayList<Scenario>();
 126  24
         for (String scenarioAsText : splitScenarios(storyAsText)) {
 127  125
             parsed.add(parseScenario(scenarioAsText));
 128  
         }
 129  24
         return parsed;
 130  
     }
 131  
 
 132  
     private List<String> splitScenarios(String storyAsText) {
 133  24
         List<String> scenarios = new ArrayList<String>();
 134  24
         String scenarioKeyword = keywords.scenario();
 135  
 
 136  
         // remove anything after scenario keyword, if found
 137  24
         if (StringUtils.contains(storyAsText, scenarioKeyword)) {
 138  14
             storyAsText = StringUtils.substringAfter(storyAsText, scenarioKeyword);
 139  
         }
 140  
 
 141  151
         for (String scenarioAsText : storyAsText.split(scenarioKeyword)) {
 142  127
             if (scenarioAsText.trim().length() > 0) {
 143  125
                 scenarios.add(scenarioKeyword + "\n" + scenarioAsText);
 144  
             }
 145  
         }
 146  24
         return scenarios;
 147  
     }
 148  
 
 149  
     private Scenario parseScenario(String scenarioAsText) {
 150  125
         String title = findScenarioTitle(scenarioAsText);
 151  125
         String scenarioWithoutTitle = removeStart(scenarioAsText, title);
 152  125
         Meta meta = findScenarioMeta(scenarioWithoutTitle);
 153  125
         ExamplesTable examplesTable = findExamplesTable(scenarioWithoutTitle);
 154  125
         GivenStories givenStories = findGivenStories(scenarioWithoutTitle);
 155  125
         if (givenStories.requireParameters()) {
 156  1
             givenStories.useExamplesTable(examplesTable);
 157  
         }
 158  125
         List<String> steps = findSteps(scenarioWithoutTitle);
 159  125
         return new Scenario(title, meta, givenStories, examplesTable, steps);
 160  
     }
 161  
 
 162  
     private String findScenarioTitle(String scenarioAsText) {
 163  125
         Matcher findingTitle = patternToPullScenarioTitleIntoGroupOne().matcher(scenarioAsText);
 164  125
         return findingTitle.find() ? findingTitle.group(1).trim() : NONE;
 165  
     }
 166  
 
 167  
     private Meta findScenarioMeta(String scenarioAsText) {
 168  125
         Matcher findingMeta = patternToPullScenarioMetaIntoGroupOne().matcher(scenarioAsText);
 169  125
         if (findingMeta.matches()) {
 170  4
             String meta = findingMeta.group(1).trim();
 171  4
             return createMeta(meta);
 172  
         }
 173  121
         return Meta.EMPTY;
 174  
     }
 175  
 
 176  
     private ExamplesTable findExamplesTable(String scenarioAsText) {
 177  125
         Matcher findingTable = patternToPullExamplesTableIntoGroupOne().matcher(scenarioAsText);
 178  125
         String tableInput = findingTable.find() ? findingTable.group(1).trim() : NONE;
 179  125
         return tableFactory.createExamplesTable(tableInput);
 180  
     }
 181  
 
 182  
     private GivenStories findGivenStories(String scenarioAsText) {
 183  125
         Matcher findingGivenStories = patternToPullGivenStoriesIntoGroupOne().matcher(scenarioAsText);
 184  125
         String givenStories = findingGivenStories.find() ? findingGivenStories.group(1).trim() : NONE;
 185  125
         return new GivenStories(givenStories);
 186  
     }
 187  
 
 188  
     private List<String> findSteps(String scenarioAsText) {
 189  125
         Matcher matcher = patternToPullStepsIntoGroupOne().matcher(scenarioAsText);
 190  125
         List<String> steps = new ArrayList<String>();
 191  125
         int startAt = 0;
 192  15186
         while (matcher.find(startAt)) {
 193  15061
             steps.add(StringUtils.substringAfter(matcher.group(1), "\n"));
 194  15061
             startAt = matcher.start(4);
 195  
         }
 196  125
         return steps;
 197  
     }
 198  
 
 199  
     // Regex Patterns
 200  
 
 201  
     private Pattern patternToPullDescriptionIntoGroupOne() {
 202  24
         String metaOrNarrativeOrScenario = concatenateWithOr(keywords.meta(), keywords.narrative(), keywords.scenario());
 203  24
         return compile("(.*?)(" + metaOrNarrativeOrScenario + ").*", DOTALL);
 204  
     }
 205  
 
 206  
     private Pattern patternToPullStoryMetaIntoGroupOne() {
 207  24
         return compile(".*" + keywords.meta() + "(.*?)\\s*(\\Z|" + keywords.narrative() + ").*", DOTALL);
 208  
     }
 209  
 
 210  
     private Pattern patternToPullNarrativeIntoGroupOne() {
 211  24
         return compile(".*" + keywords.narrative() + "(.*?)\\s*(" + keywords.scenario() + ").*", DOTALL);
 212  
     }
 213  
 
 214  
     private Pattern patternToPullNarrativeElementsIntoGroups() {
 215  3
         return compile(".*" + keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo()
 216  
                 + "(.*)", DOTALL);
 217  
     }
 218  
 
 219  
     private Pattern patternToPullScenarioTitleIntoGroupOne() {
 220  125
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 221  125
         return compile(keywords.scenario() + "((.)*?)\\s*(" + keywords.meta() + "|" + startingWords + ").*", DOTALL);
 222  
     }
 223  
 
 224  
     private Pattern patternToPullScenarioMetaIntoGroupOne() {
 225  125
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 226  125
         return compile(".*" + keywords.meta() + "(.*?)\\s*(" + keywords.givenStories() + "|" + startingWords + ").*",
 227  
                 DOTALL);
 228  
     }
 229  
 
 230  
     private Pattern patternToPullGivenStoriesIntoGroupOne() {
 231  125
         String startingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 232  125
         return compile("\\n" + keywords.givenStories() + "((.|\\n)*?)\\s*(" + startingWords + ").*", DOTALL);
 233  
     }
 234  
 
 235  
     private Pattern patternToPullStepsIntoGroupOne() {
 236  125
         String initialStartingWords = concatenateWithOr("\\n", "", keywords.startingWords());
 237  125
         String followingStartingWords = concatenateWithOr("\\n", "\\s", keywords.startingWords());
 238  125
         return compile(
 239  
                 "((" + initialStartingWords + ") (.)*?)\\s*(\\Z|" + followingStartingWords + "|\\n"
 240  
                         + keywords.examplesTable() + ")", DOTALL);
 241  
     }
 242  
 
 243  
     private Pattern patternToPullExamplesTableIntoGroupOne() {
 244  125
         return compile("\\n" + keywords.examplesTable() + "\\s*(.*)", DOTALL);
 245  
     }
 246  
 
 247  
     private String concatenateWithOr(String... keywords) {
 248  24
         return concatenateWithOr(null, null, keywords);
 249  
     }
 250  
 
 251  
     private String concatenateWithOr(String beforeKeyword, String afterKeyword, String[] keywords) {
 252  649
         StringBuilder builder = new StringBuilder();
 253  649
         String before = beforeKeyword != null ? beforeKeyword : NONE;
 254  649
         String after = afterKeyword != null ? afterKeyword : NONE;
 255  3846
         for (String keyword : keywords) {
 256  3197
             builder.append(before).append(keyword).append(after).append("|");
 257  
         }
 258  649
         return StringUtils.chomp(builder.toString(), "|"); // chop off the last
 259  
                                                            // "|"
 260  
     }
 261  
 
 262  
 }