- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我当前正在编写的应用程序中,我正在使用 cats-effect 的 IO monad在 IOApp .
如果从命令行参数“debug”开始,我会将程序流委托(delegate)到调试循环中,该循环等待用户输入并执行各种与调试相关的方法。一旦开发人员在没有任何输入的情况下按下 enter
,应用程序就会退出调试循环并退出 main 方法,从而关闭应用程序。
这个应用程序的主要方法大致如下:
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
中预期的一样:
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 monad”并确定我的程序是否包含调试循环中的语句?我是否必须调用 program.unsafeRunSync()
来检查它?
最佳答案
您可以在自己的方法中实现 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
}
}
}
关于scala - 使用 cats-effect 的 IO monad 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54767360/
如何从表中选择结果,其中 cat = '$cat',如果不相等,则从 cat 中选择所有结果,无论 $cat 是什么。这是我的查询 $sql = mysql_query("SELECT * FROM
如标题中的示例,我想要结果: 目录:home/tom/cat 第一次剪辑:tom/cat 第二次切割:/cat 最后一刀:/ 如何在 python 中实现? 最佳答案 这是您要找的吗: In [101
使用循环,我可以创建 My cat is: Cat1 ... My cat is: Cat1 但是,当我尝试使用 Cat ("cat"+i) = new Cat("Cat" + i); 我犯错了.
我目前正在用 C 和 unix 开发一个 cat 命令。这次我的任务是设计 cat -b 和 cat -s 命令。但是我已经创建了其他命令,例如 -t、-v、-n、-e。我卡在了命令 -b 中,因为每
我理解这一点的方式是命令 cat file 将显示文件的内容并 cat < file 将把文件的内容作为输入。我正在尝试这个并创建了两个文件。一个名为 file1.txt,另一个名为 file2.tx
我理解这一点的方式是命令 cat file 将显示文件的内容并 cat < file 将把文件的内容作为输入。我正在尝试这个并创建了两个文件。一个名为 file1.txt,另一个名为 file2.tx
我有一个程序可以同时读取两个输入文件。我想从标准输入中读取这个程序。我以为我会使用这样的东西: $program1 <(cat) <($program2) 但我刚刚发现 cat <(cat) 产生 .
管道的正式定义指出左侧文件的 STDOUT 将立即通过管道传输到右侧文件的 STDIN。我有两个文件,hello.txt 和 human.txt 。 cat hello.txt 返回 Hello 并且
我想编写一个 shell 脚本,它接受来自标准输入的数据,将它写入一个文件,然后用它做一些事情。 出于这个问题的目的,让我们假设我的脚本应该接受标准输入,将其写入 in.txt ,然后 grep 一个
有没有办法让cat , less等打印制表符而不是制表符被转换为空格?当我将代码从终端复制到编辑器时,我对此很恼火。 最佳答案 我在这里看到两个问题。 首先,目标编辑器可以将 TAB 转换为空格数。一
我刚刚发现执行 find . 比执行 find 慢。 |猫。这是在我的主目录中执行 time find . 3 次的结果: First: real 0m4.385s user 0m0.54
我相信该程序适用于除一个以外的大多数情况。我在 indexOF() 中添加了空格,所以像 cathrine 和 dogsuit lammas 这样的词都会被认为是非亵渎的。我看到的唯一问题是用户是否以
解决方案: 不支持 data-* 属性。您可以定义自己的属性并使用 getAttribute 调用它们。就我而言, this.dataset.cat 变为 this.getAttribute("cat
这是让我“我该怎么办?”的问题之一 这是我遇到的编译器错误: 在“SKFootmanSprite”类型上使用实例成员“getAttackUPSequence_Frames”您是否打算改用“SKFoot
我想将 2 个不同的 cat 语句合并为一个 cat 语句 cat /dir1/file1.cfg | grep -q "linux" cat /dir1/file2.cfg | grep -q "l
我已经在 C 中使用系统调用(打开、读取和写入)来模拟 Linux 系统中的“猫”功能,并且它比真实的慢... 我正在使用与真正的“cat”相同的缓冲区大小,并使用“strace”我认为它进行相同数量
我在不同的示例、教程、博客等中看到过这两种格式,但就我的生活而言,我找不到对差异的解释。有什么区别 ICriteria crit = session.CreateCriteria(typeof(Cat
我在不同的示例、教程、博客等中看到过这两种格式,但就我的生活而言,我找不到对差异的解释。有什么区别 ICriteria crit = session.CreateCriteria(typeof(Cat
我有一个包含列的表:(project_id, name) 这是一个人员列表,每个人都有其所属的项目。如果一个人在两个项目中,则重复。 我想提取一个包含以下列的表:(project_id, people
我想提取 git 存储库中保存的文件的最新版本的副本,并将其传递到脚本中进行某些处理。对于 svn 或 hg,我只使用“cat”命令: Print the specified files as the
我是一名优秀的程序员,十分优秀!