All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cucumber.cucumberexpressions.Argument Maven / Gradle / Ivy

There is a newer version: 17.1.0
Show newest version
package io.cucumber.cucumberexpressions;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Argument {
    private final ParameterType parameterType;
    private final Group group;

    static List> build(TreeRegexp treeRegexp, List> parameterTypes, String text) {
        Group group = treeRegexp.match(text);
        if (group == null) return null;

        List argGroups = group.getChildren();

        if (argGroups.size() != parameterTypes.size()) {
            throw new CucumberExpressionException(String.format("Expression /%s/ has %s capture groups (%s), but there were %s parameter types (%s)",
                    treeRegexp.pattern().pattern(),
                    argGroups.size(),
                    argGroups.stream().map(Group::getValue).collect(Collectors.toList()),
                    parameterTypes.size(),
                    parameterTypes.stream().map(ParameterType::getName).collect(Collectors.toList())
            ));
        }
        List> args = new ArrayList<>(argGroups.size());
        for (int i = 0; i < parameterTypes.size(); i++) {
            Group argGroup = argGroups.get(i);
            ParameterType parameterType = parameterTypes.get(i);
            args.add(new Argument<>(argGroup, parameterType));
        }

        return args;
    }

    public Argument(Group group, ParameterType parameterType) {
        this.group = group;
        this.parameterType = parameterType;
    }

    public Group getGroup() {
        return group;
    }

    public T getValue() {
        return parameterType.transform(group == null ? null : group.getValues());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy