Skip to content
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

Gherkin java codegen #1959

Merged
merged 9 commits into from
Apr 21, 2022
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
6 changes: 6 additions & 0 deletions gherkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt

### Changed

* [Java] the `GherkinDialect` constructor is no longer public (it's only used internally)

### Deprecated

### Removed

### Fixed

* [Java] remove shaded dependency on `com.eclipsesource.minimal-json:minimal-json`
([#1957](https://github.com/cucumber/common/issues/1957)
[#1959](https://github.com/cucumber/common/pull/1959))

## [23.0.1] - 2022-03-31

### Fixed
Expand Down
1 change: 0 additions & 1 deletion gherkin/java/.rsync
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
../../.templates/java/ .
../testdata/ testdata/
../gherkin.berp gherkin.berp
../gherkin-languages.json src/main/resources/io/cucumber/gherkin/gherkin-languages.json
53 changes: 24 additions & 29 deletions gherkin/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@
<version>[18.0.0,19.0.0)</version>
</dependency>

<dependency>
<groupId>com.eclipsesource.minimal-json</groupId>
<artifactId>minimal-json</artifactId>
<version>0.9.5</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -79,34 +73,35 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.1.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>${project.basedir}/src/main/groovy/io/cucumber/gherkin/GherkinDialects.groovy</source>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>shade</goal>
<goal>add-source</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.eclipsesource.minimal-json:minimal-json</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.eclipsesource.json</pattern>
<shadedPattern>io.cucumber.gherkin.internal.com.eclipsesource.json</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>com.eclipsesource.minimal-json:minimal-json</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
</excludes>
</filter>
</filters>
<sources>
<source>${basedir}/target/generated-sources/gherkin-dialects/java</source>
</sources>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import groovy.text.SimpleTemplateEngine
import groovy.json.JsonSlurper
import java.nio.file.Files

SimpleTemplateEngine engine = new SimpleTemplateEngine()
def templateSource = new File(project.basedir, "src/main/groovy/io/cucumber/gherkin/GherkinDialects.gsp").getText()

def jsonSlurper = new JsonSlurper()
def dialects = jsonSlurper.parseText(new File(project.basedir, "../gherkin-languages.json").getText())
def arrToString(arr) {
return arr.collect {e -> return '"' + e + '"'}.join(", ")
}
def binding = ["dialects": dialects, "arrToString": this.&arrToString]

def template = engine.createTemplate(templateSource).make(binding)
def file = new File(project.basedir, "target/generated-sources/gherkin-dialects/java/io/cucumber/gherkin/GherkinDialects.java")
Files.createDirectories(file.parentFile.toPath())
file.write(template.toString(), "UTF-8")
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.cucumber.gherkin;

import java.util.LinkedHashMap;
import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;

final class GherkinDialects {
static final Map<String, GherkinDialect> DIALECTS;

static {
Map<String, GherkinDialect> dialects = new LinkedHashMap<>();
<% dialects.eachWithIndex {name, dialect, index -> %>
dialects.put("<%= name %>", new GherkinDialect(
"<%= name %>",
"<%= dialect.name %>",
"<%= dialect.native %>",
unmodifiableList(asList(<%= arrToString(dialect.feature) %>)),
unmodifiableList(asList(<%= arrToString(dialect.rule) %>)),
unmodifiableList(asList(<%= arrToString(dialect.scenario) %>)),
unmodifiableList(asList(<%= arrToString(dialect.scenarioOutline) %>)),
unmodifiableList(asList(<%= arrToString(dialect.background) %>)),
unmodifiableList(asList(<%= arrToString(dialect.examples) %>)),
unmodifiableList(asList(<%= arrToString(dialect.given) %>)),
unmodifiableList(asList(<%= arrToString(dialect.when) %>)),
unmodifiableList(asList(<%= arrToString(dialect.then) %>)),
unmodifiableList(asList(<%= arrToString(dialect.and) %>)),
unmodifiableList(asList(<%= arrToString(dialect.but) %>))
));
<% } %>
DIALECTS = unmodifiableMap(dialects);
}
}
92 changes: 53 additions & 39 deletions gherkin/java/src/main/java/io/cucumber/gherkin/GherkinDialect.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,109 @@
package io.cucumber.gherkin;

import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;
import static java.util.Collections.unmodifiableList;

public final class GherkinDialect {
private final JsonObject keywords;
private final String language;

public GherkinDialect(String language, JsonObject keywords) {
this.language = requireNonNull(language);
this.keywords = requireNonNull(keywords);
private final String name;
private final String nativeName;
private final List<String> featureKeywords;
private final List<String> ruleKeywords;
private final List<String> scenarioKeywords;
private final List<String> scenarioOutlineKeywords;
private final List<String> backgroundKeywords;
private final List<String> examplesKeywords;
private final List<String> givenKeywords;
private final List<String> whenKeywords;
private final List<String> thenKeywords;
private final List<String> andKeywords;
private final List<String> butKeywords;
private final List<String> stepKeywords;

GherkinDialect(String language, String name, String nativeName, List<String> featureKeywords, List<String> ruleKeywords, List<String> scenarioKeywords, List<String> scenarioOutlineKeywords, List<String> backgroundKeywords, List<String> examplesKeywords, List<String> givenKeywords, List<String> whenKeywords, List<String> thenKeywords, List<String> andKeywords, List<String> butKeywords) {
this.language = language;
this.name = name;
this.nativeName = nativeName;
this.featureKeywords = featureKeywords;
this.ruleKeywords = ruleKeywords;
this.scenarioKeywords = scenarioKeywords;
this.scenarioOutlineKeywords = scenarioOutlineKeywords;
this.backgroundKeywords = backgroundKeywords;
this.examplesKeywords = examplesKeywords;
this.givenKeywords = givenKeywords;
this.whenKeywords = whenKeywords;
this.thenKeywords = thenKeywords;
this.andKeywords = andKeywords;
this.butKeywords = butKeywords;

List<String> stepKeywords = new ArrayList<>();
stepKeywords.addAll(givenKeywords);
stepKeywords.addAll(whenKeywords);
stepKeywords.addAll(thenKeywords);
stepKeywords.addAll(andKeywords);
stepKeywords.addAll(butKeywords);
this.stepKeywords = unmodifiableList(stepKeywords);
}

public List<String> getFeatureKeywords() {
return toStringList(this.keywords.get("feature").asArray());
}

private static List<String> toStringList(JsonArray array) {
List<String> result = new ArrayList<>();
for (JsonValue jsonValue : array) {
result.add(jsonValue.asString());
}
return result;
return featureKeywords;
}

public String getName() {
return keywords.getString("name", null);
aslakhellesoy marked this conversation as resolved.
Show resolved Hide resolved
return name;
}

public String getNativeName() {
return keywords.getString("native", null);
aslakhellesoy marked this conversation as resolved.
Show resolved Hide resolved
return nativeName;
}

public List<String> getRuleKeywords() {
return toStringList(keywords.get("rule").asArray());
return ruleKeywords;
}

public List<String> getScenarioKeywords() {
return toStringList(keywords.get("scenario").asArray());
return scenarioKeywords;
}

public List<String> getScenarioOutlineKeywords() {
return toStringList(keywords.get("scenarioOutline").asArray());
return scenarioOutlineKeywords;
}

public List<String> getStepKeywords() {
List<String> result = new ArrayList<>();
result.addAll(getGivenKeywords());
result.addAll(getWhenKeywords());
result.addAll(getThenKeywords());
result.addAll(getAndKeywords());
result.addAll(getButKeywords());
return result;
return stepKeywords;
}

public List<String> getBackgroundKeywords() {
return toStringList(keywords.get("background").asArray());
return backgroundKeywords;
}

public List<String> getExamplesKeywords() {
return toStringList(keywords.get("examples").asArray());
return examplesKeywords;
}

public List<String> getGivenKeywords() {
return toStringList(keywords.get("given").asArray());
return givenKeywords;
}

public List<String> getWhenKeywords() {
return toStringList(keywords.get("when").asArray());
return whenKeywords;
}

public List<String> getThenKeywords() {
return toStringList(keywords.get("then").asArray());
return thenKeywords;
}

public List<String> getAndKeywords() {
return toStringList(keywords.get("and").asArray());
return andKeywords;
}

public List<String> getButKeywords() {
return toStringList(keywords.get("but").asArray());
return butKeywords;
}

public String getLanguage() {
return language;
}
}

Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
package io.cucumber.gherkin;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.sort;
import static java.util.Collections.unmodifiableList;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;

public final class GherkinDialectProvider {
private static final JsonObject DIALECTS;
private final String defaultDialectName;

public static final String JSON_PATH = "/io/cucumber/gherkin/gherkin-languages.json";

static {
try (Reader reader = new InputStreamReader(GherkinDialectProvider.class.getResourceAsStream(JSON_PATH),
UTF_8)) {
DIALECTS = Json.parse(reader).asObject();
} catch (IOException e) {
throw new GherkinException("Unable to parse " + JSON_PATH, e);
}
}
private final String defaultDialectName;

public GherkinDialectProvider(String defaultDialectName) {
this.defaultDialectName = requireNonNull(defaultDialectName);
Expand All @@ -39,23 +19,15 @@ public GherkinDialectProvider() {
}

public GherkinDialect getDefaultDialect() {
return getDialect(defaultDialectName, null);
return getDialect(defaultDialectName).orElseThrow(() -> new ParserException.NoSuchLanguageException(defaultDialectName, null));
}

public GherkinDialect getDialect(String language, Location location) {
public Optional<GherkinDialect> getDialect(String language) {
requireNonNull(language);

JsonValue languageObject = DIALECTS.get(language);
if (languageObject == null) {
throw new ParserException.NoSuchLanguageException(language, location);
}

return new GherkinDialect(language, languageObject.asObject());
return Optional.ofNullable(GherkinDialects.DIALECTS.get(language));
}

public List<String> getLanguages() {
List<String> languages = new ArrayList<>(DIALECTS.names());
sort(languages);
return unmodifiableList(languages);
public Set<String> getLanguages() {
return GherkinDialects.DIALECTS.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public boolean match_Language(Token token) {
String language = matcher.group(1);
setTokenMatched(token, TokenType.Language, language, null, null, null);

currentDialect = dialectProvider.getDialect(language, token.location);
currentDialect = dialectProvider.getDialect(language)
.orElseThrow(() -> new ParserException.NoSuchLanguageException(language, token.location));
return true;
}
return false;
Expand Down
Loading