Skip to content

Commit

Permalink
Update scala3-library to 3.3.0 (#104)
Browse files Browse the repository at this point in the history
* Update scala3-library to 3.3.0

* Revert commit(s) 04cc207

* Update scala3-library to 3.3.0

* Update GenericSpec.scala

---------

Co-authored-by: Scala Steward <scala_steward@virtuslab.com>
  • Loading branch information
custommonkey and scala-steward authored Jun 23, 2023
1 parent f7a99f7 commit 39f4f6a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scala.util.Properties.envOrElse

val scala212 = "2.12.18"
val scala213 = "2.13.11"
val scala3 = "3.1.3"
val scala3 = "3.3.0"

lazy val supportedScalaVersions = List(scala213, scala212, scala3)

Expand Down
58 changes: 38 additions & 20 deletions src/test/scala/us/oyanglul/dhall/GenericSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,46 @@ import munit._
import org.dhallj.syntax._
import org.dhallj.codec.syntax._
import org.dhallj.codec.Decoder._

import java.util.UUID
import Shape._
import Env._
import org.dhallj.core.DhallException
import scala.reflect.ClassTag

class GenericSpec extends FunSuite {


test("generic decode product") {
val Right(expr) = """{width = 1, height = 1.1}""".parseExpr
val Right(decoded: Rectangle) = expr.as[Rectangle]
val expr = """{width = 1, height = 1.1}""".parseExpr.asRight
val decoded = expr.as[Rectangle].asRight
assertEquals(Rectangle(1, 1.1), decoded)
}

test("generic decode empty product") {
val Right(expr) = """{=}""".parseExpr
val Right(decoded) = expr.as[Empty]
val expr = """{=}""".parseExpr.asRight
val decoded = expr.as[Empty].asRight
assertEquals(decoded, Empty())
}
test("generic decode coproduct") {
val Right(expr1) =
val expr1 =
"""let Shape = <Rectangle: {width: Double, height: Double}| Circle: {radius: Double}> in Shape.Circle {radius = 1.1}""".parseExpr
val Right(expr2) =
val expr2 =
"""let Shape = <Rectangle: {width: Double, height: Double}| Circle: {radius: Double}> in Shape.Rectangle {width = 1.1, height = 1.2}""".parseExpr
val Right(decoded1) = expr1.normalize().as[Shape]
val Right(decoded2) = expr2.normalize().as[Shape]
val decoded1 = expr1.flatMap(_.normalize().as[Shape]).asRight
val decoded2 = expr2.flatMap(_.normalize().as[Shape]).asRight
assertEquals(decoded1, Circle(1.1))
assertEquals(decoded2, Rectangle(1.1, 1.2))
}

test("nested coproduct and product") {
import OuterClass._
val Right(expr) =
val expr =
"""|
|let Shape = <Rectangle: {width: Double, height: Double}| Circle: {radius: Double}>
|in {name = "Outer Class", shape = Shape.Circle {radius = 1.2}, uuid = "644BA20E-9C09-4C70-BDEB-8998ED92157B"}
|""".stripMargin.parseExpr
val Right(decoded) = expr.normalize.as[OuterClass]
|""".stripMargin.parseExpr.asRight
val decoded = expr.normalize.as[OuterClass].asRight
assertEquals(
decoded,
OuterClass(
Expand All @@ -51,31 +56,44 @@ class GenericSpec extends FunSuite {
}

test("exception") {
val Right(expr) = "{width = \"asdf\", height = 1.1}".parseExpr
val Left(error) = expr.normalize.as[Circle]
val expr = "{width = \"asdf\", height = 1.1}".parseExpr.asRight
val error = expr.normalize.as[Circle].asLeft
assertEquals(
error.getMessage,
"Error decoding missing key radius in record"
)
}

test("case object") {
val Right(expr) = """{env = <Local | Sit | Prod>.Sit}""".parseExpr
val Right(decoded) = expr.normalize().as[Config]
val expr = """{env = <Local | Sit | Prod>.Sit}""".parseExpr.asRight
val decoded = expr.normalize().as[Config].asRight
assertEquals(decoded, Config(Sit))
}

test("empty case class") {
val Right(expr) = """<Local | Sit | Prod>.Sit""".parseExpr
val Right(decoded) = expr.normalize().as[Env2]
val expr = """<Local | Sit | Prod>.Sit""".parseExpr.asRight
val decoded = expr.normalize().as[Env2].asRight
assertEquals(decoded, Env2.Sit())
}

test("list of Shape") {
val Right(expr) =
"""let Shape = <Rectangle: {width: Double, height: Double}| Circle: {radius: Double}> in {shapes = [Shape.Circle {radius = 1.1}]}""".parseExpr
val Right(decoded) = expr.normalize().as[Shapes]
val expr =
"""let Shape = <Rectangle: {width: Double, height: Double}| Circle: {radius: Double}> in {shapes = [Shape.Circle {radius = 1.1}]}""".parseExpr.asRight
val decoded = expr.normalize().as[Shapes].asRight

assertEquals(decoded, Shapes(List(Circle(1.1))))
}


private implicit class X[A](either: Either[DhallException, A])(implicit classTag: ClassTag[A]) {
def asRight: A = either match {
case Left(err) => fail(s"expected Right[${classTag.runtimeClass.getName}] but was Left($err)")
case Right(value) => value
}

def asLeft: DhallException = either match {
case Left(value) => value
case Right(value) => fail(s"expected Left[DhallException] but was Right($value)")
}
}
}

0 comments on commit 39f4f6a

Please sign in to comment.