commit 5bca1c2931ef77d9a14c5b89d50a9ad084725449 Author: Mauro Talevi <mauro.tal...@aquilonia.org> AuthorDate: Sun, 24 Nov 2013 18:38:26 +0100 Commit: Mauro Talevi <mauro.tal...@aquilonia.org> CommitDate: Sun, 24 Nov 2013 18:38:26 +0100
JBEHAVE-923: Allow filtering of scenarios in GivenStories anchors. diff --git a/jbehave-core/src/main/java/org/jbehave/core/embedder/PerformableTree.java b/jbehave-core/src/main/java/org/jbehave/core/embedder/PerformableTree.java index 6ddc764..07e00ea 100644 --- a/jbehave-core/src/main/java/org/jbehave/core/embedder/PerformableTree.java +++ b/jbehave-core/src/main/java/org/jbehave/core/embedder/PerformableTree.java @@ -176,14 +176,38 @@ public class PerformableTree { for (GivenStory givenStory : givenStories.getStories()) { RunContext childContext = context.childContextFor(givenStory); // run given story, using any parameters provided - Story storyOfPath = storyOfPath(context.configuration(), childContext.path()); + Story story = storyOfPath(context.configuration(), childContext.path()); + if ( givenStory.hasAnchorParameters() ){ + story = storyWithMatchingScenarios(story, givenStory.getAnchorParameters()); + } parameters.putAll(givenStory.getParameters()); - stories.add(performableStory(childContext, storyOfPath, parameters)); + stories.add(performableStory(childContext, story, parameters)); } } return stories; } + private Story storyWithMatchingScenarios(Story story, Map<String,String> parameters) { + if ( parameters.isEmpty() ) return story; + List<Scenario> scenarios = new ArrayList<Scenario>(); + for ( Scenario scenario : story.getScenarios() ){ + if ( matchesParameters(scenario, parameters) ){ + scenarios.add(scenario); + } + } + return new Story(story.getPath(), story.getDescription(), story.getMeta(), story.getNarrative(), scenarios); + } + + private boolean matchesParameters(Scenario scenario, Map<String, String> parameters) { + Meta meta = scenario.getMeta(); + for ( String name : parameters.keySet() ){ + if ( meta.hasProperty(name) ){ + return meta.getProperty(name).equals(parameters.get(name)); + } + } + return false; + } + /** * Returns the parsed story from the given path *