-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Showing
3 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
.../all/shared/src/test/scala/monix/newtypes/integrations/NewsubtypeCirceKeyCodecSuite.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,76 @@ | ||
/* | ||
* Copyright (c) 2021-2024 Alexandru Nedelcu. | ||
* See the project homepage at: https://newtypes.monix.io/ | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package monix.newtypes | ||
package integrations | ||
|
||
import io.circe.syntax._ | ||
import org.scalatest.EitherValues | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class NewsubtypeCirceKeyCodecSuite extends AnyFunSuite with EitherValues { | ||
import NewsubtypeCirceKeyCodecSuite._ | ||
|
||
test("NewsubtypeWrapped has JSON key codec") { | ||
val originalMap = Map(23459 -> "") | ||
val expectedMap = Map(Text("23459") -> "") | ||
|
||
// Check KeyEncoder. | ||
val expectedJson = originalMap.asJson | ||
val receivedJson = expectedMap.asJson | ||
assert(receivedJson == expectedJson) | ||
|
||
// Check KeyDecoder. | ||
val receivedMap = expectedJson.as[Map[Text, String]] | ||
assert(receivedMap == Right(expectedMap)) | ||
} | ||
|
||
test("NewsubtypeValidated has JSON key codec") { | ||
val originalMap = Map(0x7f -> "") | ||
val expectedMap = PosByte(0x7f).map(k => Map(k -> "")).value | ||
|
||
// Check KeyEncoder. | ||
val expectedJson = originalMap.asJson | ||
val receivedJson = expectedMap.asJson | ||
assert(receivedJson == expectedJson) | ||
|
||
// Check KeyDecoder. | ||
val receivedMap = expectedJson.as[Map[PosByte, String]] | ||
assert(receivedMap == Right(expectedMap)) | ||
} | ||
|
||
// NOTE: Circe does not allow custom error messages in KeyDecoder. | ||
test("NewsubtypeValidated JSON key decoder does validation") { | ||
val received = Map(0 -> "").asJson.as[Map[PosByte, String]] | ||
assert(received.isLeft) | ||
} | ||
} | ||
|
||
object NewsubtypeCirceKeyCodecSuite { | ||
type Text = Text.Type | ||
object Text extends NewsubtypeWrapped[String] with DerivedCirceKeyCodec | ||
|
||
type PosByte = PosByte.Type | ||
object PosByte extends NewsubtypeValidated[Byte] with DerivedCirceKeyCodec { | ||
override def apply(value: Byte): Either[BuildFailure[Type], Type] = | ||
Either.cond( | ||
value > 0, | ||
unsafe(value), | ||
BuildFailure() // message is not used by KeyDecoder anyway | ||
) | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...rce/all/shared/src/test/scala/monix/newtypes/integrations/NewtypeCirceKeyCodecSuite.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,76 @@ | ||
/* | ||
* Copyright (c) 2021-2024 Alexandru Nedelcu. | ||
* See the project homepage at: https://newtypes.monix.io/ | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package monix.newtypes | ||
package integrations | ||
|
||
import io.circe.syntax._ | ||
import org.scalatest.EitherValues | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class NewtypeCirceKeyCodecSuite extends AnyFunSuite with EitherValues { | ||
import NewtypeCirceKeyCodecSuite._ | ||
|
||
test("NewtypeWrapped has JSON key codec") { | ||
val originalMap = Map(12347 -> "") | ||
val expectedMap = Map(Text("12347") -> "") | ||
|
||
// Check KeyEncoder. | ||
val expectedJson = originalMap.asJson | ||
val receivedJson = expectedMap.asJson | ||
assert(receivedJson == expectedJson) | ||
|
||
// Check KeyDecoder. | ||
val receivedMap = expectedJson.as[Map[Text, String]] | ||
assert(receivedMap == Right(expectedMap)) | ||
} | ||
|
||
test("NewtypeValidated has JSON key codec") { | ||
val originalMap = Map(0x7f -> "") | ||
val expectedMap = PosByte(0x7f).map(k => Map(k -> "")).value | ||
|
||
// Check KeyEncoder. | ||
val expectedJson = originalMap.asJson | ||
val receivedJson = expectedMap.asJson | ||
assert(receivedJson == expectedJson) | ||
|
||
// Check KeyDecoder. | ||
val receivedMap = expectedJson.as[Map[PosByte, String]] | ||
assert(receivedMap == Right(expectedMap)) | ||
} | ||
|
||
// NOTE: Circe does not allow custom error messages in KeyDecoder. | ||
test("NewtypeValidated JSON key decoder does validation") { | ||
val received = Map(0 -> "").asJson.as[Map[PosByte, String]] | ||
assert(received.isLeft) | ||
} | ||
} | ||
|
||
object NewtypeCirceKeyCodecSuite { | ||
type Text = Text.Type | ||
object Text extends NewtypeWrapped[String] with DerivedCirceKeyCodec | ||
|
||
type PosByte = PosByte.Type | ||
object PosByte extends NewtypeValidated[Byte] with DerivedCirceKeyCodec { | ||
override def apply(value: Byte): Either[BuildFailure[Type], Type] = | ||
Either.cond( | ||
value > 0, | ||
unsafe(value), | ||
BuildFailure() // message is not used by KeyDecoder anyway | ||
) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...ce/all/shared/src/test/scala/monix/newtypes/integrations/NewtypeKCirceKeyCodecSuite.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,69 @@ | ||
/* | ||
* Copyright (c) 2021-2024 Alexandru Nedelcu. | ||
* See the project homepage at: https://newtypes.monix.io/ | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package monix.newtypes | ||
package integrations | ||
|
||
import cats.Id | ||
import io.circe.syntax._ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class NewtypeKCirceKeyCodecSuite extends AnyFunSuite { | ||
import NewtypeKCirceKeyCodecSuite._ | ||
|
||
test("NewtypeK has JSON key codec") { | ||
val expectedI = Map(NewId(12358) -> "") | ||
val expectedS = Map(NewId("12358") -> "") | ||
|
||
val jsonI = expectedI.asJson | ||
val jsonS = expectedS.asJson | ||
|
||
val receivedI2I = jsonI.as[Map[NewId[Int], String]] | ||
assert(receivedI2I == Right(expectedI)) | ||
|
||
val receivedI2S = jsonI.as[Map[NewId[String], String]] | ||
assert(receivedI2S == Right(expectedS)) | ||
|
||
val receivedS2I = jsonS.as[Map[NewId[Int], String]] | ||
assert(receivedS2I == Right(expectedI)) | ||
|
||
val obtainedS2S = jsonS.as[Map[NewId[String], String]] | ||
assert(obtainedS2S == Right(expectedS)) | ||
} | ||
|
||
// NOTE: Circe does not allow custom error messages in KeyDecoder. | ||
test("NewtypeK JSON key codec does validation") { | ||
val map = Map(NewId("ABCDEFG") -> "") | ||
val json = map.asJson | ||
val received = json.as[Map[NewId[Int], String]] | ||
assert(received.isLeft) | ||
} | ||
} | ||
|
||
object NewtypeKCirceKeyCodecSuite { | ||
type NewId[A] = NewId.Type[A] | ||
|
||
object NewId extends NewtypeK[Id] with DerivedCirceKeyCodec { | ||
def apply[A](a: A): NewId[A] = unsafeCoerce(a) | ||
|
||
implicit def builder[A]: HasBuilder.Aux[Type[A], A] = | ||
new HasBuilder[Type[A]] { | ||
type Source = A | ||
def build(value: A) = Right(apply(value)) | ||
} | ||
} | ||
} |