-
Notifications
You must be signed in to change notification settings - Fork 536
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
Draft: Laws testing for Queue
instances
#3676
Draft
diogocanut
wants to merge
6
commits into
typelevel:series/3.x
Choose a base branch
from
diogocanut:1618/laws-testing-for-queue-functor-instances
base: series/3.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e4a4ba
Draft: Laws testing for queue functor instances
diogocanut 9a9a7bc
wip
diogocanut d52bf2f
wip: functor tests for queue
diogocanut 06d8e00
Queue laws spec
diogocanut 6285a60
wip
diogocanut 7afde7d
Laws Spec for Dequeue
diogocanut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
tests/shared/src/test/scala/cats/effect/std/DequeueLawsSpec.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,68 @@ | ||
package cats.effect | ||
package std | ||
|
||
import cats.Eq | ||
import cats.effect._ | ||
import cats.effect.kernel.Outcome | ||
import cats.laws.discipline.{FunctorTests, InvariantTests} | ||
import cats.syntax.all._ | ||
|
||
import org.scalacheck.{Arbitrary, Gen} | ||
import org.typelevel.discipline.specs2.mutable.Discipline | ||
|
||
class DequeueLawsSpec extends BaseSpec with Discipline { | ||
|
||
sequential | ||
|
||
def toList[A](q: Dequeue[IO, A]): IO[List[A]] = | ||
for { | ||
size <- q.size | ||
list <- q.tryTakeN(size.some) | ||
} yield list | ||
|
||
def fromList[A: Arbitrary](as: List[A]): IO[Dequeue[IO, A]] = { | ||
for { | ||
queue <- Dequeue.bounded[IO, A](Int.MaxValue) | ||
_ <- as.traverse(a => queue.offer(a)) | ||
} yield queue | ||
} | ||
|
||
def genDequeue[A: Arbitrary](implicit ticker: Ticker): Gen[Dequeue[IO, A]] = | ||
for { | ||
list <- Arbitrary.arbitrary[List[A]] | ||
queue = fromList(list) | ||
outcome = unsafeRun(queue) match { | ||
case Outcome.Succeeded(a) => a | ||
case _ => None | ||
} | ||
} yield outcome.get | ||
|
||
def toListSource[A](q: DequeueSource[IO, A]): IO[List[A]] = | ||
for { | ||
size <- q.size | ||
list <- q.tryTakeN(size.some) | ||
} yield list | ||
|
||
implicit def eqForDequeueSource[A: Eq](implicit ticker: Ticker): Eq[DequeueSource[IO, A]] = | ||
Eq.by(toListSource) | ||
|
||
implicit def arbDequeueSource[A: Arbitrary]( | ||
implicit ticker: Ticker): Arbitrary[DequeueSource[IO, A]] = | ||
Arbitrary(genDequeue) | ||
|
||
{ | ||
implicit val ticker = Ticker() | ||
checkAll("DequeueFunctorLaws", FunctorTests[DequeueSource[IO, *]].functor[Int, Int, String]) | ||
} | ||
|
||
implicit def eqForDequeue[A: Eq](implicit ticker: Ticker): Eq[Dequeue[IO, A]] = | ||
Eq.by(toList) | ||
|
||
implicit def arbDequeue[A: Arbitrary](implicit ticker: Ticker): Arbitrary[Dequeue[IO, A]] = | ||
Arbitrary(genDequeue) | ||
|
||
{ | ||
implicit val ticker = Ticker() | ||
checkAll("DequeueInvariantLaws", InvariantTests[Dequeue[IO, *]].invariant[Int, Int, String]) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
tests/shared/src/test/scala/cats/effect/std/PQueueLawsSpec.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,71 @@ | ||
package cats.effect | ||
package std | ||
|
||
import cats.{Eq, Order} | ||
import cats.effect._ | ||
import cats.effect.kernel.Outcome | ||
import cats.laws.discipline.{FunctorTests, InvariantTests} | ||
import cats.syntax.all._ | ||
|
||
import org.scalacheck.{Arbitrary, Gen} | ||
import org.typelevel.discipline.specs2.mutable.Discipline | ||
|
||
class PQueueLawsSpec extends BaseSpec with Discipline { | ||
|
||
sequential | ||
|
||
def toList[A](q: PQueue[IO, A]): IO[List[A]] = | ||
for { | ||
size <- q.size | ||
list <- q.tryTakeN(size.some) | ||
} yield list | ||
|
||
def fromList[A: Arbitrary](as: List[A])(implicit ord: Order[A]): IO[PQueue[IO, A]] = { | ||
for { | ||
queue <- PQueue.bounded[IO, A](Int.MaxValue) | ||
_ <- as.traverse(a => queue.offer(a)) | ||
} yield queue | ||
} | ||
|
||
def genPQueue[A: Arbitrary](implicit ticker: Ticker, ord: Order[A]): Gen[PQueue[IO, A]] = | ||
for { | ||
list <- Arbitrary.arbitrary[List[A]] | ||
queue = fromList(list) | ||
outcome = unsafeRun(queue) match { | ||
case Outcome.Succeeded(a) => a | ||
case _ => None | ||
} | ||
} yield outcome.get | ||
|
||
def toListSource[A](q: PQueueSource[IO, A]): IO[List[A]] = | ||
for { | ||
size <- q.size | ||
list <- q.tryTakeN(size.some) | ||
} yield list | ||
|
||
implicit def eqForPQueueSource[A: Eq](implicit ticker: Ticker): Eq[PQueueSource[IO, A]] = | ||
Eq.by(toListSource) | ||
|
||
implicit def arbPQueueSource[A: Arbitrary]( | ||
implicit ticker: Ticker, | ||
ord: Order[A]): Arbitrary[PQueueSource[IO, A]] = | ||
Arbitrary(genPQueue) | ||
|
||
{ | ||
implicit val ticker = Ticker() | ||
checkAll("PQueueFunctorLaws", FunctorTests[PQueueSource[IO, *]].functor[Int, Int, String]) | ||
} | ||
|
||
implicit def eqForPQueue[A: Eq](implicit ticker: Ticker): Eq[PQueue[IO, A]] = | ||
Eq.by(toList) | ||
|
||
implicit def arbPQueue[A: Arbitrary]( | ||
implicit ticker: Ticker, | ||
ord: Order[A]): Arbitrary[PQueue[IO, A]] = | ||
Arbitrary(genPQueue) | ||
|
||
{ | ||
implicit val ticker = Ticker() | ||
checkAll("PQueueInvariantLaws", InvariantTests[PQueue[IO, *]].invariant[Int, Int, String]) | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
tests/shared/src/test/scala/cats/effect/std/QueueLawsSpec.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,83 @@ | ||
package cats.effect | ||
package std | ||
|
||
import cats.Eq | ||
import cats.effect._ | ||
import cats.effect.kernel.Outcome | ||
import cats.laws.discipline.{FunctorTests, InvariantTests} | ||
import cats.syntax.all._ | ||
|
||
import org.scalacheck.{Arbitrary, Gen} | ||
import org.typelevel.discipline.specs2.mutable.Discipline | ||
|
||
class QueueLawsSpec extends BaseSpec with Discipline { | ||
|
||
sequential | ||
|
||
def toList[A](q: Queue[IO, A]): IO[List[A]] = | ||
for { | ||
size <- q.size | ||
list <- q.tryTakeN(size.some) | ||
} yield list | ||
|
||
def fromList[A: Arbitrary](as: List[A]): IO[Queue[IO, A]] = { | ||
for { | ||
queue <- Queue.bounded[IO, A](Int.MaxValue) | ||
_ <- as.traverse(a => queue.offer(a)) | ||
} yield queue | ||
} | ||
|
||
def genQueue[A: Arbitrary](implicit ticker: Ticker): Gen[Queue[IO, A]] = | ||
for { | ||
list <- Arbitrary.arbitrary[List[A]] | ||
queue = fromList(list) | ||
outcome = unsafeRun(queue) match { | ||
case Outcome.Succeeded(a) => a | ||
case _ => None | ||
} | ||
} yield outcome.get | ||
|
||
def toListSource[A](q: QueueSource[IO, A]): IO[List[A]] = | ||
for { | ||
size <- q.size | ||
list <- q.tryTakeN(size.some) | ||
} yield list | ||
|
||
implicit def eqForQueueSource[A: Eq](implicit ticker: Ticker): Eq[QueueSource[IO, A]] = | ||
Eq.by(toListSource) | ||
|
||
implicit def arbQueueSource[A: Arbitrary]( | ||
implicit ticker: Ticker): Arbitrary[QueueSource[IO, A]] = | ||
Arbitrary(genQueue) | ||
|
||
{ | ||
implicit val ticker = Ticker() | ||
checkAll("QueueFunctorLaws", FunctorTests[QueueSource[IO, *]].functor[Int, Int, String]) | ||
} | ||
|
||
implicit def eqForQueue[A: Eq](implicit ticker: Ticker): Eq[Queue[IO, A]] = | ||
Eq.by(toList) | ||
|
||
implicit def arbQueue[A: Arbitrary](implicit ticker: Ticker): Arbitrary[Queue[IO, A]] = | ||
Arbitrary(genQueue) | ||
|
||
{ | ||
implicit val ticker = Ticker() | ||
checkAll("QueueInvariantLaws", InvariantTests[Queue[IO, *]].invariant[Int, Int, String]) | ||
} | ||
|
||
// implicit def eqForQueueSink[A: Eq](implicit ticker: Ticker): Eq[QueueSink[IO, A]] = | ||
// Eq.by(toListSink) | ||
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 was not able to transform |
||
|
||
// implicit def arbQueueSink[A: Arbitrary]( | ||
// implicit ticker: Ticker): Arbitrary[QueueSink[IO, A]] = | ||
// Arbitrary(genQueue) | ||
|
||
// { | ||
// implicit val ticker = Ticker() | ||
// checkAll( | ||
// "QueueContravariantLaws", | ||
// ContravariantTests[QueueSink[IO, *]].contravariant[Int, Int, String]) | ||
|
||
// } | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Need to refactor this code.