Skip to content

Commit

Permalink
tests: misc. API of TRef
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-py committed May 5, 2019
1 parent 849b091 commit 0554e8a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
15 changes: 1 addition & 14 deletions shared/src/test/scala/com/olegpy/stm/APITests.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.olegpy.stm

import cats.effect.{IO, SyncIO}
import cats.effect.IO
import utest._
import cats.syntax.apply._
import com.olegpy.stm.results._

object APITests extends TestSuite with BaseIOSuite {
val tests = Tests {
Expand All @@ -12,18 +11,6 @@ object APITests extends TestSuite with BaseIOSuite {
.map { _ ==> number }
}

"TRef.apply" - {
TRef(number).flatMap(_.get).result
.map { _ ==> STMSuccess(number) }
}

"TRef.in" - {
for {
tr <- TRef.in[SyncIO](number).toIO
x <- tr.get.commit[IO]
} yield assert(x == number)
}

"for-comprehension with guards" - {
def transfer(from: TRef[Int], to: TRef[Int], amt: Int): STM[Unit] =
for {
Expand Down
60 changes: 60 additions & 0 deletions shared/src/test/scala/com/olegpy/stm/TRefTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.olegpy.stm

import cats.data.State
import cats.implicits._
import cats.effect.{IO, SyncIO}
import com.olegpy.stm.results._
import utest._


object TRefTests extends TestSuite with BaseIOSuite {
val tests = Tests {

"TRef.apply" - {
TRef(number).flatMap(_.get).result
.map { _ ==> STMSuccess(number) }
}

"TRef.in" - {
for {
tr <- TRef.in[SyncIO](number).toIO
x <- tr.get.commit[IO]
} yield assert(x == number)
}

"TRef#modifyState" - {
for {
tr <- TRef.in[IO](number)
stateOp = State.get[Int] <* State.set(0)
n <- tr.modifyState(stateOp).commit[IO]
_ = n ==> number
x <- tr.get.commit[IO]
} yield x ==> 0
}

"TRef#access" - {
for {
tr <- TRef.in[IO](0)
_ <- tr.access
.filter { case (i, _) => i == 0 }
.flatMap { case (_, set) => set(number) >> tr.get }
.result.map(_ ==> STMSuccess(number))
} yield ()
}

"TRef#tryModifyState, TRef#tryModify and TRef#tryUpdate never fail" - {
for {
tr <- TRef.in[IO](number)
stateOp = State.get[Int] <* State.set(0)
_ <- (
tr.tryModifyState(stateOp),
tr.tryModify(_ => (0, 'a')),
tr.tryUpdate(_ + 1)
).tupled.commit[IO].flatMap {
case (Some(_), Some(_), true) => IO.unit
case _ => fail[Unit]
}
} yield ()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.olegpy.stm.misc

import cats.effect.IO
import cats.effect.concurrent.Deferred
import cats.implicits._
import com.olegpy.stm.BaseIOSuite
import com.olegpy.stm.results._
Expand Down

0 comments on commit 0554e8a

Please sign in to comment.