Search in sources :

Example 31 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_fewer_parameters_than_arguments_with_data_table.

@Test
void throws_arity_mismatch_exception_when_there_are_fewer_parameters_than_arguments_with_data_table() {
    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");
    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 0 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 32 with StepDefinition

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

the class StepDefinitionMatchTest method throws_could_not_convert_exception_for_transformer_and_capture_group_mismatch.

@Test
void throws_could_not_convert_exception_for_transformer_and_capture_group_mismatch() {
    stepTypeRegistry.defineParameterType(new ParameterType<>("itemQuantity", "(few|some|lots of) (cukes|gherkins)", ItemQuantity.class, // Wrong number of capture groups
    (String s) -> null));
    Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have some cukes in my belly\n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have {itemQuantity} in my belly", ItemQuantity.class);
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
    List<Argument> arguments = coreStepDefinition.matchedArguments(step);
    StepDefinitionMatch 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("Could not convert arguments for step [I have {itemQuantity} in my belly] defined at '{stubbed location with details}'.")));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) Step(io.cucumber.core.gherkin.Step) Feature(io.cucumber.core.gherkin.Feature) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) CucumberException(io.cucumber.core.exception.CucumberException) Executable(org.junit.jupiter.api.function.Executable) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 33 with StepDefinition

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

the class StepDefinitionMatchTest method rethrows_target_invocation_exceptions_from_parameter_type.

@Test
void rethrows_target_invocation_exceptions_from_parameter_type() {
    RuntimeException userException = new RuntimeException();
    stepTypeRegistry.defineParameterType(new ParameterType<>("itemQuantity", "(few|some|lots of) (cukes|gherkins)", ItemQuantity.class, (String[] s) -> {
        throw new CucumberInvocationTargetException(stubbedLocation, new InvocationTargetException(userException));
    }));
    Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have some cukes in my belly\n");
    Step step = feature.getPickles().get(0).getSteps().get(0);
    StepDefinition stepDefinition = new StubStepDefinition("I have {itemQuantity} in my belly", ItemQuantity.class);
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
    List<Argument> arguments = coreStepDefinition.matchedArguments(step);
    StepDefinitionMatch stepDefinitionMatch = new PickleStepDefinitionMatch(arguments, stepDefinition, null, step);
    Executable testMethod = () -> stepDefinitionMatch.runStep(null);
    RuntimeException actualThrown = assertThrows(RuntimeException.class, testMethod);
    assertThat(actualThrown, sameInstance(userException));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) Step(io.cucumber.core.gherkin.Step) Feature(io.cucumber.core.gherkin.Feature) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) Executable(org.junit.jupiter.api.function.Executable) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 34 with StepDefinition

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

the class CoreStepDefinitionTest method should_apply_identity_transform_to_doc_string_when_target_type_is_object.

@Test
void should_apply_identity_transform_to_doc_string_when_target_type_is_object() {
    Feature feature = TestFeatureParser.parse("" + "Feature: Test feature\n" + "  Scenario: Test scenario\n" + "     Given I have some step\n" + "       \"\"\"\n" + "       content\n" + "       \"\"\"\n");
    StepDefinition stepDefinition = new StubStepDefinition("I have some step", Object.class);
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
    Step step = feature.getPickles().get(0).getSteps().get(0);
    List<Argument> arguments = coreStepDefinition.matchedArguments(step);
    assertThat(arguments.get(0).getValue(), is(equalTo(DocString.create("content"))));
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) StepDefinition(io.cucumber.core.backend.StepDefinition) Step(io.cucumber.core.gherkin.Step) Feature(io.cucumber.core.gherkin.Feature) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Example 35 with StepDefinition

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

the class StepExpressionFactoryTest method unknown_target_type_transform_doc_string_to_doc_string.

@Test
void unknown_target_type_transform_doc_string_to_doc_string() {
    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))));
}
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)

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