From b39ecbc6341c8acad2f68f001dc934d5ef7f142f Mon Sep 17 00:00:00 2001 From: draker94 <58571912+draker94@users.noreply.github.com> Date: Fri, 29 Dec 2023 12:22:30 +0300 Subject: [PATCH] JBEHAVE-1595 Fix monitoring of steps without parameters inside composite steps Co-authored-by: Andrei Kliuchnikau --- .../org/jbehave/core/steps/StepCandidate.java | 8 ++++---- .../CompositeCandidateStepsBehaviour.java | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/jbehave-core/src/main/java/org/jbehave/core/steps/StepCandidate.java b/jbehave-core/src/main/java/org/jbehave/core/steps/StepCandidate.java index 98c3e10f3..e9433c753 100755 --- a/jbehave-core/src/main/java/org/jbehave/core/steps/StepCandidate.java +++ b/jbehave-core/src/main/java/org/jbehave/core/steps/StepCandidate.java @@ -10,7 +10,6 @@ import com.thoughtworks.paranamer.Paranamer; -import org.apache.commons.lang3.StringUtils; import org.jbehave.core.annotations.AfterScenario.Outcome; import org.jbehave.core.annotations.Given; import org.jbehave.core.annotations.Pending; @@ -164,7 +163,9 @@ public boolean matches(String step, String previousNonAndStep) { } } stepMonitor.stepMatchesType(step, previousNonAndStep, matchesType, stepType, method, stepsType); - boolean matchesPattern = stepMatcher.matcher(stripStartingWord(step)).matches(); + String stepWithoutStartingWord = stripStartingWord(step); + boolean matchesPattern = stepWithoutStartingWord.equals(patternAsString) + || stepMatcher.matcher(stepWithoutStartingWord).matches(); stepMonitor.stepMatchesPattern(step, matchesPattern, stepMatcher.pattern(), method, stepsType); // must match both type and pattern return matchesType && matchesPattern; @@ -239,8 +240,7 @@ private StepCandidate findComposedCandidate(String composedStep, String previous stepType = keywords.stepTypeFor(composedStep); } for (StepCandidate candidate : allCandidates) { - if (stepType == candidate.getStepType() && (StringUtils.endsWith(composedStep, - candidate.getPatternAsString()) || candidate.matches(composedStep, previousNonAndStep))) { + if (stepType == candidate.getStepType() && candidate.matches(composedStep, previousNonAndStep)) { return candidate; } } diff --git a/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeCandidateStepsBehaviour.java b/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeCandidateStepsBehaviour.java index 8213a1fef..ae8a68636 100755 --- a/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeCandidateStepsBehaviour.java +++ b/jbehave-core/src/test/java/org/jbehave/core/steps/CompositeCandidateStepsBehaviour.java @@ -5,7 +5,10 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.jbehave.core.steps.StepCandidateBehaviour.candidateMatchingStep; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import java.util.ArrayList; import java.util.Collections; @@ -34,6 +37,23 @@ void shouldMatchCompositesFromClassAndCreateComposedStepsUsingMatchedParameters( shouldMatchCompositesAndCreateComposedStepsUsingMatchedParameters(compositeSteps); } + @Test + void shouldMatchInnerCompositeStepWithoutParamsAndMonitorIt() { + StepMonitor monitor = mock(StepMonitor.class); + CompositeStepsWithParameterMissing steps = new CompositeStepsWithParameterMissing(); + String outerCompositeStep = "Given I am logged in as USER"; + String innerCompositeStep = "Given I log in"; + List listCandidates = steps.listCandidates(); + + StepCandidate outerCompositeCandidate = candidateMatchingStep(listCandidates, outerCompositeStep); + StepCandidate innerCompositeCandidate = candidateMatchingStep(listCandidates, innerCompositeStep); + innerCompositeCandidate.useStepMonitor(monitor); + + outerCompositeCandidate.addComposedSteps(new ArrayList<>(), outerCompositeStep, new HashMap<>(), + listCandidates); + verify(monitor).stepMatchesPattern(eq(innerCompositeStep), eq(true), any(), any(), any()); + } + @Test void shouldMatchCompositesFromFileAndCreateComposedStepsUsingMatchedParameters() { CandidateSteps compositeSteps = new CompositeCandidateSteps(new MostUsefulConfiguration(),