File: coerce_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 (30 lines) | stat: -rw-r--r-- 806 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
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Question::Validation, "#coerce" do
  let(:instance) { described_class.new }

  it "coerces lambda into proc" do
    pattern = -> { "^[^\.]+\.[^\.]+" }
    validation = described_class.new(pattern)
    expect(validation.pattern).to be_a(Proc)
  end

  it "doesn't coerce symbols" do
    pattern = :email
    validation = described_class.new(pattern)
    expect(validation.pattern).to eq(:email)
  end

  it "coerces into regex" do
    pattern = /^[^.]+\.[^.]+/
    validation = described_class.new(pattern)
    expect(validation.pattern).to be_a(Regexp)
  end

  it "fails to coerce pattern into validation" do
    pattern = Object.new
    expect {
      described_class.new(pattern)
    }.to raise_error(TTY::Prompt::ValidationCoercion)
  end
end