Search in sources :

Example 6 with Step

use of io.cucumber.core.gherkin.Step in project cucumber-jvm by cucumber.

the class StepDefinitionMatchTest method throws_arity_mismatch_exception_when_there_are_more_parameters_than_arguments.

@Test
void throws_arity_mismatch_exception_when_there_are_more_parameters_than_arguments() {
    Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have 4 cukes in my belly\n" + "       | A | B | \n" + "       | C | D | \n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have {int} cukes in my belly", Integer.TYPE, Short.TYPE, List.class);
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
    List<Argument> arguments = coreStepDefinition.matchedArguments(step);
    PickleStepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition, null, step);
    Executable testMethod = () -> stepDefinitionMatch.runStep(null);
    CucumberException actualThrown = assertThrows(CucumberException.class, testMethod);
    assertThat("Unexpected exception message", actualThrown.getMessage(), is(equalTo("Step [I have {int} cukes in my belly] is defined with 3 parameters at '{stubbed location with details}'.\n" + "However, the gherkin step has 2 arguments:\n" + " * 4\n" + " * Table:\n" + "      | A | B |\n" + "      | C | D |\n" + "\n" + "Step text: I have 4 cukes in my belly")));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Step(io.cucumber.core.gherkin.Step) CucumberException(io.cucumber.core.exception.CucumberException) Executable(org.junit.jupiter.api.function.Executable) Feature(io.cucumber.core.gherkin.Feature) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 7 with Step

use of io.cucumber.core.gherkin.Step in project cucumber-jvm by cucumber.

the class CachingGlueTest method returns_match_from_cache_if_single_found.

@Test
void returns_match_from_cache_if_single_found() throws AmbiguousStepDefinitionsException {
    StepDefinition stepDefinition1 = new MockedStepDefinition("^pattern1");
    StepDefinition stepDefinition2 = new MockedStepDefinition("^pattern2");
    glue.addStepDefinition(stepDefinition1);
    glue.addStepDefinition(stepDefinition2);
    glue.prepareGlue(stepTypeRegistry);
    URI uri = URI.create("file:path/to.feature");
    String stepText = "pattern1";
    Step pickleStep1 = getPickleStep(stepText);
    PickleStepDefinitionMatch pickleStepDefinitionMatch = glue.stepDefinitionMatch(uri, pickleStep1);
    assertThat(((CoreStepDefinition) pickleStepDefinitionMatch.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)));
    Step pickleStep2 = getPickleStep(stepText);
    PickleStepDefinitionMatch pickleStepDefinitionMatch2 = glue.stepDefinitionMatch(uri, pickleStep2);
    assertThat(((CoreStepDefinition) pickleStepDefinitionMatch2.getStepDefinition()).getStepDefinition(), is(equalTo(stepDefinition1)));
}
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 8 with Step

use of io.cucumber.core.gherkin.Step in project cucumber-jvm by cucumber.

the class CachingGlueTest method returns_match_from_cache_for_ste_with_doc_string.

