-
Notifications
You must be signed in to change notification settings - Fork 561
Add Lift{Value,Kind} instances for Resource
#4478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: series/3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package cats | ||
| package effect | ||
| package kernel | ||
|
|
||
| import cats.data.OptionT | ||
| import cats.effect.kernel.testkit.PureConcGenerators._ | ||
| import cats.effect.kernel.testkit.pure._ | ||
| import cats.laws.discipline.arbitrary.catsLawsArbitraryForOptionT | ||
| import cats.mtl.laws.discipline.{LiftKindTests, LiftValueTests} | ||
| import cats.syntax.flatMap._ | ||
| import cats.syntax.functor._ | ||
|
|
||
| import org.scalacheck.{Arbitrary, Gen} | ||
|
|
||
| import munit.DisciplineSuite | ||
|
|
||
| class ResourceSuite extends DisciplineSuite { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fwiw, there are already resource tests in core, though I don't object to having a few more in laws.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the issue is that these tests depend on |
||
| type PCT[A] = PureConc[Throwable, A] | ||
|
|
||
| private[this] val counter: PCT[Ref[PCT, Long]] = Concurrent[PCT].ref(0) | ||
|
|
||
| implicit val eqThrowable: Eq[Throwable] = Eq.fromUniversalEquals | ||
|
|
||
| implicit val arbitraryScope: Arbitrary[PCT ~> PCT] = | ||
| Arbitrary { | ||
| Gen.const { | ||
| new (PCT ~> PCT) { | ||
| def apply[A](fa: PCT[A]): PCT[A] = | ||
| for { | ||
| ref <- counter | ||
| res <- ref.update(_ + 1) >> fa | ||
| } yield res | ||
| } | ||
| } | ||
| } | ||
|
|
||
| implicit def arbitraryPCTUnit(): Arbitrary[PCT[Unit]] = | ||
| Arbitrary { | ||
| Gen.const { | ||
| counter.flatMap(_.update(_ * 10)).void | ||
| } | ||
| } | ||
|
|
||
| implicit def eqResource[F[_], A]( | ||
| implicit F: MonadCancelThrow[F], | ||
| eqFAUnit: Eq[F[(A, Unit)]] | ||
| ): Eq[Resource[F, A]] = | ||
| Eq.by { | ||
| _.allocated.flatMap { | ||
| case (acquire, release) => | ||
| release.map(acquire -> _) | ||
| } | ||
| } | ||
|
|
||
| implicit def arbitraryResource[F[_]: Functor, A]( | ||
| implicit arbFA: Arbitrary[F[A]], | ||
| arbFUnit: Arbitrary[F[Unit]] | ||
| ): Arbitrary[Resource[F, A]] = | ||
| Arbitrary { | ||
| for { | ||
| acquire <- arbFA.arbitrary | ||
| release <- arbFUnit.arbitrary | ||
| } yield Resource(acquire.map(_ -> release)) | ||
| } | ||
|
|
||
| checkAll( | ||
| "LiftValue[PureConc, Resource[PureConc, *]]", | ||
| LiftValueTests[PCT, Resource[PCT, *]].liftValue[Int, Int] | ||
| ) | ||
| checkAll( | ||
| "LiftValue[PureConc, Resource[OptionT[PureConc, *], *]]", | ||
| LiftValueTests[PCT, Resource[OptionT[PCT, *], *]].liftValue[Int, Int] | ||
| ) | ||
| checkAll( | ||
| "LiftKind[PureConc, Resource[PureConc, *]]", | ||
| LiftKindTests[PCT, Resource[PCT, *]].liftKind[Int, Int] | ||
| ) | ||
| checkAll( | ||
| "LiftKind[PureConc, Resource[OptionT[PureConc, *], *]]", | ||
| LiftKindTests[PCT, Resource[OptionT[PCT, *], *]].liftKind[Int, Int] | ||
| ) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a meaningful extension of the dependency surface area. It's not something I would rule out, since most people depend on core or std, but it's still not something to do lightly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree; however, I thought having a PR would be more compelling than just opening an issue for it