Search in sources :

Example 16 with Argument

use of io.cucumber.core.stepexpression.Argument 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 17 with Argument

use of io.cucumber.core.stepexpression.Argument 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 18 with Argument

use of io.cucumber.core.stepexpression.Argument in project cucumber-jvm by cucumber.

the class CoreStepDefinitionTest method runStepDef.

@SuppressWarnings("unchecked")
private <T> T runStepDef(Method method, boolean transposed, Feature feature) {
    StubStepDefinition stepDefinition = new StubStepDefinition("some text", transposed, method.getGenericParameterTypes());
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    CoreStepDefinition coreStepDefinition = new CoreStepDefinition(id, stepDefinition, expression);
    Step stepWithTable = feature.getPickles().get(0).getSteps().get(0);
    List<Argument> arguments = coreStepDefinition.matchedArguments(stepWithTable);
    List<Object> result = new ArrayList<>();
    for (Argument argument : arguments) {
        result.add(argument.getValue());
    }
    coreStepDefinition.getStepDefinition().execute(result.toArray(new Object[0]));
    return (T) stepDefinition.getArgs().get(0);
}
Also used : Argument(io.cucumber.core.stepexpression.Argument) ArrayList(java.util.ArrayList) Step(io.cucumber.core.gherkin.Step) StepExpression(io.cucumber.core.stepexpression.StepExpression)

Example 19 with Argument

use of io.cucumber.core.stepexpression.Argument 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 20 with Argument

use of io.cucumber.core.stepexpression.Argument in project cucumber-jvm by cucumber.

the class PickleStepDefinitionMatch method runStep.

@Override
public void runStep(TestCaseState state) throws Throwable {
    List<Argument> arguments = getArguments();
    List<ParameterInfo> parameterInfos = stepDefinition.parameterInfos();
    if (parameterInfos != null && arguments.size() != parameterInfos.size()) {
        throw arityMismatch(parameterInfos.size());
    }
    List<Object> result = new ArrayList<>();
    try {
        for (Argument argument : arguments) {
            result.add(argument.getValue());
        }
    } catch (UndefinedDataTableTypeException e) {
        throw registerDataTableTypeInConfiguration(e);
    } catch (CucumberExpressionException | CucumberDataTableException | CucumberDocStringException e) {
        CucumberInvocationTargetException targetException;
        if ((targetException = causedByCucumberInvocationTargetException(e)) != null) {
            throw removeFrameworkFrames(targetException);
        }
        throw couldNotConvertArguments(e);
    } catch (CucumberBackendException e) {
        throw couldNotInvokeArgumentConversion(e);
    } catch (CucumberInvocationTargetException e) {
        throw removeFrameworkFrames(e);
    }
    try {
        stepDefinition.execute(result.toArray(new Object[0]));
    } catch (CucumberBackendException e) {
        throw couldNotInvokeStep(e, result);
    } catch (CucumberInvocationTargetException e) {
        throw removeFrameworkFramesAndAppendStepLocation(e, getStepLocation());
    }
}
Also used : CucumberDocStringException(io.cucumber.docstring.CucumberDocStringException) Argument(io.cucumber.core.stepexpression.Argument) ArrayList(java.util.ArrayList) ParameterInfo(io.cucumber.core.backend.ParameterInfo) CucumberExpressionException(io.cucumber.cucumberexpressions.CucumberExpressionException) CucumberInvocationTargetException(io.cucumber.core.backend.CucumberInvocationTargetException) UndefinedDataTableTypeException(io.cucumber.datatable.UndefinedDataTableTypeException) CucumberBackendException(io.cucumber.core.backend.CucumberBackendException) CucumberDataTableException(io.cucumber.datatable.CucumberDataTableException)

Aggregations

Argument (io.cucumber.core.stepexpression.Argument)20 StepDefinition (io.cucumber.core.backend.StepDefinition)18 Feature (io.cucumber.core.gherkin.Feature)18 Test (org.junit.jupiter.api.Test)18 Step (io.cucumber.core.gherkin.Step)17 StepExpression (io.cucumber.core.stepexpression.StepExpression)16 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)15 Executable (org.junit.jupiter.api.function.Executable)14 CucumberException (io.cucumber.core.exception.CucumberException)12 CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)6 CucumberInvocationTargetException (io.cucumber.core.backend.CucumberInvocationTargetException)5 DataTableType (io.cucumber.datatable.DataTableType)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Located (io.cucumber.core.backend.Located)2 EventBus (io.cucumber.core.eventbus.EventBus)2 TestFeatureParser (io.cucumber.core.feature.TestFeatureParser)2 TimeServiceEventBus (io.cucumber.core.runtime.TimeServiceEventBus)2 StepExpressionFactory (io.cucumber.core.stepexpression.StepExpressionFactory)2 StepTypeRegistry (io.cucumber.core.stepexpression.StepTypeRegistry)2 ParameterType (io.cucumber.cucumberexpressions.ParameterType)2