tty-prompt 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -2
  3. data/CHANGELOG.md +12 -0
  4. data/README.md +223 -59
  5. data/lib/tty/prompt/choice.rb +83 -0
  6. data/lib/tty/prompt/choices.rb +92 -0
  7. data/lib/tty/prompt/codes.rb +32 -0
  8. data/lib/tty/prompt/cursor.rb +131 -0
  9. data/lib/tty/prompt/list.rb +209 -0
  10. data/lib/tty/prompt/mode/echo.rb +10 -9
  11. data/lib/tty/prompt/mode/raw.rb +10 -9
  12. data/lib/tty/prompt/multi_list.rb +105 -0
  13. data/lib/tty/prompt/question/validation.rb +12 -27
  14. data/lib/tty/prompt/question.rb +58 -107
  15. data/lib/tty/prompt/reader.rb +44 -11
  16. data/lib/tty/prompt/response.rb +31 -36
  17. data/lib/tty/prompt/response_delegation.rb +3 -2
  18. data/lib/tty/prompt/statement.rb +10 -10
  19. data/lib/tty/prompt/test.rb +15 -0
  20. data/lib/tty/prompt/version.rb +3 -3
  21. data/lib/tty/prompt.rb +72 -9
  22. data/lib/tty-prompt.rb +11 -0
  23. data/spec/unit/ask_spec.rb +32 -35
  24. data/spec/unit/choice/eql_spec.rb +24 -0
  25. data/spec/unit/choice/from_spec.rb +25 -0
  26. data/spec/unit/choices/add_spec.rb +14 -0
  27. data/spec/unit/choices/each_spec.rb +15 -0
  28. data/spec/unit/choices/new_spec.rb +12 -0
  29. data/spec/unit/choices/pluck_spec.rb +11 -0
  30. data/spec/unit/cursor/new_spec.rb +74 -0
  31. data/spec/unit/error_spec.rb +4 -8
  32. data/spec/unit/multi_select_spec.rb +163 -0
  33. data/spec/unit/question/character_spec.rb +5 -16
  34. data/spec/unit/question/default_spec.rb +4 -10
  35. data/spec/unit/question/in_spec.rb +15 -12
  36. data/spec/unit/question/initialize_spec.rb +1 -6
  37. data/spec/unit/question/modify_spec.rb +25 -24
  38. data/spec/unit/question/required_spec.rb +31 -0
  39. data/spec/unit/question/validate_spec.rb +25 -17
  40. data/spec/unit/question/validation/call_spec.rb +22 -0
  41. data/spec/unit/response/read_bool_spec.rb +38 -27
  42. data/spec/unit/response/read_char_spec.rb +5 -8
  43. data/spec/unit/response/read_date_spec.rb +8 -12
  44. data/spec/unit/response/read_email_spec.rb +25 -22
  45. data/spec/unit/response/read_multiple_spec.rb +11 -13
  46. data/spec/unit/response/read_number_spec.rb +12 -16
  47. data/spec/unit/response/read_range_spec.rb +10 -13
  48. data/spec/unit/response/read_spec.rb +39 -38
  49. data/spec/unit/response/read_string_spec.rb +7 -12
  50. data/spec/unit/say_spec.rb +10 -14
  51. data/spec/unit/select_spec.rb +192 -0
  52. data/spec/unit/statement/initialize_spec.rb +0 -4
  53. data/spec/unit/suggest_spec.rb +6 -9
  54. data/spec/unit/warn_spec.rb +4 -8
  55. metadata +32 -8
  56. data/spec/unit/question/argument_spec.rb +0 -30
  57. data/spec/unit/question/valid_spec.rb +0 -46
  58. data/spec/unit/question/validation/valid_value_spec.rb +0 -22
@@ -3,21 +3,24 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#in' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'reads number within range' do
11
- input << 8
12
- input.rewind
13
- q = prompt.ask("How do you like it on scale 1-10").in('1-10')
14
- expect(q.read_int).to eql 8
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << 8
9
+ prompt.input.rewind
10
+ answer = prompt.ask("How do you like it on scale 1-10", read: :int) { |q|
11
+ q.in('1-10')
12
+ }
13
+ expect(answer).to eq(8)
15
14
  end
16
15
 
17
16
  it "reads number outside of range" do
18
- input << 12
19
- input.rewind
20
- q = prompt.ask("How do you like it on scale 1-10").in('1-10')
21
- expect { q.read_int }.to raise_error(ArgumentError)
17
+ prompt = TTY::TestPrompt.new
18
+ prompt.input << 12
19
+ prompt.input.rewind
20
+ expect {
21
+ prompt.ask("How do you like it on scale 1-10", read: :int) { |q|
22
+ q.in('1-10')
23
+ }
24
+ }.to raise_error(ArgumentError)
22
25
  end
23
26
  end
@@ -3,14 +3,9 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#initialize' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
6
  let(:message) { 'Do you like me?' }
10
7
 
11
- subject(:question) { described_class.new prompt }
12
-
13
- it { expect(question.required?).to eq(false) }
8
+ subject(:question) { described_class.new(TTY::TestPrompt.new)}
14
9
 
15
10
  it { expect(question.echo).to eq(true) }
16
11
 
@@ -3,42 +3,43 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#modify' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'preserves answer for unkown modification' do
11
- input << 'piotr'
12
- input.rewind
13
- q = prompt.ask("What is your name?").modify(:none)
14
- expect(q.read_string).to eql 'piotr'
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << 'piotr'
9
+ prompt.input.rewind
10
+ answer = prompt.ask("What is your name?") { |q| q.modify(:none) }
11
+ expect(answer).to eq('piotr')
15
12
  end
16
13
 
17
14
  it 'converts to upper case' do
18
- input << 'piotr'
19
- input.rewind
20
- q = prompt.ask("What is your name?").modify(:upcase)
21
- expect(q.read_string).to eql 'PIOTR'
15
+ prompt = TTY::TestPrompt.new
16
+ prompt.input << 'piotr'
17
+ prompt.input.rewind
18
+ answer = prompt.ask("What is your name?") { |q| q.modify(:upcase) }
19
+ expect(answer).to eq('PIOTR')
22
20
  end
23
21
 
24
22
  it 'trims whitespace' do
25
- input << " Some white\t space\t \there! \n"
26
- input.rewind
27
- q = prompt.ask("Enter some text: ").modify(:trim)
28
- expect(q.read_string).to eql "Some white\t space\t \there!"
23
+ prompt = TTY::TestPrompt.new
24
+ prompt.input << " Some white\t space\t \there! \n"
25
+ prompt.input.rewind
26
+ answer = prompt.ask('Enter some text: ') { |q| q.modify(:trim) }
27
+ expect(answer).to eq("Some white\t space\t \there!")
29
28
  end
30
29
 
31
30
  it 'collapses whitespace' do
32
- input << " Some white\t space\t \there! \n"
33
- input.rewind
34
- q = prompt.ask("Enter some text: ").modify(:collapse)
35
- expect(q.read_string).to eql "Some white space here!"
31
+ prompt = TTY::TestPrompt.new
32
+ prompt.input << " Some white\t space\t \there! \n"
33
+ prompt.input.rewind
34
+ answer = prompt.ask('Enter some text: ') { |q| q.modify(:collapse) }
35
+ expect(answer).to eq(' Some white space here! ')
36
36
  end
37
37
 
38
38
  it 'strips and collapses whitespace' do
39
- input << " Some white\t space\t \there! \n"
40
- input.rewind
41
- q = prompt.ask("Enter some text: ").modify(:strip, :collapse)
42
- expect(q.read_string).to eql "Some white space here!"
39
+ prompt = TTY::TestPrompt.new
40
+ prompt.input << " Some white\t space\t \there! \n"
41
+ prompt.input.rewind
42
+ answer = prompt.ask('Enter some text: ') { |q| q.modify(:strip, :collapse) }
43
+ expect(answer).to eq('Some white space here!')
43
44
  end
44
45
  end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Prompt::Question, '#required' do
6
+ it 'requires value to be present with helper' do
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << ''
9
+ prompt.input.rewind
10
+ expect {
11
+ prompt.ask('What is your name?') { |q| q.required(true) }
12
+ }.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it 'requires value to be present with option' do
16
+ prompt = TTY::TestPrompt.new
17
+ prompt.input << ''
18
+ prompt.input.rewind
19
+ expect {
20
+ prompt.ask('What is your name?', required: true)
21
+ }.to raise_error(ArgumentError)
22
+ end
23
+
24
+ it "doesn't require value to be present" do
25
+ prompt = TTY::TestPrompt.new
26
+ prompt.input << ''
27
+ prompt.input.rewind
28
+ answer = prompt.ask('What is your name?') { |q| q.required(false) }
29
+ expect(answer).to be_nil
30
+ end
31
+ end
@@ -3,28 +3,36 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#validate' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'fails to validate input' do
11
- input << 'piotrmurach'
12
- input.rewind
13
- q = prompt.ask("What is your username?").validate(/^[^\.]+\.[^\.]+/)
14
- expect { q.read_string }.to raise_error(ArgumentError)
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << 'piotrmurach'
9
+ prompt.input.rewind
10
+ expect {
11
+ prompt.ask('What is your username?') { |q|
12
+ q.validate(/^[^\.]+\.[^\.]+/)
13
+ }
14
+ }.to raise_error(ArgumentError)
15
15
  end
16
16
 
17
17
  it 'validates input with regex' do
18
- input << 'piotr.murach'
19
- input.rewind
20
- q = prompt.ask("What is your username?").validate(/^[^\.]+\.[^\.]+/)
21
- expect(q.read_string).to eq('piotr.murach')
18
+ prompt = TTY::TestPrompt.new
19
+ prompt.input << 'piotr.murach'
20
+ prompt.input.rewind
21
+ answer = prompt.ask('What is your username?') { |q|
22
+ q.validate(/^[^\.]+\.[^\.]+/)
23
+ }
24
+ expect(answer).to eq('piotr.murach')
22
25
  end
23
26
 
24
- it 'validates input in block' do
25
- input << 'piotr.murach'
26
- input.rewind
27
- q = prompt.ask("What is your username?").validate { |arg| arg =~ /^[^\.]+\.[^\.]+/ }
28
- expect(q.read_string).to eq('piotr.murach')
27
+ it 'validates input with proc' do
28
+ prompt = TTY::TestPrompt.new
29
+ prompt.input << 'piotr.murach'
30
+ prompt.input.rewind
31
+ answer = prompt.ask('What is your username?') { |q|
32
+ q.validate { |input| input =~ /^[^\.]+\.[^\.]+/ }
33
+ }
34
+ expect(answer).to eq('piotr.murach')
29
35
  end
36
+
37
+ it 'understands custom validation like :email'
30
38
  end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ RSpec.describe TTY::Prompt::Question::Validation, '#call' do
6
+ let(:pattern) { /^[^\.]+\.[^\.]+/ }
7
+ let(:validation) { described_class.new(pattern) }
8
+
9
+ it "validates nil input" do
10
+ expect(validation.(nil)).to eq(false)
11
+ end
12
+
13
+ it "validates successfully when the value matches pattern" do
14
+ expect(validation.('piotr.murach')).to eq(true)
15
+ end
16
+
17
+ it "fails validation when not maching pattern" do
18
+ expect {
19
+ validation.('piotrmurach')
20
+ }.to raise_error(TTY::Prompt::InvalidArgument)
21
+ end
22
+ end
@@ -3,45 +3,56 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#read_bool' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'fails to read boolean' do
11
- input << 'invalid'
12
- input.rewind
13
- q = prompt.ask("Do you read books?")
14
- expect { q.read_bool }.to raise_error(Necromancer::ConversionTypeError)
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << 'invalid'
9
+ prompt.input.rewind
10
+ expect {
11
+ prompt.ask("Do you read books?", read: :bool)
12
+ }.to raise_error(Necromancer::ConversionTypeError)
13
+ end
14
+
15
+ it "handles default values" do
16
+ prompt = TTY::TestPrompt.new
17
+ prompt.input << "\n"
18
+ prompt.input.rewind
19
+ response = prompt.ask('Do you read books?', read: :bool, default: true)
20
+ expect(response).to eql(true)
21
+ expect(prompt.output.string).to eq('Do you read books?')
15
22
  end
16
23
 
17
24
  it "handles default values" do
18
- input << "\n"
19
- input.rewind
20
- q = prompt.ask("Do you read books?").default(true)
21
- expect(q.read_bool).to eql(true)
25
+ prompt = TTY::TestPrompt.new
26
+ prompt.input << "\n"
27
+ prompt.input.rewind
28
+ response = prompt.ask("Do you read books?") { |q|
29
+ q.default true
30
+ q.read :bool
31
+ }
32
+ expect(response).to eq(true)
22
33
  end
