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 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
|
# frozen_string_literal: true
RSpec.describe TTY::Reader, "#read_keypress" do
let(:input) { StringIO.new }
let(:out) { StringIO.new }
let(:env) { { "TTY_TEST" => true } }
it "reads single key press" do
reader = described_class.new(input: input, output: out, env: env)
input << "\e[Aaaaaaa\n"
input.rewind
answer = reader.read_keypress
expect(answer).to eq("\e[A")
end
it "reads multibyte key press" do
reader = described_class.new(input: input, output: out, env: env)
input << "ㄱ"
input.rewind
answer = reader.read_keypress
expect(answer).to eq("ㄱ")
end
context "when Ctrl+C pressed" do
it "defaults to raising InputInterrupt" do
reader = described_class.new(input: input, output: out, env: env)
input << "\x03"
input.rewind
expect {
reader.read_keypress
}.to raise_error(TTY::Reader::InputInterrupt)
end
it "sends interrupt signal when :signal option is chosen" do
reader = described_class.new(
input: input,
output: out,
interrupt: :signal,
env: env)
input << "\x03"
input.rewind
allow(Process).to receive(:pid).and_return(666)
allow(Process).to receive(:kill)
expect(Process).to receive(:kill).with("SIGINT", 666)
reader.read_keypress
end
it "exits with 130 code when :exit option is chosen" do
reader = described_class.new(
input: input,
output: out,
interrupt: :exit,
env: env)
input << "\x03"
input.rewind
expect {
reader.read_keypress
}.to raise_error(SystemExit)
end
it "evaluates custom handler when proc object is provided" do
handler = proc { raise ArgumentError }
reader = described_class.new(
input: input,
output: out,
interrupt: handler,
env: env)
input << "\x03"
input.rewind
expect {
reader.read_keypress
}.to raise_error(ArgumentError)
end
it "skips handler when handler is nil" do
reader = described_class.new(
input: input,
output: out,
interrupt: :noop,
env: env)
input << "\x03"
input.rewind
expect(reader.read_keypress).to eq("\x03")
end
end
end
|