Search in sources :

Example 1 with GherkinDocument

use of gherkin.ast.GherkinDocument in project cucable-plugin by trivago.

the class GherkinDocumentParser method getGherkinDocumentFromFeatureFileContent.

/**
 * Get a {@link GherkinDocument} from a feature file for further processing.
 *
 * @param featureContent a feature string.
 * @return a {@link GherkinDocument}.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
private GherkinDocument getGherkinDocumentFromFeatureFileContent(final String featureContent) throws CucablePluginException {
    Parser<GherkinDocument> gherkinDocumentParser = new Parser<>(new AstBuilder());
    GherkinDocument gherkinDocument;
    try {
        gherkinDocument = gherkinDocumentParser.parse(featureContent);
    } catch (ParserException parserException) {
        throw new CucablePluginException("Could not parse feature!");
    }
    if (gherkinDocument == null || gherkinDocument.getFeature() == null) {
        throw new CucablePluginException("Could not parse feature!");
    }
    return gherkinDocument;
}
Also used : AstBuilder(gherkin.AstBuilder) ParserException(gherkin.ParserException) CucablePluginException(com.trivago.rta.exceptions.CucablePluginException) GherkinDocument(gherkin.ast.GherkinDocument) Parser(gherkin.Parser)

Example 2 with GherkinDocument

use of gherkin.ast.GherkinDocument in project cucable-plugin by trivago.

the class GherkinDocumentParser method getGherkinDocumentFromFeatureFileContent.

/**
 * Get a {@link GherkinDocument} from a feature file for further processing.
 *
 * @param featureContent a feature string.
 * @return a {@link GherkinDocument}.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
private GherkinDocument getGherkinDocumentFromFeatureFileContent(final String featureContent) throws CucablePluginException {
    Parser<GherkinDocument> gherkinDocumentParser = new Parser<>(new AstBuilder());
    GherkinDocument gherkinDocument;
    try {
        gherkinDocument = gherkinDocumentParser.parse(featureContent);
    } catch (ParserException parserException) {
        throw new CucablePluginException(parserException.getMessage());
    }
    if (gherkinDocument == null || gherkinDocument.getFeature() == null) {
        cucableLogger.warn("No parsable gherkin.");
    }
    return gherkinDocument;
}
Also used : AstBuilder(gherkin.AstBuilder) ParserException(gherkin.ParserException) CucablePluginException(com.trivago.exceptions.CucablePluginException) GherkinDocument(gherkin.ast.GherkinDocument) TagExpressionParser(io.cucumber.tagexpressions.TagExpressionParser) Parser(gherkin.Parser)

Example 3 with GherkinDocument

use of gherkin.ast.GherkinDocument in project cucable-plugin by trivago.

the class GherkinDocumentParser method getSingleScenariosFromFeature.

/**
 * Returns a {@link com.trivago.rta.vo.SingleScenario} list from a given feature file.
 *
 * @param featureContent      A feature string.
 * @param featureFilePath     The path to the source feature file.
 * @param scenarioLineNumbers An optional line number of a scenario inside a feature file.
 * @param includeScenarioTags Optional scenario tags to include into scenario generation.
 * @param excludeScenarioTags Optional scenario tags to exclude from scenario generation.
 * @return A {@link com.trivago.rta.vo.SingleScenario} list.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
public List<SingleScenario> getSingleScenariosFromFeature(final String featureContent, final String featureFilePath, final List<Integer> scenarioLineNumbers, final List<String> includeScenarioTags, final List<String> excludeScenarioTags) throws CucablePluginException {
    GherkinDocument gherkinDocument = getGherkinDocumentFromFeatureFileContent(featureContent);
    Feature feature = gherkinDocument.getFeature();
    String featureName = feature.getKeyword() + ": " + feature.getName();
    String featureLanguage = feature.getLanguage();
    String featureDescription = feature.getDescription();
    List<String> featureTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(feature.getTags());
    ArrayList<SingleScenario> singleScenarioFeatures = new ArrayList<>();
    List<Step> backgroundSteps = new ArrayList<>();
    List<ScenarioDefinition> scenarioDefinitions = feature.getChildren();
    for (ScenarioDefinition scenarioDefinition : scenarioDefinitions) {
        String scenarioName = scenarioDefinition.getKeyword() + ": " + scenarioDefinition.getName();
        String scenarioDescription = scenarioDefinition.getDescription();
        if (scenarioDefinition instanceof Background) {
            // Save background steps in order to add them to every scenario inside the same feature
            Background background = (Background) scenarioDefinition;
            backgroundSteps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(background.getSteps());
            continue;
        }
        if (scenarioDefinition instanceof Scenario) {
            Scenario scenario = (Scenario) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenario.getLocation().getLine())) {
                SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, scenarioName, scenarioDescription, featureTags, backgroundSteps);
                addGherkinScenarioInformationToSingleScenario(scenario, singleScenario);
                if (scenarioShouldBeIncluded(singleScenario.getScenarioTags(), singleScenario.getFeatureTags(), includeScenarioTags, excludeScenarioTags)) {
                    singleScenarioFeatures.add(singleScenario);
                }
            }
            continue;
        }
        if (scenarioDefinition instanceof ScenarioOutline) {
            ScenarioOutline scenarioOutline = (ScenarioOutline) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenarioOutline.getLocation().getLine())) {
                List<SingleScenario> outlineScenarios = getSingleScenariosFromOutline(scenarioOutline, featureName, featureFilePath, featureLanguage, featureDescription, featureTags, backgroundSteps, includeScenarioTags, excludeScenarioTags);
                singleScenarioFeatures.addAll(outlineScenarios);
            }
        }
    }
    return singleScenarioFeatures;
}
Also used : SingleScenario(com.trivago.rta.vo.SingleScenario) Background(gherkin.ast.Background) ArrayList(java.util.ArrayList) GherkinDocument(gherkin.ast.GherkinDocument) Step(com.trivago.rta.vo.Step) Feature(gherkin.ast.Feature) SingleScenario(com.trivago.rta.vo.SingleScenario) Scenario(gherkin.ast.Scenario) ScenarioOutline(gherkin.ast.ScenarioOutline) ScenarioDefinition(gherkin.ast.ScenarioDefinition)

Example 4 with GherkinDocument

use of gherkin.ast.GherkinDocument in project cucable-plugin by trivago.

the class GherkinDocumentParser method getSingleScenariosFromFeature.

/**
 * Returns a {@link SingleScenario} list from a given feature file.
 *
 * @param featureContent  A feature string.
 * @param featureFilePath The path to the source feature file.
 * @return A {@link SingleScenario} list.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
public List<SingleScenario> getSingleScenariosFromFeature(final String featureContent, final String featureFilePath, final List<Integer> scenarioLineNumbers) throws CucablePluginException {
    String escapedFeatureContent = featureContent.replace("\\n", "\\\\n");
    GherkinDocument gherkinDocument;
    try {
        gherkinDocument = getGherkinDocumentFromFeatureFileContent(escapedFeatureContent);
    } catch (CucablePluginException e) {
        throw new FeatureFileParseException(featureFilePath, e.getMessage());
    }
    Feature feature = gherkinDocument.getFeature();
    if (feature == null) {
        return Collections.emptyList();
    }
    String featureName = feature.getKeyword() + ": " + feature.getName();
    String featureLanguage = feature.getLanguage();
    String featureDescription = feature.getDescription();
    List<String> featureTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(feature.getTags());
    ArrayList<SingleScenario> singleScenarioFeatures = new ArrayList<>();
    List<Step> backgroundSteps = new ArrayList<>();
    List<ScenarioDefinition> scenarioDefinitions = feature.getChildren();
    for (ScenarioDefinition scenarioDefinition : scenarioDefinitions) {
        String scenarioName = scenarioDefinition.getKeyword() + ": " + scenarioDefinition.getName();
        String scenarioDescription = scenarioDefinition.getDescription();
        if (scenarioDefinition instanceof Background) {
            // Save background steps in order to add them to every scenario inside the same feature
            Background background = (Background) scenarioDefinition;
            backgroundSteps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(background.getSteps());
            continue;
        }
        if (scenarioDefinition instanceof Scenario) {
            Scenario scenario = (Scenario) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenario.getLocation().getLine())) {
                SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, scenarioName, scenarioDescription, featureTags, backgroundSteps);
                addGherkinScenarioInformationToSingleScenario(scenario, singleScenario);
                if (scenarioShouldBeIncluded(singleScenario)) {
                    singleScenarioFeatures.add(singleScenario);
                }
            }
            continue;
        }
        if (scenarioDefinition instanceof ScenarioOutline) {
            ScenarioOutline scenarioOutline = (ScenarioOutline) scenarioDefinition;
            if (scenarioLineNumbers == null || scenarioLineNumbers.isEmpty() || scenarioLineNumbers.contains(scenarioOutline.getLocation().getLine())) {
                List<SingleScenario> outlineScenarios = getSingleScenariosFromOutline(scenarioOutline, featureName, featureFilePath, featureLanguage, featureDescription, featureTags, backgroundSteps);
                for (SingleScenario singleScenario : outlineScenarios) {
                    if (scenarioShouldBeIncluded(singleScenario)) {
                        singleScenarioFeatures.add(singleScenario);
                    }
                }
            }
        }
    }
    return singleScenarioFeatures;
}
Also used : SingleScenario(com.trivago.vo.SingleScenario) Background(gherkin.ast.Background) CucablePluginException(com.trivago.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) GherkinDocument(gherkin.ast.GherkinDocument) Step(com.trivago.vo.Step) Feature(gherkin.ast.Feature) Scenario(gherkin.ast.Scenario) SingleScenario(com.trivago.vo.SingleScenario) FeatureFileParseException(com.trivago.exceptions.filesystem.FeatureFileParseException) ScenarioOutline(gherkin.ast.ScenarioOutline) ScenarioDefinition(gherkin.ast.ScenarioDefinition)

Example 5 with GherkinDocument

use of gherkin.ast.GherkinDocument in project cucable-plugin by trivago.

the class FeatureFileConverter method generateRunnerClassesWithDesiredNumberOfRunners.

/**
 * Generate runner classes for a list of feature file paths.
 *
 * @param generatedFeatureNames  The list of generated feature file names.
 * @param numberOfDesiredRunners The number of desired runners (if set to 0, a runner is generated for each feature file path).
 * @return The number of generated runners.
 * @throws CucablePluginException see {@link CucablePluginException}.
 */
