From f770b01c86e4a549e4e6aa83436f98bc7b2d6a41 Mon Sep 17 00:00:00 2001 From: Naeim Taheri Date: Sun, 29 Sep 2024 10:47:02 -0700 Subject: [PATCH] test: RequireAllSourceFieldsUsed validation in product-to-product module --- .../chimney/TotalTransformerProductSpec.scala | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/chimney/src/test/scala/io/scalaland/chimney/TotalTransformerProductSpec.scala b/chimney/src/test/scala/io/scalaland/chimney/TotalTransformerProductSpec.scala index a5b807c24..9e92dff1d 100644 --- a/chimney/src/test/scala/io/scalaland/chimney/TotalTransformerProductSpec.scala +++ b/chimney/src/test/scala/io/scalaland/chimney/TotalTransformerProductSpec.scala @@ -519,6 +519,91 @@ class TotalTransformerProductSpec extends ChimneySpec { } } + group("""setting .requireSourceFieldsUsedExcept(_.field1, _.field2)""") { + import shapes1.{Point, Rectangle, Triangle} + + test("should fail if not all required source fields are used") { + compileErrors( + """ + Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0)) + .into[Rectangle] + .requireSourceFieldsUsedExcept() + .transform + """ + ).check( + "Chimney can't derive transformation from io.scalaland.chimney.fixtures.shapes1.Triangle to io.scalaland.chimney.fixtures.shapes1.Rectangle", + "source field(s) p3 are required to be used!" + ) + } + + test("should pass if all required source fields are either used or included in exceptions") { + Triangle(p1 = Point(0, 0), p2 = shapes1.Point(2, 2), p3 = shapes1.Point(2, 0)) + .into[Rectangle] + .requireSourceFieldsUsedExcept(_.p3) + .transform ==> Rectangle(p1 = Point(0, 0), p2 = Point(2, 2)) + } + + test("should pass if all required source fields are used (withFieldRenamed)") { + case class AnotherRectangle(p1: Point, PPPP: Point) + + Rectangle(p1 = Point(0, 0), p2 = Point(2, 2)) + .into[AnotherRectangle] + .withFieldRenamed(_.p2, _.PPPP) + .requireSourceFieldsUsedExcept() + .transform ==> AnotherRectangle(p1 = Point(0, 0), PPPP = Point(2, 2)) + } + + test("should pass if all required source fields are used (enableCustomFieldNameComparison)") { + case class AnotherRectangle(p1: Point, P2: Point) + + Rectangle(p1 = Point(0, 0), p2 = Point(2, 2)) + .into[AnotherRectangle] + .requireSourceFieldsUsedExcept() + .enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality) + .transform ==> AnotherRectangle(p1 = Point(0, 0), P2 = Point(2, 2)) + } + + test("should pass if all required source fields are used (withFieldRenamed + enableCustomFieldNameComparison)") { + case class AnotherTriangle(p1: Point, P2: Point, PPPP: Point) + + Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0)) + .into[AnotherTriangle] + .withFieldRenamed(_.p3, _.PPPP) + .requireSourceFieldsUsedExcept() + .enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality) + .transform ==> AnotherTriangle(p1 = Point(0, 0), P2 = Point(2, 2), PPPP = Point(2, 0)) + } + + test("should pass if all required source fields are either used or included in exceptions (withFieldRenamed + enableCustomFieldNameComparison)") { + case class AnotherTriangle(p11111: Point, P2: Point, PPPP: Point) + + // without p1 in exceptions + compileErrors( + """ + Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0)) + .into[AnotherTriangle] + .withFieldRenamed(_.p3, _.PPPP) + .withFieldConst(_.p11111, Point(0, 0)) + .requireSourceFieldsUsedExcept() + .enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality) + .transform ==> AnotherTriangle(p11111 = Point(0, 0), P2 = Point(2, 2), PPPP = Point(2, 0)) + """ + ).check( + "Chimney can't derive transformation from io.scalaland.chimney.fixtures.shapes1.Triangle to io.scalaland.chimney.TotalTransformerProductSpec.AnotherTriangle", + "source field(s) p1 are required to be used!" + ) + + // with p1 in exceptions + Triangle(p1 = Point(0, 0), p2 = Point(2, 2), p3 = Point(2, 0)) + .into[AnotherTriangle] + .withFieldRenamed(_.p3, _.PPPP) + .withFieldConst(_.p11111, Point(0, 0)) + .requireSourceFieldsUsedExcept(_.p1) + .enableCustomFieldNameComparison(TransformedNamesComparison.CaseInsensitiveEquality) + .transform ==> AnotherTriangle(p11111 = Point(0, 0), P2 = Point(2, 2), PPPP = Point(2, 0)) + } + } + group("flag .enableDefaultValues") { test("should be disabled by default") {