commit 4a7abd359ed6e37b10d7110f7c37f9e16aa98558
Author: Mauro Talevi <[email protected]>
AuthorDate: Thu Sep 13 15:35:01 2012 +0100
Commit: Mauro Talevi <[email protected]>
CommitDate: Thu Sep 13 15:40:00 2012 +0100
JBEHAVE-835: Handle spurious narratives as simple story description.
diff --git
a/jbehave-gherkin/src/main/java/org/jbehave/core/parsers/gherkin/GherkinStoryParser.java
b/jbehave-gherkin/src/main/java/org/jbehave/core/parsers/gherkin/GherkinStoryParser.java
index 5b558d8..141c86d 100644
---
a/jbehave-gherkin/src/main/java/org/jbehave/core/parsers/gherkin/GherkinStoryParser.java
+++
b/jbehave-gherkin/src/main/java/org/jbehave/core/parsers/gherkin/GherkinStoryParser.java
@@ -58,12 +58,14 @@ public class GherkinStoryParser extends
TransformingStoryParser {
}
private void writeNarrative(String description)
{
+ boolean valid = false;
Matcher findingNarrative = compile(".*"
+ keywords.narrative() + "(.*?)", DOTALL).matcher(description);
if (findingNarrative.matches()) {
String narrative =
findingNarrative.group(1).trim();
Matcher findingElements = compile(".*" +
keywords.inOrderTo() + "(.*)\\s*" + keywords.asA() + "(.*)\\s*" +
keywords.iWantTo()
+ "(.*)",
DOTALL).matcher(narrative);
if (findingElements.matches()) {
+ valid = true;
String inOrderTo =
findingElements.group(1).trim();
String asA =
findingElements.group(2).trim();
String iWantTo =
findingElements.group(3).trim();
@@ -73,6 +75,12 @@ public class GherkinStoryParser extends
TransformingStoryParser {
out.append(keywords.iWantTo()).append("
").append(iWantTo).append("\n\n");
}
}
+ if (!valid){
+ // if narrative format does not match,
write description as part of story description
+ out.append(description);
+
+ }
+
}
public void background(Background background) {
@@ -123,6 +131,7 @@ public class GherkinStoryParser extends
TransformingStoryParser {
};
new Parser(formatter).parse(storyAsText, "", 0);
+ System.out.println(out.toString());
return out.toString();
}
}
diff --git
a/jbehave-gherkin/src/test/java/org/jbehave/core/parsers/gherkin/GherkinStoryParserBehaviour.java
b/jbehave-gherkin/src/test/java/org/jbehave/core/parsers/gherkin/GherkinStoryParserBehaviour.java
index cb641ca..0c2f231 100644
---
a/jbehave-gherkin/src/test/java/org/jbehave/core/parsers/gherkin/GherkinStoryParserBehaviour.java
+++
b/jbehave-gherkin/src/test/java/org/jbehave/core/parsers/gherkin/GherkinStoryParserBehaviour.java
@@ -95,4 +95,20 @@ public class GherkinStoryParserBehaviour {
assertThat(narrative.iWantTo(), equalTo("drive cars on 4
wheels"));
}
+ @Test
+ public void shouldParseStoryWithUnsupportedNarrativeUsingGherkin()
throws IOException{
+ String storyAsText = "Feature: Hello Car\n"
+ + "As a car driver\n"
+ + "I want to drive cars on 4 wheels\n"
+ + "So that I can feel safer\n"
+ + "Scenario: Car can drive\n"
+ + "Given I have a car with 4 wheels\n"
+ + "Then I can drive it.\n";
+ Story story = storyParser.parseStory(storyAsText);
+ assertThat(story.getDescription().asString(), equalTo("Hello
Car\n\n"
+ + "As a car driver\n"
+ + "I want to drive cars on 4
wheels\n"
+ + "So that I can feel safer"));
+ }
+
}