Skip to content

Commit

Permalink
Bring back some tests. Remove unnecessary TODO. Add clues to assertio…
Browse files Browse the repository at this point in the history
…ns. Add additional tests for recursive types.
  • Loading branch information
katlasik committed May 15, 2023
1 parent 9986585 commit 27b3822
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/examples/patch.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ sealed abstract class Patcher[T]:
*/
def patch(value: T, fieldValues: Seq[Any]): T

object Patcher extends LowerPriorityPatcher with Derivation[Patcher]:
object Patcher extends LowerPriorityPatcher with AutoDerivation[Patcher]:
def join[T](ctx: CaseClass[Patcher, T]): Patcher[T] =
new Patcher[T]:
def patch(value: T, fieldValues: Seq[Any]): T =
Expand Down Expand Up @@ -48,4 +48,4 @@ sealed abstract class LowerPriorityPatcher:
head
}

given forSingleValue[T]: Patcher[T] = _forSingleValue.asInstanceOf[Patcher[T]]
def forSingleValue[T]: Patcher[T] = _forSingleValue.asInstanceOf[Patcher[T]]
26 changes: 13 additions & 13 deletions src/test/AnnotationsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class AnnotationsTests extends munit.FunSuite:

test("show the scala.deprecated annotation on a field") {
val res = summon[Show[String, Deprecated]].show(Deprecated(10))
assert(res.contains("MyAnnotation(0)"))
assert(res.contains("scala.deprecated"))
assert(clue(res).contains("MyAnnotation(0)"))
assert(clue(res).contains("scala.deprecated"))
}

test("inherit annotations from parent trait") {
Expand Down Expand Up @@ -74,17 +74,17 @@ class AnnotationsTests extends munit.FunSuite:
assertEquals(subtypeAnnotations(1).mkString, "MyAnnotation(2)")
}

// TODO - not compiling
// test("serialize case class with Java annotations by skipping them") {
// val res = Show.derived[MyDto].show(MyDto("foo", 42))
// assertEquals(res, "MyDto{MyAnnotation(0)}(foo=foo,bar=42)")
// }

// TODO - not compiling
// test("serialize case class with Java annotations which comes from external module by skipping them") {
// val res = Show.derived[JavaAnnotatedCase].show(JavaAnnotatedCase(1))
// assertEquals(res, "JavaAnnotatedCase(v=1)")
// }
// TODO - not compiling
// test("serialize case class with Java annotations by skipping them") {
// val res = Show.derived[MyDto].show(MyDto("foo", 42))
// assertEquals(res, "MyDto{MyAnnotation(0)}(foo=foo,bar=42)")
// }

// TODO - not compiling
// test("serialize case class with Java annotations which comes from external module by skipping them") {
// val res = Show.derived[JavaAnnotatedCase].show(JavaAnnotatedCase(1))
// assertEquals(res, "JavaAnnotatedCase(v=1)")
// }

object AnnotationsTests:

Expand Down
91 changes: 46 additions & 45 deletions src/test/OtherTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OtherTests extends munit.FunSuite:
Show.derived[Beta]
""")
assert(
error contains "No given instance of type magnolia1.examples.Show[String, Alpha] was found."
clue(error) contains "No given instance of type magnolia1.examples.Show[String, Alpha] was found."
)
}

Expand All @@ -25,14 +25,16 @@ class OtherTests extends munit.FunSuite:
Show.derived[Gamma]
""")
assert(
error contains "No given instance of type magnolia1.examples.Show[String, Unit] was found."
clue(error) contains "No given instance of type magnolia1.examples.Show[String, Unit] was found."
)
}

test("not attempt to derive instances for refined types") {
val error = compileErrors("Show.derived[Character]")
assert(
error contains "No given instance of type magnolia1.examples.Show[String, Long & magnolia1.tests.OtherTests.Character.Tag] was found."
clue(
error
) contains "No given instance of type magnolia1.examples.Show[String, Long & magnolia1.tests.OtherTests.Character.Tag] was found."
)
}

Expand All @@ -41,53 +43,52 @@ class OtherTests extends munit.FunSuite:
assert(error.isEmpty)
}

// TODO - not working: "Maximal number of successive inlines (32) exceeded"
// TODO - not working: "Maximal number of successive inlines (32) exceeded"
// test("not attempt to derive instances for Java enums") {
// val error = compileErrors("Show.derived[WeekDay]")
// assert(error contains "No given instance of type deriving.Mirror.Of[magnolia1.tests.WeekDay] was found for parameter x$1 of method derived in trait Derivation.")
// }

// TODO - not working: java.lang.IllegalArgumentException: Cannot patch single value `Person(Bob,42)` with patch sequence of size 2
// test("patch a Person via a Patcher[Entity]") {
// given Patcher[String] = Patcher.forSingleValue[String]
// given Patcher[Int] = Patcher.forSingleValue[Int]
// val person = Person("Bob", 42)
// val res = summon[Patcher[Entity]].patch(person, Seq(null, 21))

// assertEquals(res, Person("Bob", 21))
// }

// TODO - not wworking: Cannot patch single value `Person(Bob,42)` with patch sequence of size 3
// test("throw on an illegal patch attempt with field count mismatch") {
// // these two implicits can be removed once https://github.com/propensive/magnolia/issues/58 is closed
// given Patcher[String] = Patcher.forSingleValue[String]
// given Patcher[Int] = Patcher.forSingleValue[Int]

// val res = try {
// val person = Person("Bob", 42)
// summon[Patcher[Entity]].patch(person, Seq(null, 21, "killer"))
// } catch {
// case NonFatal(e) => e.getMessage
// }
// assertEquals(res, "Cannot patch value `Person(Bob,42)`, expected 2 fields but got 3")
// }

// TODO - not working: Cannot patch single value `Person(Bob,42)` with patch sequence of size 2
// test("throw on an illegal patch attempt with field type mismatch") {
// // these two implicits can be removed once https://github.com/propensive/magnolia/issues/58 is closed
// given Patcher[String] = Patcher.forSingleValue[String]
// given Patcher[Int] = Patcher.forSingleValue[Int]

// val res = try {
// val person = Person("Bob", 42)
// summon[Patcher[Entity]].patch(person, Seq(null, "killer"))
// "it worked"
// } catch {
// case NonFatal(e) => e.getMessage
// }
// assert(res.contains("scala.Symbol cannot be cast to"))
// assert(res.contains("java.lang.Integer"))
// }
test("patch a Person via a Patcher[Entity]") {
given Patcher[String] = Patcher.forSingleValue[String]
given Patcher[Int] = Patcher.forSingleValue[Int]
val person = Person("Bob", 42)
val res = summon[Patcher[Entity]].patch(person, Seq(null, 21))

assertEquals(res, Person("Bob", 21))
}

test("throw on an illegal patch attempt with field count mismatch") {
// these two implicits can be removed once https://github.com/propensive/magnolia/issues/58 is closed
given Patcher[String] = Patcher.forSingleValue[String]
given Patcher[Int] = Patcher.forSingleValue[Int]

val res =
try {
val person = Person("Bob", 42)
summon[Patcher[Entity]].patch(person, Seq(null, 21, "killer"))
} catch {
case NonFatal(e) => e.getMessage
}
assertEquals(res, "Cannot patch value `Person(Bob,42)`, expected 2 fields but got 3")
}

// TODO - test hanging
// test("throw on an illegal patch attempt with field type mismatch") {
// // these two implicits can be removed once https://github.com/propensive/magnolia/issues/58 is closed
// given Patcher[String] = Patcher.forSingleValue[String]
// given Patcher[Int] = Patcher.forSingleValue[Int]
//
// val res = try {
// val person = Person("Bob", 42)
// summon[Patcher[Entity]].patch(person, Seq(null, "killer"))
// "it worked"
// } catch {
// case NonFatal(e) => e.getMessage
// }
// assert(res.contains("java.lang.String cannot be cast to"))
// assert(res.contains("java.lang.Integer"))
// }

object OtherTests:
case class Character(id: Character.Id)
Expand Down
4 changes: 2 additions & 2 deletions src/test/ProductsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ class ProductsTests extends munit.FunSuite:
test("show chained error stack when leaf instance is missing") {
val error = compileErrors("Show.derived[Schedule]")
assert(
error contains "No given instance of type magnolia1.examples.Show[String, Seq[magnolia1.tests.ProductsTests.Event]] was found."
clue(error) contains "No given instance of type magnolia1.examples.Show[String, Seq[magnolia1.tests.ProductsTests.Event]] was found."
)
}

test("show chained error stack") {
val error = compileErrors("Show.derived[(Int, Seq[(Double, String)])]")
assert(
error contains "No given instance of type magnolia1.examples.Show[String, Seq[(Double, String)]] was found."
clue(error) contains "No given instance of type magnolia1.examples.Show[String, Seq[(Double, String)]] was found."
)
}

Expand Down
18 changes: 17 additions & 1 deletion src/test/RecursiveTypesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ class RecursiveTypesTests extends munit.FunSuite:
| Seq[magnolia1.tests.RecursiveTypesTests.Recursive]
|] was found.
|""".stripMargin
assert(error contains expectedError)
assert(clue(error) contains expectedError)
}

test("serialize a CeList") {
val printResult = summon[Print[CeList]].print(CeColon(3, CeColon(2, CeNil(2))))
val showResult = summon[Show[String, CeList]].show(CeColon(3, CeColon(2, CeNil(2))))

assert(clue(printResult) == "CeColon(3,CeColon(2,CeNil(2)))")
assert(clue(showResult) == "CeColon(head=3,tail=CeColon(head=2,tail=CeNil(head=2)))")
}

object RecursiveTypesTests:
Expand All @@ -120,3 +128,11 @@ object RecursiveTypesTests:

case class KArray(value: List[KArray]) derives Eq
case class Wrapper(v: Option[KArray])

sealed trait CeList
case class CeColon(head: Int, tail: CeList) extends CeList
case class CeNil(head: Int) extends CeList

object CeList:
given Show[String, CeList] = Show.derived
given Print[CeList] = Print.derived[CeList]
12 changes: 9 additions & 3 deletions src/test/SumsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ class SumsTests extends munit.FunSuite:
) {
val error = compileErrors("Show.derived[Parent]")
assert(
error contains "No given instance of type deriving.Mirror.Of[magnolia1.tests.SumsTests.Parent] was found for parameter x$1 of method derived in trait Derivation."
clue(
error
) contains "No given instance of type deriving.Mirror.Of[magnolia1.tests.SumsTests.Parent] was found for parameter x$1 of method derived in trait Derivation."
)
assert(
error contains "trait Parent is not a generic sum because its child trait BadChild is not a generic product because it is not a case class"
clue(
error
) contains "trait Parent is not a generic sum because its child trait BadChild is not a generic product because it is not a case class"
)
}

Expand All @@ -182,7 +186,9 @@ class SumsTests extends munit.FunSuite:
) {
val error = compileErrors("Show.derived[GoodChild]")
assert(
error contains "trait GoodChild is not a generic sum because its child class Dewey is not a generic product because it is not a case class"
clue(
error
) contains "trait GoodChild is not a generic sum because its child class Dewey is not a generic product because it is not a case class"
)
}

Expand Down

0 comments on commit 27b3822

Please sign in to comment.