take

take: IO capture for Gleam tests.

Intercepts stdout, stderr, or both streams during a function call and returns the captured output as a string. Works with Erlang and JavaScript targets.

The with_ variants return both the function’s result and the captured output. The capture_ variants discard the result and return only the captured string.

import take

pub fn greeting_test() {
  let #(result, output) = take.with_stdout(fn() {
    io.println("Hello!")
    42
  })

  assert 42 == result
  assert "Hello!\n" == output
}

Values

pub fn capture_stderr(f: fn() -> a) -> String

Runs f and returns the captured stderr, discarding the return value.

pub fn capture_stdio(f: fn() -> a) -> #(String, String)

Runs f and returns captured stdout and stderr, discarding the return value.

pub fn capture_stdout(f: fn() -> a) -> String

Runs f and returns the captured stdout, discarding the return value.

pub fn with_stderr(f: fn() -> a) -> #(a, String)

Runs f and captures everything written to stderr. Returns a tuple of the function’s return value and the captured output.

pub fn with_stdio(f: fn() -> a) -> #(a, String, String)

Runs f and captures both stdout and stderr. Returns a tuple of the function’s return value, captured stdout, and captured stderr.

pub fn with_stdout(f: fn() -> a) -> #(a, String)

Runs f and captures everything written to stdout. Returns a tuple of the function’s return value and the captured output.

Search Document