| 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 | 4 | this(new RegexStoryParser()); |
| 28 | 4 | } |
| 29 | |
|
| 30 | |
public GherkinStoryParser(StoryParser delegate){ |
| 31 | 4 | super(delegate, new GherkinTransformer()); |
| 32 | 4 | } |
| 33 | |
|
| 34 | 16 | public static class GherkinTransformer implements StoryTransformer { |
| 35 | |
|
| 36 | |
private LocalizedKeywords keywords; |
| 37 | |
|
| 38 | |
public GherkinTransformer() { |
| 39 | 4 | this(new LocalizedKeywords()); |
| 40 | 4 | } |
| 41 | |
|
| 42 | 4 | public GherkinTransformer(LocalizedKeywords keywords) { |
| 43 | 4 | this.keywords = keywords; |
| 44 | 4 | } |
| 45 | |
|
| 46 | |
public String transform(String storyAsText) { |
| 47 | 4 | final StringBuffer out = new StringBuffer(); |
| 48 | |
|
| 49 | 4 | Formatter formatter = new Formatter(){ |
| 50 | |
public void uri(String uri) { |
| 51 | 4 | out.append(uri).append("\n"); |
| 52 | 4 | } |
| 53 | |
|
| 54 | |
public void feature(Feature feature) { |
| 55 | 4 | out.append(feature.getName()).append("\n\n"); |
| 56 | 4 | String description = feature.getDescription(); |
| 57 | 4 | writeNarrative(description); |
| 58 | 4 | } |
| 59 | |
|
| 60 | |
private void writeNarrative(String description) { |
| 61 | 4 | boolean valid = false; |
| 62 | 4 | Matcher findingNarrative = compile(".*" + keywords.narrative() + "(.*?)", DOTALL).matcher(description); |
| 63 | 4 | if (findingNarrative.matches()) { |
| 64 | 1 | String narrative = findingNarrative.group(1).trim(); |
| 65 | 1 | Matcher findingElements = compile(".*" + keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" + keywords.iWantTo() |
| 66 | |
+ "(.*)", DOTALL).matcher(narrative); |
| 67 | 1 | if (findingElements.matches()) { |
| 68 | 1 | valid = true; |
| 69 | 1 | String inOrderTo = findingElements.group(1).trim(); |
| 70 | 1 | String asA = findingElements.group(2).trim(); |
| 71 | 1 | String iWantTo = findingElements.group(3).trim(); |
| 72 | 1 | out.append(keywords.narrative()).append("\n"); |
| 73 | 1 | out.append(keywords.inOrderTo()).append(" ").append(inOrderTo).append("\n"); |
| 74 | 1 | out.append(keywords.asA()).append(" ").append(asA).append("\n"); |
| 75 | 1 | out.append(keywords.iWantTo()).append(" ").append(iWantTo).append("\n\n"); |
| 76 | |
} |
| 77 | |
} |
| 78 | 4 | if (!valid){ |
| 79 | |
|
| 80 | 3 | out.append(description); |
| 81 | |
|
| 82 | |
} |
| 83 | |
|
| 84 | 4 | } |
| 85 | |
|
| 86 | |
public void background(Background background) { |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
public void scenario(Scenario scenario) { |
| 90 | 3 | out.append(keywords.scenario()+scenario.getName()).append("\n\n"); |
| 91 | 3 | } |
| 92 | |
|
| 93 | |
public void scenarioOutline(ScenarioOutline scenarioOutline) { |
| 94 | 1 | out.append(keywords.scenario()+scenarioOutline.getName()).append("\n\n"); |
| 95 | 1 | } |
| 96 | |
|
| 97 | |
public void examples(Examples examples) { |
| 98 | 1 | out.append(keywords.examplesTable()+examples.getName()).append("\n"); |
| 99 | 1 | writeRows(examples.getRows()); |
| 100 | 1 | } |
| 101 | |
|
| 102 | |
public void step(Step step) { |
| 103 | 9 | out.append(step.getKeyword()+step.getName()).append("\n"); |
| 104 | 9 | writeRows(step.getRows()); |
| 105 | 9 | } |
| 106 | |
|
| 107 | |
public void eof() { |
| 108 | 4 | } |
| 109 | |
|
| 110 | |
public void syntaxError(String state, String event, |
| 111 | |
List<String> legalEvents, String uri, Integer line) { |
| 112 | 0 | } |
| 113 | |
|
| 114 | |
public void done() { |
| 115 | 0 | } |
| 116 | |
|
| 117 | |
public void close() { |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
private void writeRows(List<? extends Row> rows) { |
| 121 | 10 | if ( rows != null && rows.size() > 0 ){ |
| 122 | 2 | for ( Row row : rows ){ |
| 123 | 10 | out.append("|"); |
| 124 | 10 | for ( String c : row.getCells() ){ |
| 125 | 20 | out.append(c).append("|"); |
| 126 | |
} |
| 127 | 10 | out.append("\n"); |
| 128 | |
} |
| 129 | |
} |
| 130 | 10 | } |
| 131 | |
|
| 132 | |
}; |
| 133 | 4 | new Parser(formatter).parse(storyAsText, "", 0); |
| 134 | 4 | System.out.println(out.toString()); |
| 135 | 4 | return out.toString(); |
| 136 | |
} |
| 137 | |
} |
| 138 | |
|
| 139 | |
} |