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

Support objects as ScalaDsl definitions. #156

Merged
merged 3 commits into from
Jan 21, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,6 +2,7 @@
/*.iws
/*.ipr
*.iml
.idea/
.settings
.project
.classpath
Expand Down
4 changes: 2 additions & 2 deletions groovy/pom.xml
Expand Up @@ -84,12 +84,12 @@ import gherkin.I18n

def _ = File.separator
def engine = new SimpleTemplateEngine()
def templateSource = new File("${basedir}${_}src${_}main${_}code_generator${_}I18n.groovy.txt").getText()
def templateSource = new File(/${basedir}${_}src${_}main${_}code_generator${_}I18n.groovy.txt/).getText()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be some Groovy syntax I'm unfamiliar with. Can you explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Groovy User Guide calls it "Slashy String literals". It makes '/' the only escape sequence recognized inside the literal making it safe for interpolating Windows file names and since the antrun plugin doesn't sanitize ${baseDir} it's the only option for making it work cleanly under Windows. I haven't had a chance to check it under Linux so I isolated those changes so people can cherry pick around them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dwskoog hmm, I get errors on OS X with those changes. If you pop into #cucumber on irc.freenode.net we can discuss it.


I18n.all.each { i18n ->
def binding = ["i18n":i18n]
template = engine.createTemplate(templateSource).make(binding)
def file = new File("${basedir}${_}target${_}generated-sources${_}i18n${_}java${_}cucumber${_}runtime${_}groovy${_}${i18n.underscoredIsoCode.toUpperCase()}.java")
def file = new File(/${basedir}${_}target${_}generated-sources${_}i18n${_}java${_}cucumber${_}runtime${_}groovy${_}${i18n.underscoredIsoCode.toUpperCase()}.java/)
file.parentFile.mkdirs()
file.write(template.toString(), "UTF-8")
}
Expand Down
4 changes: 2 additions & 2 deletions java/pom.xml
Expand Up @@ -51,13 +51,13 @@ import gherkin.I18n

def _ = File.separator
def engine = new SimpleTemplateEngine()
def templateSource = new File("${project.basedir}${_}src${_}main${_}code_generator${_}I18n.java.txt").getText()
def templateSource = new File(/${project.basedir}${_}src${_}main${_}code_generator${_}I18n.java.txt/).getText()

I18n.all.each { i18n ->
i18n.codeKeywords.each { kw ->
def binding = ["i18n":i18n, "kw":kw]
template = engine.createTemplate(templateSource).make(binding)
def file = new File("${basedir}${_}target${_}generated-sources${_}i18n${_}java${_}cucumber${_}annotation${_}${i18n.underscoredIsoCode}${_}${kw}.java")
def file = new File(/${basedir}${_}target${_}generated-sources${_}i18n${_}java${_}cucumber${_}annotation${_}${i18n.underscoredIsoCode}${_}${kw}.java/)
file.parentFile.mkdirs()
file.write(template.toString(), "UTF-8")
}
Expand Down
4 changes: 2 additions & 2 deletions scala/pom.xml
Expand Up @@ -93,11 +93,11 @@ import groovy.text.SimpleTemplateEngine

def _ = File.separator
def engine = new SimpleTemplateEngine()
def templateSource = new File("${basedir}${_}src${_}main${_}code_generator${_}I18n.scala.txt").getText()
def templateSource = new File(/${basedir}${_}src${_}main${_}code_generator${_}I18n.scala.txt/).getText()

template = engine.createTemplate(templateSource)
template = template.make(null)
def file = new File("${basedir}${_}target${_}i18n${_}cucumber${_}runtime${_}I18n.scala")
def file = new File(/${basedir}${_}target${_}i18n${_}cucumber${_}runtime${_}I18n.scala/)
file.parentFile.mkdirs()
file.write(template.toString(), "UTF-8")
]]></groovy>
Expand Down
28 changes: 26 additions & 2 deletions scala/src/main/scala/cucumber/runtime/ScalaBackend.scala
Expand Up @@ -4,6 +4,7 @@ package runtime
import _root_.java.util.{List => JList}

import gherkin.formatter.model.Step
import _root_.java.lang.reflect.Modifier
import snippets.SnippetGenerator
import io.ResourceLoader
import io.ClasspathResourceLoader
Expand All @@ -12,7 +13,7 @@ import scala.collection.JavaConversions._

class ScalaBackend(ignore:ResourceLoader) extends Backend {
private var snippetGenerator = new SnippetGenerator(new ScalaSnippetGenerator())
private var instances:Seq[ScalaDsl] = Nil
private var instances:Seq[ScalaDsl] = Nil

def getStepDefinitions = instances.flatMap(_.stepDefinitions)

Expand All @@ -31,7 +32,30 @@ class ScalaBackend(ignore:ResourceLoader) extends Backend {
}

def loadGlue(glue: Glue, gluePaths: JList[String]) {
instances = gluePaths flatMap { new ClasspathResourceLoader().instantiateSubclasses(classOf[ScalaDsl], _, Array(), Array()) }
val cl = new ClasspathResourceLoader()
val dslClasses = gluePaths flatMap {cl.getDescendants(classOf[ScalaDsl], _) } filter { cls =>
try {
cls.getDeclaredConstructor()
true
} catch {
case e => false
}
}
val (clsClasses, objClasses) = dslClasses partition { cls =>
try {
Modifier.isPublic (cls.getConstructor().getModifiers)
} catch {
case e => false
}
}
val objInstances = objClasses map {cls =>
val instField = cls.getDeclaredField("MODULE$")
instField.setAccessible(true)
instField.get(null).asInstanceOf[ScalaDsl]
}
val clsInstances = (clsClasses map {_.newInstance()})

instances = objInstances ++ clsInstances

getStepDefinitions map {glue.addStepDefinition(_)}
getBeforeHooks map {glue.addBeforeHook(_)}
Expand Down
10 changes: 6 additions & 4 deletions scala/src/test/scala/cucumber/runtime/StepDefs.scala
Expand Up @@ -2,10 +2,12 @@ package cucumber.runtime

import junit.framework.Assert._

class RpnCalculatorStepDefinitions extends ScalaDsl with EN {
object RpnCalculatorStepDefinitions extends ScalaDsl with EN {
Given("""^I have (\d+) "([^"]*)" in my belly$"""){ (arg0:Int, arg1:String) =>
}

Then("""^I am "([^"]*)"$"""){ (arg0:String) =>
}
}

class ThenDefs extends ScalaDsl with EN {
Then("""^I am "([^"]*)"$"""){ (arg0:String) =>
}
}