highline_wrapper 0.1.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,108 @@
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
+ indicate_default_message: true,
25
+ secret: false,
26
+ default: '',
27
+ required: false
28
+ }
29
+ end
30
+
31
+ it 'should ask the highline client ask' do
32
+ expect(highline).to receive(:ask)
33
+ subject.ask(Faker::Lorem.sentence, options)
34
+ end
35
+
36
+ it 'should return the value the user selects' do
37
+ answer = Faker::Lorem.sentence
38
+ allow(highline).to receive(:ask).and_return(answer)
39
+ resp = subject.ask(Faker::Lorem.sentence, options)
40
+ expect(resp).to eq(answer)
41
+ end
42
+
43
+ it 'should return empty string if the user skips' do
44
+ allow(highline).to receive(:ask).and_return('')
45
+ resp = subject.ask(Faker::Lorem.sentence, options)
46
+ expect(resp).to eq('')
47
+ end
48
+
49
+ it 'should call to print the default message' do
50
+ allow(highline).to receive(:ask).and_return('')
51
+ expect(subject).to receive(:print_default_message)
52
+ subject.ask(Faker::Lorem.sentence, options)
53
+ end
54
+ end
55
+
56
+ context 'with required set to true' do
57
+ let(:options) do
58
+ {
59
+ secret: false,
60
+ default: 'something-goes-here',
61
+ required: true
62
+ }
63
+ end
64
+
65
+ it 'should return the value the user selects' do
66
+ answer = Faker::Lorem.sentence
67
+ allow(highline).to receive(:ask).and_return(answer)
68
+ resp = subject.ask(Faker::Lorem.sentence, options)
69
+ expect(resp).to eq(answer)
70
+ end
71
+
72
+ it 'should recurse multiple times if the user skips' do
73
+ allow(highline).to receive(:ask).and_return('', '', Faker::Lorem.sentence)
74
+ expect(subject).to receive(:ask).exactly(3).times.and_call_original
75
+ subject.ask(Faker::Lorem.sentence, options)
76
+ end
77
+ end
78
+
79
+ context 'with the default set and the required set to false' do
80
+ let(:default_string) { Faker::Lorem.sentence }
81
+ let(:options) do
82
+ {
83
+ indicate_default_message: false,
84
+ secret: false,
85
+ default: default_string,
86
+ required: false
87
+ }
88
+ end
89
+
90
+ it 'should return the value the user selects' do
91
+ answer = Faker::Lorem.sentence
92
+ allow(highline).to receive(:ask).and_return(answer)
93
+ resp = subject.ask(Faker::Lorem.sentence, options)
94
+ expect(resp).to eq(answer)
95
+ end
96
+
97
+ it 'should return the default value the user skips' do
98
+ allow(highline).to receive(:ask).and_return('')
99
+ expect(subject.ask(Faker::Lorem.sentence, options)).to eq(default_string)
100
+ end
101
+
102
+ it 'should not call to print the default message' do
103
+ allow(highline).to receive(:ask).and_return('')
104
+ expect(subject).not_to receive(:print_default_message)
105
+ subject.ask(Faker::Lorem.sentence, options)
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,114 @@
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
+
64
+ describe '#should print out a message indicating EMPTY was selected, and return the defaults' do
65
+ context 'when default message is true' do
66
+ let(:options) do
67
+ {
68
+ defaults: :default,
69
+ indicate_default_message: true
70
+ }
71
+ end
72
+
73
+ it 'should return the default in the options' do
74
+ expect(subject.send(:return_empty_defaults, options)).to eq(:default)
75
+ end
76
+
77
+ it 'should puts a message' do
78
+ expect(subject).to receive(:puts)
79
+ expect(subject.send(:return_empty_defaults, options)).to eq(:default)
80
+ end
81
+ end
82
+
83
+ context 'when default message is false' do
84
+ let(:options) do
85
+ {
86
+ defaults: :default,
87
+ indicate_default_message: false
88
+ }
89
+ end
90
+
91
+ it 'should return the default in the options' do
92
+ expect(subject.send(:return_empty_defaults, options)).to eq(:default)
93
+ end
94
+
95
+ it 'should not puts a message' do
96
+ expect(subject).not_to receive(:puts)
97
+ expect(subject.send(:return_empty_defaults, options)).to eq(:default)
98
+ end
99
+ end
100
+
101
+ context 'when a singular default is present, but not plural' do
102
+ let(:options) do
103
+ {
104
+ default: :default,
105
+ indicate_default_message: false
106
+ }
107
+ end
108
+
109
+ it 'should return the default in the options' do
110
+ expect(subject.send(:return_empty_defaults, options)).to eq(:default)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,108 @@
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
+ indicate_default_message: true,
25
+ default: true,
26
+ required: false
27
+ }
28
+ end
29
+
30
+ it 'should ask the highline client ask' do
31
+ expect(highline).to receive(:ask).and_return('Y')
32
+ subject.ask(Faker::Lorem.sentence, options)
33
+ end
34
+
35
+ it 'should return the value the user selects' do
36
+ allow(highline).to receive(:ask).and_return('n')
37
+ resp = subject.ask(Faker::Lorem.sentence, options)
38
+ expect(resp).to eq(false)
39
+ end
40
+
41
+ it 'should return true if the user skips' do
42
+ allow(highline).to receive(:ask).and_return('')
43
+ resp = subject.ask(Faker::Lorem.sentence, options)
44
+ expect(resp).to eq(true)
45
+ end
46
+
47
+ it 'should call to print the default message' do
48
+ allow(highline).to receive(:ask).and_return('')
49
+ expect(subject).to receive(:print_default_message)
50
+ subject.ask(Faker::Lorem.sentence, options)
51
+ end
52
+
53
+ it 'should recurse if the answer given is unparseable' do
54
+ allow(highline).to receive(:ask).and_return('yep', 'yessss', 'yes')
55
+ expect(subject).to receive(:recurse).exactly(2).times.and_call_original
56
+ subject.ask(Faker::Lorem.sentence, options)
57
+ end
58
+ end
59
+
60
+ context 'with required set to true' do
61
+ let(:options) do
62
+ {
63
+ default: false,
64
+ required: true
65
+ }
66
+ end
67
+
68
+ it 'should return the value the user selects' do
69
+ allow(highline).to receive(:ask).and_return('y')
70
+ resp = subject.ask(Faker::Lorem.sentence, options)
71
+ expect(resp).to eq(true)
72
+ end
73
+
74
+ it 'should recurse multiple times if the user skips' do
75
+ allow(highline).to receive(:ask).and_return('', '', 'y')
76
+ expect(subject).to receive(:ask).exactly(3).times.and_call_original
77
+ subject.ask(Faker::Lorem.sentence, options)
78
+ end
79
+ end
80
+
81
+ context 'with required set to false' do
82
+ let(:options) do
83
+ {
84
+ indicate_default_message: false,
85
+ default: false,
86
+ required: false
87
+ }
88
+ end
89
+
90
+ it 'should return the value the user selects' do
91
+ allow(highline).to receive(:ask).and_return('y')
92
+ resp = subject.ask(Faker::Lorem.sentence, options)
93
+ expect(resp).to eq(true)
94
+ end
95
+
96
+ it 'should return the default if the user skips' do
97
+ allow(highline).to receive(:ask).and_return('')
98
+ resp = subject.ask(Faker::Lorem.sentence, options)
99
+ expect(resp).to eq(false)
100
+ end
101
+
102
+ it 'should not call to print the default message' do
103
+ allow(highline).to receive(:ask).and_return('')
104
+ expect(subject).not_to receive(:print_default_message)
105
+ subject.ask(Faker::Lorem.sentence, options)
106
+ end
107
+ end
108
+ 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.2.2
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-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: highline
@@ -112,18 +112,18 @@ 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: []
@@ -131,17 +131,25 @@ extra_rdoc_files: []
131
131
  files:
