Class: TUITD::TestRunner
- Inherits:
-
Object
- Object
- TUITD::TestRunner
- Defined in:
- lib/tui_td/test_runner.rb
Overview
Executes TUI tests defined in JSON format.
plan = File.read("test/hello.json")
results = TUITD::TestRunner.new(plan).run
puts results[:passed] # => true
JSON format:
{
"name": "My test",
"rows": 24, "cols": 80, "timeout": 10,
"chdir": "/path/to/workdir",
"before_all": [{"start": "my_tui", "env": {"FOO": "bar"}}],
"steps": [
{"wait_for_text": "> "},
{"send": "hello\n"},
{"assert_text": "hello"},
{"assert_fg": [0, 0], "is": "cyan"}
],
"after_all": [{"close": true}]
}
Per-step “timeout” overrides the top-level default:
{"wait_for_text": "Slow", "timeout": 60}
Defined Under Namespace
Classes: Result
Instance Method Summary collapse
-
#initialize(source, on_step: nil) ⇒ TestRunner
constructor
A new instance of TestRunner.
- #run ⇒ Object
Constructor Details
#initialize(source, on_step: nil) ⇒ TestRunner
Returns a new instance of TestRunner.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tui_td/test_runner.rb', line 33 def initialize(source, on_step: nil) raw = source.is_a?(String) ? JSON.parse(source) : source @plan = raw.transform_keys(&:to_sym) @plan[:steps] = @plan[:steps].map { |s| s.transform_keys(&:to_sym) } @plan[:before_all] = @plan[:before_all]&.map { |s| s.transform_keys(&:to_sym) } @plan[:after_all] = @plan[:after_all]&.map { |s| s.transform_keys(&:to_sym) } @on_step = on_step rescue JSON::ParserError => e raise Error, "Invalid JSON: #{e.}" @plan[:steps] = @plan[:steps].map { |s| s.transform_keys(&:to_sym) } @plan[:before_all] = @plan[:before_all]&.map { |s| s.transform_keys(&:to_sym) } @plan[:after_all] = @plan[:after_all]&.map { |s| s.transform_keys(&:to_sym) } @on_step = on_step end |
Instance Method Details
#run ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/tui_td/test_runner.rb', line 48 def run driver = nil rows = @plan[:rows] || 40 cols = @plan[:cols] || 120 timeout = @plan[:timeout] || 30 chdir = @plan[:chdir] hooks = [ { label: :before_all, steps: @plan[:before_all] || [] }, { label: :main, steps: @plan[:steps] }, { label: :after_all, steps: @plan[:after_all] || [] } ] all_results = [] all_passed = true total_steps = hooks.sum { |p| p[:steps].size } hooks.each do |phase| phase[:steps].each do |step| action = step.keys.first.to_s value = step.values.first begin step_timeout = step[:timeout] || timeout r = case action when "start" driver&.close env = step[:env] || {} env = env.transform_keys(&:to_sym).transform_values(&:to_s) if env.is_a?(Hash) driver = Driver.new(value.to_s, rows: rows, cols: cols, timeout: step_timeout, chdir: chdir, env: env) driver.start Result.new(step: action, passed: true, message: "Started: #{value}") when "send" ensure_driver!(driver) driver.send(value.to_s) Result.new(step: action, passed: true, message: "Sent #{value.to_s.length} characters") when "send_key" ensure_driver!(driver) driver.send_keys(value.to_s.to_sym) Result.new(step: action, passed: true, message: "Sent key: #{value}") when "wait_for_text" ensure_driver!(driver) driver.wait_for_text(value.to_s) Result.new(step: action, passed: true, message: "Found: #{value}") when "wait_for_stable" ensure_driver!(driver) driver.wait_for_stable Result.new(step: action, passed: true, message: "Stable") when "assert_text", "assert_not_text", "assert_regex" check_text(driver, value, action) when "assert_fg" check_color(driver, step, :fg) when "assert_bg" check_color(driver, step, :bg) when "assert_style" ensure_driver!(driver) row, col = coords(step) state = State.new(driver.state_data) actual = state.style_at(row, col) expected = {} expected[:bold] = step[:bold] unless step[:bold].nil? expected[:italic] = step[:italic] unless step[:italic].nil? expected[:underline] = step[:underline] unless step[:underline].nil? match = expected.all? { |k, v| actual[k] == v } if match Result.new(step: action, passed: true, message: "Style at [#{row},#{col}] matches #{expected}") else Result.new(step: action, passed: false, message: "Style at [#{row},#{col}] is #{actual}, expected #{expected}") end when "screenshot" ensure_driver!(driver) path = value.is_a?(String) ? value : "/tmp/tui_td_#{Time.now.to_i}.png" driver.screenshot(path) Result.new(step: action, passed: true, message: "Saved: #{path}") when "html" ensure_driver!(driver) path = value.is_a?(String) ? value : "/tmp/tui_td_#{Time.now.to_i}.html" HtmlRenderer.new(driver.state_data).render(path) Result.new(step: action, passed: true, message: "Saved: #{path}") when "wait_for_exit" ensure_driver!(driver) driver.wait_for_exit status = driver.exitstatus Result.new(step: action, passed: true, message: "Exited with status #{status}") when "assert_exit" ensure_driver!(driver) expected = value.to_s.to_i actual = driver.exitstatus if actual == expected Result.new(step: action, passed: true, message: "Exit status #{expected} matches") else Result.new(step: action, passed: false, message: "Exit status #{actual}, expected #{expected}") end when "close" driver&.close driver = nil Result.new(step: action, passed: true, message: "Closed") else Result.new(step: action, passed: false, message: "Unknown action: #{action}") end rescue StandardError => e r = Result.new(step: action, passed: false, message: "#{e.class}: #{e.}") end all_results << r all_passed &&= r.passed if @on_step state_data = nil begin state_data = driver.state_data if driver rescue StandardError # ignore — state retrieval is best-effort end @on_step.call( index: all_results.size - 1, total: total_steps, action: action, value: value, result: r, driver: driver, state_data: state_data ) end end end driver&.close { name: @plan[:name] || "(unnamed)", passed: all_passed, results: all_results.map(&:to_h) } end |