Allow `ask' action to customize choices prompt

This commit is contained in:
Thibault Jouan 2014-01-21 01:19:17 +00:00
parent 7e062e06a1
commit 0d0ed541c8
4 changed files with 8 additions and 8 deletions

View File

@ -4,9 +4,9 @@ Feature: `ask' recipe keyword
Given a recipe with:
"""
task :ask_letter do
letter = ask 'Which letter?', %w[A B]
letter = ask 'Which letter?', [[:a, ?A], [:b, ?B]]
echo letter
echo letter.inspect
end
"""
When I execute the recipe interactively
@ -17,6 +17,6 @@ Feature: `ask' recipe keyword
0: A
1: B
Choice:
B
:b
"""
And the exit status must be 0

View File

@ -10,11 +10,11 @@ module Producer
def prompt(question, choices)
cs = choices.each_with_index.inject('') do |m, (c, i)|
m += "#{i}: #{c}\n"
m += "#{i}: #{c.last}\n"
end
output.puts "#{question}\n#{cs}Choice:"
choice = input.gets
choices[choice.to_i]
choices[choice.to_i].first
end
end
end

View File

@ -19,7 +19,7 @@ module Producer::Core
describe '#prompt' do
let(:question) { 'Which letter?' }
let(:choices) { %w[A B] }
let(:choices) { [[:a, ?A], [:b, ?B]] }
it 'prompts choices' do
prompter.prompt question, choices
@ -34,7 +34,7 @@ module Producer::Core
it 'returns value for entry chosen by user' do
input.puts '1'
input.rewind
expect(prompter.prompt question, choices).to eq 'B'
expect(prompter.prompt question, choices).to eq :b
end
end
end

View File

@ -85,7 +85,7 @@ module Producer::Core
describe '#ask' do
let(:question) { 'Which letter?' }
let(:choices) { %w[A B] }
let(:choices) { [[:a, ?A], [:b, ?B]] }
let(:prompter_class) { double('prompter class').as_null_object }
subject(:ask) { dsl.ask question, choices,
prompter: prompter_class }