Module: Clack::Testing
- Defined in:
- lib/clack/testing.rb
Overview
First-class test helpers for simulating prompt interactions.
Works with RSpec, Minitest, or any test framework. Provides a DSL for feeding keystrokes to prompts without a real terminal.
Defined Under Namespace
Classes: KeyQueue, PromptDriver
Constant Summary collapse
- KEYS =
Key constants matching what KeyReader returns
{ enter: "\r", escape: "\e", ctrl_c: "\u0003", ctrl_d: "\u0004", up: "\e[A", down: "\e[B", right: "\e[C", left: "\e[D", backspace: "\u007F", space: " ", tab: "\t", shift_tab: "\e[Z" }.freeze
Class Method Summary collapse
-
.simulate(prompt_method, **kwargs) {|PromptDriver| ... } ⇒ Object
Simulate a prompt interaction by feeding a predefined key sequence.
-
.simulate_with_output(prompt_method, **kwargs) {|PromptDriver| ... } ⇒ Array(Object, String)
Capture the rendered output of a prompt simulation.
Class Method Details
.simulate(prompt_method, **kwargs) {|PromptDriver| ... } ⇒ Object
Simulate a prompt interaction by feeding a predefined key sequence.
134 135 136 137 138 139 140 141 142 |
# File 'lib/clack/testing.rb', line 134 def simulate(prompt_method, **kwargs, &block) driver = PromptDriver.new block.call(driver) input = KeyQueue.new(driver.keys.dup) output = StringIO.new prompt_method.call(**kwargs, input: input, output: output) end |
.simulate_with_output(prompt_method, **kwargs) {|PromptDriver| ... } ⇒ Array(Object, String)
Capture the rendered output of a prompt simulation. Returns both the result and the raw output string.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/clack/testing.rb', line 151 def simulate_with_output(prompt_method, **kwargs, &block) driver = PromptDriver.new block.call(driver) input = KeyQueue.new(driver.keys.dup) output = StringIO.new result = prompt_method.call(**kwargs, input: input, output: output) [result, output.string] end |