Skip to content

Commit

Permalink
add additional tests, remove Playground
Browse files Browse the repository at this point in the history
  • Loading branch information
arainko committed Nov 25, 2023
1 parent 9db055e commit c99267a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 233 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private[ducktape] object Planner {
case (source, dest) if source.tpe.repr <:< dest.tpe.repr =>
Plan.Upcast(source.tpe, dest.tpe, sourceContext, destContext)

case Optional(_, srcName, srcParamStruct) -> Optional(_, destName, destParamStruct) =>
case Optional(_, srcParamStruct) -> Optional(_, destParamStruct) =>
Plan.BetweenOptions(
srcParamStruct.tpe,
destParamStruct.tpe,
Expand All @@ -45,7 +45,7 @@ private[ducktape] object Planner {
recurse(srcParamStruct, destParamStruct, sourceContext, destContext)
)

case struct -> Optional(_, _, paramStruct) =>
case struct -> Optional(_, paramStruct) =>
Plan.BetweenNonOptionOption(
struct.tpe,
paramStruct.tpe,
Expand All @@ -54,7 +54,7 @@ private[ducktape] object Planner {
recurse(struct, paramStruct, sourceContext, destContext)
)

case Collection(_, _, srcParamStruct) -> Collection(destCollTpe, _, destParamStruct) =>
case Collection(_, srcParamStruct) -> Collection(destCollTpe, destParamStruct) =>
Plan.BetweenCollections(
destCollTpe,
srcParamStruct.tpe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ private[ducktape] object Structure {
function: io.github.arainko.ducktape.internal.Function
) extends Structure

case class Optional(tpe: Type[? <: Option[?]], name: String, paramStruct: Structure) extends Structure
case class Optional(tpe: Type[? <: Option[?]], paramStruct: Structure) extends Structure

case class Collection(tpe: Type[? <: Iterable[?]], name: String, paramStruct: Structure) extends Structure
case class Collection(tpe: Type[? <: Iterable[?]], paramStruct: Structure) extends Structure

case class Singleton(tpe: Type[?], name: String, value: Expr[Any]) extends Structure

Expand Down Expand Up @@ -63,15 +63,14 @@ private[ducktape] object Structure {

def of[A: Type](using Quotes): Structure = {
import quotes.reflect.*
given Printer[TypeRepr] = Printer.TypeReprShortCode

Logger.loggedInfo("Structure"):
Type.of[A] match {
case tpe @ '[Option[param]] =>
Structure.Optional(tpe, tpe.repr.show, Structure.of[param])
Structure.Optional(tpe, Structure.of[param])

case tpe @ '[Iterable[param]] =>
Structure.Collection(tpe, tpe.repr.show, Structure.of[param])
Structure.Collection(tpe, Structure.of[param])

case tpe @ '[AnyVal] if tpe.repr.typeSymbol.flags.is(Flags.Case) =>
val repr = tpe.repr
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ trait DucktapeSuite extends FunSuite {
val errors = compiletime.testing.typeCheckErrors(code).map(_.message).toSet
assertEquals(errors, expected.toSet, "Error did not contain expected value")
}

extension [A] (inline self: A) {
inline def code: A = internal.CodePrinter.code(self)

inline def structure: A = internal.CodePrinter.structure(self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,26 @@ class DerivedTransformerSuite extends DucktapeSuite {

val source = Source(1, "")

val expected = Dest(0, "asd")
val expected = Dest(0, "asd")
val actual = source.to[Dest]
assertEquals(actual, expected)
}

test("Transformer.define* can be used to create recursive transformers") {
final case class Rec[+A](value: Int, rec: Option[Rec[A]])

val source = Rec(1, Some(Rec(2, Some(Rec(3, None)))))
val expected = Rec(1, Some(Rec(2, Some(Rec(3, None)))))

locally {
given transformer: Transformer[Rec[Int], Rec[Int | String]] = Transformer.define[Rec[Int], Rec[Int | String]].build()
assertEquals(transformer.transform(source), expected)
}

locally {
given transformer: Transformer[Rec[Int], Rec[Int | String]] = Transformer.defineVia[Rec[Int]](Rec[Int | String]).build()
assertEquals(transformer.transform(source), expected)
}
}

}

0 comments on commit c99267a

Please sign in to comment.