@Test
void returns_match_from_cache_for_ste_with_doc_string() throws AmbiguousStepDefinitionsException {
    StepDefinition stepDefinition1 = new MockedStepDefinition("^pattern1", String.class);
    StepDefinition stepDefinition2 = new MockedStepDefinition("^pattern2", String.class);
    glue.addStepDefinition(stepDefinition1);
    glue.addStepDefinition(stepDefinition2);
    glue.prepareGlue(stepTypeRegistry);
    URI uri = URI.create("file:path/to.feature");
    String stepText = "pattern1";
    Step pickleStep1 = getPickleStepWithDocString(stepText, "doc string 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(match1.getArguments().get(0).getValue(), is(equalTo("doc string 1")));
    // check second match
    Step pickleStep2 = getPickleStepWithDocString(stepText, "doc string 2");
    PickleStepDefinitionMatch match2 = glue.stepDefinitionMatch(uri, pickleStep2);
    // check arguments
    assertThat(match2.getArguments().get(0).getValue(), is(equalTo("doc string 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 9 with Step

use of io.cucumber.core.gherkin.Step in project cucumber-jvm by cucumber.

the class CachingGlueTest method returns_no_match_after_evicting_scenario_scoped.

@Test
void returns_no_match_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();
    glue.prepareGlue(stepTypeRegistry);
    PickleStepDefinitionMatch pickleStepDefinitionMatch2 = glue.stepDefinitionMatch(uri, pickleStep1);
    assertThat(pickleStepDefinitionMatch2, nullValue());
}
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 10 with Step

use of io.cucumber.core.gherkin.Step in project cucumber-jvm by cucumber.

the class AmbiguousStepDefinitionsExceptionTest method can_report_ambiguous_step_definitions.

@Test
void can_report_ambiguous_step_definitions() {
    Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have 4 cukes in my belly\n");
    Step mockPickleStep = feature.getPickles().get(0).getSteps().get(0);
    PickleStepDefinitionMatch mockPickleStepDefinitionMatchOne = mock(PickleStepDefinitionMatch.class);
    when(mockPickleStepDefinitionMatchOne.getPattern()).thenReturn("PickleStepDefinitionMatchOne_Pattern");
    when(mockPickleStepDefinitionMatchOne.getLocation()).thenReturn("PickleStepDefinitionMatchOne_Location");
    PickleStepDefinitionMatch mockPickleStepDefinitionMatchTwo = mock(PickleStepDefinitionMatch.class);
    when(mockPickleStepDefinitionMatchTwo.getPattern()).thenReturn("PickleStepDefinitionMatchTwo_Pattern");
    when(mockPickleStepDefinitionMatchTwo.getLocation()).thenReturn("PickleStepDefinitionMatchTwo_Location");
    List<PickleStepDefinitionMatch> matches = asList(mockPickleStepDefinitionMatchOne, mockPickleStepDefinitionMatchTwo);
    AmbiguousStepDefinitionsException expectedThrown = new AmbiguousStepDefinitionsException(mockPickleStep, matches);
    assertAll(() -> assertThat(expectedThrown.getMessage(), is(equalTo("" + "\"I have 4 cukes in my belly\" matches more than one step definition:\n" + "  \"PickleStepDefinitionMatchOne_Pattern\" in PickleStepDefinitionMatchOne_Location\n" + "  \"PickleStepDefinitionMatchTwo_Pattern\" in PickleStepDefinitionMatchTwo_Location"))), () -> assertThat(expectedThrown.getCause(), is(nullValue())), () -> assertThat(expectedThrown.getMatches(), is(equalTo(matches))));
}
Also used : Step(io.cucumber.core.gherkin.Step) Feature(io.cucumber.core.gherkin.Feature) Test(org.junit.jupiter.api.Test)

Aggregations

Step (io.cucumber.core.gherkin.Step)37 Test (org.junit.jupiter.api.Test)26 StepDefinition (io.cucumber.core.backend.StepDefinition)22 Feature (io.cucumber.core.gherkin.Feature)22 Argument (io.cucumber.core.stepexpression.Argument)17 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)15 StepExpression (io.cucumber.core.stepexpression.StepExpression)14 Executable (org.junit.jupiter.api.function.Executable)14 CucumberException (io.cucumber.core.exception.CucumberException)12 URI (java.net.URI)11 SnippetGenerator (io.cucumber.core.snippets.SnippetGenerator)8 ParameterTypeRegistry (io.cucumber.cucumberexpressions.ParameterTypeRegistry)8 CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)5 CucumberInvocationTargetException (io.cucumber.core.backend.CucumberInvocationTargetException)4 DataTableType (io.cucumber.datatable.DataTableType)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Pickle (io.cucumber.core.gherkin.Pickle)3 ArrayList (java.util.ArrayList)3 Located (io.cucumber.core.backend.Located)2 EventBus (io.cucumber.core.eventbus.EventBus)2