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

com.pdsl.gherkin.models.GherkinString Maven / Gradle / Ivy

Go to download

The Polymorphic DSL test framework was designed to solve the challenges with testing large, complex systems. Modern architecture requires software to run as distrubited systems or on multiple platforms. The conventional cost of testing these systems is quite high. PDSL allows a user to describe the system under test using a DSL of some kind: a picture, sentences in natural languages, graphs, etc. Using a common DSL allows someone to make deeply scalable tests. A simple change to the DSL could generate dozens of tests providing coverage through many layers of the test pyramid or even multiple applications.

The newest version!
package com.pdsl.gherkin.models;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GherkinString {
    private static final Pattern parameterPattern;

    static {
        String regex = "(<[^>]*>)";
        Pattern pattern = Pattern.compile(regex);
        parameterPattern = pattern;
    }

    private final Set parameterSubstitutionKeys;
    private final String rawString;

    public GherkinString(String data) {
        Matcher matcher = parameterPattern.matcher(data);
        List p = new ArrayList<>();
        if (matcher.find()) {
            for (int i = 0; i < matcher.groupCount(); i++) {
                p.add(matcher.group(i));
            }
        }
        this.parameterSubstitutionKeys = Set.copyOf(p);
        this.rawString = data;
    }

    public boolean hasSubstitutions() {
        return !parameterSubstitutionKeys.isEmpty();
    }

    public String getRawString() {
        return rawString;
    }

    public String getStringWithSubstitutions(Map substitutions) {
        if (!hasSubstitutions()) {
            return rawString;
        }
        String substitutedString = rawString;
        try {
            for (Map.Entry entry : substitutions.entrySet()) {
                substitutedString = substitutedString.replaceAll(entry.getKey(), Matcher.quoteReplacement(entry.getValue()).trim());
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("There was a problem when doing gherkin string substitutions!%n\tRaw string: " + rawString + "%n\tSubstitutions: " + substitutions.toString());
        }
        // If we didn't substitute everything in the string it was half formed
        if (parameterPattern.matcher(substitutedString).find()) {
            throw new MissingSubstitutionValuesException(String.format("Did not have all needed substitutions for gherkin string: %s%n\tValid Values: %s", rawString,
                    Arrays.toString(substitutions.keySet().toArray())));
        }
        return substitutedString;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy