File: from_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 (149 lines) | stat: -rw-r--r-- 4,646 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
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
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Choice, "#from" do
  it "skips Choice instance" do
    choice = described_class.new(:large, 1)

    expect(described_class.from(choice)).to eq(choice)
  end

  it "creates choice from a string" do
    expected_choice = described_class.new("large", "large")
    choice = described_class.from("large")

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq("large")
  end

  it "creates choice from a symbol" do
    expected_choice = described_class.new(:large, :large)
    choice = described_class.from(:large)

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq(:large)
    expect(choice.value).to eq(:large)
  end

  it "creates choice from an array with name only and defaults value" do
    expected_choice = described_class.new("large", "large")
    choice = described_class.from([:large])

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq("large")
  end

  it "creates choice from an array with name and a value" do
    expected_choice = described_class.new("large", 1)
    choice = described_class.from([:large, 1])

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq(1)
  end

  it "creates choice from an array with name and false value" do
    expected_choice = described_class.new("large", false)
    choice = described_class.from([:large, false])

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq(false)
  end

  it "creates choice from array with name and nil value" do
    expected_choice = described_class.new("none", nil)
    choice = described_class.from([:none, nil])

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("none")
    expect(choice.value).to eq(nil)
  end

  it "creates choice from a hash with a value" do
    expected_choice = described_class.new("large", 1)
    choice = described_class.from({large: 1})

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq(1)
  end

  it "creates choice from a hash with a nil value" do
    expected_choice = described_class.new("large", nil)
    choice = described_class.from({large: nil})

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq(nil)
  end

  it "creates choice from an array with key-value pair" do
    expected_choice = described_class.new("large", 1)
    choice = described_class.from([{"large" => 1}])

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq(1)
  end

  it "creates choice from an array with a hash with name and value keys" do
    expected_choice = described_class.new("large", 1)
    choice = described_class.from([{name: "large", value: 1}])

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq(1)
  end

  it "creates choice from an array with a hash without value key" do
    expected_choice = described_class.new("large", "large")
    choice = described_class.from([{name: "large"}])

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("large")
    expect(choice.value).to eq("large")
  end

  it "creates choice from a hash with name, value and key keys" do
    default = {key: "h", name: "Help", value: :help}
    expected_choice = described_class.new("Help", :help, key: "h")
    choice = described_class.from(default)

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("Help")
    expect(choice.value).to eq(:help)
    expect(choice.disabled?).to eq(false)
  end

  it "creates disabled choice" do
    expected_choice = described_class.new("Disabled", :none, disabled: true)
    choice = described_class.from({
      name: "Disabled",
      value: :none,
      disabled: "unavailable"
    })

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq("Disabled")
    expect(choice.value).to eq(:none)
    expect(choice.disabled?).to eq(true)
  end

  it "creates choice from an arbitrary object that responds to to_s call" do
    stub_const("Size", Class.new do
      def to_s
        "large"
      end
    end)
    size = Size.new
    expected_choice = described_class.new(size, size)
    choice = described_class.from(size)

    expect(choice).to eq(expected_choice)
    expect(choice.name).to eq(size)
    expect(choice.value).to eq(size)
    expect(choice.disabled?).to eq(false)
  end
end