Search in sources :

Example 36 with StepDefinition

use of io.cucumber.core.backend.StepDefinition in project cucumber-jvm by cucumber.

the class StepExpressionFactoryTest method docstring_and_datatable_match_same_step_definition.

@Test
void docstring_and_datatable_match_same_step_definition() {
    String docString = "A rather long and boring string of documentation";
    StepDefinition stepDefinition = new StubStepDefinition("Given some stuff:", UNKNOWN_TYPE);
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    List<Argument> match = expression.match("Given some stuff:", docString, null);
    assertThat(match.get(0).getValue(), is(equalTo(DocString.create(docString))));
    match = expression.match("Given some stuff:", table);
    assertThat(match.get(0).getValue(), is(equalTo(DataTable.create(table))));
}
Also used : StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) DocString(io.cucumber.docstring.DocString) Test(org.junit.jupiter.api.Test)

Example 37 with StepDefinition

use of io.cucumber.core.backend.StepDefinition in project cucumber-jvm by cucumber.

the class Java8LambdaStepDefinitionMarksCorrectStackElementTest method exception_from_step_should_be_defined_at_step_definition_class.

@Test
void exception_from_step_should_be_defined_at_step_definition_class() {
    LambdaGlueRegistry.INSTANCE.set(myLambdaGlueRegistry);
    new SomeLambdaStepDefs();
    final StepDefinition stepDefinition = myLambdaGlueRegistry.getStepDefinition();
    CucumberInvocationTargetException exception = assertThrows(CucumberInvocationTargetException.class, () -> stepDefinition.execute(new Object[0]));
    assertThat(exception.getInvocationTargetExceptionCause(), new CustomTypeSafeMatcher<Throwable>("exception with matching stack trace") {

        @Override
        protected boolean matchesSafely(Throwable item) {
            return Arrays.stream(item.getStackTrace()).filter(stepDefinition::isDefinedAt).findFirst().filter(stackTraceElement -> SomeLambdaStepDefs.class.getName().equals(stackTraceElement.getClassName())).isPresent();
        }
    });
}
Also used : CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) StepDefinition(io.cucumber.core.backend.StepDefinition) Test(org.junit.jupiter.api.Test)

Example 38 with StepDefinition

use of io.cucumber.core.backend.StepDefinition in project cucumber-jvm by cucumber.

the class CachingGlueTest method returns_fresh_match_from_cache_after_evicting_scenario_scoped.

@Test
void returns_fresh_match_from_cache_after_evicting_scenario_scoped() throws AmbiguousStepDefinitionsException {
    URI uri = URI.create("file:path/to.feature");
    String stepText = "pattern1";
    Step pickleStep1 = getPickleStep(stepText);
    StepDefinition stepDefinition1 = new MockedScenarioScopedStepDefinition("^pattern1");
    glue.addStepDefinition(stepDefinition1);
    glue.prepareGlue(stepTypeRegistry);
    PickleStepDefinitionMatch pickleStepDefinitionMatch = glue.stepDefinitionMatch(uri, pickleStep1);
    assertThat(((CoreStepDefinition) pickleStepDefinitionMatch.getStepDefinition()).getStepDefinition(), is(equalTo(stepDefinition1)));
    glue.removeScenarioScopedGlue();
    StepDefinition stepDefinition2 = new MockedScenarioScopedStepDefinition("^pattern1");
    glue.addStepDefinition(stepDefinition2);
    glue.prepareGlue(stepTypeRegistry);
    PickleStepDefinitionMatch pickleStepDefinitionMatch2 = glue.stepDefinitionMatch(uri, pickleStep1);
    assertThat(((CoreStepDefinition) pickleStepDefinitionMatch2.getStepDefinition()).getStepDefinition(), is(equalTo(stepDefinition2)));
}
Also used : StepDefinition(io.cucumber.core.backend.StepDefinition) Step(io.cucumber.core.gherkin.Step) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 39 with StepDefinition

use of io.cucumber.core.backend.StepDefinition in project cucumber-jvm by cucumber.

the class CachingGlueTest method returns_match_from_cache_for_step_with_table.

