TestConsole

zio.test.TestConsole
See theTestConsole companion object
trait TestConsole extends Console, 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")

Attributes

Companion
object
Source
TestConsole.scala
Graph
Supertypes
trait Restorable
trait Console
trait Serializable
class Object
trait Matchable
class Any
Show all
Known subtypes
class Test

Members list

Type members

Inherited classlikes

trait UnsafeAPI extends Serializable

Attributes

Inherited from:
Console
Source
Console.scala
Supertypes
trait Serializable
class Object
trait Matchable
class Any

Value members

Abstract methods

def clearInput(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestConsole.scala
def clearOutput(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestConsole.scala
def clearOutputErr(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestConsole.scala
def debug[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]

Attributes

Source
TestConsole.scala
def feedLines(lines: String*)(implicit trace: Trace): UIO[Unit]

Attributes

Source
TestConsole.scala
def output(implicit trace: Trace): UIO[Vector[String]]

Attributes

Source
TestConsole.scala
def outputErr(implicit trace: Trace): UIO[Vector[String]]

Attributes

Source
TestConsole.scala
def silent[R, E, A](zio: ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A]

Attributes

Source
TestConsole.scala

Inherited methods

def readLine(prompt: String)(implicit trace: Trace): IO[IOException, String]

Attributes

Inherited from:
Console
Source
Console.scala

Attributes

Inherited from:
Console
Source
Console.scala

Inherited and Abstract methods

def printError(line: => Any)(implicit trace: Trace): IO[IOException, Unit]

Attributes

Inherited from:
Console
Source
Console.scala
def printLine(line: => Any)(implicit trace: Trace): IO[IOException, Unit]

Attributes

Inherited from:
Console
Source
Console.scala
def printLineError(line: => Any)(implicit trace: Trace): IO[IOException, Unit]

Attributes

Inherited from:
Console
Source
Console.scala
def readLine(implicit trace: Trace): IO[IOException, String]

Attributes

Inherited from:
Console
Source
Console.scala
def save(implicit trace: Trace): UIO[UIO[Unit]]

Attributes

Inherited from:
Restorable
Source
Restorable.scala