Search in sources :

Example 11 with Argument

use of gherkin.formatter.Argument in project cucumber-jvm by cucumber.

the class StepDefinitionMatch method transformedArgs.

/**
     * @param step    the step to run
     * @param xStream used to convert a string to declared stepdef arguments
     * @return an Array matching the types or {@code parameterTypes}, or an array of String if {@code parameterTypes} is null
     */
private Object[] transformedArgs(Step step, LocalizedXStreams.LocalizedXStream xStream) {
    int argumentCount = getArguments().size();
    if (step.getRows() != null) {
        argumentCount++;
    } else if (step.getDocString() != null) {
        argumentCount++;
    }
    Integer parameterCount = stepDefinition.getParameterCount();
    if (parameterCount != null && argumentCount != parameterCount) {
        throw arityMismatch(parameterCount);
    }
    List<Object> result = new ArrayList<Object>();
    int n = 0;
    for (Argument a : getArguments()) {
        ParameterInfo parameterInfo = getParameterType(n, String.class);
        Object arg = parameterInfo.convert(a.getVal(), xStream);
        result.add(arg);
        n++;
    }
    if (step.getRows() != null) {
        result.add(tableArgument(step, n, xStream));
    } else if (step.getDocString() != null) {
        ParameterInfo parameterInfo = getParameterType(n, String.class);
        Object arg = parameterInfo.convert(step.getDocString().getValue(), xStream);
        result.add(arg);
    }
    return result.toArray(new Object[result.size()]);
}
Also used : Argument(gherkin.formatter.Argument) ArrayList(java.util.ArrayList)

Example 12 with Argument

use of gherkin.formatter.Argument in project cucumber-jvm by cucumber.

the class StepDefinitionMatchTest method gives_nice_error_message_when_conversion_fails.

@Test
public void gives_nice_error_message_when_conversion_fails() throws Throwable {
    StepDefinition stepDefinition = mock(StepDefinition.class);
    when(stepDefinition.getParameterCount()).thenReturn(1);
    when(stepDefinition.getParameterType(0, String.class)).thenReturn(new ParameterInfo(Thang.class, null, null, null));
    Step stepWithoutDocStringOrTable = mock(Step.class);
    when(stepWithoutDocStringOrTable.getDocString()).thenReturn(null);
    when(stepWithoutDocStringOrTable.getRows()).thenReturn(null);
    StepDefinitionMatch stepDefinitionMatch = new StepDefinitionMatch(Arrays.asList(new Argument(0, "blah")), stepDefinition, "some.feature", stepWithoutDocStringOrTable, new LocalizedXStreams(classLoader));
    try {
        stepDefinitionMatch.runStep(ENGLISH);
        fail();
    } catch (CucumberException expected) {
        assertEquals("Don't know how to convert \"blah\" into cucumber.runtime.StepDefinitionMatchTest$Thang.\n" + "Try writing your own converter:\n" + "\n" + "@cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter(ThangConverter.class)\n" + "public class Thang {}\n", expected.getMessage());
    }
}
Also used : Argument(gherkin.formatter.Argument) Step(gherkin.formatter.model.Step) LocalizedXStreams(cucumber.runtime.xstream.LocalizedXStreams) Test(org.junit.Test)

Aggregations

Argument (gherkin.formatter.Argument)12 Step (gherkin.formatter.model.Step)9 LocalizedXStreams (cucumber.runtime.xstream.LocalizedXStreams)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 DocString (gherkin.formatter.model.DocString)3 I18n (gherkin.I18n)2 DataTableRow (gherkin.formatter.model.DataTableRow)2 List (java.util.List)2 StepDefinition (cucumber.runtime.StepDefinition)1 CucumberFeature (cucumber.runtime.model.CucumberFeature)1 CucumberTagStatement (cucumber.runtime.model.CucumberTagStatement)1 Arrays.asList (java.util.Arrays.asList)1 Matcher (java.util.regex.Matcher)1