TestConsole

trait TestConsole extends Console with Restorable

TestConsole provides a testable interface for programs interacting with the console by modeling input and output as reading from and writing to input and output buffers maintained by TestConsole and backed by a Ref.

All calls to print and printLine using the TestConsole will write the string to the output buffer and all calls to readLine will take a string from the input buffer. To facilitate debugging, by default output will also be rendered to standard output. You can enable or disable this for a scope using debug, silent, or the corresponding test aspects.

TestConsole has several methods to access and manipulate the content of these buffers including feedLines to feed strings to the input buffer that will then be returned by calls to readLine, output to get the content of the output buffer from calls to print and printLine, and clearInput and clearOutput to clear the respective buffers.

Together, these functions make it easy to test programs interacting with the console.

import zio.Console._
import zio.test.TestConsole
import zio.ZIO

val sayHello = for {
 name <- readLine
 _    <- printLine("Hello, " + name + "!")
} yield ()

for {
 _ <- TestConsole.feedLines("John", "Jane", "Sally")
 _ <- ZIO.collectAll(List.fill(3)(sayHello))
 result <- TestConsole.output
} yield result == Vector("Hello, John!\n", "Hello, Jane!\n", "Hello, Sally!\n")
Companion:
object
trait Console
class Object
trait Matchable
class Any
class Test

Value members

Abstract methods

def clearInput(implicit trace: Trace): UIO[Unit]
def clearOutput(implicit trace: Trace): UIO[Unit]
def clearOutputErr(implicit trace: Trace): UIO[Unit]
def debug[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]
def feedLines(lines: String*)(implicit trace: Trace): UIO[Unit]
def output(implicit trace: Trace): UIO[Vector[String]]
def outputErr(implicit trace: Trace): UIO[Vector[String]]
def silent[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]

Inherited methods

def printError(line: => Any)(implicit trace: Trace): IO[IOException, Unit]
Inherited from:
Console
def printLine(line: => Any)(implicit trace: Trace): IO[IOException, Unit]
Inherited from:
Console
def printLineError(line: => Any)(implicit trace: Trace): IO[IOException, Unit]
Inherited from:
Console
def readLine(prompt: String)(implicit trace: Trace): IO[IOException, String]
Inherited from:
Console
def readLine(implicit trace: Trace): IO[IOException, String]
Inherited from:
Console
def save(implicit trace: Trace): UIO[UIO[Unit]]
Inherited from:
Restorable