在Scala中为解析器组合器创建测试

时间:2019-09-23 11:23:22

标签: scala testing parser-combinators

考虑this源代码,该源代码在Scala中为术语语言实现了解析器。用于测试其功能的主要功能定义为Ñ

def main(args: Array[String]): Unit = {
    val stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val tokens = new lexical.Scanner(stdin.readLine())
    phrase(term)(tokens) match {
      case Success(trees, _) =>
        for (t <- path(trees))
          println(t)
        try {
          print("Big step: ")
          println(eval(trees))
        } catch {
          case TermIsStuck(t) => println("Stuck term: " + t)
        }
      case e =>
        println(e)
    }
  }

但是,我想通过在具体输入上调用term解析器来测试部分功能。如何提供此输入?传递字符串不起作用...

1 个答案:

答案 0 :(得分:1)

只需将stdin.readLine()替换为所需的输入即可,所以

phrase(term)(new lexical.Scanner("the string you want to test")) match {
  case Success(trees, _) => ...
  case err => ...
}
相关问题