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

io.cucumber.junit.UndefinedStepException Maven / Gradle / Ivy

package io.cucumber.junit;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

final class UndefinedStepException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    UndefinedStepException(Collection snippets) {
        super(createMessage(snippets), null, false, false);
    }

    private static String createMessage(Collection snippets) {
        StringBuilder sb = new StringBuilder("This step is undefined");
        appendSnippets(snippets, sb);
        return sb.toString();
    }

    private static void appendSnippets(Collection snippets, StringBuilder sb) {
        if (snippets.isEmpty()) {
            return;
        }
        sb.append(". You can implement it using the snippet(s) below:\n\n");
        snippets.forEach(snippet -> {
            sb.append(snippet);
            sb.append("\n");
        });
    }

    UndefinedStepException(String stepText, Collection snippets, Collection> otherSnippets) {
        super(createMessage(stepText, snippets, otherSnippets), null, false, false);
    }

    private static String createMessage(
            String stepText, Collection snippets, Collection> otherSnippets
    ) {
        Set otherUniqueSnippets = otherSnippets.stream()
                .flatMap(Collection::stream)
                .collect(Collectors.toCollection(LinkedHashSet::new));

        otherUniqueSnippets.removeAll(snippets);

        StringBuilder sb = new StringBuilder("The step \"" + stepText + "\" is undefined");
        appendSnippets(snippets, sb);
        appendOtherSnippets(otherUniqueSnippets, sb);
        return sb.toString();
    }

    private static void appendOtherSnippets(Collection otherSnippets, StringBuilder sb) {
        if (otherSnippets.isEmpty()) {
            return;
        }
        sb.append("\n");
        sb.append("\n");
        sb.append("Some other steps were also undefined:\n\n");
        otherSnippets.forEach(snippet -> {
            sb.append(snippet);
            sb.append("\n");
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy