Skip to content

Commit

Permalink
airspec (internal): Remove unnecessary return type Surface.of[R] gene…
Browse files Browse the repository at this point in the history
…ration (#3169)

Resolves #3168
  • Loading branch information
xerial authored Aug 28, 2023
1 parent 946224d commit 0b67070
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 61 deletions.
24 changes: 6 additions & 18 deletions airspec/src/main/scala-2/wvlet/airspec/AirSpecMacros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,34 @@ private[airspec] object AirSpecMacros {

def test0Impl[R: c.WeakTypeTag](c: sm.Context)(body: c.Tree): c.Tree = {
import c.universe._
val r = implicitly[c.WeakTypeTag[R]].tpe
q"""{
import wvlet.airspec.AirSpecTestBuilder._
val self = ${c.prefix}
val surface = wvlet.airframe.surface.Surface.of[${r}]
self.addF0(surface, wvlet.airframe.LazyF0(${body}))
self.addF0(wvlet.airframe.LazyF0(${body}))
}"""
}

def test1Impl[D1: c.WeakTypeTag, R: c.WeakTypeTag](c: sm.Context)(body: c.Tree): c.Tree = {
import c.universe._
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val r = implicitly[c.WeakTypeTag[R]].tpe
q"""{
import wvlet.airspec.AirSpecTestBuilder._
val self = ${c.prefix}
val surface = wvlet.airframe.surface.Surface.of[${r}]
val d1 = wvlet.airframe.surface.Surface.of[${d1}]
self.addF1(d1, surface, ${body})
self.addF1(d1, ${body})
}"""
}

def test2Impl[D1: c.WeakTypeTag, D2: c.WeakTypeTag, R: c.WeakTypeTag](c: sm.Context)(body: c.Tree): c.Tree = {
import c.universe._
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val d2 = implicitly[c.WeakTypeTag[D2]].tpe
val r = implicitly[c.WeakTypeTag[R]].tpe
q"""{
import wvlet.airspec.AirSpecTestBuilder._
val self = ${c.prefix}
val surface = wvlet.airframe.surface.Surface.of[${r}]
val d1 = wvlet.airframe.surface.Surface.of[${d1}]
val d2 = wvlet.airframe.surface.Surface.of[${d2}]
self.addF2(d1, d2, surface, ${body})
self.addF2(d1, d2, ${body})
}"""
}

Expand All @@ -66,15 +60,13 @@ private[airspec] object AirSpecMacros {
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val d2 = implicitly[c.WeakTypeTag[D2]].tpe
val d3 = implicitly[c.WeakTypeTag[D3]].tpe
val r = implicitly[c.WeakTypeTag[R]].tpe
q"""{
import wvlet.airspec.AirSpecTestBuilder._
val self = ${c.prefix}
val surface = wvlet.airframe.surface.Surface.of[${r}]
val d1 = wvlet.airframe.surface.Surface.of[${d1}]
val d2 = wvlet.airframe.surface.Surface.of[${d2}]
val d3 = wvlet.airframe.surface.Surface.of[${d3}]
self.addF3(d1, d2, d3, surface, ${body})
self.addF3(d1, d2, d3, ${body})
}"""
}

Expand All @@ -86,16 +78,14 @@ private[airspec] object AirSpecMacros {
val d2 = implicitly[c.WeakTypeTag[D2]].tpe
val d3 = implicitly[c.WeakTypeTag[D3]].tpe
val d4 = implicitly[c.WeakTypeTag[D4]].tpe
val r = implicitly[c.WeakTypeTag[R]].tpe
q"""{
import wvlet.airspec.AirSpecTestBuilder._
val self = ${c.prefix}
val surface = wvlet.airframe.surface.Surface.of[${r}]
val d1 = wvlet.airframe.surface.Surface.of[${d1}]
val d2 = wvlet.airframe.surface.Surface.of[${d2}]
val d3 = wvlet.airframe.surface.Surface.of[${d3}]
val d4 = wvlet.airframe.surface.Surface.of[${d4}]
self.addF4(d1, d2, d3, d4, surface, ${body})
self.addF4(d1, d2, d3, d4, ${body})
}"""
}

Expand All @@ -115,17 +105,15 @@ private[airspec] object AirSpecMacros {
val d3 = implicitly[c.WeakTypeTag[D3]].tpe
val d4 = implicitly[c.WeakTypeTag[D4]].tpe
val d5 = implicitly[c.WeakTypeTag[D5]].tpe
val r = implicitly[c.WeakTypeTag[R]].tpe
q"""{
import wvlet.airspec.AirSpecTestBuilder._
val self = ${c.prefix}
val surface = wvlet.airframe.surface.Surface.of[${r}]
val d1 = wvlet.airframe.surface.Surface.of[${d1}]
val d2 = wvlet.airframe.surface.Surface.of[${d2}]
val d3 = wvlet.airframe.surface.Surface.of[${d3}]
val d4 = wvlet.airframe.surface.Surface.of[${d4}]
val d5 = wvlet.airframe.surface.Surface.of[${d5}]
self.addF5(d1, d2, d3, d4, d5, surface, ${body})
self.addF5(d1, d2, d3, d4, d5, ${body})
}"""
}

Expand Down
24 changes: 11 additions & 13 deletions airspec/src/main/scala-2/wvlet/airspec/AirSpecSpiCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,43 @@ class AirSpecTestBuilder(val spec: AirSpecSpi, val name: String, val design: Des

object AirSpecTestBuilder {
implicit class Helper(val v: AirSpecTestBuilder) extends AnyVal {
def addF0[R](r: Surface, body: wvlet.airframe.LazyF0[R]): Unit = {
v.spec.addLocalTestDef(AirSpecDefF0(v.name, v.design, r, body))
def addF0[R](body: wvlet.airframe.LazyF0[R]): Unit = {
v.spec.addLocalTestDef(AirSpecDefF0(v.name, v.design, body))
}
def addF1[D1, R](d1: Surface, r: Surface, body: D1 => R): Unit = {
def addF1[D1, R](d1: Surface, body: D1 => R): Unit = {
val spec = body match {
case s: Seq[_] =>
// Workaround for: https://github.com/wvlet/airframe/issues/1845
AirSpecDefF0(v.name, v.design, r, wvlet.airframe.LazyF0(() => body))
AirSpecDefF0(v.name, v.design, wvlet.airframe.LazyF0(() => body))
case _ =>
AirSpecDefF1(v.name, v.design, d1, r, body)
AirSpecDefF1(v.name, v.design, d1, body)
}
v.spec.addLocalTestDef(spec)
}
def addF2[D1, D2, R](d1: Surface, d2: Surface, r: Surface, body: (D1, D2) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF2(v.name, v.design, d1, d2, r, body))
def addF2[D1, D2, R](d1: Surface, d2: Surface, body: (D1, D2) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF2(v.name, v.design, d1, d2, body))
}
def addF3[D1, D2, D3, R](d1: Surface, d2: Surface, d3: Surface, r: Surface, body: (D1, D2, D3) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF3(v.name, v.design, d1, d2, d3, r, body))
def addF3[D1, D2, D3, R](d1: Surface, d2: Surface, d3: Surface, body: (D1, D2, D3) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF3(v.name, v.design, d1, d2, d3, body))
}
def addF4[D1, D2, D3, D4, R](
d1: Surface,
d2: Surface,
d3: Surface,
d4: Surface,
r: Surface,
body: (D1, D2, D3, D4) => R
): Unit = {
v.spec.addLocalTestDef(AirSpecDefF4(v.name, v.design, d1, d2, d3, d4, r, body))
v.spec.addLocalTestDef(AirSpecDefF4(v.name, v.design, d1, d2, d3, d4, body))
}
def addF5[D1, D2, D3, D4, D5, R](
d1: Surface,
d2: Surface,
d3: Surface,
d4: Surface,
d5: Surface,
r: Surface,
body: (D1, D2, D3, D4, D5) => R
): Unit = {
v.spec.addLocalTestDef(AirSpecDefF5(v.name, v.design, d1, d2, d3, d4, d5, r, body))
v.spec.addLocalTestDef(AirSpecDefF5(v.name, v.design, d1, d2, d3, d4, d5, body))
}
}
}
45 changes: 21 additions & 24 deletions airspec/src/main/scala-3/wvlet/airspec/AirSpecSpiCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,36 @@ class AirSpecTestBuilder(val spec: AirSpecSpi, val name: String, val design: Des
object AirSpecTestBuilder extends wvlet.log.LogSupport {
implicit class Helper(val v: AirSpecTestBuilder) extends AnyVal {

def addF0[R](r: Surface, body: wvlet.airframe.LazyF0[R]): Unit = {
v.spec.addLocalTestDef(AirSpecDefF0(v.name, v.design, r, body))
def addF0[R](body: wvlet.airframe.LazyF0[R]): Unit = {
v.spec.addLocalTestDef(AirSpecDefF0(v.name, v.design, body))
}
def addF1[D1, R](d1: Surface, r: Surface, body: D1 => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF1(v.name, v.design, d1, r, body))
def addF1[D1, R](d1: Surface, body: D1 => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF1(v.name, v.design, d1, body))
}
def addF2[D1, D2, R](d1: Surface, d2: Surface, r: Surface, body: (D1, D2) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF2(v.name, v.design, d1, d2, r, body))
def addF2[D1, D2, R](d1: Surface, d2: Surface, body: (D1, D2) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF2(v.name, v.design, d1, d2, body))
}
def addF3[D1, D2, D3, R](d1: Surface, d2: Surface, d3: Surface, r: Surface, body: (D1, D2, D3) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF3(v.name, v.design, d1, d2, d3, r, body))
def addF3[D1, D2, D3, R](d1: Surface, d2: Surface, d3: Surface, body: (D1, D2, D3) => R): Unit = {
v.spec.addLocalTestDef(AirSpecDefF3(v.name, v.design, d1, d2, d3, body))
}
def addF4[D1, D2, D3, D4, R](
d1: Surface,
d2: Surface,
d3: Surface,
d4: Surface,
r: Surface,
body: (D1, D2, D3, D4) => R
): Unit = {
v.spec.addLocalTestDef(AirSpecDefF4(v.name, v.design, d1, d2, d3, d4, r, body))
v.spec.addLocalTestDef(AirSpecDefF4(v.name, v.design, d1, d2, d3, d4, body))
}
def addF5[D1, D2, D3, D4, D5, R](
d1: Surface,
d2: Surface,
d3: Surface,
d4: Surface,
d5: Surface,
r: Surface,
body: (D1, D2, D3, D4, D5) => R
): Unit = {
v.spec.addLocalTestDef(AirSpecDefF5(v.name, v.design, d1, d2, d3, d4, d5, r, body))
v.spec.addLocalTestDef(AirSpecDefF5(v.name, v.design, d1, d2, d3, d4, d5, body))
}
}
}
Expand All @@ -80,13 +78,13 @@ private[airspec] object AirSpecMacros {
def test0Impl[R](self: Expr[AirSpecTestBuilder], body: Expr[R])(using Type[R], Quotes): Expr[Unit] = {
'{
import AirSpecTestBuilder._
${ self }.addF0(Surface.of[R], LazyF0(${ body }))
${ self }.addF0(LazyF0(${ body }))
}
}

def test1Impl[D1, R](self: Expr[AirSpecTestBuilder], body: Expr[D1 => R])(using
Type[R],
Type[D1],
Type[R],
Quotes
): Expr[Unit] = {
import quotes.reflect.*
Expand All @@ -96,50 +94,50 @@ private[airspec] object AirSpecMacros {
case '{ $x: t } if TypeRepr.of[t] =:= TypeRepr.of[Nil.type] || TypeRepr.of[t] <:< TypeRepr.of[Seq[_]] =>
'{
import AirSpecTestBuilder._
${ self }.addF0(Surface.of[R], LazyF0.apply(${ body }))
${ self }.addF0(LazyF0.apply(${ body }))
}
case _ =>
'{
import AirSpecTestBuilder._
${ self }.addF1(Surface.of[D1], Surface.of[R], ${ body })
${ self }.addF1(Surface.of[D1], ${ body })
}
}
}

def test2Impl[D1, D2, R](
self: Expr[AirSpecTestBuilder],
body: Expr[(D1, D2) => R]
)(using Type[R], Type[D1], Type[D2], Quotes): Expr[Unit] = {
)(using Type[D1], Type[D2], Type[R], Quotes): Expr[Unit] = {
'{
import AirSpecTestBuilder._
${ self }.addF2(Surface.of[D1], Surface.of[D2], Surface.of[R], ${ body })
${ self }.addF2(Surface.of[D1], Surface.of[D2], ${ body })
}
}

def test3Impl[D1, D2, D3, R](
self: Expr[AirSpecTestBuilder],
body: Expr[(D1, D2, D3) => R]
)(using Type[R], Type[D1], Type[D2], Type[D3], Quotes): Expr[Unit] = {
)(using Type[D1], Type[D2], Type[D3], Type[R], Quotes): Expr[Unit] = {
'{
import AirSpecTestBuilder._
${ self }.addF3(Surface.of[D1], Surface.of[D2], Surface.of[D3], Surface.of[R], ${ body })
${ self }.addF3(Surface.of[D1], Surface.of[D2], Surface.of[D3], ${ body })
}
}

def test4Impl[D1, D2, D3, D4, R](
self: Expr[AirSpecTestBuilder],
body: Expr[(D1, D2, D3, D4) => R]
)(using Type[R], Type[D1], Type[D2], Type[D3], Type[D4], Quotes): Expr[Unit] = {
)(using Type[D1], Type[D2], Type[D3], Type[D4], Type[R], Quotes): Expr[Unit] = {
'{
import AirSpecTestBuilder._
${ self }.addF4(Surface.of[D1], Surface.of[D2], Surface.of[D3], Surface.of[D4], Surface.of[R], ${ body })
${ self }.addF4(Surface.of[D1], Surface.of[D2], Surface.of[D3], Surface.of[D4], ${ body })
}
}

def test5Impl[D1, D2, D3, D4, D5, R](
self: Expr[AirSpecTestBuilder],
body: Expr[(D1, D2, D3, D4, D5) => R]
)(using Type[R], Type[D1], Type[D2], Type[D3], Type[D4], Type[D5], Quotes): Expr[Unit] = {
)(using Type[D1], Type[D2], Type[D3], Type[D4], Type[D5], Type[R], Quotes): Expr[Unit] = {
'{
import AirSpecTestBuilder._
${ self }.addF5(
Expand All @@ -148,7 +146,6 @@ private[airspec] object AirSpecMacros {
Surface.of[D3],
Surface.of[D4],
Surface.of[D5],
Surface.of[R],
${ body }
)
}
Expand Down
6 changes: 0 additions & 6 deletions airspec/src/main/scala/wvlet/airspec/AirSpecDef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private[airspec] case class MethodAirSpecDef(methodSurface: MethodSurface) exten
private[airspec] case class AirSpecDefF0[R](
name: String,
design: Design => Design,
returnType: Surface,
body: LazyF0[R]
) extends AirSpecDef {
override def run(context: AirSpecContext, session: Session): Any = {
Expand All @@ -63,7 +62,6 @@ private[airspec] case class AirSpecDefF1[D1, R](
name: String,
design: Design => Design,
dep1Type: Surface,
returnType: Surface,
body: D1 => R
) extends AirSpecDef {

Expand All @@ -78,7 +76,6 @@ private[airspec] case class AirSpecDefF2[D1, D2, R](
design: Design => Design,
dep1Type: Surface,
dep2Type: Surface,
returnType: Surface,
body: (D1, D2) => R
) extends AirSpecDef {

Expand All @@ -95,7 +92,6 @@ private[airspec] case class AirSpecDefF3[D1, D2, D3, R](
dep1Type: Surface,
dep2Type: Surface,
dep3Type: Surface,
returnType: Surface,
body: (D1, D2, D3) => R
) extends AirSpecDef {

Expand All @@ -114,7 +110,6 @@ private[airspec] case class AirSpecDefF4[D1, D2, D3, D4, R](
dep2Type: Surface,
dep3Type: Surface,
dep4Type: Surface,
returnType: Surface,
body: (D1, D2, D3, D4) => R
) extends AirSpecDef {

Expand All @@ -135,7 +130,6 @@ private[airspec] case class AirSpecDefF5[D1, D2, D3, D4, D5, R](
dep3Type: Surface,
dep4Type: Surface,
dep5Type: Surface,
returnType: Surface,
body: (D1, D2, D3, D4, D5) => R
) extends AirSpecDef {

Expand Down

0 comments on commit 0b67070

Please sign in to comment.