| 1 | |
package org.jbehave.core.steps; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.Map; |
| 6 | |
|
| 7 | |
import org.jbehave.core.annotations.ScenarioType; |
| 8 | |
import org.jbehave.core.configuration.Keywords; |
| 9 | |
import org.jbehave.core.i18n.LocalizedKeywords; |
| 10 | |
import org.jbehave.core.model.Meta; |
| 11 | |
import org.jbehave.core.model.Scenario; |
| 12 | |
import org.jbehave.core.model.Story; |
| 13 | |
import org.jbehave.core.steps.AbstractStepResult.Pending; |
| 14 | |
import org.jbehave.core.steps.StepCreator.PendingStep; |
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
public class MarkUnmatchedStepsAsPending implements StepCollector { |
| 21 | |
|
| 22 | |
private final StepFinder stepFinder; |
| 23 | |
private final Keywords keywords; |
| 24 | |
|
| 25 | |
public MarkUnmatchedStepsAsPending() { |
| 26 | 640 | this(new StepFinder()); |
| 27 | 640 | } |
| 28 | |
|
| 29 | |
public MarkUnmatchedStepsAsPending(StepFinder stepFinder) { |
| 30 | 641 | this(stepFinder, new LocalizedKeywords()); |
| 31 | 641 | } |
| 32 | |
|
| 33 | |
public MarkUnmatchedStepsAsPending(Keywords keywords) { |
| 34 | 0 | this(new StepFinder(), keywords); |
| 35 | 0 | } |
| 36 | |
|
| 37 | 641 | public MarkUnmatchedStepsAsPending(StepFinder stepFinder, Keywords keywords) { |
| 38 | 641 | this.stepFinder = stepFinder; |
| 39 | 641 | this.keywords = keywords; |
| 40 | 641 | } |
| 41 | |
|
| 42 | |
public List<Step> collectBeforeOrAfterStoriesSteps(List<CandidateSteps> candidateSteps, Stage stage) { |
| 43 | 4 | List<Step> steps = new ArrayList<Step>(); |
| 44 | 4 | for (CandidateSteps candidates : candidateSteps) { |
| 45 | 6 | steps.addAll(createSteps(candidates.listBeforeOrAfterStories(), stage)); |
| 46 | |
} |
| 47 | 4 | return steps; |
| 48 | |
} |
| 49 | |
|
| 50 | |
public List<Step> collectBeforeOrAfterStorySteps(List<CandidateSteps> candidateSteps, Story story, Stage stage, |
| 51 | |
boolean givenStory) { |
| 52 | 9 | List<Step> steps = new ArrayList<Step>(); |
| 53 | 9 | for (CandidateSteps candidates : candidateSteps) { |
| 54 | 11 | steps.addAll(createSteps(candidates.listBeforeOrAfterStory(givenStory), story.getMeta(), stage)); |
| 55 | |
} |
| 56 | 9 | return steps; |
| 57 | |
} |
| 58 | |
|
| 59 | |
public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage, ScenarioType type) { |
| 60 | 13 | List<Step> steps = new ArrayList<Step>(); |
| 61 | 13 | for (CandidateSteps candidates : candidateSteps) { |
| 62 | 19 | List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario(type); |
| 63 | 19 | if (stage == Stage.BEFORE) { |
| 64 | 10 | steps.addAll(createSteps(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage)); |
| 65 | |
} else { |
| 66 | 9 | steps.addAll(0, createStepsUponOutcome(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage)); |
| 67 | |
} |
| 68 | 19 | } |
| 69 | 13 | return steps; |
| 70 | |
} |
| 71 | |
|
| 72 | |
private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, Stage stage) { |
| 73 | 6 | return createSteps(beforeOrAfter, null, stage); |
| 74 | |
} |
| 75 | |
|
| 76 | |
private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, Meta meta, Stage stage) { |
| 77 | 27 | List<Step> steps = new ArrayList<Step>(); |
| 78 | 27 | for (BeforeOrAfterStep step : beforeOrAfter) { |
| 79 | 40 | if (stage == step.getStage()) { |
| 80 | 20 | steps.add(meta == null ? step.createStep() : step.createStepWith(meta)); |
| 81 | |
} |
| 82 | |
} |
| 83 | 27 | return steps; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps, Scenario scenario, |
| 87 | |
Map<String, String> parameters) { |
| 88 | 10 | List<Step> steps = new ArrayList<Step>(); |
| 89 | 10 | addMatchedScenarioSteps(scenario, steps, parameters, candidateSteps); |
| 90 | 10 | return steps; |
| 91 | |
} |
| 92 | |
|
| 93 | |
private List<Step> createStepsUponOutcome(List<BeforeOrAfterStep> beforeOrAfter, Meta storyAndScenarioMeta, Stage stage) { |
| 94 | 9 | List<Step> steps = new ArrayList<Step>(); |
| 95 | 9 | for (BeforeOrAfterStep step : beforeOrAfter) { |
| 96 | 16 | if (stage == step.getStage()) { |
| 97 | 8 | steps.add(step.createStepUponOutcome(storyAndScenarioMeta)); |
| 98 | |
} |
| 99 | |
} |
| 100 | 9 | return steps; |
| 101 | |
} |
| 102 | |
|
| 103 | |
private void addMatchedScenarioSteps(Scenario scenario, List<Step> steps, Map<String, String> namedParameters, |
| 104 | |
List<CandidateSteps> candidateSteps) { |
| 105 | 10 | List<StepCandidate> allCandidates = stepFinder.collectCandidates(candidateSteps); |
| 106 | 10 | String previousNonAndStep = null; |
| 107 | 10 | for (String stepAsString : scenario.getSteps()) { |
| 108 | |
|
| 109 | 17 | Step step = StepCreator.createPendingStep(stepAsString, previousNonAndStep); |
| 110 | 17 | List<Step> composedSteps = new ArrayList<Step>(); |
| 111 | 17 | List<StepCandidate> prioritisedCandidates = stepFinder.prioritise(stepAsString, allCandidates); |
| 112 | 17 | for (StepCandidate candidate : prioritisedCandidates) { |
| 113 | 26 | if (candidate.ignore(stepAsString)) { |
| 114 | |
|
| 115 | |
|
| 116 | 1 | step = StepCreator.createIgnorableStep(stepAsString); |
| 117 | 1 | break; |
| 118 | |
} |
| 119 | 25 | if (matchesCandidate(stepAsString, previousNonAndStep, candidate)) { |
| 120 | |
|
| 121 | 7 | if (candidate.isPending()) { |
| 122 | 0 | ((PendingStep) step).annotatedOn(candidate.getMethod()); |
| 123 | |
} else { |
| 124 | 7 | step = candidate.createMatchedStep(stepAsString, namedParameters); |
| 125 | 7 | if ( candidate.isComposite() ){ |
| 126 | 1 | candidate.addComposedSteps(composedSteps, stepAsString, namedParameters, allCandidates); |
| 127 | |
} |
| 128 | |
} |
| 129 | 7 | if (!(candidate.isAndStep(stepAsString) || candidate.isIgnorableStep(stepAsString))) { |
| 130 | |
|
| 131 | 7 | previousNonAndStep = stepAsString; |
| 132 | |
} |
| 133 | |
break; |
| 134 | |
} |
| 135 | |
} |
| 136 | 17 | if ( !(keywords.isAndStep(stepAsString) || keywords.isIgnorableStep(stepAsString)) ){ |
| 137 | 13 | previousNonAndStep = stepAsString; |
| 138 | |
} |
| 139 | 17 | steps.add(step); |
| 140 | 17 | steps.addAll(composedSteps); |
| 141 | 17 | } |
| 142 | 10 | } |
| 143 | |
|
| 144 | |
private boolean matchesCandidate(String step, String previousNonAndStep, StepCandidate candidate) { |
| 145 | 25 | if (previousNonAndStep != null) { |
| 146 | 15 | return candidate.matches(step, previousNonAndStep); |
| 147 | |
} |
| 148 | 10 | return candidate.matches(step); |
| 149 | |
} |
| 150 | |
|
| 151 | |
} |