Skip to content

[Core] Support Gherkin Rule keyword #1840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
# 1.1 Semver check
- stage: test
jdk: openjdk8
script: mvn verify -Pcheck-semantic-version -DskipTests=true
script: mvn verify -Pcheck-semantic-version -DskipTests=true -DskipITs=true
env: CHECK_SEMANTIC_VERSION=true

# 1.2 Check JDK 8,10,ea
Expand All @@ -28,8 +28,7 @@ jobs:
- jdk: oraclejdk11
env: JAVADOC=true
script:
- mvn clean verify -DskipTests=true
- mvn javadoc:aggregate
- mvn javadoc:jar

branches:
only:
Expand Down
70 changes: 70 additions & 0 deletions gherkin-messages/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>5.0.0-RC3-SNAPSHOT</version>
</parent>

<properties>
<project.Automatic-Module-Name>io.cucumber.core.gherkin.messages</project.Automatic-Module-Name>
</properties>

<artifactId>cucumber-gherkin-messages</artifactId>
<packaging>jar</packaging>
<name>Cucumber-JVM: Gherkin Messages</name>

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>gherkin</artifactId>
<version>${gherkin-messages.version}</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-gherkin</artifactId>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>messages</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>io.cucumber:gherkin</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>io.cucumber.gherkin</pattern>
<shadedPattern>io.cucumber.core.gherkin.messages.internal.gherkin</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.cucumber.core.gherkin.messages;

import io.cucumber.messages.Messages.GherkinDocument;
import io.cucumber.messages.Messages.GherkinDocument.Feature.FeatureChild;
import io.cucumber.messages.Messages.GherkinDocument.Feature.FeatureChild.RuleChild;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario.Examples;
import io.cucumber.messages.Messages.GherkinDocument.Feature.Step;
import io.cucumber.messages.Messages.GherkinDocument.Feature.TableRow;
import io.cucumber.messages.Messages.Location;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Objects.requireNonNull;

final class CucumberQuery {
private final Map<String, Step> gherkinStepById = new HashMap<>();
private final Map<String, Scenario> gherkinScenarioById = new HashMap<>();
private final Map<String, Location> locationBySourceId = new HashMap<>();

void update(GherkinDocument gherkinDocument) {
for (FeatureChild featureChild : gherkinDocument.getFeature().getChildrenList()) {
if (featureChild.hasBackground()) {
this.updateBackground(
featureChild.getBackground(),
gherkinDocument.getUri()
);
}

if (featureChild.hasScenario()) {
this.updateScenario(
featureChild.getScenario(),
gherkinDocument.getUri()
);
}

if (featureChild.hasRule()) {
for (RuleChild ruleChild : featureChild.getRule().getChildrenList()) {
if (ruleChild.hasBackground()) {
this.updateBackground(
ruleChild.getBackground(),
gherkinDocument.getUri()
);
}

if (ruleChild.hasScenario()) {
this.updateScenario(
ruleChild.getScenario(),
gherkinDocument.getUri()
);
}
}
}
}
}

private void updateScenario(Scenario scenario, String uri) {
gherkinScenarioById.put(requireNonNull(scenario.getId()), scenario);
locationBySourceId.put(requireNonNull(scenario.getId()), scenario.getLocation());
updateStep(scenario.getStepsList());

for (Examples examples: scenario.getExamplesList()) {
for (TableRow tableRow: examples.getTableBodyList()) {
this.locationBySourceId.put(requireNonNull(tableRow.getId()), tableRow.getLocation());
}
}
}

private void updateBackground(GherkinDocument.Feature.Background background, String uri) {
updateStep(background.getStepsList());
}

private void updateStep(List<Step> stepsList) {
for (Step step : stepsList) {
locationBySourceId.put(requireNonNull(step.getId()), step.getLocation());
gherkinStepById.put(requireNonNull(step.getId()), step);
}
}

Step getGherkinStep(String sourceId) {
return requireNonNull(gherkinStepById.get(requireNonNull(sourceId)));
}

Scenario getGherkinScenario(String sourceId) {
return requireNonNull(gherkinScenarioById.get(requireNonNull(sourceId)));
}

Location getLocation(String sourceId) {
Location location = locationBySourceId.get(requireNonNull(sourceId));
return requireNonNull(location);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.cucumber.core.gherkin.messages;

import io.cucumber.core.gherkin.DataTableArgument;
import io.cucumber.messages.Messages.PickleStepArgument.PickleTable;

import java.util.AbstractList;
import java.util.List;

final class GherkinMessagesDataTableArgument implements DataTableArgument {

private final CellView cells;
private final int line;

GherkinMessagesDataTableArgument(PickleTable table, int line) {
this.cells = new CellView(table);
this.line = line;
}

@Override
public List<List<String>> cells() {
return cells;
}

@Override
public int getLine() {
return line;
}

private static class CellView extends AbstractList<List<String>> {
private final PickleTable table;

CellView(PickleTable table) {
this.table = table;
}

@Override
public List<String> get(int row) {
return new AbstractList<String>() {
@Override
public String get(int column) {
return table.getRows(row).getCells(column).getValue();
}

@Override
public int size() {
return table.getRows(row).getCellsCount();
}
};
}

@Override
public int size() {
return table.getRowsCount();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.cucumber.core.gherkin.messages;

import io.cucumber.core.gherkin.DocStringArgument;
import io.cucumber.messages.Messages.PickleStepArgument.PickleDocString;

final class GherkinMessagesDocStringArgument implements DocStringArgument {

private final PickleDocString docString;
private final int line;

GherkinMessagesDocStringArgument(PickleDocString docString, int line) {
this.docString = docString;
this.line = line;
}

@Override
public String getContent() {
return docString.getContent();
}

@Override
public String getContentType() {
return docString.getContentType();
}

@Override
public int getLine() {
return line;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.cucumber.core.gherkin.messages;

import io.cucumber.core.gherkin.Example;
import io.cucumber.core.gherkin.Location;
import io.cucumber.messages.Messages.GherkinDocument.Feature.TableRow;

final class GherkinMessagesExample implements Example {

private final TableRow tableRow;
private final int rowIndex;

GherkinMessagesExample(TableRow tableRow, int rowIndex) {
this.tableRow = tableRow;
this.rowIndex = rowIndex;
}

@Override
public String getKeyWord() {
return null;
}

@Override
public String getName() {
return "Example #" + rowIndex;
}

@Override
public Location getLocation() {
return GherkinMessagesLocation.from(tableRow.getLocation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.cucumber.core.gherkin.messages;

import io.cucumber.core.gherkin.Example;
import io.cucumber.core.gherkin.Examples;
import io.cucumber.core.gherkin.Location;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

final class GherkinMessagesExamples implements Examples {

private final io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario.Examples examples;

GherkinMessagesExamples(io.cucumber.messages.Messages.GherkinDocument.Feature.Scenario.Examples examples) {
this.examples = examples;
}

@Override
public Stream<Example> children() {
AtomicInteger row = new AtomicInteger(1);
return examples.getTableBodyList().stream()
.map(tableRow -> new GherkinMessagesExample(tableRow, row.getAndIncrement()));
}

@Override
public String getKeyWord() {
return examples.getKeyword();
}

@Override
public String getName() {
return examples.getName();
}

@Override
public Location getLocation() {
return GherkinMessagesLocation.from(examples.getLocation());
}
}
Loading