Drafter.Test.Harness (drafter v0.3.1)

Copy Markdown View Source

Manages the lifecycle of a headless TUI app instance for testing.

Starts the required services (HeadlessDriver, Compositor, ThemeManager, Event.Manager) and spawns the app event loop without any terminal I/O. Returns a context map used by Drafter.Test functions. Call stop_app/1 to cleanly shut down all services started by the harness.

Summary

Types

The context returned by start_app/3 and consumed by Drafter.Test and stop_app/1.

Functions

Returns a specification to start this module under a supervisor.

Returns {:ok, init_arg} unchanged.

Start a headless app and every service it needs, returning a test context.

Shut down the app and every service start_app/3 started.

Types

context()

@type context() :: %{
  app_module: module(),
  app_pid: pid(),
  app_monitor: reference(),
  props: map(),
  test_pid: pid(),
  session_pids: %{required(atom()) => pid()}
}

The context returned by start_app/3 and consumed by Drafter.Test and stop_app/1.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

init(init_arg)

@spec init(term()) :: {:ok, term()}

Returns {:ok, init_arg} unchanged.

start_app(app_module, props \\ %{}, opts \\ [])

@spec start_app(module(), map(), keyword()) :: {:ok, context()} | {:error, term()}

Start a headless app and every service it needs, returning a test context.

Starts an unnamed Drafter.Event.Manager, Drafter.Compositor, Drafter.ThemeManager, Drafter.EventHandler and the screen manager plus the named Drafter.Test.HeadlessDriver, links them to the caller, and spawns the app loop with the session's pids copied into its process dictionary. props is passed to the app's mount/1 and defaults to %{}.

Returns {:ok, context}, {:error, :already_started} when a service of the same name is already running (that service is linked to the caller first), or the first service's own {:error, reason}.

Options

  • :test_pid - process the headless driver reports output to. Default: the calling process.
  • :size - terminal size as {columns, rows}. Default: {80, 24}.

stop_app(ctx)

@spec stop_app(context()) :: :ok

Shut down the app and every service start_app/3 started.

Sends :shutdown to the app loop and waits up to 500 ms for it to exit, killing it if it does not. Always returns :ok, including when a service has already stopped.