Search in sources :

Example 1 with StepdefBody

use of cucumber.api.java8.StepdefBody in project cucumber-jvm by cucumber.

the class Java8StepDefinition method getParameterInfos.

private List<ParameterInfo> getParameterInfos(Class<? extends StepdefBody> bodyClass, TypeIntrospector typeIntrospector, int parameterCount) throws Exception {
    Type genericInterface = bodyClass.getGenericInterfaces()[0];
    Type[] argumentTypes;
    if (genericInterface instanceof ParameterizedType) {
        argumentTypes = ((ParameterizedType) genericInterface).getActualTypeArguments();
    } else {
        Class<? extends StepdefBody> interfac3 = (Class<? extends StepdefBody>) bodyClass.getInterfaces()[0];
        argumentTypes = typeIntrospector.getGenericTypes(bodyClass, interfac3);
    }
    Type[] argumentTypesOfCorrectLength = new Type[parameterCount];
    System.arraycopy(argumentTypes, argumentTypes.length - parameterCount, argumentTypesOfCorrectLength, 0, parameterCount);
    verifyNotListOrMap(argumentTypesOfCorrectLength);
    return ParameterInfo.fromTypes(argumentTypesOfCorrectLength);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) StepdefBody(cucumber.api.java8.StepdefBody)

Example 2 with StepdefBody

use of cucumber.api.java8.StepdefBody in project cucumber-jvm by cucumber.

the class Java8StepDefinitionTest method should_pass_for_param_with_generic_list.

@Test
public void should_pass_for_param_with_generic_list() throws Exception {
    try {
        StepdefBody body = (StepdefBody.A1<List<String>>) p1 -> {
        };
        new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, body, typeIntrospector);
    } catch (CucumberException expected) {
        assertEquals("Can't use java.util.List in lambda step definition. Declare a DataTable argument instead and convert manually with asList/asLists/asMap/asMaps", expected.getMessage());
    }
}
Also used : Java8StepDefinition(cucumber.runtime.java.Java8StepDefinition) CucumberException(cucumber.runtime.CucumberException) StepdefBody(cucumber.api.java8.StepdefBody) Test(org.junit.Test)

Example 3 with StepdefBody

use of cucumber.api.java8.StepdefBody in project cucumber-jvm by cucumber.

the class Java8StepDefinitionTest method should_calculate_parameters_count_from_body_with_one_param.

@Test
public void should_calculate_parameters_count_from_body_with_one_param() throws Exception {
    StepdefBody body = (StepdefBody.A1<String>) p1 -> {
    };
    Java8StepDefinition def = new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, body, typeIntrospector);
    assertEquals(new Integer(1), def.getParameterCount());
}
Also used : Java8StepDefinition(cucumber.runtime.java.Java8StepDefinition) StepdefBody(cucumber.api.java8.StepdefBody) Test(org.junit.Test)

Example 4 with StepdefBody

use of cucumber.api.java8.StepdefBody in project cucumber-jvm by cucumber.

the class Java8StepDefinitionTest method should_fail_for_param_with_non_generic_list.

@Test
public void should_fail_for_param_with_non_generic_list() throws Exception {
    try {
        StepdefBody body = (StepdefBody.A1<List>) p1 -> {
        };
        new Java8StepDefinition(Pattern.compile("^I have (\\d) some step (.*)$"), 0, body, typeIntrospector);
    } catch (CucumberException expected) {
        assertEquals("Can't use java.util.List in lambda step definition. Declare a DataTable argument instead and convert manually with asList/asLists/asMap/asMaps", expected.getMessage());
    }
}
Also used : Java8StepDefinition(cucumber.runtime.java.Java8StepDefinition) CucumberException(cucumber.runtime.CucumberException) StepdefBody(cucumber.api.java8.StepdefBody) Test(org.junit.Test)

Example 5 with StepdefBody

use of cucumber.api.java8.StepdefBody in project cucumber-jvm by cucumber.

the class Java8StepDefinitionTest method should_calculate_parameters_count_from_body_with_two_params.

@Test
public void should_calculate_parameters_count_from_body_with_two_params() throws Exception {
    StepdefBody body = (StepdefBody.A2<String, String>) (p1, p2) -> {
    };
    Java8StepDefinition def = new Java8StepDefinition(Pattern.compile("^I have some step $"), 0, body, typeIntrospector);
    assertEquals(new Integer(2), def.getParameterCount());
}
Also used : Java8StepDefinition(cucumber.runtime.java.Java8StepDefinition) StepdefBody(cucumber.api.java8.StepdefBody) Test(org.junit.Test)

Aggregations

StepdefBody (cucumber.api.java8.StepdefBody)5 Java8StepDefinition (cucumber.runtime.java.Java8StepDefinition)4 Test (org.junit.Test)4 CucumberException (cucumber.runtime.CucumberException)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1