File: expand_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 (325 lines) | stat: -rw-r--r-- 9,206 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
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
318
319
320
321
322
323
324
325
# frozen_string_literal: true

RSpec.describe TTY::Prompt, "#expand" do
  subject(:prompt) { TTY::Prompt::Test.new }

  let(:choices) {
    [{
      key: "y",
      name: "Overwrite",
      value: :yes
    }, {
      key: "n",
      name: "Skip",
      value: :no
    }, {
      key: "a",
      name: "Overwrite all",
      value: :all
    }, {
      key: "d",
      name: "Show diff",
      value: :diff
    }, {
      key: "q",
      name: "Quit",
      value: :quit
    }]
  }

  it "expands default option" do
    prompt.input << "\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?", choices)
    expect(result).to eq(:yes)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\e[2K\e[1G",
      "Overwrite Gemfile? \e[32mOverwrite\e[0m\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "changes default option" do
    prompt.input << "\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?", choices, default: 3)
    expect(result).to eq(:all)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [y,n,\e[32ma\e[0m,d,q,h] ",
      "\e[2K\e[1G",
      "Overwrite Gemfile? \e[32mOverwrite all\e[0m\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "sets quiet mode" do
    prompt.input << "\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?", choices, quiet: true)
    expect(result).to eq(:yes)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\e[2K\e[1G"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "sets quiet mode through DSL" do
    prompt.input << "\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?") do |q|
      q.choice key: "y", name: "Overwrite",      value: :yes
      q.choice key: "n", name: "Skip",          value: :no
      q.choice key: "a", name: "Overwrite all", value: :all
      q.choice key: "d", name: "Show diff",     value: :diff
      q.choice key: "q", name: "Quit",          value: :quit
      q.quiet true
    end
    expect(result).to eq(:yes)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\e[2K\e[1G"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "expands chosen option with extra information" do
    prompt.input << "a\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?", choices)
    expect(result).to eq(:all)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\e[2K\e[1G",
      "Overwrite Gemfile? (enter \"h\" for help) [y,n,\e[32ma\e[0m,d,q,h] ",
      "a\n",
      "\e[32m>> \e[0mOverwrite all",
      "\e[A\e[1G\e[55C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? \e[32mOverwrite all\e[0m\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "expands help option and then defaults" do
    prompt.input << "h\nd\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?", choices)
    expect(result).to eq(:diff)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\e[2K\e[1G",
      "Overwrite Gemfile? (enter \"h\" for help) [y,n,a,d,q,\e[32mh\e[0m] h\n",
      "\e[32m>> \e[0mprint help",
      "\e[A\e[1G\e[55C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? \n",
      "  y - Overwrite\n",
      "  n - Skip\n",
      "  a - Overwrite all\n",
      "  d - Show diff\n",
      "  q - Quit\n",
      "  h - print help\n",
      "  Choice [y]: ",
      "\e[2K\e[1G\e[1A" * 7,
      "\e[2K\e[1G",
      "Overwrite Gemfile? \n",
      "  y - Overwrite\n",
      "  n - Skip\n",
      "  a - Overwrite all\n",
      "  \e[32md - Show diff\e[0m\n",
      "  q - Quit\n",
      "  h - print help\n",
      "  Choice [y]: d",
      "\e[2K\e[1G\e[1A" * 7,
      "\e[2K\e[1G",
      "Overwrite Gemfile? \e[32mShow diff\e[0m\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "automatically expands hint" do
    prompt.input << "d\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?", choices, auto_hint: true)
    expect(result).to eq(:diff)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\n\e[32m>> \e[0mOverwrite",
      "\e[A\e[1G\e[54C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? (enter \"h\" for help) [y,n,a,\e[32md\e[0m,q,h] ",
      "d\n",
      "\e[32m>> \e[0mShow diff",
      "\e[A\e[1G\e[55C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? \e[32mShow diff\e[0m\n",
      "\e[32m>> \e[0mShow diff",
      "\e[A\e[1G\e[28C\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "informs about invalid input when automatically expanding hint" do
    prompt.on(:keypress) { |e| prompt.trigger(:keybackspace) if e.value == "w" }
    prompt.input << "y" << "y" << "\u007F" << "\r"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?", choices, defualt: 1, auto_hint: true)
    expect(result).to eq(:yes)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\n\e[32m>> \e[0mOverwrite",
      "\e[A\e[1G\e[54C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "y\n",
      "\e[32m>> \e[0mOverwrite",
      "\e[A\e[1G\e[55C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? (enter \"h\" for help) [y,n,a,d,q,h] ",
      "yy\n",
      "\e[32m>> \e[0minvalid option",
      "\e[A\e[1G\e[56C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "y\n",
      "\e[32m>> \e[0mOverwrite",
      "\e[A\e[1G\e[55C",
      "\e[2K\e[1G",
      "\e[1B",
      "\e[2K\e[1G",
      "\e[A\e[1G",
      "Overwrite Gemfile? \e[32mOverwrite\e[0m\n",
      "\e[32m>> \e[0mOverwrite",
      "\e[A\e[1G\e[28C\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "specifies options through DSL" do
    prompt.input << "\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?") do |q|
      q.default 4

      q.choice key: "y", name: "Overwrite",     value: :yes
      q.choice key: "n", name: "Skip",          value: :no
      q.choice key: "a", name: "Overwrite all", value: :all
      q.choice key: "d", name: "Show diff",     value: :diff
      q.choice key: "q", name: "Quit",          value: :quit
    end

    expect(result).to eq(:diff)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [y,n,a,\e[32md\e[0m,q,h] ",
      "\e[2K\e[1G",
      "Overwrite Gemfile? \e[32mShow diff\e[0m\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "specifies options through DSL and executes value" do
    prompt.input << "\n"
    prompt.input.rewind

    result = prompt.expand("Overwrite Gemfile?") do |q|
      q.choice key: "y", name: "Overwrite"      do :ok end
      q.choice key: "n", name: "Skip",          value: :no
      q.choice key: "a", name: "Overwrite all", value: :all
      q.choice key: "d", name: "Show diff",     value: :diff
      q.choice key: "q", name: "Quit",          value: :quit
    end

    expect(result).to eq(:ok)

    expected_output = [
      "Overwrite Gemfile? (enter \"h\" for help) [\e[32my\e[0m,n,a,d,q,h] ",
      "\e[2K\e[1G",
      "Overwrite Gemfile? \e[32mOverwrite\e[0m\n"
    ].join

    expect(prompt.output.string).to eq(expected_output)
  end

  it "fails to expand due to lack of key attribute" do
    choices = [{name: "Overwrite", value: :yes}]

    expect {
      prompt.expand("Overwrite Gemfile?", choices)
    }.to raise_error(TTY::Prompt::ConfigurationError, /Choice Overwrite is missing a :key attribute/)
  end

  it "fails to expand due to wrong key length" do
    choices = [{key: "long", name: "Overwrite", value: :yes}]

    expect {
      prompt.expand("Overwrite Gemfile?", choices)
    }.to raise_error(TTY::Prompt::ConfigurationError, /Choice key `long` is more than one character long/)
  end

  it "fails to expand due to reserve key" do
    choices = [{key: "h", name: "Overwrite", value: :yes}]

    expect {
      prompt.expand("Overwrite Gemfile?", choices)
    }.to raise_error(TTY::Prompt::ConfigurationError, /Choice key `h` is reserved for help menu/)
  end

  it "fails to expand due to duplicate key" do
    choices = [{key: "y", name: "Overwrite", value: :yes},
               {key: "y", name: "Change", value: :yes}]

    expect {
      prompt.expand("Overwrite Gemfile?", choices)
    }.to raise_error(TTY::Prompt::ConfigurationError, /Choice key `y` is a duplicate/)
  end
end