Coverage Report - org.jbehave.core.steps.MarkUnmatchedStepsAsPending
 
Classes in this File Line Coverage Branch Coverage Complexity
MarkUnmatchedStepsAsPending
100%
50/50
96%
27/28
2.5
 
 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  
  * StepCollector that marks unmatched steps as {@link Pending}. It uses a
 13  
  * {@link StepFinder} to collect and prioritise {@link StepCandidate}s.
 14  
  */
 15  
 public class MarkUnmatchedStepsAsPending implements StepCollector {
 16  
 
 17  
         private final StepFinder stepFinder;
 18  
 
 19  
     public MarkUnmatchedStepsAsPending() {
 20  396
                 this(new StepFinder());
 21  396
         }
 22  
 
 23  397
         public MarkUnmatchedStepsAsPending(StepFinder stepFinder) {
 24  397
                 this.stepFinder = stepFinder;
 25  397
         }
 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  
         private List<Step> createSteps(List<BeforeOrAfterStep> beforeOrAfter,
 47  
                         Stage stage) {
 48  18
                 List<Step> steps = new ArrayList<Step>();
 49  18
                 for (BeforeOrAfterStep step : beforeOrAfter) {
 50  20
                         if (stage == step.getStage()) {
 51  10
                                 steps.add(step.createStep());
 52  
                         }
 53  
                 }
 54  18
                 return steps;
 55  
         }
 56  
 
 57  
         public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps,
 58  
                         Scenario scenario, Map<String, String> tableRow) {
 59  7
                 List<Step> steps = new ArrayList<Step>();
 60  
 
 61  7
                 addMatchedScenarioSteps(scenario, steps, tableRow, candidateSteps);
 62  7
                 addBeforeAndAfterScenarioSteps(steps, candidateSteps);
 63  
 
 64  7
                 return steps;
 65  
         }
 66  
 
 67  
         private void addBeforeAndAfterScenarioSteps(List<Step> steps,
 68  
                         List<CandidateSteps> candidateSteps) {
 69  7
                 for (CandidateSteps candidates : candidateSteps) {
 70  10
                         steps.addAll(0, createSteps(candidates.listBeforeOrAfterScenario(),
 71  
                                         Stage.BEFORE));
 72  
                 }
 73  
 
 74  7
                 for (CandidateSteps candidates : candidateSteps) {
 75  10
                         steps.addAll(createStepsUponOutcome(candidates
 76  
                                         .listBeforeOrAfterScenario(), Stage.AFTER));
 77  
                 }
 78  7
         }
 79  
 
 80  
         private List<Step> createStepsUponOutcome(
 81  
                         List<BeforeOrAfterStep> beforeOrAfter, Stage stage) {
 82  10
                 List<Step> steps = new ArrayList<Step>();
 83  10
                 for (BeforeOrAfterStep step : beforeOrAfter) {
 84  4
                         if (stage == step.getStage()) {
 85  2
                                 steps.add(step.createStepUponOutcome());
 86  
                         }
 87  
                 }
 88  10
                 return steps;
 89  
         }
 90  
 
 91  
         private void addMatchedScenarioSteps(Scenario scenario, List<Step> steps,
 92  
                         Map<String, String> tableRow, List<CandidateSteps> candidateSteps) {
 93  7
         List<StepCandidate> allCandidates = stepFinder.collectCandidates(candidateSteps);
 94  7
                 String previousNonAndStep = null;
 95  7
                 for (String stepAsString : scenario.getSteps()) {
 96  
                         // pending is default step, overridden below
 97  8
                         Step step = StepCreator.createPendingStep(stepAsString);
 98  8
                         for (StepCandidate candidate : stepFinder.prioritise(stepAsString, allCandidates)) {
 99  9
                                 if (candidate.ignore(stepAsString)) {
 100  
                                         // ignorable steps are added
 101  
                                         // so they can be reported
 102  1
                                         step = StepCreator.createIgnorableStep(stepAsString);
 103  1
                                         break;
 104  
                                 }
 105  8
                                 if (matchesCandidate(stepAsString, previousNonAndStep,
 106  
                                                 candidate)) {
 107  5
                                         step = candidate.createMatchedStep(stepAsString, tableRow);
 108  5
                                         if (!candidate.isAndStep(stepAsString)) {
 109  
                                                 // only update previous step if not AND step
 110  5
                                                 previousNonAndStep = stepAsString;
 111  
                                         }
 112  
                                         break;
 113  
                                 }
 114  
                         }
 115  8
                         steps.add(step);
 116  8
                 }
 117  7
         }
 118  
 
 119  
         private boolean matchesCandidate(String step, String previousNonAndStep,
 120  
                         StepCandidate candidate) {
 121  8
                 if (previousNonAndStep != null) {
 122  2
                         return candidate.matches(step, previousNonAndStep);
 123  
                 }
 124  6
                 return candidate.matches(step);
 125  
         }
 126  
 
 127  
 }