Skip to content

Change the way we infer the types of steps arguments #206

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 1 commit into from
May 15, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ See also the [CHANGELOG](https://github.com/cucumber/cucumber-jvm/blob/master/CH

### Changed

- [Internal] Rewrite the way types of step arguments are inferred

### Deprecated

### Removed
Expand Down
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ lazy val cucumberScala = (projectMatrix in file("cucumber-scala"))
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((2, n)) if n <= 12 =>
List("org.scala-lang.modules" %% "scala-collection-compat" % "2.4.4")
case Some((3, 0)) =>
List("io.github.gaeljw" %% "typetrees" % "0.4.0")
case _ => Nil
}
},
Expand Down
53 changes: 31 additions & 22 deletions cucumber-scala/src/main/codegen/gen.scala
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@

/*
* Generates the evil looking apply methods in StepDsl#StepBody for Function1 to Function22
*/
for (i <- 1 to 22) {
val ts = (1 to i).map("T".+).mkString(", ")
val f = "(" + ts + ") => Any"
val p1 = "def apply[" + ts + "](f: " + f + ")"
val p2 = "(implicit " + (1 to i).map(n => "m" + n + ":Manifest[T" + n + "]").mkString(", ") + ")"
val register = "\n register(" +(1 to i).map(n => "m" + n).mkString(", ") + ") {\n"
val pf = " case List(" + (1 to i).map("a" + _ + ":AnyRef").mkString(", ") + ") => \n f(" + (1 to i).map(n => "a" + n + ".asInstanceOf[T" + n + "]").mkString(",\n ") + ")\n"
val otherwise = " case _ =>\n throw new IncorrectStepDefinitionException()\n"
val closeRegister = " }\n}"

println(p1 + p2 + ": Unit = { " + register + pf + otherwise + closeRegister + "\n")
}

/*
* Generates the evil looking apply methods in StepDsl#StepBody for Function1 to Function22
* Scala 3
*/
for (i <- 1 to 22) {
val ts = (1 to i).map("T".+).mkString(", ")
val tagsDef = (1 to i).map(n => s"val t$n: TypeTreeTag = typeTreeTag[T$n]").mkString("\n")
val tagsParam = (1 to i).map(n => s"t$n").mkString(", ")
val implicits = (1 to i).map(n => s"t$n: Stepable[T$n]").mkString(", ")
val implicitsParams = (1 to i).map(n => s"t$n").mkString(", ")
val listParams = (1 to i).map("a" + _ + ":AnyRef").mkString(", ")
val pf = (1 to i).map(n => "a" + n + ".asInstanceOf[T" + n + "]").mkString(",\n ")

println(s"""
|inline def apply[$ts](f: ($ts) => Any): Unit = {
| $tagsDef
| register($tagsParam) {
|def apply[$ts](f: ($ts) => Any)(using $implicits): Unit = {
| register($implicitsParams) {
| case List($listParams) =>
| f($pf)
| case _ =>
Expand Down Expand Up @@ -58,4 +41,30 @@ for (i <- 1 to 22) {
|""".stripMargin

println(template)
}
}

/*
* Generates the Stepable implicit methods
*/
for (i <- (1 to 9).reverse) {

val underscores = (1 to i).map(_ => "_").mkString(", ")
val types = (1 to i).map(j => s"X$j").mkString(", ")
val typesStepable = (1 to i).map(j => s"X$j: Stepable").mkString(", ")
val typeArgs = (1 to i).map(j => s"implicitly[Stepable[X$j]].asJavaType").mkString(", ")

val template =
s"""
|implicit def stepable$i[T[$underscores], $typesStepable](implicit ct: ClassTag[T[$types]]): Stepable[T[$types]] =
| new Stepable[T[$types]] {
| def asJavaType: JavaType =
| new ScalaParameterizedType(
| ct.runtimeClass,
| Array(
| $typeArgs
| )
| )
| }""".stripMargin

println(template)
}

This file was deleted.

Loading