From 579cdb1281e7f3e16b13abc591c5e06b1aa32db5 Mon Sep 17 00:00:00 2001 From: Mathias Date: Thu, 14 Dec 2017 14:20:39 +0100 Subject: Add `CaseClass.rawConstruct` and new `Patcher` example --- tests/src/main/scala/tests.scala | 41 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'tests/src/main') diff --git a/tests/src/main/scala/tests.scala b/tests/src/main/scala/tests.scala index e275dee..ca3cc71 100644 --- a/tests/src/main/scala/tests.scala +++ b/tests/src/main/scala/tests.scala @@ -1,13 +1,14 @@ package magnolia.tests import language.experimental.macros - import magnolia._ import estrapade._ import contextual.data.scalac._ import contextual.data.fqt._ import contextual.data.txt._ +import scala.util.control.NonFatal + sealed trait Tree[+T] case class Leaf[+L](value: L) extends Tree[L] case class Branch[+B](left: Tree[B], right: Tree[B]) extends Tree[B] @@ -237,6 +238,44 @@ object Tests extends TestApp { |""") } + test("patch a Person via a Patcher[Entity]") { + // these two implicits can be removed once https://github.com/propensive/magnolia/issues/58 is closed + implicit val stringPatcher = Patcher.forSingleValue[String] + implicit val intPatcher = Patcher.forSingleValue[Int] + + val person = Person("Bob", 42) + implicitly[Patcher[Entity]] + .patch(person, Seq(null, 21)) + }.assert(_ == 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 + implicit val stringPatcher = Patcher.forSingleValue[String] + implicit val intPatcher = Patcher.forSingleValue[Int] + + try { + val person = Person("Bob", 42) + implicitly[Patcher[Entity]] + .patch(person, Seq(null, 21, 'killer)) + } catch { + case NonFatal(e) => e.getMessage + } + }.assert(_ == "Cannot patch value `Person(Bob,42)`, expected 2 fields but got 3") + + 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 + implicit val stringPatcher = Patcher.forSingleValue[String] + implicit val intPatcher = Patcher.forSingleValue[Int] + + try { + val person = Person("Bob", 42) + implicitly[Patcher[Entity]] + .patch(person, Seq(null, 'killer)) + } catch { + case NonFatal(e) => e.getMessage + } + }.assert(_ == "scala.Symbol cannot be cast to java.lang.Integer") + class ParentClass() { case class LocalClass(name: String) -- cgit v1.2.3