File: convert_date_spec.rb

package info (click to toggle)
ruby-tty-prompt 0.23.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,452 kB
  • sloc: ruby: 8,847; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download
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