| 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.model.Scenario; |
| 8 | |
import org.jbehave.core.model.Story; |
| 9 | |
import org.jbehave.core.steps.AbstractStepResult.Pending; |
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
public class MarkUnmatchedStepsAsPending implements StepCollector { |
| 16 | |
|
| 17 | |
private final StepFinder stepFinder; |
| 18 | |
|
| 19 | |
public MarkUnmatchedStepsAsPending() { |
| 20 | 426 | this(new StepFinder()); |
| 21 | 426 | } |
| 22 | |
|
| 23 | 427 | public MarkUnmatchedStepsAsPending(StepFinder stepFinder) { |
| 24 | 427 | this.stepFinder = stepFinder; |
| 25 | 427 | } |
| 26 | |
|
| 27 | |
public List<Step> collectBeforeOrAfterStoriesSteps(List<CandidateSteps> candidateSteps, Stage stage) { |
| 28 | 2 | List<Step> steps = new ArrayList<Step>(); |
| 29 | 2 | for (CandidateSteps candidates : candidateSteps) { |
| 30 | 4 | steps.addAll(createSteps(candidates |
| 31 | |
.listBeforeOrAfterStories(), stage)); |
| 32 | |
} |
| 33 | 2 | return steps; |
| 34 | |
} |
| 35 | |
|
| 36 | |
public List<Step> collectBeforeOrAfterStorySteps(List<CandidateSteps> candidateSteps, |
| 37 | |
Story story, Stage stage, boolean givenStory) { |
| 38 | 2 | List<Step> steps = new ArrayList<Step>(); |
| 39 | 2 | for (CandidateSteps candidates : candidateSteps) { |
| 40 | 4 | steps.addAll(createSteps(candidates |
| 41 | |
.listBeforeOrAfterStory(givenStory), stage)); |
| 42 | |
} |
| 43 | 2 | return steps; |
| 44 | |
} |
| 45 | |
|
| 46 | |
public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Stage stage) { |
| 47 | 2 | List<Step> steps = new ArrayList<Step>(); |
| 48 | 2 | for (CandidateSteps candidates : candidateSteps) { |
| 49 | 4 | List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario(); |
| 50 | 4 | if (stage == Stage.BEFORE) |
| 51 | 2 | steps.addAll(createSteps(beforeOrAfterScenarioSteps, stage)); |
| 52 | |
else |
| 53 | 2 | steps.addAll(createStepsUponOutcome(beforeOrAfterScenarioSteps, stage)); |
| 54 | 4 | } |
| 55 | |
|
| 56 | 2 | return steps; |
| 57 | |
} |
| 58 | |
|
| 59 | |
private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter, |
| 60 | |
Stage stage) { |
| 61 | 10 | List<Step> steps = new ArrayList<Step>(); |
| 62 | 10 | for (BeforeOrAfterStep step : beforeOrAfter) { |
| 63 | 20 | if (stage == step.getStage()) { |
| 64 | 10 | steps.add(step.createStep()); |
| 65 | |
} |
| 66 | |
} |
| 67 | 10 | return steps; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps, |
| 71 | |
Scenario scenario, Map<String, String> parameters) { |
| 72 | 6 | List<Step> steps = new ArrayList<Step>(); |
| 73 | 6 | addMatchedScenarioSteps(scenario, steps, parameters, candidateSteps); |
| 74 | 6 | return steps; |
| 75 | |
} |
| 76 | |
|
| 77 | |
private List<Step> createStepsUponOutcome( |
| 78 | |
List<BeforeOrAfterStep> beforeOrAfter, Stage stage) { |
| 79 | 2 | List<Step> steps = new ArrayList<Step>(); |
| 80 | 2 | for (BeforeOrAfterStep step : beforeOrAfter) { |
| 81 | 4 | if (stage == step.getStage()) { |
| 82 | 2 | steps.add(step.createStepUponOutcome()); |
| 83 | |
} |
| 84 | |
} |
| 85 | 2 | return steps; |
| 86 | |
} |
| 87 | |
|
| 88 | |
private void addMatchedScenarioSteps(Scenario scenario, List<Step> steps, |
| 89 | |
Map<String, String> namedParameters, List<CandidateSteps> candidateSteps) { |
| 90 | 6 | List<StepCandidate> allCandidates = stepFinder.collectCandidates(candidateSteps); |
| 91 | 6 | String previousNonAndStep = null; |
| 92 | 6 | for (String stepAsString : scenario.getSteps()) { |
| 93 | |
|
| 94 | 7 | Step step = StepCreator.createPendingStep(stepAsString); |
| 95 | 7 | for (StepCandidate candidate : stepFinder.prioritise(stepAsString, allCandidates)) { |
| 96 | 8 | if (candidate.ignore(stepAsString)) { |
| 97 | |
|
| 98 | |
|
| 99 | 1 | step = StepCreator.createIgnorableStep(stepAsString); |
| 100 | 1 | break; |
| 101 | |
} |
| 102 | 7 | if (matchesCandidate(stepAsString, previousNonAndStep, |
| 103 | |
candidate)) { |
| 104 | 4 | step = candidate.createMatchedStep(stepAsString, namedParameters); |
| 105 | 4 | if (!candidate.isAndStep(stepAsString)) { |
| 106 | |
|
| 107 | 4 | previousNonAndStep = stepAsString; |
| 108 | |
} |
| 109 | |
break; |
| 110 | |
} |
| 111 | |
} |
| 112 | 7 | steps.add(step); |
| 113 | 7 | } |
| 114 | 6 | } |
| 115 | |
|
| 116 | |
private boolean matchesCandidate(String step, String previousNonAndStep, |
| 117 | |
StepCandidate candidate) { |
| 118 | 7 | if (previousNonAndStep != null) { |
| 119 | 2 | return candidate.matches(step, previousNonAndStep); |
| 120 | |
} |
| 121 | 5 | return candidate.matches(step); |
| 122 | |
} |
| 123 | |
|
| 124 | |
} |