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

cucumber.runtime.java.AbstractJavaSnippet Maven / Gradle / Ivy

There is a newer version: 7.18.1
Show newest version
package cucumber.runtime.java;

import cucumber.runtime.snippets.Snippet;
import io.cucumber.datatable.DataTable;

import java.lang.reflect.Type;
import java.util.Map;

abstract class AbstractJavaSnippet implements Snippet {
    @Override
    public final String arguments(Map arguments) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Map.Entry argType : arguments.entrySet()) {
            if (first) {
                first = false;
            } else {
                sb.append(", ");
            }
            sb.append(getArgType(argType.getValue())).append(" ").append(argType.getKey());
        }
        return sb.toString();
    }

    private String getArgType(Type argType) {
        if (argType instanceof Class) {
            Class cType = (Class) argType;
            if (cType.equals(DataTable.class)) {
                return cType.getName();
            }
            return cType.getSimpleName();
        }

        // Got a better idea? Send a PR.
        return argType.toString();
    }

    @Override
    public final String tableHint() {
        return "" +
            "    // For automatic transformation, change DataTable to one of\n" +
            "    // E, List, List>, List>, Map or\n" +
            "    // Map>. E,K,V must be a String, Integer, Float,\n" +
            "    // Double, Byte, Short, Long, BigInteger or BigDecimal.\n" +
            "    //\n" +
            "    // For other transformations you can register a DataTableType.\n"; //TODO: Add doc URL
    }

    @Override
    public final String escapePattern(String pattern) {
        return pattern.replace("\\", "\\\\").replace("\"", "\\\"");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy