From 23b90aedeafda6cd969464dcf66dffd8f8087ab3 Mon Sep 17 00:00:00 2001 From: Mateusz Kubuszok Date: Thu, 16 Jan 2025 11:03:08 +0100 Subject: [PATCH] Test recursion and using implicits in nested fields during patching --- .../chimney/PatcherProductSpec.scala | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/chimney/src/test/scala/io/scalaland/chimney/PatcherProductSpec.scala b/chimney/src/test/scala/io/scalaland/chimney/PatcherProductSpec.scala index 9d3dacd6f..903872d07 100644 --- a/chimney/src/test/scala/io/scalaland/chimney/PatcherProductSpec.scala +++ b/chimney/src/test/scala/io/scalaland/chimney/PatcherProductSpec.scala @@ -223,4 +223,32 @@ class PatcherProductSpec extends ChimneySpec { .patch ==> exampleUserWithOptionalField.copy(phone = None) } } + + test("Patcher should work in nested fields") { + case class Foo(a: Int, b: String, c: Double) + case class Bar(c: Double, a: Int) + case class Nested[A](value: A) + + val foo = Nested(Foo(0, "", 0.0)) + val bar = Nested(Bar(10.0, 10)) + + foo.patchUsing(bar) ==> Nested(Foo(10, "", 10.0)) + foo.using(bar).patch ==> Nested(Foo(10, "", 10.0)) + } + + test("Patcher should use implicits in nested fields") { + case class Foo(a: Int, b: String, c: Double) + case class Bar(c: Double, a: Int) + case class Nested[A](value: A) + + val foo = Nested(Foo(0, "", 0.0)) + val bar = Nested(Bar(10.0, 10)) + + implicit val patcher: Patcher[Foo, Bar] = new Patcher[Foo, Bar] { + def patch(obj: Foo, patch: Bar): Foo = Foo(patch.a * 2, obj.b, patch.c * 3) + } + + foo.patchUsing(bar) ==> Nested(Foo(20, "", 30.0)) + foo.using(bar).patch ==> Nested(Foo(20, "", 30.0)) + } }