highline_wrapper 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'highline_wrapper'
5
+
6
+ describe HighlineWrapper::OpenEndedQuestion do
7
+ let(:response) { double(:response, readline: true, to_i: 3) }
8
+ let(:highline) { double(:highline_cli, ask: response) }
9
+
10
+ before do
11
+ allow(HighLine).to receive(:new).and_return(highline)
12
+ allow(subject).to receive(:puts)
13
+ end
14
+
15
+ after do
16
+ HighlineWrapper::OpenEndedQuestion.instance_variable_set('@highline', nil)
17
+ end
18
+
19
+ subject { HighlineWrapper::OpenEndedQuestion }
20
+
21
+ context 'with the options as defaults' do
22
+ let(:options) do
23
+ {
24
+ secret: false,
25
+ default: '',
26
+ required: false
27
+ }
28
+ end
29
+
30
+ it 'should ask the highline client ask' do
31
+ expect(highline).to receive(:ask)
32
+ subject.ask(Faker::Lorem.sentence, options)
33
+ end
34
+
35
+ it 'should return the value the user selects' do
36
+ answer = Faker::Lorem.sentence
37
+ allow(highline).to receive(:ask).and_return(answer)
38
+ resp = subject.ask(Faker::Lorem.sentence, options)
39
+ expect(resp).to eq(answer)
40
+ end
41
+
42
+ it 'should return empty string if the user skips' do
43
+ allow(highline).to receive(:ask).and_return('')
44
+ resp = subject.ask(Faker::Lorem.sentence, options)
45
+ expect(resp).to eq('')
46
+ end
47
+ end
48
+
49
+ context 'with required set to true' do
50
+ let(:options) do
51
+ {
52
+ secret: false,
53
+ default: 'something-goes-here',
54
+ required: true
55
+ }
56
+ end
57
+
58
+ it 'should return the value the user selects' do
59
+ answer = Faker::Lorem.sentence
60
+ allow(highline).to receive(:ask).and_return(answer)
61
+ resp = subject.ask(Faker::Lorem.sentence, options)
62
+ expect(resp).to eq(answer)
63
+ end
64
+
65
+ it 'should recurse multiple times if the user skips' do
66
+ allow(highline).to receive(:ask).and_return('', '', Faker::Lorem.sentence)
67
+ expect(subject).to receive(:ask).exactly(3).times.and_call_original
68
+ subject.ask(Faker::Lorem.sentence, options)
69
+ end
70
+ end
71
+
72
+ context 'with the default set and the required set to false' do
73
+ let(:default_string) { Faker::Lorem.sentence }
74
+ let(:options) do
75
+ {
76
+ secret: false,
77
+ default: default_string,
78
+ required: false
79
+ }
80
+ end
81
+
82
+ it 'should return the value the user selects' do
83
+ answer = Faker::Lorem.sentence
84
+ allow(highline).to receive(:ask).and_return(answer)
85
+ resp = subject.ask(Faker::Lorem.sentence, options)
86
+ expect(resp).to eq(answer)
87
+ end
88
+
89
+ it 'should return the default value the user skips' do
90
+ allow(highline).to receive(:ask).and_return('')
91
+ expect(subject.ask(Faker::Lorem.sentence, options)).to eq(default_string)
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'highline_wrapper'
5
+
6
+ describe HighlineWrapper::Question do
7
+ let(:response) { double(:response, readline: true, to_i: 3) }
8
+ let(:highline) { double(:highline_cli, ask: response) }
9
+
10
+ before do
11
+ allow(HighLine).to receive(:new).and_return(highline)
12
+ allow(subject).to receive(:puts)
13
+ end
14
+
15
+ after do
16
+ HighlineWrapper::Question.instance_variable_set('@highline', nil)
17
+ end
18
+
19
+ subject { HighlineWrapper::Question }
20
+
21
+ describe '#highline' do
22
+ it 'should start a new highline client' do
23
+ expect(HighLine).to receive(:new)
24
+ subject.send(:highline)
25
+ end
26
+ end
27
+
28
+ describe '#ask_highline' do
29
+ it 'should ask the highline client a question' do
30
+ expect(HighLine).to receive(:new)
31
+ subject.send(:ask_highline, Faker::Lorem.sentence)
32
+ end
33
+
34
+ it 'should accept an optional secret parameter' do
35
+ expect(HighLine).to receive(:new)
36
+ subject.send(:ask_highline, Faker::Lorem.sentence, secret: true)
37
+ end
38
+ end
39
+
40
+ describe '#format_options' do
41
+ it 'should format the prompt and choices into a single string with new lines' do
42
+ response = "Prompt\n1. Choice 1\n2. Choice 2"
43
+ expect(subject.format_options('Prompt', ['Choice 1', 'Choice 2'])).to eq(response)
44
+ end
45
+ end
46
+
47
+ describe '#format_selection' do
48
+ let(:choices) { %w[one two three] }
49
+ let(:index) { 1 }
50
+
51
+ context 'with_index as false' do
52
+ it 'should format the selection based on the index into a hash' do
53
+ expect(subject.format_selection(choices, index, false)).to eq({ value: 'two' })
54
+ end
55
+ end
56
+
57
+ context 'with_index as true' do
58
+ it 'should format the selection based on the index into a hash' do
59
+ expect(subject.format_selection(choices, index, true)).to eq({ value: 'two', index: 1 })
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+ require 'highline_wrapper'
5
+
6
+ describe HighlineWrapper::YesNoQuestion do
7
+ let(:response) { double(:response, readline: true, to_i: 3) }
8
+ let(:highline) { double(:highline_cli, ask: response) }
9
+
10
+ before do
11
+ allow(HighLine).to receive(:new).and_return(highline)
12
+ allow(subject).to receive(:puts)
13
+ end
14
+
15
+ after do
16
+ HighlineWrapper::YesNoQuestion.instance_variable_set('@highline', nil)
17
+ end
18
+
19
+ subject { HighlineWrapper::YesNoQuestion }
20
+
21
+ context 'with the options as defaults' do
22
+ let(:options) do
23
+ {
24
+ default: true,
25
+ required: false
26
+ }
27
+ end
28
+
29
+ it 'should ask the highline client ask' do
30
+ expect(highline).to receive(:ask)
31
+ subject.ask(Faker::Lorem.sentence, options)
32
+ end
33
+
34
+ it 'should return the value the user selects' do
35
+ allow(highline).to receive(:ask).and_return('n')
36
+ resp = subject.ask(Faker::Lorem.sentence, options)
37
+ expect(resp).to eq(false)
38
+ end
39
+
40
+ it 'should return true if the user skips' do
41
+ allow(highline).to receive(:ask).and_return('')
42
+ resp = subject.ask(Faker::Lorem.sentence, options)
43
+ expect(resp).to eq(true)
44
+ end
45
+ end
46
+
47
+ context 'with required set to true' do
48
+ let(:options) do
49
+ {
50
+ default: false,
51
+ required: true
52
+ }
53
+ end
54
+
55
+ it 'should return the value the user selects' do
56
+ allow(highline).to receive(:ask).and_return('y')
57
+ resp = subject.ask(Faker::Lorem.sentence, options)
58
+ expect(resp).to eq(true)
59
+ end
60
+
61
+ it 'should recurse multiple times if the user skips' do
62
+ allow(highline).to receive(:ask).and_return('', '', 'y')
63
+ expect(subject).to receive(:ask).exactly(3).times.and_call_original
64
+ subject.ask(Faker::Lorem.sentence, options)
65
+ end
66
+ end
67
+
68
+ context 'with required set to false' do
69
+ let(:options) do
70
+ {
71
+ default: false,
72
+ required: false
73
+ }
74
+ end
75
+
76
+ it 'should return the value the user selects' do
77
+ allow(highline).to receive(:ask).and_return('y')
78
+ resp = subject.ask(Faker::Lorem.sentence, options)
79
+ expect(resp).to eq(true)
80
+ end
81
+
82
+ it 'should return the default if the user skips' do
83
+ allow(highline).to receive(:ask).and_return('')
84
+ resp = subject.ask(Faker::Lorem.sentence, options)
85
+ expect(resp).to eq(false)
86
+ end
87
+ end
88
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: highline_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emma Sax
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-02 00:00:00.000000000 Z
11
+ date: 2021-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: highline
@@ -112,32 +112,41 @@ dependencies:
112
112
  name: rubocop
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ">="
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '0'
117
+ version: '1.10'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ">="
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '0'
124
+ version: '1.10'
125
125
  description: Making it easier to ask simple questions, such as multiple choice questions,
