Search in sources :

Example 6 with StepDefinition

use of io.cucumber.core.backend.StepDefinition 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 StepDefinition

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

the class StepExpressionFactoryTest method creates_a_step_expression.

@Test
void creates_a_step_expression() {
    StepDefinition stepDefinition = new StubStepDefinition("Given a step");
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    assertThat(expression.getSource(), is("Given a step"));
    assertThat(expression.getExpressionType(), is(CucumberExpression.class));
    assertThat(expression.match("Given a step"), is(emptyList()));
}
Also used : StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) CucumberExpression(io.cucumber.cucumberexpressions.CucumberExpression) Test(org.junit.jupiter.api.Test)

Example 8 with StepDefinition

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

the class StepExpressionFactoryTest method empty_table_cells_are_presented_as_null_to_transformer.

@SuppressWarnings("unchecked")
@Test
void empty_table_cells_are_presented_as_null_to_transformer() {
    registry.setDefaultDataTableEntryTransformer((map, valueType, tableCellByTypeTransformer) -> objectMapper.convertValue(map, objectMapper.constructType(valueType)));
    StepDefinition stepDefinition = new StubStepDefinition("Given some stuff:", getTypeFromStepDefinition());
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    List<List<String>> table = asList(asList("name", "amount", "unit"), asList("chocolate", null, "tbsp"));
    List<Argument> match = expression.match("Given some stuff:", table);
    List<Ingredient> ingredients = (List<Ingredient>) match.get(0).getValue();
    Ingredient ingredient = ingredients.get(0);
    assertThat(ingredient.name, is(equalTo("chocolate")));
}
Also used : StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 9 with StepDefinition

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

the class CachingGlueTest method throws_duplicate_error_on_dupe_stepdefs.

@Test
void throws_duplicate_error_on_dupe_stepdefs() {
    StepDefinition a = mock(StepDefinition.class);
    when(a.getPattern()).thenReturn("hello");
    when(a.getLocation()).thenReturn("foo.bf:10");
    glue.addStepDefinition(a);
    StepDefinition b = mock(StepDefinition.class);
    when(b.getPattern()).thenReturn("hello");
    when(b.getLocation()).thenReturn("bar.bf:90");
    glue.addStepDefinition(b);
    DuplicateStepDefinitionException exception = assertThrows(DuplicateStepDefinitionException.class, () -> glue.prepareGlue(stepTypeRegistry));
    assertThat(exception.getMessage(), equalTo("Duplicate step definitions in foo.bf:10 and bar.bf:90"));
}
Also used : StepDefinition(io.cucumber.core.backend.StepDefinition) Test(org.junit.jupiter.api.Test)

Example 10 with StepDefinition

use of io.cucumber.core.backend.StepDefinition 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)

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