Skip to content

Commit

Permalink
split RuntimeGlue.stepDefinitionMatch test into separate test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz.suski authored and mpkorstanje committed Dec 9, 2017
1 parent 7cb03ad commit 4221231
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
16 changes: 7 additions & 9 deletions core/src/main/java/cucumber/runtime/RuntimeGlue.java
Expand Up @@ -74,16 +74,14 @@ public StepDefinitionMatch stepDefinitionMatch(String featurePath, PickleStep st
} }
if (matches.size() == 1) { if (matches.size() == 1) {
StepDefinitionMatch match = matches.get(0); StepDefinitionMatch match = matches.get(0);
matchedStepDefinitionsCache.put(stepText, new CacheEntry(match.getStepDefinition(),match.getArguments())); matchedStepDefinitionsCache.put(stepText, new CacheEntry(match.getStepDefinition(), match.getArguments()));
return match; return match;
} else {
throw new AmbiguousStepDefinitionsException(step, matches);
} }
throw new AmbiguousStepDefinitionsException(step, matches);
} }


private List<StepDefinitionMatch> stepDefinitionMatches(String featurePath, PickleStep step) { private List<StepDefinitionMatch> stepDefinitionMatches(String featurePath, PickleStep step) {
List<StepDefinitionMatch> result = new ArrayList<StepDefinitionMatch>(); List<StepDefinitionMatch> result = new ArrayList<StepDefinitionMatch>();

for (StepDefinition stepDefinition : stepDefinitionsByPattern.values()) { for (StepDefinition stepDefinition : stepDefinitionsByPattern.values()) {
List<Argument> arguments = stepDefinition.matchedArguments(step); List<Argument> arguments = stepDefinition.matchedArguments(step);
if (arguments != null) { if (arguments != null) {
Expand All @@ -109,25 +107,26 @@ public void removeScenarioScopedGlue() {


private void removeScenarioScopedHooks(List<HookDefinition> beforeHooks1) { private void removeScenarioScopedHooks(List<HookDefinition> beforeHooks1) {
Iterator<HookDefinition> hookIterator = beforeHooks1.iterator(); Iterator<HookDefinition> hookIterator = beforeHooks1.iterator();
while(hookIterator.hasNext()) { while (hookIterator.hasNext()) {
HookDefinition hook = hookIterator.next(); HookDefinition hook = hookIterator.next();
if(hook.isScenarioScoped()) { if (hook.isScenarioScoped()) {
hookIterator.remove(); hookIterator.remove();
} }
} }
} }


private void removeScenarioScopedStepdefs() { private void removeScenarioScopedStepdefs() {
Iterator<Map.Entry<String, StepDefinition>> stepdefs = stepDefinitionsByPattern.entrySet().iterator(); Iterator<Map.Entry<String, StepDefinition>> stepdefs = stepDefinitionsByPattern.entrySet().iterator();
while(stepdefs.hasNext()) { while (stepdefs.hasNext()) {
StepDefinition stepDefinition = stepdefs.next().getValue(); StepDefinition stepDefinition = stepdefs.next().getValue();
if(stepDefinition.isScenarioScoped()) { if (stepDefinition.isScenarioScoped()) {
stepdefs.remove(); stepdefs.remove();
} }
} }
} }


static final class CacheEntry { static final class CacheEntry {

StepDefinition stepDefinition; StepDefinition stepDefinition;
List<Argument> arguments; List<Argument> arguments;


Expand All @@ -136,5 +135,4 @@ private CacheEntry(StepDefinition stepDefinition, List<Argument> arguments) {
this.arguments = arguments; this.arguments = arguments;
} }
} }

} }
72 changes: 48 additions & 24 deletions core/src/test/java/cucumber/runtime/RuntimeGlueTest.java
Expand Up @@ -18,14 +18,15 @@
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;


public class RuntimeGlueTest { public class RuntimeGlueTest {


private RuntimeGlue glue; private RuntimeGlue glue;


@Before @Before
public void setUp() throws Exception { public void setUp() {
glue = new RuntimeGlue(new LocalizedXStreams(Thread.currentThread().getContextClassLoader())); glue = new RuntimeGlue(new LocalizedXStreams(Thread.currentThread().getContextClassLoader()));
} }


Expand Down Expand Up @@ -80,26 +81,59 @@ public void removes_glue_that_is_scenario_scoped() {
} }


@Test @Test
public void step_definition_match() { public void returns_null_if_no_matching_steps_found() {
StepDefinition stepDefinition = getStepDefinitionMockWithPattern("pattern1");
glue.addStepDefinition(stepDefinition);
String featurePath = "someFeature.feature";

PickleStep pickleStep = getPickleStep("pattern");
assertNull(glue.stepDefinitionMatch(featurePath, pickleStep));
verify(stepDefinition).matchedArguments(pickleStep);
}

@Test
public void returns_match_from_cache_if_single_found() {
StepDefinition stepDefinition1 = getStepDefinitionMockWithPattern("^pattern1");
StepDefinition stepDefinition2 = getStepDefinitionMockWithPattern("^pattern2");
glue.addStepDefinition(stepDefinition1);
glue.addStepDefinition(stepDefinition2);
String featurePath = "someFeature.feature";
String stepText = "pattern1";

PickleStep pickleStep1 = getPickleStep(stepText);
assertEquals(stepDefinition1, glue.stepDefinitionMatch(featurePath, pickleStep1).getStepDefinition());
//verify if all defs are checked
verify(stepDefinition1).matchedArguments(pickleStep1);
verify(stepDefinition2).matchedArguments(pickleStep1);

//check cache
RuntimeGlue.CacheEntry entry = glue.matchedStepDefinitionsCache.get(stepText);
assertEquals(stepDefinition1,entry.stepDefinition);

PickleStep pickleStep2 = getPickleStep(stepText);
assertEquals(stepDefinition1, glue.stepDefinitionMatch(featurePath, pickleStep2).getStepDefinition());
//verify that match wasn't called again
verify(stepDefinition1).matchedArguments(any(PickleStep.class));
verify(stepDefinition2).matchedArguments(any(PickleStep.class));

}


@Test
public void throws_ambiguous_steps_def_exception_when_many_patterns_match() {
StepDefinition stepDefinition1 = getStepDefinitionMockWithPattern("pattern1"); StepDefinition stepDefinition1 = getStepDefinitionMockWithPattern("pattern1");
StepDefinition stepDefinition2 = getStepDefinitionMockWithPattern("^pattern2"); StepDefinition stepDefinition2 = getStepDefinitionMockWithPattern("^pattern2");
StepDefinition stepDefinition3 = getStepDefinitionMockWithPattern("^pattern[1,3]"); StepDefinition stepDefinition3 = getStepDefinitionMockWithPattern("^pattern[1,3]");

glue.addStepDefinition(stepDefinition1); glue.addStepDefinition(stepDefinition1);
glue.addStepDefinition(stepDefinition2); glue.addStepDefinition(stepDefinition2);
glue.addStepDefinition(stepDefinition3); glue.addStepDefinition(stepDefinition3);

String featurePath = "someFeature.feature"; String featurePath = "someFeature.feature";
assertNull(glue.stepDefinitionMatch(featurePath, getPickleStep("pattern")));

assertEquals("^pattern2",glue.stepDefinitionMatch(featurePath, getPickleStep("pattern2")).getPattern());
assertEquals("^pattern2",glue.matchedStepDefinitionsCache.get("pattern2").stepDefinition.getPattern());
assertTrue(glue.matchedStepDefinitionsCache.get("pattern2").arguments.isEmpty());
assertEquals("^pattern2",glue.stepDefinitionMatch(featurePath, getPickleStep("pattern2")).getPattern());


assertEquals("^pattern[1,3]",glue.stepDefinitionMatch(featurePath, getPickleStep("pattern3")).getPattern()); checkAmbiguousCalled(featurePath);
//try again to verify if we don't cache when there is ambiguous step
checkAmbiguousCalled(featurePath);
}


private void checkAmbiguousCalled(String featurePath) {
boolean ambiguousCalled = false; boolean ambiguousCalled = false;
try { try {


Expand All @@ -109,29 +143,19 @@ public void step_definition_match() {
ambiguousCalled = true; ambiguousCalled = true;
} }
assertTrue(ambiguousCalled); assertTrue(ambiguousCalled);
ambiguousCalled = false;
//try again to verify if we don't cache when there is duplicate step
try {

glue.stepDefinitionMatch(featurePath, getPickleStep("pattern1"));
} catch (AmbiguousStepDefinitionsException e) {
assertEquals(2,e.getMatches().size());
ambiguousCalled = true;
}
assertTrue(ambiguousCalled);
} }


private PickleStep getPickleStep(String text) { private static PickleStep getPickleStep(String text) {
return new PickleStep(text, Collections.<Argument>emptyList(), Collections.<PickleLocation>emptyList()); return new PickleStep(text, Collections.<Argument>emptyList(), Collections.<PickleLocation>emptyList());
} }


private StepDefinition getStepDefinitionMockWithPattern(String pattern) { private static StepDefinition getStepDefinitionMockWithPattern(String pattern) {
final JdkPatternArgumentMatcher jdkPatternArgumentMatcher = new JdkPatternArgumentMatcher(Pattern.compile(pattern)); final JdkPatternArgumentMatcher jdkPatternArgumentMatcher = new JdkPatternArgumentMatcher(Pattern.compile(pattern));
StepDefinition stepDefinition = mock(StepDefinition.class); StepDefinition stepDefinition = mock(StepDefinition.class);
when(stepDefinition.getPattern()).thenReturn(pattern); when(stepDefinition.getPattern()).thenReturn(pattern);
when(stepDefinition.matchedArguments(any(PickleStep.class))).then(new Answer<Object>() { when(stepDefinition.matchedArguments(any(PickleStep.class))).then(new Answer<Object>() {
@Override @Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable { public Object answer(InvocationOnMock invocationOnMock) {
return jdkPatternArgumentMatcher.argumentsFrom(invocationOnMock.getArgumentAt(0,PickleStep.class).getText()); return jdkPatternArgumentMatcher.argumentsFrom(invocationOnMock.getArgumentAt(0,PickleStep.class).getText());
} }
}); });
Expand Down

0 comments on commit 4221231

Please sign in to comment.