private int generateRunnerClassesWithDesiredNumberOfRunners(final List<String> generatedFeatureNames, final int numberOfDesiredRunners) throws CucablePluginException {
    int targetRunnerNumber = numberOfDesiredRunners;
    List<String> scenarioNames = propertyManager.getScenarioNames();
    if (targetRunnerNumber == 0) {
        targetRunnerNumber = generatedFeatureNames.size();
    }
    List<List<String>> generatedFeatureNamesPerRunner = new ArrayList<>(targetRunnerNumber);
    for (int i = 0; i < targetRunnerNumber; i++) {
        generatedFeatureNamesPerRunner.add(new ArrayList<>());
    }
    int currentRunnerIndex = 0;
    int matchCount = 0;
    for (String generatedFeatureName : generatedFeatureNames) {
        if (scenarioNames.isEmpty()) {
            generatedFeatureNamesPerRunner.get(currentRunnerIndex).add(generatedFeatureName);
            currentRunnerIndex++;
            if (currentRunnerIndex >= targetRunnerNumber) {
                currentRunnerIndex = 0;
            }
        } else {
            // Move all scenarios matching a scenario name into its own group.
            String scenarioText = fileIO.readContentFromFile(propertyManager.getGeneratedFeatureDirectory() + "/" + generatedFeatureName + ".feature");
            if (scenarioText != null) {
                Parser<GherkinDocument> parser = new Parser<>(new AstBuilder());
                String language = parser.parse(scenarioText).getFeature().getLanguage();
                int listIndex = gherkinDocumentParser.matchScenarioWithScenarioNames(language, scenarioText);
                if (listIndex >= 0) {
                    generatedFeatureNamesPerRunner.get(listIndex).add(generatedFeatureName);
                    matchCount++;
                }
            }
        }
    }
    if (!scenarioNames.isEmpty() && matchCount == 0) {
        throw new CucablePluginException("No matching scenarios found for specified scenario names - " + Arrays.toString(scenarioNames.toArray()) + "!");
    }
    int runnerFileCounter = 0;
    for (List<String> generatedFeatureNamesForSingleRunner : generatedFeatureNamesPerRunner) {
        if (generatedFeatureNamesForSingleRunner.size() > 0) {
            generateRunnerClass(generatedFeatureNamesForSingleRunner);
            runnerFileCounter++;
        }
    }
    return runnerFileCounter;
}
Also used : AstBuilder(gherkin.AstBuilder) CucablePluginException(com.trivago.exceptions.CucablePluginException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) GherkinDocument(gherkin.ast.GherkinDocument) GherkinDocumentParser(com.trivago.gherkin.GherkinDocumentParser) Parser(gherkin.Parser)

Aggregations

GherkinDocument (gherkin.ast.GherkinDocument)5 CucablePluginException (com.trivago.exceptions.CucablePluginException)3 AstBuilder (gherkin.AstBuilder)3 Parser (gherkin.Parser)3 ArrayList (java.util.ArrayList)3 ParserException (gherkin.ParserException)2 Background (gherkin.ast.Background)2 Feature (gherkin.ast.Feature)2 Scenario (gherkin.ast.Scenario)2 ScenarioDefinition (gherkin.ast.ScenarioDefinition)2 ScenarioOutline (gherkin.ast.ScenarioOutline)2 FeatureFileParseException (com.trivago.exceptions.filesystem.FeatureFileParseException)1 GherkinDocumentParser (com.trivago.gherkin.GherkinDocumentParser)1 CucablePluginException (com.trivago.rta.exceptions.CucablePluginException)1 SingleScenario (com.trivago.rta.vo.SingleScenario)1 Step (com.trivago.rta.vo.Step)1 SingleScenario (com.trivago.vo.SingleScenario)1 Step (com.trivago.vo.Step)1 TagExpressionParser (io.cucumber.tagexpressions.TagExpressionParser)1 List (java.util.List)1