132
132
  - Gemfile
133
133
  - Guardfile
134
- - LICENSE
134
+ - LICENSE.md
135
135
  - README.md
136
136
  - Rakefile
137
137
  - lib/highline_wrapper.rb
138
- - lib/highline_wrapper/client.rb
138
+ - lib/highline_wrapper/checkbox_question.rb
139
+ - lib/highline_wrapper/multiple_choice_question.rb
140
+ - lib/highline_wrapper/open_ended_question.rb
141
+ - lib/highline_wrapper/question.rb
139
142
  - lib/highline_wrapper/version.rb
140
- - spec/highline_wrapper/client_spec.rb
143
+ - lib/highline_wrapper/yes_no_question.rb
144
+ - spec/highline_wrapper/checkbox_question_spec.rb
145
+ - spec/highline_wrapper/multiple_choice_question_spec.rb
146
+ - spec/highline_wrapper/open_ended_question_spec.rb
147
+ - spec/highline_wrapper/question_spec.rb
148
+ - spec/highline_wrapper/yes_no_question_spec.rb
141
149
  - spec/spec_helper.rb
142
150
  homepage: https://github.com/emmahsax/highline_wrapper
143
151
  licenses:
144
- - MIT
152
+ - BSD-3-Clause
145
153
  metadata: {}
146
154
  post_install_message:
147
155
  rdoc_options: []
@@ -158,10 +166,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
158
166
  - !ruby/object:Gem::Version
159
167
  version: '0'
160
168
  requirements: []
161
- rubygems_version: 3.2.3
169
+ rubygems_version: 3.2.15
162
170
  signing_key:
163
171
  specification_version: 4
164
- summary: A little wrapper for Highline
172
+ summary: A little wrapper for HighLine
165
173
  test_files:
166
174
  - spec/spec_helper.rb
167
- - spec/highline_wrapper/client_spec.rb
175
+ - spec/highline_wrapper/checkbox_question_spec.rb
176
+ - spec/highline_wrapper/multiple_choice_question_spec.rb
177
+ - spec/highline_wrapper/open_ended_question_spec.rb
178
+ - spec/highline_wrapper/question_spec.rb
179
+ - spec/highline_wrapper/yes_no_question_spec.rb
data/LICENSE DELETED
@@ -1,22 +0,0 @@
1
- Copyright (c) 2021 Emma Sax
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -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