Search code examples
scalatestingparser-combinators

Create tests for parser combinators in Scala


Consider this source code that implements a parser for a term language in Scala. The main function, designed to test its functionality is defined asÑ

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)
    }
  }

However, I want to test part of the functionality by calling the term parser on concrete input. How can I provide this input? Passing a string does not work...


Solution

  • Just replace stdin.readLine() with your desired input, so

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