23
34
 
24
35
  it 'reads negative boolean' do
25
- input << 'No'
26
- input.rewind
27
- q = prompt.ask("Do you read books?")
28
- answer = q.read_bool
29
- expect(answer).to eql false
36
+ prompt = TTY::TestPrompt.new
37
+ prompt.input << 'No'
38
+ prompt.input.rewind
39
+ response = prompt.ask('Do you read books?', read: :bool)
40
+ expect(response).to eq(false)
30
41
  end
31
42
 
32
43
  it 'reads positive boolean' do
33
- input << 'Yes'
34
- input.rewind
35
- q = prompt.ask("Do you read books?")
36
- answer = q.read_bool
37
- expect(answer).to eql true
44
+ prompt = TTY::TestPrompt.new
45
+ prompt.input << 'Yes'
46
+ prompt.input.rewind
47
+ response = prompt.ask("Do you read books?", read: :bool)
48
+ expect(response).to eq(true)
38
49
  end
39
50
 
40
51
  it 'reads single positive boolean' do
41
- input << 'y'
42
- input.rewind
43
- q = prompt.ask("Do you read books?")
44
- answer = q.read_bool
45
- expect(answer).to eql true
52
+ prompt = TTY::TestPrompt.new
53
+ prompt.input << 'y'
54
+ prompt.input.rewind
55
+ response = prompt.ask('Do you read books?', read: :bool)
56
+ expect(response).to eq(true)
46
57
  end
47
58
  end
@@ -3,14 +3,11 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#read_char' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'reads single character' do
11
- input << "abcde"
12
- input.rewind
13
- q = prompt.ask("What is your favourite letter?")
14
- expect(q.read_char).to eql "a"
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << "abcde"
9
+ prompt.input.rewind
10
+ response = prompt.ask("What is your favourite letter?", read: :char)
11
+ expect(response).to eq('a')
15
12
  end
16
13
  end
@@ -3,18 +3,14 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#read_date' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'reads date' do
11
- input << "20th April 1887"
12
- input.rewind
13
- q = prompt.ask("When were your born?")
14
- answer = q.read_date
15
- expect(answer).to be_kind_of Date
16
- expect(answer.day).to eql 20
17
- expect(answer.month).to eql 4
18
- expect(answer.year).to eql 1887
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << "20th April 1887"
9
+ prompt.input.rewind
10
+ response = prompt.ask("When were your born?", read: :date)
11
+ expect(response).to be_kind_of(Date)
12
+ expect(response.day).to eq(20)
13
+ expect(response.month).to eq(4)
14
+ expect(response.year).to eq(1887)
19
15
  end
20
16
  end
@@ -3,40 +3,43 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#read_email' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  context 'with valid email' do
11
7
  it 'reads empty' do
12
- input << ""
13
- input.rewind
14
- q = prompt.ask("What is your email?")
15
- expect(q.read_email).to eql(nil)
8
+ prompt = TTY::TestPrompt.new
9
+ prompt.input << ""
10
+ prompt.input.rewind
11
+ response = prompt.ask("What is your email?", read: :email)
12
+ expect(response).to eql(nil)
16
13
  end
17
14
 
18
15
  it 'reads valid email' do
19
- input << "piotr@example.com"
20
- input.rewind
21
- q = prompt.ask("What is your email?")
22
- expect(q.read_email).to eql "piotr@example.com"
16
+ prompt = TTY::TestPrompt.new
17
+ prompt.input << "piotr@example.com"
18
+ prompt.input.rewind
19
+ response = prompt.ask('What is your email?', read: :email)
20
+ expect(response).to eq('piotr@example.com')
23
21
  end
24
22
  end
25
23
 
26
24
  context 'with invalid email' do
27
25
  it 'fails to read invalid email' do
28
- input << "this will@neverwork"
29
- input.rewind
30
- q = prompt.ask("What is your email?")
31
- expect { q.read_email }.to raise_error(TTY::Prompt::InvalidArgument)
26
+ prompt = TTY::TestPrompt.new
27
+ prompt.input << "this will@neverwork"
28
+ prompt.input.rewind
29
+ expect {
30
+ prompt.ask("What is your email?", read: :email)
31
+ }.to raise_error(TTY::Prompt::InvalidArgument)
32
32
  end
