Skip to content

Commit

Permalink
Add RandomId and RandomIdOf (#288)
Browse files Browse the repository at this point in the history
* Add RandomId helper

* Remove unused imports

* fix missing .flatten method in TestFrameworkApi
  • Loading branch information
grzegorz-bielski authored Aug 12, 2024
1 parent 85888b9 commit b957602
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
18 changes: 18 additions & 0 deletions core/src/main/scala/com/evolutiongaming/catshelper/RandomId.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.evolutiongaming.catshelper

import cats.kernel.Eq
import cats.{Order, Show}

final case class RandomId(value: String) extends AnyVal {
override def toString = value
}

object RandomId {
implicit val eqRandomId: Eq[RandomId] = Eq.fromUniversalEquals

implicit val showRandomId: Show[RandomId] = Show.fromToString

implicit val orderingRandomId: Ordering[RandomId] = Ordering.by(_.value )

implicit val orderRandomId: Order[RandomId] = Order.fromOrdering
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.evolutiongaming.catshelper

import java.util.UUID

import cats.effect.Sync

trait RandomIdOf[F[_]] {
def apply: F[RandomId]
}

object RandomIdOf {

def apply[F[_]](implicit fa: RandomIdOf[F]): RandomIdOf[F] = fa

def uuid[F[_] : Sync]: RandomIdOf[F] = new RandomIdOf[F] {

def apply = Sync[F].delay { RandomId(UUID.randomUUID().toString) }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.evolutiongaming.catshelper

import cats.effect.implicits._
import cats.effect.kernel.Ref
import cats.effect.unsafe.IORuntime
import cats.effect.{IO, Resource}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.evolutiongaming.catshelper

import cats.effect.IO
import com.evolutiongaming.catshelper.IOSuite._
import org.scalatest.funsuite.AsyncFunSuite
import org.scalatest.matchers.should.Matchers

class RandomIdSpec extends AsyncFunSuite with Matchers {
test("uuid apply") {
RandomIdOf
.uuid[IO]
.apply
.map { _.value should not be empty }
.run()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.evolutiongaming.catshelper
import cats.effect.kernel.Deferred
import cats.effect.{IO, Resource}
import cats.implicits._
import cats.effect.implicits._
import com.evolutiongaming.catshelper.CatsHelper._
import com.evolutiongaming.catshelper.IOSuite._
import org.scalatest.funsuite.AsyncFunSuite
Expand Down

0 comments on commit b957602

Please sign in to comment.