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 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
# frozen_string_literal: true
RSpec.describe TTY::Prompt, "confirmation" do
subject(:prompt) { TTY::Prompt::Test.new }
context "#yes?" do
it "agrees with question" do
prompt.input << "yes"
prompt.input.rewind
expect(prompt.yes?("Are you a human?")).to eq(true)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m y",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m ye",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m yes",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mYes\e[0m\n"
].join)
end
it "disagrees with question" do
prompt.input << "no"
prompt.input.rewind
expect(prompt.yes?("Are you a human?")).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m n",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m no",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mno\e[0m\n"
].join)
end
it "warns about invalid entry when using defaults" do
prompt.input << "test"
prompt.input.rewind
prompt.yes?("Are you a human?")
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m t",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m te",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m tes",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m test",
"\e[31m>>\e[0m Invalid input.\e[1A",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"Are you a human? \e[32mYes\e[0m\n"
].join)
end
it "assumes default true" do
prompt.input << "\r"
prompt.input.rewind
expect(prompt.yes?("Are you a human?")).to eq(true)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m \n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mYes\e[0m\n"
].join)
end
it "changes default" do
prompt.input << "\n"
prompt.input.rewind
expect(prompt.yes?("Are you a human?", default: false)).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m \n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mNo\e[0m\n"
].join)
end
it "infers default value from a word" do
prompt.input << "\n"
prompt.input.rewind
expect(prompt.yes?("Are you a human?", default: "no")).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m \n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mNo\e[0m\n"
].join)
end
it "fails to infer default value from a word" do
prompt.input << "\n"
prompt.input.rewind
expect {
prompt.yes?("Are you a human?", default: "unknown")
}.to raise_error(TTY::Prompt::InvalidArgument,
"default needs to be `true` or `false`")
end
it "defaults suffix and converter" do
prompt.input << "Nope\n"
prompt.input.rewind
result = prompt.yes?("Are you a human?") do |q|
q.positive "Yup"
q.negative "nope"
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Yup/nope)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m N",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m No",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nop",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope\n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mnope\e[0m\n"
].join)
end
it "defaults positive and negative" do
prompt.input << "Nope\n"
prompt.input.rewind
result = prompt.yes?("Are you a human?") do |q|
q.suffix "Yup/nope"
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Yup/nope)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m N",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m No",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nop",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope",
"\e[2K\e[1GAre you a human? \e[90m(Yup/nope)\e[0m Nope\n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mnope\e[0m\n"
].join)
end
it "accepts regex conflicting characters as suffix" do
prompt.input << "]\n"
prompt.input.rewind
result = prompt.yes?("Are you a human? [ as yes and ] as no") do |q|
q.suffix "[/]"
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? [ as yes and ] as no \e[90m([/])\e[0m ",
"\e[2K\e[1GAre you a human? [ as yes and ] as no \e[90m([/])\e[0m ]",
"\e[2K\e[1GAre you a human? [ as yes and ] as no \e[90m([/])\e[0m ]\n",
"\e[1A\e[2K\e[1G",
"Are you a human? [ as yes and ] as no \e[32m]\e[0m\n"
].join)
end
it "customizes question through options" do
prompt.input << "\r"
prompt.input.rewind
result = prompt.yes?("Are you a human?", suffix: "Agree/Disagree",
positive: "Agree",
negative: "Disagree")
expect(result).to eq(true)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Agree/Disagree)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m \n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mAgree\e[0m\n"
].join)
end
it "customizes question through DSL" do
prompt.input << "disagree\r"
prompt.input.rewind
conversion = proc { |input| !input.match(/^agree$/i).nil? }
result = prompt.yes?("Are you a human?") do |q|
q.suffix "Agree/Disagree"
q.positive "Agree"
q.negative "Disagree"
q.convert conversion
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Agree/Disagree)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m d",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m di",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m dis",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disa",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disag",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagr",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagre",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagree",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m disagree\n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mDisagree\e[0m\n"
].join)
end
it "obeys quiet mode" do
prompt.input << "\r"
prompt.input.rewind
expect(prompt.yes?("Are you a human?", quiet: true)).to eq(true)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m \n",
"\e[1A\e[2K\e[1G"
].join)
end
end
context "#no?" do
it "agrees with question" do
prompt.input << "no"
prompt.input.rewind
expect(prompt.no?("Are you a human?")).to eq(true)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m n",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m no",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mNo\e[0m\n"
].join)
end
it "disagrees with question" do
prompt.input << "yes"
prompt.input.rewind
expect(prompt.no?("Are you a human?")).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m y",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m ye",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m yes",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32myes\e[0m\n"
].join)
end
it "warns about invalid entry when using defaults" do
prompt.input << "test"
prompt.input.rewind
prompt.no?("Are you a human?")
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m t",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m te",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m tes",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m test",
"\e[31m>>\e[0m Invalid input.\e[1A",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"Are you a human? \e[32mNo\e[0m\n"
].join)
end
it "assumes default false" do
prompt.input << "\r"
prompt.input.rewind
expect(prompt.no?("Are you a human?")).to eq(true)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(y/N)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(y/N)\e[0m \n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mNo\e[0m\n"
].join)
end
it "changes default" do
prompt.input << "\r"
prompt.input.rewind
expect(prompt.no?("Are you a human?", default: true)).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Y/n)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Y/n)\e[0m \n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mYes\e[0m\n"
].join)
end
it "defaults suffix and converter" do
prompt.input << "Yup\n"
prompt.input.rewind
result = prompt.no?("Are you a human?") do |q|
q.positive "yup"
q.negative "Nope"
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(yup/Nope)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Y",
"\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yu",
"\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yup",
"\e[2K\e[1GAre you a human? \e[90m(yup/Nope)\e[0m Yup\n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32myup\e[0m\n"
].join)
end
it "customizes question through DSL" do
prompt.input << "agree\r"
prompt.input.rewind
conversion = proc { |input| !input.match(/^agree$/i).nil? }
result = prompt.no?("Are you a human?") do |q|
q.suffix "Agree/Disagree"
q.positive "Agree"
q.negative "Disagree"
q.convert conversion
end
expect(result).to eq(false)
expect(prompt.output.string).to eq([
"Are you a human? \e[90m(Agree/Disagree)\e[0m ",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m a",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m ag",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agr",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agre",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agree",
"\e[2K\e[1GAre you a human? \e[90m(Agree/Disagree)\e[0m agree\n",
"\e[1A\e[2K\e[1G",
"Are you a human? \e[32mAgree\e[0m\n"
].join)
end
end
end
|