33
33
 
34
- it 'reads invalid and asks again' do
35
- input << "this will@neverwork\nthis.will@example.com"
36
- input.rewind
37
- q = prompt.ask("What is your email?").on_error(:retry)
38
- expect(q.read_email).to eql "this.will@example.com"
39
- expect(output.string).to eql "What is your email?\nWhat is your email?\n"
34
+ xit 'reads invalid and asks again' do
35
+ prompt = TTY::TestPrompt.new
36
+ prompt.input << "this will@neverwork\nthis.will@example.com"
37
+ prompt.input.rewind
38
+ response = prompt.ask("What is your email?", read: :email) do |q|
39
+ q.on_error :retry
40
+ end
41
+ expect(response).to eq('this.will@example.com')
42
+ expect(prompt.output.string).to eq("What is your email?\nWhat is your email?")
40
43
  end
41
44
  end
42
45
  end
@@ -2,22 +2,20 @@
2
2
 
3
3
  require 'spec_helper'
4
4
 
5
- RSpec.describe TTY::Prompt::Question, '#read_multiple' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
5
+ RSpec.describe TTY::Prompt::Question, '#read_multiline' do
10
6
  it 'reads multiple lines' do
11
- input << "First line\nSecond line\nThird line"
12
- input.rewind
13
- q = prompt.ask("Provide description?")
14
- expect(q.read_multiple).to eq("First line\nSecond line\nThird line")
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << "First line\nSecond line\nThird line"
9
+ prompt.input.rewind
10
+ answer = prompt.ask("Provide description?", read: :multiline)
11
+ expect(answer).to eq("First line\nSecond line\nThird line")
15
12
  end
16
13
 
17
14
  it 'terminates on empty lines' do
18
- input << "First line\n\nSecond line"
19
- input.rewind
20
- q = prompt.ask("Provide description?")
21
- expect(q.read_multiple).to eq("First line\n")
15
+ prompt = TTY::TestPrompt.new
16
+ prompt.input << "First line\n\nSecond line"
17
+ prompt.input.rewind
18
+ answer = prompt.ask("Provide description?")
19
+ expect(answer).to eq("First line\n")
22
20
  end
23
21
  end
@@ -3,26 +3,22 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#read_numbers' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'reads integer' do
11
- input << 35
12
- input.rewind
13
- q = prompt.ask("What temperature?")
14
- answer = q.read_int
15
- expect(answer).to be_kind_of Integer
16
- expect(answer).to eql 35
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << 35
9
+ prompt.input.rewind
10
+ answer = prompt.ask("What temperature?", read: :int)
11
+ expect(answer).to be_a(Integer)
12
+ expect(answer).to eq(35)
17
13
  end
18
14
 
19
15
  it 'reads float' do
16
+ prompt = TTY::TestPrompt.new
20
17
  number = 6.666
21
- input << number
22
- input.rewind
23
- q = prompt.ask("How tall are you?")
24
- answer = q.read_float
25
- expect(answer).to be_kind_of Float
26
- expect(answer).to eql number
18
+ prompt.input << number
19
+ prompt.input.rewind
20
+ answer = prompt.ask('How tall are you?', read: :float)
21
+ expect(answer).to be_a(Float)
22
+ expect(answer).to eq(number)
27
23
  end
28
24
  end
@@ -3,24 +3,21 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#read_range' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  it 'converts with valid range' do
11
- input << "20-30"
12
- input.rewind
13
- response = prompt.ask("Which age group?").read_range
14
-
15
- expect(response).to be_kind_of Range
16
- expect(response).to eql (20..30)
7
+ prompt = TTY::TestPrompt.new
8
+ prompt.input << "20-30"
9
+ prompt.input.rewind
10
+ answer = prompt.ask("Which age group?", read: :range)
11
+ expect(answer).to be_a(Range)
12
+ expect(answer).to eq(20..30)
17
13
  end
18
14
 
19
15
  it "fails to convert to range" do
20
- input << "abcd"
21
- input.rewind
16
+ prompt = TTY::TestPrompt.new
17
+ prompt.input << "abcd"
18
+ prompt.input.rewind
22
19
  expect {
23
- prompt.ask("Which age group?").read_range
20
+ prompt.ask('Which age group?', read: :range)
24
21
  }.to raise_error(Necromancer::ConversionTypeError)
