Search in sources :

Example 16 with StepExpression

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

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

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

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

the class CachingGlue method prepareGlue.

void prepareGlue(StepTypeRegistry stepTypeRegistry) throws DuplicateStepDefinitionException {
    StepExpressionFactory stepExpressionFactory = new StepExpressionFactory(stepTypeRegistry, bus);
    // TODO: separate prepared and unprepared glue into different classes
    parameterTypeDefinitions.forEach(ptd -> {
        ParameterType<?> parameterType = ptd.parameterType();
        stepTypeRegistry.defineParameterType(parameterType);
        emitParameterTypeDefined(parameterType);
    });
    dataTableTypeDefinitions.forEach(dtd -> stepTypeRegistry.defineDataTableType(dtd.dataTableType()));
    docStringTypeDefinitions.forEach(dtd -> stepTypeRegistry.defineDocStringType(dtd.docStringType()));
    if (defaultParameterTransformers.size() == 1) {
        DefaultParameterTransformerDefinition definition = defaultParameterTransformers.get(0);
        ParameterByTypeTransformer transformer = definition.parameterByTypeTransformer();
        stepTypeRegistry.setDefaultParameterTransformer(transformer);
    } else if (defaultParameterTransformers.size() > 1) {
        throw new DuplicateDefaultParameterTransformers(defaultParameterTransformers);
    }
    if (defaultDataTableEntryTransformers.size() == 1) {
        DefaultDataTableEntryTransformerDefinition definition = defaultDataTableEntryTransformers.get(0);
        TableEntryByTypeTransformer transformer = definition.tableEntryByTypeTransformer();
        stepTypeRegistry.setDefaultDataTableEntryTransformer(transformer);
    } else if (defaultDataTableEntryTransformers.size() > 1) {
        throw new DuplicateDefaultDataTableEntryTransformers(defaultDataTableEntryTransformers);
    }
    if (defaultDataTableCellTransformers.size() == 1) {
        DefaultDataTableCellTransformerDefinition definition = defaultDataTableCellTransformers.get(0);
        TableCellByTypeTransformer transformer = definition.tableCellByTypeTransformer();
        stepTypeRegistry.setDefaultDataTableCellTransformer(transformer);
    } else if (defaultDataTableCellTransformers.size() > 1) {
        throw new DuplicateDefaultDataTableCellTransformers(defaultDataTableCellTransformers);
    }
    // TODO: Redefine hooks for each scenario, similar to how we're doing
    // for CoreStepDefinition
    beforeHooks.forEach(this::emitHook);
    beforeStepHooks.forEach(this::emitHook);
    stepDefinitions.forEach(stepDefinition -> {
        StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
        CoreStepDefinition coreStepDefinition = new CoreStepDefinition(bus.generateId(), stepDefinition, expression);
        CoreStepDefinition previous = stepDefinitionsByPattern.get(stepDefinition.getPattern());
        if (previous != null) {
            throw new DuplicateStepDefinitionException(previous, stepDefinition);
        }
        stepDefinitionsByPattern.put(coreStepDefinition.getExpression().getSource(), coreStepDefinition);
        emitStepDefined(coreStepDefinition);
    });
    afterStepHooks.forEach(this::emitHook);
    afterHooks.forEach(this::emitHook);
}
Also used : DefaultDataTableCellTransformerDefinition(io.cucumber.core.backend.DefaultDataTableCellTransformerDefinition) StepExpressionFactory(io.cucumber.core.stepexpression.StepExpressionFactory) ParameterByTypeTransformer(io.cucumber.cucumberexpressions.ParameterByTypeTransformer) TableEntryByTypeTransformer(io.cucumber.datatable.TableEntryByTypeTransformer) DefaultParameterTransformerDefinition(io.cucumber.core.backend.DefaultParameterTransformerDefinition) StepExpression(io.cucumber.core.stepexpression.StepExpression) DefaultDataTableEntryTransformerDefinition(io.cucumber.core.backend.DefaultDataTableEntryTransformerDefinition) TableCellByTypeTransformer(io.cucumber.datatable.TableCellByTypeTransformer)

Example 20 with StepExpression

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

the class PrettyFormatterTest method should_mark_nested_arguments_as_part_of_enclosing_argument.

@Test
void should_mark_nested_arguments_as_part_of_enclosing_argument() {
    Formats formats = ansi();
    PrettyFormatter prettyFormatter = new PrettyFormatter(new ByteArrayOutputStream());
    StepTypeRegistry registry = new StepTypeRegistry(Locale.ENGLISH);
    StepExpressionFactory stepExpressionFactory = new StepExpressionFactory(registry, bus);
    StepDefinition stepDefinition = new StubStepDefinition("^the order is placed( and (not( yet)? )?confirmed)?$", String.class);
    StepExpression expression = stepExpressionFactory.createExpression(stepDefinition);
    String stepText = "the order is placed and not yet confirmed";
    String formattedText = prettyFormatter.formatStepText("Given ", stepText, formats.get("passed"), formats.get("passed_arg"), createArguments(expression.match(stepText)));
    assertThat(formattedText, equalTo(AnsiEscapes.GREEN + "Given " + AnsiEscapes.RESET + AnsiEscapes.GREEN + "the order is placed" + AnsiEscapes.RESET + AnsiEscapes.GREEN + AnsiEscapes.INTENSITY_BOLD + " and not yet confirmed" + AnsiEscapes.RESET));
}
Also used : StepTypeRegistry(io.cucumber.core.stepexpression.StepTypeRegistry) StepExpressionFactory(io.cucumber.core.stepexpression.StepExpressionFactory) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) StepDefinition(io.cucumber.core.backend.StepDefinition) StubStepDefinition(io.cucumber.core.backend.StubStepDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BytesContainsString.bytesContainsString(io.cucumber.core.plugin.BytesContainsString.bytesContainsString) StepExpression(io.cucumber.core.stepexpression.StepExpression) Test(org.junit.jupiter.api.Test)

Aggregations

StepExpression (io.cucumber.core.stepexpression.StepExpression)20 StepDefinition (io.cucumber.core.backend.StepDefinition)18 Test (org.junit.jupiter.api.Test)18 Argument (io.cucumber.core.stepexpression.Argument)16 StubStepDefinition (io.cucumber.core.backend.StubStepDefinition)15 Feature (io.cucumber.core.gherkin.Feature)15 Step (io.cucumber.core.gherkin.Step)14 Executable (org.junit.jupiter.api.function.Executable)11 CucumberException (io.cucumber.core.exception.CucumberException)9 StepExpressionFactory (io.cucumber.core.stepexpression.StepExpressionFactory)6 StepTypeRegistry (io.cucumber.core.stepexpression.StepTypeRegistry)5 CucumberInvocationTargetException (io.cucumber.core.backend.CucumberInvocationTargetException)4 DataTableType (io.cucumber.datatable.DataTableType)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 BytesContainsString.bytesContainsString (io.cucumber.core.plugin.BytesContainsString.bytesContainsString)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 CucumberBackendException (io.cucumber.core.backend.CucumberBackendException)2 Located (io.cucumber.core.backend.Located)2 EventBus (io.cucumber.core.eventbus.EventBus)2 TestFeatureParser (io.cucumber.core.feature.TestFeatureParser)2