Coverage Report - org.jbehave.core.steps.MarkUnmatchedStepsAsPending
 
Classes in this File Line Coverage Branch Coverage Complexity
MarkUnmatchedStepsAsPending
95%
64/67
90%
36/40
2.615
 
 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  
  * StepCollector that marks unmatched steps as {@link Pending}. It uses a
 18  
  * {@link StepFinder} to collect and prioritise {@link StepCandidate}s.
 19  
  */
 20  
 public class MarkUnmatchedStepsAsPending implements StepCollector {
 21  
 
 22  
     private final StepFinder stepFinder;
 23  
     private final Keywords keywords;
 24  
 
 25  
     public MarkUnmatchedStepsAsPending() {
 26  618
         this(new StepFinder());
 27  618
     }
 28  
 
 29  
     public MarkUnmatchedStepsAsPending(StepFinder stepFinder) {
 30  619
         this(stepFinder, new LocalizedKeywords());
 31  619
     }
 32  
 
 33  
     public MarkUnmatchedStepsAsPending(Keywords keywords) {
 34  0
         this(new StepFinder(), keywords);
 35  0
     }
 36  
 
 37  619
    public MarkUnmatchedStepsAsPending(StepFinder stepFinder, Keywords keywords) {
 38  619
         this.stepFinder = stepFinder;
 39  619
         this.keywords = keywords;
 40  619
     }
 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  7
         List<Step> steps = new ArrayList<Step>();
 53  7
         for (CandidateSteps candidates : candidateSteps) {
 54  9
             steps.addAll(createSteps(candidates.listBeforeOrAfterStory(givenStory), story.getMeta(), stage));
 55  
         }
 56  7
         return steps;
 57  
     }
 58  
 
 59  
     public List<Step> collectBeforeOrAfterScenarioSteps(List<CandidateSteps> candidateSteps, Meta storyAndScenarioMeta, Stage stage, ScenarioType type) {
 60  11
         List<Step> steps = new ArrayList<Step>();
 61  11
         for (CandidateSteps candidates : candidateSteps) {
 62  17
             List<BeforeOrAfterStep> beforeOrAfterScenarioSteps = candidates.listBeforeOrAfterScenario(type);
 63  17
             if (stage == Stage.BEFORE) {
 64  9
                 steps.addAll(createSteps(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage));
 65  
             } else {
 66  8
                 steps.addAll(0, createStepsUponOutcome(beforeOrAfterScenarioSteps, storyAndScenarioMeta, stage));
 67  
             }
 68  17
         }
 69  11
         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  24
         List<Step> steps = new ArrayList<Step>();
 78  24
         for (BeforeOrAfterStep step : beforeOrAfter) {
 79  40
             if (stage == step.getStage()) {
 80  20
                 steps.add(meta == null ? step.createStep() : step.createStepWith(meta));
 81  
             }
 82  
         }
 83  24
         return steps;
 84  
     }
 85  
 
 86  
     public List<Step> collectScenarioSteps(List<CandidateSteps> candidateSteps, Scenario scenario,
 87  
             Map<String, String> parameters) {
 88  9
         List<Step> steps = new ArrayList<Step>();
 89  9
         addMatchedScenarioSteps(scenario, steps, parameters, candidateSteps);
 90  9
         return steps;
 91  
     }
 92  
 
 93  
     private List<Step> createStepsUponOutcome(List<BeforeOrAfterStep> beforeOrAfter, Meta storyAndScenarioMeta, Stage stage) {
 94  8
         List<Step> steps = new ArrayList<Step>();
 95  8
         for (BeforeOrAfterStep step : beforeOrAfter) {
 96  16
             if (stage == step.getStage()) {
 97  8
                 steps.add(step.createStepUponOutcome(storyAndScenarioMeta));
 98  
             }
 99  
         }
 100  8
         return steps;
 101  
     }
 102  
 
 103  
     private void addMatchedScenarioSteps(Scenario scenario, List<Step> steps, Map<String, String> namedParameters,
 104  
             List<CandidateSteps> candidateSteps) {
 105  9
         List<StepCandidate> allCandidates = stepFinder.collectCandidates(candidateSteps);
 106  9
         String previousNonAndStep = null;
 107  9
         for (String stepAsString : scenario.getSteps()) {
 108  
             // pending is default step, overridden below
 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  
                     // ignorable steps are added
 115  
                     // so they can be reported
 116  1
                     step = StepCreator.createIgnorableStep(stepAsString);
 117  1
                     break;
 118  
                 }
 119  25
                 if (matchesCandidate(stepAsString, previousNonAndStep, candidate)) {
 120  
                     // step matches candidate
 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  
                         // only update previous step if not AND or IGNORABLE step
 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  9
     }
 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  
 }