25
22
  end
26
23
  end
@@ -3,66 +3,67 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  RSpec.describe TTY::Prompt::Question, '#read' do
6
- let(:input) { StringIO.new }
7
- let(:output) { StringIO.new }
8
- let(:prompt) { TTY::Prompt.new(input, output) }
9
-
10
6
  context 'with no mask' do
11
7
  it 'asks with echo on' do
12
- input << "password"
13
- input.rewind
14
- q = prompt.ask("What is your password: ").echo(true)
15
- expect(q.read).to eql("password")
16
- expect(output.string).to eql('What is your password: ')
17
- expect(q.mask?).to eq(false)
8
+ prompt = TTY::TestPrompt.new
9
+ prompt.input << "password"
10
+ prompt.input.rewind
11
+ answer = prompt.ask("What is your password: ") { |q| q.echo(true) }
12
+ expect(answer).to eql("password")
13
+ expect(prompt.output.string).to eql('What is your password: ')
18
14
  end
19
15
 
20
16
  it 'asks with echo off' do
21
- input << "password"
22
- input.rewind
23
- q = prompt.ask("What is your password: ").echo(false)
24
- expect(q.read).to eql("password")
25
- expect(output.string).to eql('What is your password: ')
26
- expect(q.mask?).to eq(false)
17
+ prompt = TTY::TestPrompt.new
18
+ prompt.input << "password"
19
+ prompt.input.rewind
20
+ answer = prompt.ask("What is your password: ") { |q| q.echo(false) }
21
+ expect(answer).to eql("password")
22
+ expect(prompt.output.string).to eql('What is your password: ')
27
23
  end
28
24
  end
29
25
 
30
26
  context 'with mask' do
31
27
  it 'masks output with character' do
32
- input << "password\n"
33
- input.rewind
34
- q = prompt.ask("What is your password: ").mask('*')
35
- expect(q.read).to eql("password")
36
- expect(output.string).to eql('What is your password: ********')
37
- expect(q.mask?).to eq(true)
28
+ prompt = TTY::TestPrompt.new
29
+ prompt.input << "password\n"
30
+ prompt.input.rewind
31
+ answer = prompt.ask("What is your password: ") { |q| q.mask('*') }
32
+ expect(answer).to eql("password")
33
+ expect(prompt.output.string).to eql('What is your password: ********')
38
34
  end
39
35
 
40
36
  it 'ignores mask if echo is off' do
41
- input << "password"
42
- input.rewind
43
- q = prompt.ask("What is your password: ").echo(false).mask('*')
44
- expect(q.read).to eql("password")
45
- expect(output.string).to eql('What is your password: ')
46
- expect(q.mask?).to eq(true)
37
+ prompt = TTY::TestPrompt.new
38
+ prompt.input << "password"
39
+ prompt.input.rewind
40
+ answer = prompt.ask('What is your password: ') do |q|
41
+ q.echo false
42
+ q.mask '*'
43
+ end
44
+ expect(answer).to eql("password")
45
+ expect(prompt.output.string).to eql('What is your password: ')
47
46
  end
48
47
  end
49
48
 
50
49
  context 'with mask and echo as options' do
51
50
  it 'asks with options' do
52
- input << "password"
53
- input.rewind
54
- q = prompt.ask("What is your password: ", echo: false, mask: '*')
55
- expect(q.read).to eq("password")
51
+ prompt = TTY::TestPrompt.new
52
+ prompt.input << "password"
53
+ prompt.input.rewind
54
+ answer = prompt.ask("What is your password: ", echo: false, mask: '*')
55
+ expect(answer).to eq("password")
56
56
  end
57
57
 
58
58
  it 'asks with block' do
59
- input << "password"
60
- input.rewind
61
- q = prompt.ask "What is your password: " do
62
- echo false
63
- mask '*'
59
+ prompt = TTY::TestPrompt.new
60
+ prompt.input << "password"
61
+ prompt.input.rewind
62
+ answer = prompt.ask "What is your password: " do |q|
63
+ q.echo false
64
+ q.mask '*'
64
65
  end
65
- expect(q.read).to eq("password")
66
+ expect(answer).to eq("password")
66
67
  end
67
68
  end
68
69
  end