-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b17cc21
commit 8ae8524
Showing
5 changed files
with
112 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
chimney-protobufs/src/test/scala/io/scalaland/chimney/ProtobufBuildInSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.scalaland.chimney | ||
|
||
// format: off | ||
import io.scalaland.chimney.dsl._ | ||
// format: on | ||
import io.scalaland.chimney.examples.pb | ||
import io.scalaland.chimney.fixtures.{addressbook, order} | ||
|
||
class ProtobufBuildInSpec extends ChimneySpec { | ||
|
||
test("transform value classes between their primitive representations") { | ||
|
||
addressbook.PersonName("John").transformInto[String] ==> "John" | ||
addressbook.PersonId(5).transformInto[Int] ==> 5 | ||
addressbook.Email("[email protected]").transformInto[String] ==> "[email protected]" | ||
} | ||
|
||
test("not compile if target type is wrong for value class") { | ||
|
||
compileErrorsFixed(""" addressbook.PersonName("John").transformInto[Int] """) | ||
.check( | ||
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonName to scala.Int" | ||
) | ||
|
||
compileErrorsFixed(""" addressbook.PersonId(5).transformInto[String] """) | ||
.check( | ||
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonId to java.lang.String" | ||
) | ||
|
||
compileErrorsFixed(""" addressbook.Email("[email protected]").transformInto[Float] """) | ||
.check( | ||
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.Email to scala.Float" | ||
) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
chimney-protobufs/src/test/scala/io/scalaland/chimney/ProtobufEnumSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.scalaland.chimney | ||
|
||
// format: off | ||
import io.scalaland.chimney.dsl._ | ||
// format: on | ||
import io.scalaland.chimney.examples.pb | ||
import io.scalaland.chimney.fixtures.{addressbook, order} | ||
|
||
class ProtobufEnumSpec extends ChimneySpec { | ||
|
||
test("transform enum represented as sealed trait hierarchy") { | ||
|
||
(addressbook.MOBILE: addressbook.PhoneType) | ||
.transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.MOBILE | ||
(addressbook.HOME: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.HOME | ||
(addressbook.WORK: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.WORK | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,40 +6,7 @@ import io.scalaland.chimney.dsl._ | |
import io.scalaland.chimney.examples.pb | ||
import io.scalaland.chimney.fixtures.{addressbook, order} | ||
|
||
class PBTransformationSpec extends ChimneySpec { | ||
|
||
test("transform value classes between their primitive representations") { | ||
|
||
addressbook.PersonName("John").transformInto[String] ==> "John" | ||
addressbook.PersonId(5).transformInto[Int] ==> 5 | ||
addressbook.Email("[email protected]").transformInto[String] ==> "[email protected]" | ||
} | ||
|
||
test("not compile if target type is wrong for value class") { | ||
|
||
compileErrorsFixed(""" addressbook.PersonName("John").transformInto[Int] """) | ||
.check( | ||
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonName to scala.Int" | ||
) | ||
|
||
compileErrorsFixed(""" addressbook.PersonId(5).transformInto[String] """) | ||
.check( | ||
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.PersonId to java.lang.String" | ||
) | ||
|
||
compileErrorsFixed(""" addressbook.Email("[email protected]").transformInto[Float] """) | ||
.check( | ||
"Chimney can't derive transformation from io.scalaland.chimney.fixtures.addressbook.Email to scala.Float" | ||
) | ||
} | ||
|
||
test("transform enum represented as sealed trait hierarchy") { | ||
|
||
(addressbook.MOBILE: addressbook.PhoneType) | ||
.transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.MOBILE | ||
(addressbook.HOME: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.HOME | ||
(addressbook.WORK: addressbook.PhoneType).transformInto[pb.addressbook.PhoneType] ==> pb.addressbook.PhoneType.WORK | ||
} | ||
class ProtobufMessageSpec extends ChimneySpec { | ||
|
||
group("transform bigger case classes") { | ||
|
||
|
@@ -172,51 +139,4 @@ class PBTransformationSpec extends ChimneySpec { | |
} | ||
} | ||
} | ||
|
||
group("transformer sealed traits generated from oneof") { | ||
|
||
test("AddressBookType (oneof value - sealed contains single-value wrappers around actual products)") { | ||
val domainType: addressbook.AddressBookType = addressbook.AddressBookType.Private("test") | ||
val pbType: pb.addressbook.AddressBookType = | ||
pb.addressbook.AddressBookType.of( | ||
pb.addressbook.AddressBookType.Value.Private(pb.addressbook.AddressBookType.Private.of("test")) | ||
) | ||
|
||
domainType.into[pb.addressbook.AddressBookType.Value].transform ==> pbType.value | ||
|
||
pbType.value | ||
.intoPartial[addressbook.AddressBookType] | ||
.withCoproductInstancePartial[pb.addressbook.AddressBookType.Value.Empty.type](_ => partial.Result.fromEmpty) | ||
.transform | ||
.asOption ==> Some(domainType) | ||
locally { | ||
// format: off | ||
import protobufs._ | ||
// format: on | ||
pbType.value.intoPartial[addressbook.AddressBookType].transform.asOption ==> Some(domainType) | ||
} | ||
} | ||
|
||
test("CustomerStatus (oneof sealed_value - flat representation with additional Empty vase)") { | ||
val domainStatus: order.CustomerStatus = order.CustomerStatus.CustomerRegistered | ||
val pbStatus: pb.order.CustomerStatus = pb.order.CustomerRegistered() | ||
|
||
domainStatus.into[pb.order.CustomerStatus].transform ==> pbStatus | ||
|
||
pbStatus | ||
.intoPartial[order.CustomerStatus] | ||
.withCoproductInstancePartial[pb.order.CustomerStatus.Empty.type](_ => partial.Result.fromEmpty) | ||
.withCoproductInstance[pb.order.CustomerStatus.NonEmpty](_.transformInto[order.CustomerStatus]) | ||
.transform | ||
.asOption ==> Some(domainStatus) | ||
} | ||
|
||
test("PaymentStatus (oneof sealed_value_optional - flat representation wrapped in Option)") { | ||
val domainStatus: Option[order.PaymentStatus] = Option(order.PaymentStatus.PaymentRequested) | ||
val pbStatus: Option[pb.order.PaymentStatus] = Option(pb.order.PaymentRequested()) | ||
|
||
domainStatus.into[Option[pb.order.PaymentStatus]].transform ==> pbStatus | ||
pbStatus.into[Option[order.PaymentStatus]].transform ==> domainStatus | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
chimney-protobufs/src/test/scala/io/scalaland/chimney/ProtobufOneOfSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.scalaland.chimney | ||
|
||
// format: off | ||
import io.scalaland.chimney.dsl._ | ||
// format: on | ||
import io.scalaland.chimney.examples.pb | ||
import io.scalaland.chimney.fixtures.{addressbook, order} | ||
|
||
class ProtobufOneOfSpec extends ChimneySpec { | ||
|
||
group("transformer sealed traits generated from oneof") { | ||
|
||
test("AddressBookType (oneof value - sealed contains single-value wrappers around actual products)") { | ||
val domainType: addressbook.AddressBookType = addressbook.AddressBookType.Private("test") | ||
val pbType: pb.addressbook.AddressBookType = | ||
pb.addressbook.AddressBookType.of( | ||
pb.addressbook.AddressBookType.Value.Private(pb.addressbook.AddressBookType.Private.of("test")) | ||
) | ||
|
||
domainType.into[pb.addressbook.AddressBookType.Value].transform ==> pbType.value | ||
|
||
pbType.value | ||
.intoPartial[addressbook.AddressBookType] | ||
.withCoproductInstancePartial[pb.addressbook.AddressBookType.Value.Empty.type](_ => partial.Result.fromEmpty) | ||
.transform | ||
.asOption ==> Some(domainType) | ||
locally { | ||
// format: off | ||
import protobufs._ | ||
// format: on | ||
pbType.value.intoPartial[addressbook.AddressBookType].transform.asOption ==> Some(domainType) | ||
} | ||
} | ||
|
||
test("CustomerStatus (oneof sealed_value - flat representation with additional Empty vase)") { | ||
val domainStatus: order.CustomerStatus = order.CustomerStatus.CustomerRegistered | ||
val pbStatus: pb.order.CustomerStatus = pb.order.CustomerRegistered() | ||
|
||
domainStatus.into[pb.order.CustomerStatus].transform ==> pbStatus | ||
|
||
pbStatus | ||
.intoPartial[order.CustomerStatus] | ||
.withCoproductInstancePartial[pb.order.CustomerStatus.Empty.type](_ => partial.Result.fromEmpty) | ||
.withCoproductInstance[pb.order.CustomerStatus.NonEmpty](_.transformInto[order.CustomerStatus]) | ||
.transform | ||
.asOption ==> Some(domainStatus) | ||
} | ||
|
||
test("PaymentStatus (oneof sealed_value_optional - flat representation wrapped in Option)") { | ||
val domainStatus: Option[order.PaymentStatus] = Option(order.PaymentStatus.PaymentRequested) | ||
val pbStatus: Option[pb.order.PaymentStatus] = Option(pb.order.PaymentRequested()) | ||
|
||
domainStatus.into[Option[pb.order.PaymentStatus]].transform ==> pbStatus | ||
pbStatus.into[Option[order.PaymentStatus]].transform ==> domainStatus | ||
} | ||
} | ||
} |