|
71 | 71 | expect(sleep_calls.count(1.5)).to eq(1) # pause after . |
72 | 72 | end |
73 | 73 | end |
| 74 | + |
| 75 | + context "interrupt functionality" do |
| 76 | + before do |
| 77 | + allow_any_instance_of(Object).to receive(:sleep) |
| 78 | + end |
| 79 | + |
| 80 | + it "works without interrupt parameter (backwards compatibility)" do |
| 81 | + expect { described_class.write("hello") }.to output("hello\n").to_stdout |
| 82 | + end |
| 83 | + |
| 84 | + it "works with interrupt: false (backwards compatibility)" do |
| 85 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: false) }.to output("hello\n").to_stdout |
| 86 | + end |
| 87 | + |
| 88 | + context "when interrupt is enabled" do |
| 89 | + let(:mock_stdin) { instance_double(IO) } |
| 90 | + |
| 91 | + before do |
| 92 | + allow($stdin).to receive(:raw).and_yield(mock_stdin) |
| 93 | + end |
| 94 | + |
| 95 | + it "outputs full message when no key is pressed (interrupt: true)" do |
| 96 | + allow(IO).to receive(:select).and_return(nil) |
| 97 | + |
| 98 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: true) }.to output("hello\n").to_stdout |
| 99 | + end |
| 100 | + |
| 101 | + it "outputs full message when no key is pressed (interrupt: :enter)" do |
| 102 | + allow(IO).to receive(:select).and_return(nil) |
| 103 | + |
| 104 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: :enter) }.to output("hello\n").to_stdout |
| 105 | + end |
| 106 | + |
| 107 | + it "outputs full message when no key is pressed (interrupt: :any)" do |
| 108 | + allow(IO).to receive(:select).and_return(nil) |
| 109 | + |
| 110 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: :any) }.to output("hello\n").to_stdout |
| 111 | + end |
| 112 | + |
| 113 | + it "outputs full message when no key is pressed (interrupt: array)" do |
| 114 | + allow(IO).to receive(:select).and_return(nil) |
| 115 | + |
| 116 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: ["q"]) }.to output("hello\n").to_stdout |
| 117 | + end |
| 118 | + |
| 119 | + it "prints remaining text immediately when enter is pressed (interrupt: true)" do |
| 120 | + call_count = 0 |
| 121 | + allow(IO).to receive(:select) do |
| 122 | + call_count += 1 |
| 123 | + call_count == 2 ? [mock_stdin] : nil |
| 124 | + end |
| 125 | + allow(mock_stdin).to receive(:getc).and_return("\n") |
| 126 | + |
| 127 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: true) }.to output("hello\n").to_stdout |
| 128 | + end |
| 129 | + |
| 130 | + it "prints remaining text immediately when enter is pressed (interrupt: :enter)" do |
| 131 | + call_count = 0 |
| 132 | + allow(IO).to receive(:select) do |
| 133 | + call_count += 1 |
| 134 | + call_count == 2 ? [mock_stdin] : nil |
| 135 | + end |
| 136 | + allow(mock_stdin).to receive(:getc).and_return("\r") |
| 137 | + |
| 138 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: :enter) }.to output("hello\n").to_stdout |
| 139 | + end |
| 140 | + |
| 141 | + it "prints remaining text immediately when any key is pressed (interrupt: :any)" do |
| 142 | + call_count = 0 |
| 143 | + allow(IO).to receive(:select) do |
| 144 | + call_count += 1 |
| 145 | + call_count == 2 ? [mock_stdin] : nil |
| 146 | + end |
| 147 | + allow(mock_stdin).to receive(:getc).and_return("x") |
| 148 | + |
| 149 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: :any) }.to output("hello\n").to_stdout |
| 150 | + end |
| 151 | + |
| 152 | + it "prints remaining text when matching key is pressed (interrupt: array)" do |
| 153 | + call_count = 0 |
| 154 | + allow(IO).to receive(:select) do |
| 155 | + call_count += 1 |
| 156 | + call_count == 3 ? [mock_stdin] : nil |
| 157 | + end |
| 158 | + allow(mock_stdin).to receive(:getc).and_return("q") |
| 159 | + |
| 160 | + expect do |
| 161 | + described_class.write("hello", 0.1, 1.5, true, interrupt: %w[q x]) |
| 162 | + end.to output("hello\n").to_stdout |
| 163 | + end |
| 164 | + |
| 165 | + it "does not interrupt when non-matching key is pressed (interrupt: array)" do |
| 166 | + allow(IO).to receive(:select).and_return([mock_stdin]) |
| 167 | + allow(mock_stdin).to receive(:getc).and_return("a", "b", "c", "d", "e") |
| 168 | + |
| 169 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: ["q"]) }.to output("hello\n").to_stdout |
| 170 | + end |
| 171 | + |
| 172 | + it "does not interrupt when non-enter key is pressed (interrupt: :enter)" do |
| 173 | + allow(IO).to receive(:select).and_return([mock_stdin]) |
| 174 | + allow(mock_stdin).to receive(:getc).and_return("x", "y", "z", "a", "b") |
| 175 | + |
| 176 | + expect { described_class.write("hello", 0.1, 1.5, true, interrupt: :enter) }.to output("hello\n").to_stdout |
| 177 | + end |
| 178 | + |
| 179 | + it "handles empty string with interrupt enabled" do |
| 180 | + expect { described_class.write("", 0.1, 1.5, true, interrupt: true) }.to output("\n").to_stdout |
| 181 | + end |
| 182 | + |
| 183 | + it "handles single character with interrupt enabled" do |
| 184 | + allow(IO).to receive(:select).and_return(nil) |
| 185 | + |
| 186 | + expect { described_class.write("x", 0.1, 1.5, true, interrupt: true) }.to output("x\n").to_stdout |
| 187 | + end |
| 188 | + |
| 189 | + it "respects line_break: false with interrupt" do |
| 190 | + allow(IO).to receive(:select).and_return(nil) |
| 191 | + |
| 192 | + expect { described_class.write("hi", 0.1, 1.5, false, interrupt: true) }.to output("hi").to_stdout |
| 193 | + end |
| 194 | + end |
| 195 | + end |
74 | 196 | end |
75 | 197 | end |
0 commit comments