フロリアンBaierl:
シナリオ
アプリケーションでは、私は現在、私は猫効果の使用しています書いていIOモナドをしてIOApp。
コマンドライン引数「デバッグ」で開始した場合、私はデバッグループに私のプログラムの流れをdelegetingだということを、ユーザの入力と実行、デバッグ関連の方法のすべての種類を待ちます。すぐに現像機などのenter
任意の入力なしに、アプリケーションは、デバッグループを終了し、メインメソッドを終了し、このようにアプリケーションを終了します。
このアプリケーションの主な方法は、おおよそ次のようになります。
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
import cats.effect.{ExitCode, IO, IOApp}
import cats.implicits._
object Main extends IOApp {
val BlockingFileIO: ExecutionContextExecutor = ExecutionContext.fromExecutor(blockingIOCachedThreadPool)
def run(args: List[String]): IO[ExitCode] = for {
_ <- IO { println ("Running with args: " + args.mkString(","))}
debug = args.contains("debug")
// do all kinds of other stuff like initializing a webserver, file IO etc.
// ...
_ <- if(debug) debugLoop else IO.unit
} yield ExitCode.Success
def debugLoop: IO[Unit] = for {
_ <- IO(println("Debug mode: exit application be pressing ENTER."))
_ <- IO.shift(BlockingFileIO) // readLine might block for a long time so we shift to another thread
input <- IO(StdIn.readLine()) // let it run until user presses return
_ <- IO.shift(ExecutionContext.global) // shift back to main thread
_ <- if(input == "b") {
// do some debug relevant stuff
IO(Unit) >> debugLoop
} else {
shutDown()
}
} yield Unit
// shuts down everything
def shutDown(): IO[Unit] = ???
}
私の場合、たとえば今、私がテストしたいrun
私に期待されるようにメソッドの振る舞いをScalaTest
S:
import org.scalatest.FlatSpec
class MainSpec extends FlatSpec{
"Main" should "enter the debug loop if args contain 'debug'" in {
val program: IO[ExitCode] = Main.run("debug" :: Nil)
// is there some way I can 'search through the IO monad' and determine if my program contains the statements from the debug loop?
}
}
私の質問
私は何とか「モナド反復は、IOを通じて/検索」と私のプログラムは、デバッグループからのステートメントが含まれているかどうかを判断することはできますか?私が呼び出す必要がありますprogram.unsafeRunSync()
ことを確認することに?
Yuval Itzchakov:
あなたはのロジックを実装することができrun
ますが、前方戻り値の型に制限されていない場合は、独自のメソッドの内部で、かつテストその代わりに、run
独自の実装に。以来run
力を手にIO[ExitCode]
、あなたは戻り値から表現できる過言ではありません。一般的には、「検索」する方法はありませんIO
副作用があり、計算を説明し、それだけで価値としての価値が。あなたはそれが価値を根底だ検査したい場合は、世界の終わり(自分の中にそれを実行することにより、そうmain
あなたは、メソッド)、またはあなたのテストのためにunsafeRunSync
それは。
例えば:
sealed trait RunResult extends Product with Serializable
case object Run extends RunResult
case object Debug extends RunResult
def run(args: List[String]): IO[ExitCode] = {
run0(args) >> IO.pure(ExitCode.Success)
}
def run0(args: List[String]): IO[RunResult] = {
for {
_ <- IO { println("Running with args: " + args.mkString(",")) }
debug = args.contains("debug")
runResult <- if (debug) debugLoop else IO.pure(Run)
} yield runResult
}
def debugLoop: IO[Debug.type] =
for {
_ <- IO(println("Debug mode: exit application be pressing ENTER."))
_ <- IO.shift(BlockingFileIO) // readLine might block for a long time so we shift to another thread
input <- IO(StdIn.readLine()) // let it run until user presses return
_ <- IO.shift(ExecutionContext.global) // shift back to main thread
_ <- if (input == "b") {
// do some debug relevant stuff
IO(Unit) >> debugLoop
} else {
shutDown()
}
} yield Debug
// shuts down everything
def shutDown(): IO[Unit] = ???
}
そして、あなたのテストで:
import org.scalatest.FlatSpec
class MainSpec extends FlatSpec {
"Main" should "enter the debug loop if args contain 'debug'" in {
val program: IO[RunResult] = Main.run0("debug" :: Nil)
program.unsafeRunSync() match {
case Debug => // do stuff
case Run => // other stuff
}
}
}