spec/unit/question/in_spec.rb in tty-prompt-0.2.0 vs spec/unit/question/in_spec.rb in tty-prompt-0.3.0

- old
+ new

@@ -1,26 +1,58 @@ # encoding: utf-8 -require 'spec_helper' - RSpec.describe TTY::Prompt::Question, '#in' do - it 'reads number within range' do - prompt = TTY::TestPrompt.new - prompt.input << 8 + + subject(:prompt) { TTY::TestPrompt.new } + + it "reads range from option" do + prompt.input << '8' prompt.input.rewind - answer = prompt.ask("How do you like it on scale 1-10", read: :int) { |q| + answer = prompt.ask("How do you like it on scale 1-10?", in: '1-10') + expect(answer).to eq('8') + end + + it 'reads number within string range' do + prompt.input << '8' + prompt.input.rewind + answer = prompt.ask("How do you like it on scale 1-10?") do |q| q.in('1-10') - } - expect(answer).to eq(8) + end + expect(answer).to eq('8') + expect(prompt.output.string).to eq([ + "How do you like it on scale 1-10? ", + "\e[1000D\e[K\e[1A", + "\e[1000D\e[K", + "How do you like it on scale 1-10? \e[32m8\e[0m\n", + ].join) end - it "reads number outside of range" do - prompt = TTY::TestPrompt.new - prompt.input << 12 + it 'reads number within digit range' do + prompt.input << '8.1' prompt.input.rewind - expect { - prompt.ask("How do you like it on scale 1-10", read: :int) { |q| - q.in('1-10') - } - }.to raise_error(ArgumentError) + answer = prompt.ask("How do you like it on scale 1-10?") do |q| + q.in(1.0..11.5) + end + expect(answer).to eq('8.1') + expect(prompt.output.string).to eq([ + "How do you like it on scale 1-10? ", + "\e[1000D\e[K\e[1A", + "\e[1000D\e[K", + "How do you like it on scale 1-10? \e[32m8.1\e[0m\n", + ].join) + end + + it 'reads letters within range' do + prompt.input << 'E' + prompt.input.rewind + answer = prompt.ask("Your favourite vitamin? (A-K)") do |q| + q.in('A-K') + end + expect(answer).to eq('E') + expect(prompt.output.string).to eq([ + "Your favourite vitamin? (A-K) ", + "\e[1000D\e[K\e[1A", + "\e[1000D\e[K", + "Your favourite vitamin? (A-K) \e[32mE\e[0m\n" + ].join) end end