1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
# frozen_string_literal: true
RSpec.describe TTY::Prompt::Question, "#keypress" do
subject(:prompt) { TTY::Prompt::Test.new }
it "receives line feed with echo on" do
prompt.input << "\n"
prompt.input.rewind
answer = prompt.keypress("Press key:", echo: true)
expect(answer).to eq("\n")
expect(prompt.output.string).to eq([
"Press key: ",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"Press key: \n\n"
].join)
end
it "asks for a keypress with echo on" do
prompt.input << "abcd"
prompt.input.rewind
answer = prompt.keypress("Press key:", echo: true)
expect(answer).to eq("a")
expect(prompt.output.string).to eq([
"Press key: ",
"\e[2K\e[1G",
"Press key: \e[32ma\e[0m\n"
].join)
end
it "asks for a keypress with echo off" do
prompt.input << "abcd"
prompt.input.rewind
answer = prompt.keypress("Press key:")
expect(answer).to eq("a")
expect(prompt.output.string).to eq([
"Press key: ",
"\e[2K\e[1G",
"Press key: \n"
].join)
end
it "interrupts input" do
prompt = TTY::Prompt::Test.new(interrupt: :exit)
prompt.input << "\x03"
prompt.input.rewind
expect {
prompt.keypress("Press key:")
}.to raise_error(SystemExit)
end
it "timeouts when no key provided" do
prompt = TTY::Prompt::Test.new(interrupt: :exit)
prompt.keypress("Press any key or continue in :countdown", timeout: 0.01)
expect(prompt.output.string).to include("Press any key or continue in 0.00")
end
end
|