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