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
|
# frozen_string_literal: true
RSpec.describe TTY::Prompt::Question, "convert date" do
subject(:prompt) { TTY::Prompt::Test.new }
it "fails to convert date" do
prompt.input << "x"
prompt.input.rewind
prompt.ask("When were you born?", convert: :date)
expect(prompt.output.string).to eq([
"When were you born? ",
"\e[2K\e[1G",
"When were you born? x",
"\e[31m>>\e[0m Cannot convert `x` to 'date' type",
"\e[1A\e[2K\e[1G",
"When were you born? ",
"\e[2K\e[1G\e[1A\e[2K\e[1G",
"When were you born? \n"
].join)
end
it "converts date" do
prompt.input << "20th April 1887"
prompt.input.rewind
response = prompt.ask("When were your born?", convert: :date)
expect(response).to be_kind_of(Date)
expect(response.day).to eq(20)
expect(response.month).to eq(4)
expect(response.year).to eq(1887)
end
it "converts datetime" do
prompt.input << "20th April 1887"
prompt.input.rewind
response = prompt.ask("When were your born?", convert: :datetime)
expect(response).to be_kind_of(DateTime)
expect(response.day).to eq(20)
expect(response.month).to eq(4)
expect(response.year).to eq(1887)
end
end
|