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.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/CHANGELOG.md +12 -0
- data/README.md +223 -59
- data/lib/tty/prompt/choice.rb +83 -0
- data/lib/tty/prompt/choices.rb +92 -0
- data/lib/tty/prompt/codes.rb +32 -0
- data/lib/tty/prompt/cursor.rb +131 -0
- data/lib/tty/prompt/list.rb +209 -0
- data/lib/tty/prompt/mode/echo.rb +10 -9
- data/lib/tty/prompt/mode/raw.rb +10 -9
- data/lib/tty/prompt/multi_list.rb +105 -0
- data/lib/tty/prompt/question/validation.rb +12 -27
- data/lib/tty/prompt/question.rb +58 -107
- data/lib/tty/prompt/reader.rb +44 -11
- data/lib/tty/prompt/response.rb +31 -36
- data/lib/tty/prompt/response_delegation.rb +3 -2
- data/lib/tty/prompt/statement.rb +10 -10
- data/lib/tty/prompt/test.rb +15 -0
- data/lib/tty/prompt/version.rb +3 -3
- data/lib/tty/prompt.rb +72 -9
- data/lib/tty-prompt.rb +11 -0
- data/spec/unit/ask_spec.rb +32 -35
- data/spec/unit/choice/eql_spec.rb +24 -0
- data/spec/unit/choice/from_spec.rb +25 -0
- data/spec/unit/choices/add_spec.rb +14 -0
- data/spec/unit/choices/each_spec.rb +15 -0
- data/spec/unit/choices/new_spec.rb +12 -0
- data/spec/unit/choices/pluck_spec.rb +11 -0
- data/spec/unit/cursor/new_spec.rb +74 -0
- data/spec/unit/error_spec.rb +4 -8
- data/spec/unit/multi_select_spec.rb +163 -0
- data/spec/unit/question/character_spec.rb +5 -16
- data/spec/unit/question/default_spec.rb +4 -10
- data/spec/unit/question/in_spec.rb +15 -12
- data/spec/unit/question/initialize_spec.rb +1 -6
- data/spec/unit/question/modify_spec.rb +25 -24
- data/spec/unit/question/required_spec.rb +31 -0
- data/spec/unit/question/validate_spec.rb +25 -17
- data/spec/unit/question/validation/call_spec.rb +22 -0
- data/spec/unit/response/read_bool_spec.rb +38 -27
- data/spec/unit/response/read_char_spec.rb +5 -8
- data/spec/unit/response/read_date_spec.rb +8 -12
- data/spec/unit/response/read_email_spec.rb +25 -22
- data/spec/unit/response/read_multiple_spec.rb +11 -13
- data/spec/unit/response/read_number_spec.rb +12 -16
- data/spec/unit/response/read_range_spec.rb +10 -13
- data/spec/unit/response/read_spec.rb +39 -38
- data/spec/unit/response/read_string_spec.rb +7 -12
- data/spec/unit/say_spec.rb +10 -14
- data/spec/unit/select_spec.rb +192 -0
- data/spec/unit/statement/initialize_spec.rb +0 -4
- data/spec/unit/suggest_spec.rb +6 -9
- data/spec/unit/warn_spec.rb +4 -8
- metadata +32 -8
- data/spec/unit/question/argument_spec.rb +0 -30
- data/spec/unit/question/valid_spec.rb +0 -46
- 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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
|
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
|
-
|
19
|
-
input
|
20
|
-
|
21
|
-
expect {
|
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
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
|
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
|
-
|
19
|
-
input
|
20
|
-
|
21
|
-
|
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
|
-
|
26
|
-
input
|
27
|
-
|
28
|
-
|
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
|
-
|
33
|
-
input
|
34
|
-
|
35
|
-
|
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
|
-
|
40
|
-
input
|
41
|
-
|
42
|
-
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
expect {
|
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
|
-
|
19
|
-
input.
|
20
|
-
|
21
|
-
|
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
|
25
|
-
|
26
|
-
input.
|
27
|
-
|
28
|
-
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
expect {
|
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
|
-
|
19
|
-
input
|
20
|
-
|
21
|
-
|
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
|
-
|
26
|
-
input
|
27
|
-
|
28
|
-
|
29
|
-
expect(
|
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
|
-
|
34
|
-
input
|
35
|
-
|
36
|
-
|
37
|
-
expect(
|
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
|
-
|
42
|
-
input
|
43
|
-
|
44
|
-
|
45
|
-
expect(
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
|
15
|
-
expect(
|
16
|
-
expect(
|
17
|
-
expect(
|
18
|
-
expect(
|
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
|
-
|
13
|
-
input
|
14
|
-
|
15
|
-
|
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
|
-
|
20
|
-
input.
|
21
|
-
|
22
|
-
|
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
|
-
|
29
|
-
input
|
30
|
-
|
31
|
-
expect {
|
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
|
-
|
35
|
-
|
36
|
-
input.
|
37
|
-
|
38
|
-
|
39
|
-
|
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, '#
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
|
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
|
-
|
19
|
-
input
|
20
|
-
|
21
|
-
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
answer =
|
15
|
-
expect(answer).to
|
16
|
-
expect(answer).to
|
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
|
-
|
24
|
-
answer
|
25
|
-
expect(answer).to
|
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
|
-
|
12
|
-
input
|
13
|
-
|
14
|
-
|
15
|
-
expect(
|
16
|
-
expect(
|
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
|
-
|
21
|
-
input
|
16
|
+
prompt = TTY::TestPrompt.new
|
17
|
+
prompt.input << "abcd"
|
18
|
+
prompt.input.rewind
|
22
19
|
expect {
|
23
|
-
prompt.ask(
|
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
|
-
|
13
|
-
input
|
14
|
-
|
15
|
-
|
16
|
-
expect(
|
17
|
-
expect(
|
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
|
-
|
22
|
-
input
|
23
|
-
|
24
|
-
|
25
|
-
expect(
|
26
|
-
expect(
|
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
|
-
|
33
|
-
input
|
34
|
-
|
35
|
-
|
36
|
-
expect(
|
37
|
-
expect(
|
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
|
-
|
42
|
-
input
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
53
|
-
input
|
54
|
-
|
55
|
-
|
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
|
-
|
60
|
-
input
|
61
|
-
|
62
|
-
|
63
|
-
|
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(
|
66
|
+
expect(answer).to eq("password")
|
66
67
|
end
|
67
68
|
end
|
68
69
|
end
|