Coverage Report - org.jbehave.core.parsers.gherkin.GherkinStoryParser
 
Classes in this File Line Coverage Branch Coverage Complexity
GherkinStoryParser
100%
4/4
N/A
1.45
GherkinStoryParser$GherkinTransformer
100%
10/10
N/A
1.45
GherkinStoryParser$GherkinTransformer$1
95%
63/66
88%
16/18
1.45
 
 1  
 package org.jbehave.core.parsers.gherkin;
 2  
 
 3  
 import static java.util.regex.Pattern.DOTALL;
 4  
 import static java.util.regex.Pattern.compile;
 5  
 import gherkin.formatter.Formatter;
 6  
 import gherkin.formatter.model.Background;
 7  
 import gherkin.formatter.model.Examples;
 8  
 import gherkin.formatter.model.Feature;
 9  
 import gherkin.formatter.model.Row;
 10  
 import gherkin.formatter.model.Scenario;
 11  
 import gherkin.formatter.model.ScenarioOutline;
 12  
 import gherkin.formatter.model.Step;
 13  
 import gherkin.parser.Parser;
 14  
 
 15  
 import java.util.List;
 16  
 import java.util.regex.Matcher;
 17  
 
 18  
 import org.jbehave.core.i18n.LocalizedKeywords;
 19  
 import org.jbehave.core.parsers.RegexStoryParser;
 20  
 import org.jbehave.core.parsers.StoryParser;
 21  
 import org.jbehave.core.parsers.StoryTransformer;
 22  
 import org.jbehave.core.parsers.TransformingStoryParser;
 23  
 
 24  
 public class GherkinStoryParser extends TransformingStoryParser {
 25  
 
 26  
         public GherkinStoryParser(){
 27  5
                 this(new RegexStoryParser());
 28  5
         }
 29  
 
 30  
         public GherkinStoryParser(StoryParser delegate){
 31  5
                 super(delegate, new GherkinTransformer());
 32  5
         }
 33  
 
 34  30
         public static class GherkinTransformer implements StoryTransformer {
 35  
 
 36  
                 private LocalizedKeywords keywords;
 37  
                 
 38  
                 public GherkinTransformer() {
 39  5
                         this(new LocalizedKeywords());
 40  5
                 }
 41  
 
 42  5
                 public GherkinTransformer(LocalizedKeywords keywords) {
 43  5
                         this.keywords = keywords;
 44  5
                 }
 45  
 
 46  
                 public String transform(String storyAsText) {
 47  5
                         final StringBuffer out = new StringBuffer();
 48  
 
 49  5
                         Formatter formatter = new Formatter(){
 50  
                                 public void uri(String uri) {
 51  5
                                         out.append(uri).append("\n");
 52  5
                                 }
 53  
 
 54  
                                 public void feature(Feature feature) {
 55  5
                                         out.append(feature.getName()).append("\n\n");
 56  5
                                         String description = feature.getDescription();
 57  5
                                         writeNarrative(description);
 58  5
                                 }
 59  
 
 60  
                                 private void writeNarrative(String description) {
 61  5
                     boolean matches = false;
 62  5
                                         Matcher findingNarrative = compile(".*" + keywords.narrative() + "(.*?)", DOTALL).matcher(description);
 63  5
                                 if (findingNarrative.matches()) {
 64  2
                                     String narrative = findingNarrative.group(1).trim();
 65  2
                                     matches = writeNarrativeWithDefaultSyntax(out, narrative);
 66  2
                                     if (!matches){
 67  1
                                         matches = writeNarrativeWithAlternativeSyntax(out, narrative);
 68  
                                     }
 69  
                                 }
 70  5
                     if (!matches){
 71  
                                         // if narrative format does not match, write description as part of story description
 72  3
                                         out.append(description);
 73  
                                 }                               
 74  5
                                 }
 75  
 
 76  
                 private boolean writeNarrativeWithDefaultSyntax(final StringBuffer out, String narrative) {
 77  2
                     boolean matches = false;
 78  2
                     Matcher findingElements = compile(".*" + keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo()
 79  
                             + "(.*)", DOTALL).matcher(narrative);
 80  2
                     if (findingElements.matches()) {
 81  1
                         String inOrderTo = findingElements.group(1).trim();
 82  1
                         String asA = findingElements.group(2).trim();
 83  1
                         String iWantTo = findingElements.group(3).trim();
 84  1
                         out.append(keywords.narrative()).append("\n");
 85  1
                         out.append(keywords.inOrderTo()).append(" ").append(inOrderTo).append("\n");
 86  1
                         out.append(keywords.asA()).append(" ").append(asA).append("\n");
 87  1
                         out.append(keywords.iWantTo()).append(" ").append(iWantTo).append("\n\n");
 88  1
                         matches = true;
 89  
                     }
 90  2
                     return matches;
 91  
                 }
 92  
 
 93  
                 private boolean writeNarrativeWithAlternativeSyntax(final StringBuffer out, String narrative) {
 94  1
                     boolean matches = false;
 95  1
                     Matcher findingElements = compile(".*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo() + "(.*)\\s*" + keywords.soThat()
 96  
                             + "(.*)", DOTALL).matcher(narrative);
 97  1
                     if (findingElements.matches()) {
 98  1
                         String asA = findingElements.group(1).trim();
 99  1
                         String iWantTo = findingElements.group(2).trim();
 100  1
                         String soThat = findingElements.group(3).trim();
 101  1
                         out.append(keywords.narrative()).append("\n");
 102  1
                         out.append(keywords.asA()).append(" ").append(asA).append("\n");
 103  1
                         out.append(keywords.iWantTo()).append(" ").append(iWantTo).append("\n\n");                          
 104  1
                         out.append(keywords.soThat()).append(" ").append(soThat).append("\n");
 105  1
                         matches = true;
 106  
                     }
 107  1
                     return matches;
 108  
                 }
 109  
 
 110  
                                 public void background(Background background) {
 111  1
                     out.append(keywords.lifecycle()+background.getName()).append("\n")
 112  
                        .append(keywords.before()+"\n");
 113  1
                                 }
 114  
 
 115  
                                 public void scenario(Scenario scenario) {
 116  4
                                         out.append("\n").append(keywords.scenario()+scenario.getName()).append("\n\n");
 117  4
                                 }
 118  
 
 119  
                                 public void scenarioOutline(ScenarioOutline scenarioOutline) {
 120  1
                                         out.append("\n").append(keywords.scenario()+scenarioOutline.getName()).append("\n\n");
 121  1
                                 }
 122  
 
 123  
                                 public void examples(Examples examples) {
 124  1
                                         out.append("\n").append(keywords.examplesTable()+examples.getName()).append("\n");
 125  1
                                         writeRows(examples.getRows());
 126  1
                                 }
 127  
 
 128  
                                 public void step(Step step) {
 129  12
                                         out.append(step.getKeyword()+step.getName()).append("\n");
 130  12
                                         writeRows(step.getRows());
 131  12
                                 }
 132  
 
 133  
                                 public void eof() {
 134  5
                                 }
 135  
 
 136  
                                 public void syntaxError(String state, String event,
 137  
                                                 List<String> legalEvents, String uri, Integer line) {
 138  0
                                 }
 139  
 
 140  
                                 public void done() {
 141  0
                                 }
 142  
 
 143  
                                 public void close() {
 144  0
                                 }
 145  
                                 
 146  
                                 private void writeRows(List<? extends Row> rows) {
 147  13
                                         if ( rows != null && rows.size() > 0 ){
 148  2
                                                 for ( Row row : rows ){
 149  10
                                                         out.append("|");
 150  10
                                                         for ( String c : row.getCells() ){
 151  20
                                                                 out.append(c).append("|");
 152  20
                                                         }
 153  10
                                                         out.append("\n");
 154  10
                                                 }
 155  
                                         }
 156  13
                                 }
 157  
 
 158  
                         };
 159  5
                         new Parser(formatter).parse(storyAsText, "", 0);
 160  5
                         return out.toString();
 161  
                 }
 162  
         }
 163  
 
 164  
 }