126
- yes/no questions, etc, using Highline
126
+ yes/no questions, etc, using HighLine
127
127
  email:
128
128
  executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
132
  - Gemfile
133
+ - Gemfile.lock
133
134
  - Guardfile
134
135
  - LICENSE
135
136
  - README.md
136
137
  - Rakefile
137
138
  - lib/highline_wrapper.rb
138
- - lib/highline_wrapper/client.rb
139
+ - lib/highline_wrapper/checkbox_question.rb
140
+ - lib/highline_wrapper/multiple_choice_question.rb
141
+ - lib/highline_wrapper/open_ended_question.rb
142
+ - lib/highline_wrapper/question.rb
139
143
  - lib/highline_wrapper/version.rb
140
- - spec/highline_wrapper/client_spec.rb
144
+ - lib/highline_wrapper/yes_no_question.rb
145
+ - spec/highline_wrapper/checkbox_question_spec.rb
146
+ - spec/highline_wrapper/multiple_choice_question_spec.rb
147
+ - spec/highline_wrapper/open_ended_question_spec.rb
148
+ - spec/highline_wrapper/question_spec.rb
149
+ - spec/highline_wrapper/yes_no_question_spec.rb
141
150
  - spec/spec_helper.rb
142
151
  homepage: https://github.com/emmahsax/highline_wrapper
