diff --git a/build.sc b/build.sc index 182ca047..a159c4b6 100644 --- a/build.sc +++ b/build.sc @@ -105,12 +105,12 @@ trait MorphirModule extends Cross.Module[String] with CrossPlatform { object sdk extends Module { object core extends CrossPlatform with CrossValue { trait Shared extends MorphirCommonCrossModule with MorphirPublishModule { - def ivyDeps = Agg( - Deps.org.`scala-lang`.modules.`scala-collection-compat`, - Deps.com.`47Deg`.memeid4s - ) + def ivyDeps = Agg(Deps.org.`scala-lang`.modules.`scala-collection-compat`) } object jvm extends Shared with MorphirJVMModule { + def ivyDeps = super.ivyDeps() ++ Agg( + Deps.com.`47Deg`.memeid4s + ) object test extends ScalaTests with CommonZioTestModule } diff --git a/morphir/sdk/core/src/morphir/sdk/UUID.scala b/morphir/sdk/core/jvm/src/UUID.scala similarity index 89% rename from morphir/sdk/core/src/morphir/sdk/UUID.scala rename to morphir/sdk/core/jvm/src/UUID.scala index 1fdd7991..626a97f4 100644 --- a/morphir/sdk/core/src/morphir/sdk/UUID.scala +++ b/morphir/sdk/core/jvm/src/UUID.scala @@ -1,10 +1,11 @@ -package morphir.sdk +import memeid4s.UUID as MUUID import scala.util.Try -import memeid4s.{ UUID => MUUID } object UUID { - val Nil: UUID = memeid4s.UUID.Nil + type UUID = MUUID + + val Nil: UUID = MUUID.Nil type V1 = MUUID.V1 @@ -29,10 +30,6 @@ object UUID { def random: UUID = MUUID.V4.random } - def unapply(str: String): Option[UUID] = - if (str.nonEmpty) UUID.from(str).toOption - else None - /** Creates a valid [[UUID]] from two [[_root_.scala.Long Long]] values representing the most/least significant bits. * * @param msb diff --git a/morphir/sdk/core/src/morphir/sdk/package.scala b/morphir/sdk/core/src/morphir/sdk/package.scala index 62506775..9aa0421d 100644 --- a/morphir/sdk/core/src/morphir/sdk/package.scala +++ b/morphir/sdk/core/src/morphir/sdk/package.scala @@ -19,5 +19,4 @@ package morphir package object sdk { val StringModule: StringModule = String type StringModule = String.type - type UUID = memeid4s.UUID } diff --git a/morphir/sdk/core/test/jvm/src/UUIDSpec.scala b/morphir/sdk/core/test/jvm/src/UUIDSpec.scala new file mode 100644 index 00000000..b7323344 --- /dev/null +++ b/morphir/sdk/core/test/jvm/src/UUIDSpec.scala @@ -0,0 +1,82 @@ +import morphir.testing.MorphirBaseSpec +import zio.test.* +import zio.test.Assertion.* +import memeid4s.{ UUID => MUUID } + +object UUIDSpec extends MorphirBaseSpec { + def spec = suite("UUID Tests")( + test("Generate UUID from valid String") { + val uuid = UUID.V4.random + assert(UUID.from(uuid.toString))(isRight(equalTo(uuid))) + }, + test("Return Throwable from invalid String") { + assert(UUID.from("0f769c4185a634208b09bb63bce12014"))(isLeft) + }, + test("UUID from msb and lsb") { + check(Gen.long) { (msb) => + assertEquals(UUID.from(msb, 0xc000000000000000L), MUUID.from(msb, 0xc000000000000000L)) + } + }, + suite("V1 UUID")( + test("Generate unique V1 UUIDs") { + assert((1 to 999).map(_ => UUID.V1.next()).toSet.size)(equalTo(999)) + } + ), + suite("V3 UUID")( + test("Generate same V3 UUID for same namespace and name") { + val namespace = UUID.V4.random + val name = "Test Name!" + val u1 = UUID.V3(namespace, name) + val u2 = UUID.V3(namespace, name) + assertEquals(u1, u2) + }, + test("Generate unique V3 UUID for unique namespace") { + val namespace1 = UUID.V4.random + val namespace2 = UUID.V4.random + val name = "Test Name!" + val u1 = UUID.V3(namespace1, name) + val u2 = UUID.V3(namespace2, name) + assertTrue(u1 != u2) + }, + test("Generate unique V3 UUID for unique names") { + val namespace = UUID.V4.random + val name1 = "Test Name!" + val name2 = "Test Name 2!" + val u1 = UUID.V3(namespace, name1) + val u2 = UUID.V3(namespace, name2) + assertTrue(u1 != u2) + } + ), + suite("V4 UUID")( + test("Generate Uniquely Random V4 UUIDs") { + assert((1 to 999).map(_ => UUID.V4.random).toSet.size)(equalTo(999)) + } + ), + suite("V5 UUID")( + test("Generate same V5 UUID for same namespace and name") { + val namespace = UUID.V4.random + val name = "Test Name!" + val u1 = UUID.V5(namespace, name) + val u2 = UUID.V5(namespace, name) + assertEquals(u1, u2) + }, + test("Generate unique V5 UUID for unique namespace") { + val namespace1 = UUID.V4.random + val namespace2 = UUID.V4.random + val name = "Test Name!" + val u1 = UUID.V5(namespace1, name) + val u2 = UUID.V5(namespace2, name) + assertTrue(u1 != u2) + }, + test("Generate unique V5 UUID for unique names") { + val namespace = UUID.V4.random + val name1 = "Test Name!" + val name2 = "Test Name 2!" + val u1 = UUID.V5(namespace, name1) + val u2 = UUID.V5(namespace, name2) + assertTrue(u1 != u2) + } + ) + ) + +} diff --git a/morphir/sdk/core/test/src/morphir/sdk/UUIDSpec.scala b/morphir/sdk/core/test/src/morphir/sdk/UUIDSpec.scala deleted file mode 100644 index d93b47f5..00000000 --- a/morphir/sdk/core/test/src/morphir/sdk/UUIDSpec.scala +++ /dev/null @@ -1,25 +0,0 @@ -package morphir.sdk - -import morphir.testing.MorphirBaseSpec -import zio.test._ -import zio.test.Assertion._ - -object UUIDSpec extends MorphirBaseSpec { - def spec = suite("UUID Tests") { - test("Generate V3 UUID") { - val namespace = UUID.V4.random - val name = "Test Name!" - val u = UUID.V3(namespace, name) - println(u) - assertTrue(true) - } -// test("Generate V5 UUID") { -// val namespace = UUID.V1.next() -// val name = "Test Name!" -// val u = UUID.V5(namespace, name) -// println(u) -// assertTrue(true) -// } - } - -}