Search in sources :

Example 1 with Argument

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

the class JdkPatternArgumentMatcher method argumentsFrom.

public List<Argument> argumentsFrom(String stepName) {
    Matcher matcher = pattern.matcher(stepName);
    if (matcher.lookingAt()) {
        List<Argument> arguments = new ArrayList<Argument>(matcher.groupCount());
        for (int i = 1; i <= matcher.groupCount(); i++) {
            int startIndex = matcher.start(i);
            arguments.add(new Argument(startIndex == -1 ? null : startIndex, matcher.group(i)));
        }
        return arguments;
    } else {
        return null;
    }
}
Also used : Argument(gherkin.formatter.Argument) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList)

Example 2 with Argument

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

the class StepDefinitionMatch method createArgumentsForErrorMessage.

private List<Argument> createArgumentsForErrorMessage(Step step) {
    List<Argument> arguments = new ArrayList<Argument>(getArguments());
    if (step.getDocString() != null) {
        arguments.add(new Argument(-1, "DocString:" + step.getDocString().getValue()));
    }
    if (step.getRows() != null) {
        List<List<String>> rows = map(step.getRows(), new Mapper<DataTableRow, List<String>>() {

            @Override
            public List<String> map(DataTableRow row) {
                return row.getCells();
            }
        });
        arguments.add(new Argument(-1, "Table:" + rows.toString()));
    }
    return arguments;
}
Also used : Argument(gherkin.formatter.Argument) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DataTableRow(gherkin.formatter.model.DataTableRow)

Example 3 with Argument

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

the class StepdefGenerator method generate.

public List<MetaStepdef> generate(Collection<StepDefinition> stepDefinitions, List<CucumberFeature> features) {
    List<MetaStepdef> result = new ArrayList<MetaStepdef>();
    List<StepDefinition> sortedStepdefs = new ArrayList<StepDefinition>();
    sortedStepdefs.addAll(stepDefinitions);
    Collections.sort(sortedStepdefs, STEP_DEFINITION_COMPARATOR);
    for (StepDefinition stepDefinition : sortedStepdefs) {
        MetaStepdef metaStepdef = new MetaStepdef();
        metaStepdef.source = stepDefinition.getPattern();
        // TODO = get the flags too
        metaStepdef.flags = "";
        for (CucumberFeature feature : features) {
            List<CucumberTagStatement> cucumberTagStatements = feature.getFeatureElements();
            for (CucumberTagStatement tagStatement : cucumberTagStatements) {
                List<Step> steps = tagStatement.getSteps();
                for (Step step : steps) {
                    List<Argument> arguments = stepDefinition.matchedArguments(step);
                    if (arguments != null) {
                        MetaStepdef.MetaStep ms = new MetaStepdef.MetaStep();
                        ms.name = step.getName();
                        for (Argument argument : arguments) {
                            MetaStepdef.MetaArgument ma = new MetaStepdef.MetaArgument();
                            ma.offset = argument.getOffset();
                            ma.val = argument.getVal();
                            ms.args.add(ma);
                        }
                        metaStepdef.steps.add(ms);
                    }
                }
            }
            Collections.sort(cucumberTagStatements, CUCUMBER_TAG_STATEMENT_COMPARATOR);
        }
        result.add(metaStepdef);
    }
    return result;
}
Also used : Argument(gherkin.formatter.Argument) ArrayList(java.util.ArrayList) Step(gherkin.formatter.model.Step) CucumberFeature(cucumber.runtime.model.CucumberFeature) StepDefinition(cucumber.runtime.StepDefinition) CucumberTagStatement(cucumber.runtime.model.CucumberTagStatement)

Example 4 with Argument

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

the class StepDefinitionMatchTest method throws_arity_mismatch_exception_when_there_are_fewer_parameters_than_arguments.

@Test
public void throws_arity_mismatch_exception_when_there_are_fewer_parameters_than_arguments() throws Throwable {
    Step step = new Step(null, "Given ", "I have 4 cukes in my belly", 1, null, null);
    StepDefinition stepDefinition = new StubStepDefinition(new Object(), Object.class.getMethod("toString"), "some pattern");
    StepDefinitionMatch stepDefinitionMatch = new StepDefinitionMatch(asList(new Argument(7, "4")), stepDefinition, null, step, new LocalizedXStreams(getClass().getClassLoader()));
    try {
        stepDefinitionMatch.runStep(new I18n("en"));
        fail();
    } catch (CucumberException expected) {
        assertEquals("Arity mismatch: Step Definition 'toString' with pattern [some pattern] is declared with 0 parameters. However, the gherkin step has 1 arguments [4]. \n" + "Step: Given I have 4 cukes in my belly", expected.getMessage());
    }
}
Also used : Argument(gherkin.formatter.Argument) Step(gherkin.formatter.model.Step) LocalizedXStreams(cucumber.runtime.xstream.LocalizedXStreams) I18n(gherkin.I18n) Test(org.junit.Test)

Example 5 with Argument

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

the class StepDefinitionMatchTest method throws_arity_mismatch_exception_when_there_are_more_parameters_than_arguments.

@Test
public void throws_arity_mismatch_exception_when_there_are_more_parameters_than_arguments() throws Throwable {
    Step step = new Step(null, "Given ", "I have 4 cukes in my belly", 1, new ArrayList<DataTableRow>(), null);
    StepDefinition stepDefinition = new StubStepDefinition(new Object(), WithTwoParams.class.getMethod("withTwoParams", Integer.TYPE, Short.TYPE, List.class), "some pattern");
    StepDefinitionMatch stepDefinitionMatch = new StepDefinitionMatch(asList(new Argument(7, "4")), stepDefinition, null, step, new LocalizedXStreams(getClass().getClassLoader()));
    try {
        stepDefinitionMatch.runStep(new I18n("en"));
        fail();
    } catch (CucumberException expected) {
        assertEquals("Arity mismatch: Step Definition 'withTwoParams' with pattern [some pattern] is declared with 3 parameters. However, the gherkin step has 2 arguments [4, Table:[]]. \n" + "Step: Given I have 4 cukes in my belly", expected.getMessage());
    }
}
Also used : Argument(gherkin.formatter.Argument) Step(gherkin.formatter.model.Step) LocalizedXStreams(cucumber.runtime.xstream.LocalizedXStreams) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) DataTableRow(gherkin.formatter.model.DataTableRow) I18n(gherkin.I18n) 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