Search in sources :

Example 11 with SingleScenario

use of com.trivago.vo.SingleScenario in project cucable-plugin by trivago.

the class FeatureFileContentRenderer method getRenderedFeatureFileContent.

/**
 * Get the complete content based on multiple features that can be written to a valid feature file.
 *
 * @return the feature file content.
 */
private String getRenderedFeatureFileContent(List<SingleScenario> singleScenarios) {
    StringBuilder renderedContent = new StringBuilder();
    SingleScenario firstScenario = singleScenarios.get(0);
    addLanguage(renderedContent, firstScenario.getFeatureLanguage());
    addTags(renderedContent, firstScenario.getFeatureTags());
    addNameAndDescription(renderedContent, firstScenario.getFeatureName(), firstScenario.getFeatureDescription());
    addBackgroundSteps(renderedContent, firstScenario.getBackgroundSteps());
    for (SingleScenario singleScenario : singleScenarios) {
        renderedContent.append(LINE_SEPARATOR);
        List<String> scenarioTags = singleScenario.getScenarioTags();
        if (scenarioTags != null && firstScenario.getFeatureTags() != null) {
            scenarioTags.removeAll(firstScenario.getFeatureTags());
        }
        addTags(renderedContent, scenarioTags);
        addTags(renderedContent, singleScenario.getExampleTags());
        addNameAndDescription(renderedContent, singleScenario.getScenarioName(), singleScenario.getScenarioDescription());
        addSteps(renderedContent, singleScenario.getSteps());
    }
    addComments(renderedContent, firstScenario.getFeatureFilePath());
    return renderedContent.toString();
}
Also used : SingleScenario(com.trivago.vo.SingleScenario)

Example 12 with SingleScenario

use of com.trivago.vo.SingleScenario 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 13 with SingleScenario

use of com.trivago.vo.SingleScenario in project cucable-plugin by trivago.

the class GherkinDocumentParser method getSingleScenariosFromOutline.

/**
 * Returns a list of Cucable single scenarios from a Gherkin scenario outline.
 *
 * @param scenarioOutline A Gherkin {@link ScenarioOutline}.
 * @param featureName     The name of the feature this scenario outline belongs to.
 * @param featureFilePath The source path of the feature file.
 * @param featureLanguage The feature language this scenario outline belongs to.
 * @param featureTags     The feature tags of the parent feature.
 * @param backgroundSteps Return a Cucable {@link SingleScenario} list.
 */
private List<SingleScenario> getSingleScenariosFromOutline(final ScenarioOutline scenarioOutline, final String featureName, final String featureFilePath, final String featureLanguage, final String featureDescription, final List<String> featureTags, final List<Step> backgroundSteps) {
    // Retrieve the translation of "Scenario" in the target language and add it to the scenario
    String translatedScenarioKeyword = gherkinTranslations.getScenarioKeyword(featureLanguage);
    String scenarioName = translatedScenarioKeyword + ": " + scenarioOutline.getName();
    String scenarioDescription = scenarioOutline.getDescription();
    List<String> scenarioTags = gherkinToCucableConverter.convertGherkinTagsToCucableTags(scenarioOutline.getTags());
    List<SingleScenario> outlineScenarios = new ArrayList<>();
    List<Step> steps = gherkinToCucableConverter.convertGherkinStepsToCucableSteps(scenarioOutline.getSteps());
    if (scenarioOutline.getExamples().isEmpty()) {
        cucableLogger.warn("Scenario outline '" + scenarioOutline.getName() + "' without example table!");
        return outlineScenarios;
    }
    for (Examples exampleTable : scenarioOutline.getExamples()) {
        Map<String, List<String>> exampleMap = gherkinToCucableConverter.convertGherkinExampleTableToCucableExampleMap(exampleTable);
        String firstColumnHeader = (String) exampleMap.keySet().toArray()[0];
        int rowCount = exampleMap.get(firstColumnHeader).size();
        // For each example row, create a new single scenario
        for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
            SingleScenario singleScenario = new SingleScenario(featureName, featureFilePath, featureLanguage, featureDescription, replacePlaceholderInString(scenarioName, exampleMap, rowIndex), scenarioDescription, featureTags, backgroundSteps);
            List<Step> substitutedSteps = substituteStepExamplePlaceholders(steps, exampleMap, rowIndex);
            singleScenario.setSteps(substitutedSteps);
            singleScenario.setScenarioTags(scenarioTags);
            singleScenario.setExampleTags(gherkinToCucableConverter.convertGherkinTagsToCucableTags(exampleTable.getTags()));
            outlineScenarios.add(singleScenario);
        }
    }
    return outlineScenarios;
}
Also used : SingleScenario(com.trivago.vo.SingleScenario) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Step(com.trivago.vo.Step) Examples(gherkin.ast.Examples)

Example 14 with SingleScenario

use of com.trivago.vo.SingleScenario in project cucable-plugin by trivago.

the class FeatureFileConverterTest method convertToSingleScenariosAndMultiRunnersTest.