@Test
void returns_match_from_cache_for_step_with_table() throws AmbiguousStepDefinitionsException {
    StepDefinition stepDefinition1 = new MockedStepDefinition("^pattern1", DataTable.class);
    StepDefinition stepDefinition2 = new MockedStepDefinition("^pattern2", DataTable.class);
    glue.addStepDefinition(stepDefinition1);
    glue.addStepDefinition(stepDefinition2);
    glue.prepareGlue(stepTypeRegistry);
    URI uri = URI.create("file:path/to.feature");
    String stepText = "pattern1";
    Step pickleStep1 = getPickleStepWithSingleCellTable(stepText, "cell 1");
    PickleStepDefinitionMatch match1 = glue.stepDefinitionMatch(uri, pickleStep1);
    assertThat(((CoreStepDefinition) match1.getStepDefinition()).getStepDefinition(), is(equalTo(stepDefinition1)));
    // check cache
    assertThat(glue.getStepPatternByStepText().get(stepText), is(equalTo(stepDefinition1.getPattern())));
    CoreStepDefinition coreStepDefinition = glue.getStepDefinitionsByPattern().get(stepDefinition1.getPattern());
    assertThat(coreStepDefinition.getStepDefinition(), is(equalTo(stepDefinition1)));
    // check arguments
    assertThat(((DataTable) match1.getArguments().get(0).getValue()).cell(0, 0), is(equalTo("cell 1")));
    // check second match
    Step pickleStep2 = getPickleStepWithSingleCellTable(stepText, "cell 2");
    PickleStepDefinitionMatch match2 = glue.stepDefinitionMatch(uri, pickleStep2);
    // check arguments
    assertThat(((DataTable) match2.getArguments().get(0).getValue()).cell(0, 0), is(equalTo("cell 2")));
}
Also used : StepDefinition(io.cucumber.core.backend.StepDefinition) Step(io.cucumber.core.gherkin.Step) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 40 with StepDefinition

use of io.cucumber.core.backend.StepDefinition in project cucumber-jvm by cucumber.

the class CachingGlueTest method throws_ambiguous_steps_def_exception_when_many_patterns_match.

@Test
void throws_ambiguous_steps_def_exception_when_many_patterns_match() {
    StepDefinition stepDefinition1 = new MockedStepDefinition("pattern1");
    StepDefinition stepDefinition2 = new MockedStepDefinition("^pattern2");
    StepDefinition stepDefinition3 = new MockedStepDefinition("^pattern[1,3]");
    glue.addStepDefinition(stepDefinition1);
    glue.addStepDefinition(stepDefinition2);
    glue.addStepDefinition(stepDefinition3);
    glue.prepareGlue(stepTypeRegistry);
    URI uri = URI.create("file:path/to.feature");
    checkAmbiguousCalled(uri);
    // try again to verify if we don't cache when there is ambiguous step
    checkAmbiguousCalled(uri);
}
Also used : StepDefinition(io.cucumber.core.backend.StepDefinition) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Aggregations

StepDefinition (io.cucumber.core.backend.StepDefinition)43 Test (org.junit.jupiter.api.Test)42 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)29 Step (io.cucumber.core.gherkin.Step)22 Feature (io.cucumber.core.gherkin.Feature)18 Argument (io.cucumber.core.stepexpression.Argument)18 StepExpression (io.cucumber.core.stepexpression.StepExpression)18 Executable (org.junit.jupiter.api.function.Executable)14 CucumberException (io.cucumber.core.exception.CucumberException)13 URI (java.net.URI)9 DataTableType (io.cucumber.datatable.DataTableType)6 CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)5 CucumberInvocationTargetException (io.cucumber.core.backend.CucumberInvocationTargetException)5 StepExpressionFactory (io.cucumber.core.stepexpression.StepExpressionFactory)5 StepTypeRegistry (io.cucumber.core.stepexpression.StepTypeRegistry)5 DocString (io.cucumber.docstring.DocString)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Arrays.asList (java.util.Arrays.asList)4 List (java.util.List)4 BytesContainsString.bytesContainsString (io.cucumber.core.plugin.BytesContainsString.bytesContainsString)3