Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satorg committed Oct 13, 2024
1 parent 7d6fe2f commit 7c89b8f
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 0 deletions.
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
)
}
}
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
)
}
}
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))
}
}
}

0 comments on commit 7c89b8f

Please sign in to comment.