Skip to content

Commit

Permalink
Fix and re-enable Gosu
Browse files Browse the repository at this point in the history
 - Gosu runtime now requires Java 8; how can we skip this module when Travis build with openjdk7 or oraclejdk7?
 - Thanks to @kausnz for supplying the clever reflection solution
  • Loading branch information
DPUkyle authored and mpkorstanje committed Jun 25, 2017
1 parent 940b97a commit 7edd7f1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 113 deletions.
1 change: 1 addition & 0 deletions History.md
@@ -1,5 +1,6 @@
## [2.0.0-SNAPSHOT](https://github.com/cucumber/cucumber-jvm/compare/v1.2.5...master) (In Git)

* [Gosu] Fix and re-enable Gosu for 2.0.0 ([#1155](https://github.com/cucumber/cucumber-jvm/pull/1155), [#1086](https://github.com/cucumber/cucumber-jvm/pull/1086), [#874](https://github.com/cucumber/cucumber-jvm/pull/874) Kyle Moore, M.P. Korstanje)
* [Core] Fix issue where ComplexTypeWriter would create unbalanced tables. ([#1042](https://github.com/cucumber/cucumber-jvm/pull/1042) Roy Jacobs, M.P. Korstanje)
* [Guice] Use the ContextClassLoader when loading InjectorSource. ([#1036](https://github.com/cucumber/cucumber-jvm/pull/1036), [#1037](https://github.com/cucumber/cucumber-jvm/pull/1037) Kyle Moore)
* [Core] Allow global registration of custom XStream converters. ([#1010](https://github.com/cucumber/cucumber-jvm/pull/1010), [#1009](https://github.com/cucumber/cucumber-jvm/issues/1009) Chris Rankin)
Expand Down
27 changes: 16 additions & 11 deletions gosu/pom.xml
@@ -1,4 +1,6 @@
<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">
<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>
Expand All @@ -12,21 +14,24 @@
<packaging>jar</packaging>
<name>Cucumber-JVM: Gosu</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-core</artifactId>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>gherkin</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.gosu-lang.gosu</groupId>
Expand Down
83 changes: 22 additions & 61 deletions gosu/src/main/java/cucumber/runtime/gosu/GlueSource.java
@@ -1,73 +1,34 @@
package cucumber.runtime.gosu;

import cucumber.runtime.io.Resource;
import gw.lang.launch.IArgInfo;
import gw.lang.launch.IBooleanArgKey;
import gw.lang.launch.IProgramSource;
import gw.lang.launch.IStringArgKey;
import gw.lang.reflect.ReflectUtil;
import gw.lang.reflect.gs.GosuClassPathThing;
import gw.lang.reflect.gs.IProgramInstance;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
class GlueSource {

class GlueSource implements IProgramSource {
private final StringBuilder sourceBuilder = new StringBuilder();

@Override
public String getRawPath() {
// This file doesn't really exist, but we have to say something I guess!
return "cucumber.gsp";
}

@Override
public File getFile() {
return null;
}

@Override
public InputStream openInputStream() throws IOException {
return new ByteArrayInputStream(sourceBuilder.toString().getBytes("UTF-8"));
/**
* Performs minimal initialization of the Gosu TypeSystem; this is required for later calls to ReflectUtil#getClass
*/
public GlueSource() {
GosuClassPathThing.init();
}

/**
* Given a glue Resource, calculates the FQN of the Gosu Program as a class,
* loads it via reflection and calls its evaluate method.
* @param glueScript
*/
public void addGlueScript(Resource glueScript) {
// https://groups.google.com/d/msg/gosu-lang/yMJnzQwuFpo/msg81GNGlAYJ
String className = glueScript.getClassName(".gsp");
sourceBuilder.append("(" + className + ".Type as java.lang.Class).getDeclaredMethod( \"evaluate\", {gw.lang.reflect.gs.IExternalSymbolMap} ).invoke( new " + className + "(), {null})\n");
Class clazz = ReflectUtil.getClass(className).getBackingClass();
try {
((IProgramInstance)(clazz.newInstance())).evaluate(null);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

public IArgInfo toArgInfo() {
return new IArgInfo() {
@Override
public boolean consumeArg(IBooleanArgKey iBooleanArgKey) {
return false;
}

@Override
public String consumeArg(IStringArgKey iStringArgKey) {
return null;
}

@Override
public void processUnknownArgs() {

}

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

@Override
public IProgramSource getProgramSource() {
return GlueSource.this;
}

@Override
public List<String> getArgsList() {
return null;
}
};
}
}
14 changes: 6 additions & 8 deletions gosu/src/main/java/cucumber/runtime/gosu/GosuBackend.java
Expand Up @@ -7,8 +7,7 @@
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.snippets.FunctionNameGenerator;
import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Step;
import gw.lang.Gosu;
import gherkin.pickles.PickleStep;
import gw.lang.function.AbstractBlock;

import java.util.List;
Expand Down Expand Up @@ -36,9 +35,6 @@ public void loadGlue(Glue glue, List<String> gluePaths) {
source.addGlueScript(glueScript);
}
}

Gosu gosu = new Gosu();
gosu.start(source.toArgInfo());
}

@Override
Expand All @@ -57,10 +53,12 @@ public void disposeWorld() {
}

@Override
public String getSnippet(Step step, FunctionNameGenerator functionNameGenerator) {
return snippetGenerator.getSnippet(step, null); }
public String getSnippet(PickleStep step, String keyword, FunctionNameGenerator functionNameGenerator) {
return snippetGenerator.getSnippet(step, keyword, functionNameGenerator);
}

public void addStepDefinition(String regexp, Object body) {
@SuppressWarnings("unused") // this is indeed invoked by static methods on cucumber.api.gosu.en.Dsl
public void addStepDefinition( String regexp, Object body) {
AbstractBlock block = (AbstractBlock) body;
glue.addStepDefinition(new GosuStepDefinition(Pattern.compile(regexp), block, currentLocation()));
}
Expand Down
11 changes: 5 additions & 6 deletions gosu/src/main/java/cucumber/runtime/gosu/GosuStepDefinition.java
@@ -1,11 +1,10 @@
package cucumber.runtime.gosu;

import cucumber.runtime.Argument;
import cucumber.runtime.JdkPatternArgumentMatcher;
import cucumber.runtime.ParameterInfo;
import cucumber.runtime.StepDefinition;
import gherkin.I18n;
import gherkin.formatter.Argument;
import gherkin.formatter.model.Step;
import gherkin.pickles.PickleStep;
import gw.lang.function.AbstractBlock;
import gw.lang.reflect.IType;

Expand Down Expand Up @@ -41,8 +40,8 @@ private List<ParameterInfo> getParameterInfos() {
return result;
}
@Override
public List<Argument> matchedArguments(Step step) {
return argumentMatcher.argumentsFrom(step.getName());
public List<Argument> matchedArguments(PickleStep step) {
return argumentMatcher.argumentsFrom(step.getText());
}

@Override
Expand All @@ -61,7 +60,7 @@ public ParameterInfo getParameterType(int n, Type argumentType) {
}

@Override
public void execute(I18n i18n, Object[] args) throws Throwable {
public void execute(String language, Object[] args) throws Throwable {
// TODO: Add timeout
block.invokeWithArgs(args);
}
Expand Down
34 changes: 17 additions & 17 deletions gosu/src/test/java/cucumber/runtime/gosu/GosuSnippetTest.java
@@ -1,21 +1,21 @@
package cucumber.runtime.gosu;

import cucumber.runtime.snippets.SnippetGenerator;
import gherkin.formatter.model.Comment;
import gherkin.formatter.model.DataTableRow;
import gherkin.formatter.model.DocString;
import gherkin.formatter.model.Step;
import gherkin.pickles.Argument;
import gherkin.pickles.PickleCell;
import gherkin.pickles.PickleRow;
import gherkin.pickles.PickleStep;
import gherkin.pickles.PickleString;
import gherkin.pickles.PickleTable;
import org.junit.Test;

import java.util.Collections;
import java.util.List;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;

public class GosuSnippetTest {

private static final List<Comment> NO_COMMENTS = Collections.emptyList();
private static final String GIVEN_KEYWORD = "Given";

@Test
public void generatesPlainSnippet() {
Expand Down Expand Up @@ -66,7 +66,7 @@ public void generatesSnippetWithDocString() {
" // Write code here that turns the phrase above into concrete actions\n" +
" throw new PendingException()\n" +
"})\n";
assertEquals(expected, snippetForDocString("I have:", new DocString("text/plain", "hello", 1)));
assertEquals(expected, snippetForDocString("I have:", new PickleString(null, "hello")));
}

@Test
Expand All @@ -76,22 +76,22 @@ public void generatesSnippetWithDataTable() {
" // Write code here that turns the phrase above into concrete actions\n" +
" throw new PendingException()\n" +
"})\n";
List<DataTableRow> dataTable = asList(new DataTableRow(NO_COMMENTS, asList("col1"), 1));
PickleTable dataTable = new PickleTable(asList(new PickleRow(asList(new PickleCell(null, "col1")))));
assertEquals(expected, snippetForDataTable("I have:", dataTable));
}

private String snippetFor(String name) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, null);
return new SnippetGenerator(new GosuSnippet()).getSnippet(step, null);
PickleStep step = new PickleStep(name, Collections.emptyList(), Collections.emptyList());
return new SnippetGenerator(new GosuSnippet()).getSnippet(step, GIVEN_KEYWORD, null);
}

private String snippetForDocString(String name, DocString docString) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, null, docString);
return new SnippetGenerator(new GosuSnippet()).getSnippet(step, null);
private String snippetForDocString(String name, PickleString docString) {
PickleStep step = new PickleStep(name, asList((Argument)docString), Collections.emptyList());
return new SnippetGenerator(new GosuSnippet()).getSnippet(step, GIVEN_KEYWORD, null);
}

private String snippetForDataTable(String name, List<DataTableRow> dataTable) {
Step step = new Step(NO_COMMENTS, "Given ", name, 0, dataTable, null);
return new SnippetGenerator(new GosuSnippet()).getSnippet(step, null);
private String snippetForDataTable(String name, PickleTable dataTable) {
PickleStep step = new PickleStep(name, asList((Argument)dataTable), Collections.emptyList());
return new SnippetGenerator(new GosuSnippet()).getSnippet(step, GIVEN_KEYWORD, null);
}
}
12 changes: 2 additions & 10 deletions pom.xml
Expand Up @@ -36,7 +36,7 @@
<clojure.version>1.8.0</clojure.version>
<!-- This is only here for the example project to use, the 3 scala projects maintain their own versions -->
<scala.latest.version>2.11.8</scala.latest.version>
<gosu.version>1.2</gosu.version>
<gosu.version>1.14.6</gosu.version>
<rhino.version>1.7.7.1</rhino.version>
<jsoup.version>1.9.2</jsoup.version>
<testng.version>6.9.10</testng.version>
Expand Down Expand Up @@ -549,14 +549,6 @@
</dependencies>
</dependencyManagement>

<repositories>
<!--<repository>-->
<!--<id>gosu-lang.org-releases</id>-->
<!--<name>Official Gosu website (releases)</name>-->
<!--<url>http://gosu-lang.org/repositories/m2/releases</url>-->
<!--</repository>-->
</repositories>

<!--
this adds a repository that the plugins will use for dependency resolution
Any items that we use in a plugin's <dependency> phase can be resolved from here, as well as central
Expand All @@ -569,7 +561,6 @@
</pluginRepositories>

<modules>
<!--<module>gosu</module>-->
<module>core</module>
<module>java</module>
<module>testng</module>
Expand Down Expand Up @@ -598,6 +589,7 @@
<jdk>1.8</jdk>
</activation>
<modules>
<module>gosu</module>
<module>java8</module>
<module>kotlin-java8</module>
</modules>
Expand Down

0 comments on commit 7edd7f1

Please sign in to comment.