@Test
public void convertToSingleScenariosAndMultiRunnersTest() throws Exception {
    String generatedFeatureDir = testFolder.getRoot().getPath().concat("/features/");
    String generatedRunnerDir = testFolder.getRoot().getPath().concat("/runners/");
    final String FEATURE_FILE_NAME = "FEATURE_FILE.feature";
    propertyManager.setNumberOfTestRuns(1);
    propertyManager.setDesiredNumberOfRunners(1);
    propertyManager.setGeneratedFeatureDirectory(generatedFeatureDir);
    propertyManager.setGeneratedRunnerDirectory(generatedRunnerDir);
    propertyManager.setParallelizationMode("scenarios");
    when(fileIO.readContentFromFile(FEATURE_FILE_NAME)).thenReturn("TEST_CONTENT");
    List<CucableFeature> cucableFeatures = new ArrayList<>();
    CucableFeature cucableFeature = new CucableFeature(FEATURE_FILE_NAME, null);
    cucableFeatures.add(cucableFeature);
    when(fileSystemManager.getPathsFromCucableFeature(cucableFeature)).thenReturn(Collections.singletonList(Paths.get(cucableFeature.getName())));
    List<SingleScenario> scenarioList = new ArrayList<>();
    SingleScenario singleScenario = getSingleScenario();
    scenarioList.add(singleScenario);
    scenarioList.add(singleScenario);
    when(gherkinDocumentParser.getSingleScenariosFromFeature("TEST_CONTENT", FEATURE_FILE_NAME, null)).thenReturn(scenarioList);
    String featureFileContent = "test";
    when(featureFileContentRenderer.getRenderedFeatureFileContent(singleScenario)).thenReturn(featureFileContent);
    when(runnerFileContentRenderer.getRenderedRunnerFileContent(any(FeatureRunner.class))).thenReturn("RUNNER_CONTENT");
    featureFileConverter.generateParallelizableFeatures(cucableFeatures);
    verify(fileIO, times(3)).writeContentToFile(anyString(), anyString());
}
Also used : SingleScenario(com.trivago.vo.SingleScenario) ArrayList(java.util.ArrayList) FeatureRunner(com.trivago.vo.FeatureRunner) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CucableFeature(com.trivago.vo.CucableFeature) Test(org.junit.Test)

Example 15 with SingleScenario

use of com.trivago.vo.SingleScenario in project cucable-plugin by trivago.

the class FeatureFileConverterTest method convertToSingleScenariosAndRunnersTest.

@Test
public void convertToSingleScenariosAndRunnersTest() throws Exception {
    String generatedFeatureDir = testFolder.getRoot().getPath().concat("/features/");
    String generatedRunnerDir = testFolder.getRoot().getPath().concat("/runners/");
    final String FEATURE_FILE_NAME = "FEATURE_FILE.feature";
    propertyManager.setNumberOfTestRuns(1);
    propertyManager.setGeneratedFeatureDirectory(generatedFeatureDir);
    propertyManager.setGeneratedRunnerDirectory(generatedRunnerDir);
    when(fileIO.readContentFromFile(FEATURE_FILE_NAME)).thenReturn("TEST_CONTENT");
    List<CucableFeature> cucableFeatures = new ArrayList<>();
    CucableFeature cucableFeature = new CucableFeature(FEATURE_FILE_NAME, null);
    cucableFeatures.add(cucableFeature);
    when(fileSystemManager.getPathsFromCucableFeature(cucableFeature)).thenReturn(Collections.singletonList(Paths.get(cucableFeature.getName())));
    List<SingleScenario> scenarioList = new ArrayList<>();
    SingleScenario singleScenario = getSingleScenario();
    scenarioList.add(singleScenario);
    when(gherkinDocumentParser.getSingleScenariosFromFeature("TEST_CONTENT", FEATURE_FILE_NAME, null)).thenReturn(scenarioList);
    String featureFileContent = "test";
    when(featureFileContentRenderer.getRenderedFeatureFileContent(singleScenario)).thenReturn(featureFileContent);
    when(runnerFileContentRenderer.getRenderedRunnerFileContent(any(FeatureRunner.class))).thenReturn("RUNNER_CONTENT");
    featureFileConverter.generateParallelizableFeatures(cucableFeatures);
    ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
    verify(logger, times(1)).info(logCaptor.capture(), any(CucableLogger.CucableLogLevel.class), any(CucableLogger.CucableLogLevel.class), any(CucableLogger.CucableLogLevel.class));
    assertThat(logCaptor.getAllValues().get(0), is("Cucable created 1 separate feature file and 1 runner."));
    verify(fileIO, times(2)).writeContentToFile(anyString(), anyString());
}
Also used : SingleScenario(com.trivago.vo.SingleScenario) ArrayList(java.util.ArrayList) FeatureRunner(com.trivago.vo.FeatureRunner) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CucableFeature(com.trivago.vo.CucableFeature) Test(org.junit.Test)

Aggregations

SingleScenario (com.trivago.vo.SingleScenario)23 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)10 CucableFeature (com.trivago.vo.CucableFeature)7 FeatureRunner (com.trivago.vo.FeatureRunner)7 Step (com.trivago.vo.Step)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 DataTable (com.trivago.vo.DataTable)2 CucablePluginException (com.trivago.exceptions.CucablePluginException)1 FeatureFileParseException (com.trivago.exceptions.filesystem.FeatureFileParseException)1 Background (gherkin.ast.Background)1 Examples (gherkin.ast.Examples)1 Feature (gherkin.ast.Feature)1 GherkinDocument (gherkin.ast.GherkinDocument)1 Scenario (gherkin.ast.Scenario)1 ScenarioDefinition (gherkin.ast.ScenarioDefinition)1 ScenarioOutline (gherkin.ast.ScenarioOutline)1 List (java.util.List)1