143
152
  licenses:
@@ -161,7 +170,11 @@ requirements: []
161
170
  rubygems_version: 3.2.3
162
171
  signing_key:
163
172
  specification_version: 4
164
- summary: A little wrapper for Highline
173
+ summary: A little wrapper for HighLine
165
174
  test_files:
166
175
  - spec/spec_helper.rb
167
- - spec/highline_wrapper/client_spec.rb
176
+ - spec/highline_wrapper/checkbox_question_spec.rb
177
+ - spec/highline_wrapper/multiple_choice_question_spec.rb
178
+ - spec/highline_wrapper/open_ended_question_spec.rb
179
+ - spec/highline_wrapper/question_spec.rb
180
+ - spec/highline_wrapper/yes_no_question_spec.rb
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class HighlineWrapper
4
- class Client
5
- def ask(prompt, secret)
6
- highline.ask(prompt) do |conf|
7
- conf.readline = true
8
- if secret
9
- conf.echo = false
10
- conf.echo = '*'
11
- end
12
- end.to_s
13
- end
14
-
15
- def ask_yes_no(prompt, preference)
16
- answer = highline.ask(prompt) do |conf|
17
- conf.readline = true
18
- end.to_s
19
-
20
- answer.empty? ? preference : !!(answer =~ /^y/i)
21
- end
22
-
23
- def ask_multiple_choice(prompt, choices, with_index)
24
- index = highline.ask(format_options(prompt, choices)) do |conf|
25
- conf.readline = true
26
- end.to_i - 1
27
-
28
- if with_index
29
- { choice: choices[index], index: index }
30
- else
31
- choices[index]
32
- end
33
- end
34
-
35
- # rubocop:disable Metrics/AbcSize
36
- # rubocop:disable Metrics/MethodLength
37
- def ask_checkbox(prompt, choices, provide_indices)
38
- indices = []
39
- selected = []
40
-
41
- answer = highline.ask(format_options(prompt, choices)) do |conf|
42
- conf.readline = true
43
- end
44
-
45
- answer.split(',').each { |i| indices << i.strip.to_i - 1 }
46
-
47
- if provide_indices
48
- indices.each { |index| selected << { choice: choices[index], index: index } }
49
- else
50
- indices.each { |index| selected << choices[index] }
51
- end
52
-
53
- selected
54
- end
55
- # rubocop:enable Metrics/MethodLength
56
- # rubocop:enable Metrics/AbcSize
57
-
58
- private def format_options(prompt, choices)
59
- choices_as_string_options = ''.dup
60
- choices.each_with_index { |choice, index| choices_as_string_options << "#{index + 1}. #{choice}\n" }
61
- "#{prompt}\n#{choices_as_string_options.strip}"
62
- end
63
-
64
- private def highline
65
- @highline ||= HighLine.new
66
- end
67
- end
68
- end
@@ -1,109 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'highline_wrapper'
5
-
6
- describe HighlineWrapper::Client do
7
- let(:response) { double(:response, readline: true, to_i: 3) }
8
- let(:highline) { double(:highline_cli, ask: response) }
9
-
10
- before do
11
- allow(HighLine).to receive(:new).and_return(highline)
12
- end
13
-
14
- describe '#ask' do
15
- it 'should ask the highline client ask' do
16
- expect(highline).to receive(:ask)
17
- subject.ask(Faker::Lorem.sentence, false)
18
- end
19
-
20
- it 'should return a string' do
21
- expect(subject.ask(Faker::Lorem.sentence, true)).to be_a(String)
22
- end
23
- end
24
-
25
- describe '#ask_yes_no' do
26
- it 'should ask the highline client ask' do
27
- expect(highline).to receive(:ask)
28
- subject.ask_yes_no(Faker::Lorem.sentence, true)
29
- end
30
-
31
- it 'should return a boolean' do
32
- expect(subject.ask_yes_no(Faker::Lorem.sentence, false)).to be_falsey
33
- end
34
-
35
- it 'should return true if we say yes' do
36
- allow(response).to receive(:to_s).and_return('y')
37
- expect(subject.ask_yes_no(Faker::Lorem.sentence, false)).to be_truthy
38
- end
39
-
40
- it 'should return false if we say yes' do
41
- allow(response).to receive(:to_s).and_return('no')
42
- expect(subject.ask_yes_no(Faker::Lorem.sentence, true)).to be_falsey
43
- end
44
-
45
- context 'when preferring true' do
46
- it 'should return true if empty' do
47
- allow(response).to receive(:to_s).and_return('')
48
- expect(subject.ask_yes_no(Faker::Lorem.sentence, true)).to be_truthy
49
- end
50
-
51
- it 'should return false if empty' do
52
- allow(response).to receive(:to_s).and_return('')
53
- expect(subject.ask_yes_no(Faker::Lorem.sentence, false)).to be_falsey
54
- end
55
- end
56
- end
57
-
58
- describe '#ask_multiple_choice' do
59
- it 'should ask the highline client ask' do
60
- expect(highline).to receive(:ask)
61
- subject.ask_multiple_choice(Faker::Lorem.sentence, %w[one two three], false)
62
- end
63
-
64
- context 'when not including the index' do
65
- it 'should return an array of one from the options' do
66
- resp = subject.ask_multiple_choice(Faker::Lorem.sentence, %w[one two three], false)
67
- expect(resp).to be_a(String)
68
- expect(resp).to eq('three')
69
- end
70
- end
71
-
72
- context 'when including the index' do
73
- it 'should return an array of two' do
74
- resp = subject.ask_multiple_choice(Faker::Lorem.sentence, %w[one two three], true)
75
- expect(resp).to be_a(Hash)
76
- expect(resp[:choice]).to eq('three')
77
- expect(resp[:index]).to eq(2)
78
- end
79
- end
80
- end
81
-
82
- describe '#ask_checkbox' do
83
- it 'should ask the highline client ask' do
84
- expect(highline).to receive(:ask).and_return('2')
85
- subject.ask_checkbox(Faker::Lorem.sentence, %w[one two three], false)
86
- end
87
-
88
- context 'when not including the index' do
89
- it 'should return an array of one from the options' do
90
- allow(highline).to receive(:ask).and_return('1, 3')
91
- resp = subject.ask_checkbox(Faker::Lorem.sentence, %w[one two three], false)
92
- expect(resp).to be_a(Array)
93
- expect(resp.count).to eq(2)
94
- expect(resp).to include('one')
95
- end
96
- end
97
-
98
- context 'when including the index' do
99
- it 'should return an array of two' do
100
- allow(highline).to receive(:ask).and_return('1, 3')
101
- resp = subject.ask_checkbox(Faker::Lorem.sentence, %w[one two three], true)
102
- expect(resp).to be_a(Array)
103
- expect(resp.count).to eq(2)
104
- expect(resp).to include({ choice: 'one', index: 0 })
105
- expect(resp).to include({ choice: 'three', index: 2 })
106
- end
107
- end
108
- end
109
- end