highline_wrapper 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,226 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'highline_wrapper'
5
-
6
- describe HighlineWrapper::CheckboxQuestion do
7
- let(:choices) { %w[one two three] }
8
- let(:response) { double(:response, readline: true, to_i: 3) }
9
- let(:highline) { double(:highline_cli, ask: response) }
10
-
11
- before do
12
- allow(HighLine).to receive(:new).and_return(highline)
13
- allow(subject).to receive(:puts)
14
- end
15
-
16
- after do
17
- HighlineWrapper::CheckboxQuestion.instance_variable_set('@highline', nil)
18
- end
19
-
20
- subject { HighlineWrapper::CheckboxQuestion }
21
-
22
- context 'with the options as defaults' do
23
- let(:options) do
24
- {
25
- with_indexes: false,
26
- defaults: [],
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, choices, options)
34
- end
35
-
36
- it 'should return the value the user selects' do
37
- allow(highline).to receive(:ask).and_return('1,3')
38
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
39
- expect(resp).to eq([{ value: 'one' }, { value: 'three' }])
40
- end
41
-
42
- it 'should return empty array if the user skips' do
43
- allow(highline).to receive(:ask).and_return('')
44
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
45
- expect(resp).to eq([])
46
- end
47
-
48
- it 'should call the return empty defaults if the user skips' do
49
- allow(highline).to receive(:ask).and_return('')
50
- expect(subject).to receive(:return_empty_defaults)
51
- subject.ask(Faker::Lorem.sentence, choices, options)
52
- end
53
- end
54
-
55
- context 'with required set to true' do
56
- context 'with_indexes set to false' do
57
- let(:options) do
58
- {
59
- with_indexes: false,
60
- defaults: [],
61
- required: true
62
- }
63
- end
64
-
65
- it 'should return the value the user selects' do
66
- allow(highline).to receive(:ask).and_return('1,2')
67
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
68
- expect(resp).to eq([{ value: 'one' }, { value: 'two' }])
69
- end
70
-
71
- it 'should recurse multiple times if the user skips' do
72
- allow(highline).to receive(:ask).and_return('', '', '3')
73
- expect(subject).to receive(:ask).exactly(3).times.and_call_original
74
- subject.ask(Faker::Lorem.sentence, choices, options)
75
- end
76
- end
77
-
78
- context 'with_indexes set to true' do
79
- let(:options) do
80
- {
81
- with_indexes: true,
82
- defaults: [],
83
- required: true
84
- }
85
- end
86
-
87
- it 'should return the value the user selects' do
88
- allow(highline).to receive(:ask).and_return('1, 3')
89
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
90
- expect(resp).to eq([{ value: 'one', index: 0 }, { value: 'three', index: 2 }])
91
- end
92
-
93
- it 'should recurse multiple times if the user skips' do
94
- allow(highline).to receive(:ask).and_return('', '', '3')
95
- expect(subject).to receive(:ask).exactly(3).times.and_call_original
96
- subject.ask(Faker::Lorem.sentence, choices, options)
97
- end
98
- end
99
- end
100
-
101
- context 'with required set to false' do
102
- context 'with_indexes set to false' do
103
- context 'with defaults set' do
104
- let(:options) do
105
- {
106
- indicate_default_message: true,
107
- with_indexes: false,
108
- defaults: ['two'],
109
- required: false
110
- }
111
- end
112
-
113
- it 'should return the value the user selects' do
114
- allow(highline).to receive(:ask).and_return('1, 2')
115
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
116
- expect(resp).to eq([{ value: 'one' }, { value: 'two' }])
117
- end
118
-
119
- it 'should return the default if the user skips' do
120
- allow(highline).to receive(:ask).and_return('')
121
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
122
- expect(resp).to eq([{ value: 'two' }])
123
- end
124
-
125
- it 'should call to return the defaults if the user skips' do
126
- allow(highline).to receive(:ask).and_return('')
127
- expect(subject).to receive(:return_defaults).and_call_original
128
- expect(subject).to receive(:puts)
129
- subject.ask(Faker::Lorem.sentence, choices, options)
130
- end
131
-
132
- context 'when the indicate_default_message is false' do
133
- let(:options) do
134
- {
135
- indicate_default_message: false,
136
- with_indexes: false,
137
- defaults: ['two'],
138
- required: false
139
- }
140
- end
141
-
142
- it 'should call to return the defaults if the user skips' do
143
- allow(highline).to receive(:ask).and_return('')
144
- expect(subject).to receive(:return_defaults)
145
- expect(subject).not_to receive(:puts)
146
- subject.ask(Faker::Lorem.sentence, choices, options)
147
- end
148
- end
149
- end
150
-
151
- context 'with defaults set to []' do
152
- let(:options) do
153
- {
154
- with_indexes: false,
155
- defaults: [],
156
- required: false
157
- }
158
- end
159
-
160
- it 'should return the value the user selects' do
161
- allow(highline).to receive(:ask).and_return('1,3')
162
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
163
- expect(resp).to eq([{ value: 'one' }, { value: 'three' }])
164
- end
165
-
166
- it 'should return nil if the user skips' do
167
- allow(highline).to receive(:ask).and_return('')
168
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
169
- expect(resp).to eq([])
170
- end
171
- end
172
- end
173
-
174
- context 'with_indexes set to true' do
175
- context 'with default set' do
176
- let(:options) do
177
- {
178
- with_indexes: true,
179
- defaults: ['two'],
180
- required: false
181
- }
182
- end
183
-
184
- it 'should return the value the user selects' do
185
- allow(highline).to receive(:ask).and_return('1,2')
186
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
187
- expect(resp).to eq([{ value: 'one', index: 0 }, { value: 'two', index: 1 }])
188
- end
189
-
190
- it 'should return the default if the user skips' do
191
- allow(highline).to receive(:ask).and_return('')
192
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
193
- expect(resp).to eq([{ value: 'two', index: 1 }])
194
- end
195
- end
196
-
197
- context 'with default nil' do
198
- let(:options) do
199
- {
200
- with_indexes: true,
201
- defaults: [],
202
- required: false
203
- }
204
- end
205
-
206
- it 'should return the value the user selects' do
207
- allow(highline).to receive(:ask).and_return('1')
208
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
209
- expect(resp).to eq([{ value: 'one', index: 0 }])
210
- end
211
-
212
- it 'should return [] if the user skips' do
213
- allow(highline).to receive(:ask).and_return('')
214
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
215
- expect(resp).to eq([])
216
- end
217
-
218
- it 'should call the return empty defaults message' do
219
- allow(highline).to receive(:ask).and_return('')
220
- expect(subject).to receive(:return_empty_defaults)
221
- subject.ask(Faker::Lorem.sentence, choices, options)
222
- end
223
- end
224
- end
225
- end
226
- end
@@ -1,226 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
- require 'highline_wrapper'
5
-
6
- describe HighlineWrapper::MultipleChoiceQuestion do
7
- let(:choices) { %w[one two three] }
8
- let(:response) { double(:response, readline: true, to_i: 3) }
9
- let(:highline) { double(:highline_cli, ask: response) }
10
-
11
- before do
12
- allow(HighLine).to receive(:new).and_return(highline)
13
- allow(subject).to receive(:puts)
14
- end
15
-
16
- after do
17
- HighlineWrapper::MultipleChoiceQuestion.instance_variable_set('@highline', nil)
18
- end
19
-
20
- subject { HighlineWrapper::MultipleChoiceQuestion }
21
-
22
- context 'with the options as defaults' do
23
- let(:options) do
24
- {
25
- with_index: false,
26
- default: nil,
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, choices, options)
34
- end
35
-
36
- it 'should return the value the user selects' do
37
- allow(highline).to receive(:ask).and_return(1)
38
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
39
- expect(resp).to eq({ value: 'one' })
40
- end
41
-
42
- it 'should return nil if the user skips' do
43
- allow(highline).to receive(:ask).and_return(0)
44
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
45
- expect(resp).to eq(nil)
46
- end
47
-
48
- it 'should call the return empty defaults if the user skips' do
49
- allow(highline).to receive(:ask).and_return('')
50
- expect(subject).to receive(:return_empty_defaults)
51
- subject.ask(Faker::Lorem.sentence, choices, options)
52
- end
53
- end
54
-
55
- context 'with required set to true' do
56
- context 'with_index set to false' do
57
- let(:options) do
58
- {
59
- with_index: false,
60
- default: Faker::Lorem.word,
61
- required: true
62
- }
63
- end
64
-
65
- it 'should return the value the user selects' do
66
- allow(highline).to receive(:ask).and_return(1)
67
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
68
- expect(resp).to eq({ value: 'one' })
69
- end
70
-
71
- it 'should recurse multiple times if the user skips' do
72
- allow(highline).to receive(:ask).and_return(0, 0, 3)
73
- expect(subject).to receive(:ask).exactly(3).times.and_call_original
74
- subject.ask(Faker::Lorem.sentence, choices, options)
75
- end
76
- end
77
-
78
- context 'with_index set to true' do
79
- let(:options) do
80
- {
81
- with_index: true,
82
- default: nil,
83
- required: true
84
- }
85
- end
86
-
87
- it 'should return the value the user selects' do
88
- allow(highline).to receive(:ask).and_return(1)
89
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
90
- expect(resp).to eq({ value: 'one', index: 0 })
91
- end
92
-
93
- it 'should recurse multiple times if the user skips' do
94
- allow(highline).to receive(:ask).and_return(0, 0, 3)
95
- expect(subject).to receive(:ask).exactly(3).times.and_call_original
96
- subject.ask(Faker::Lorem.sentence, choices, options)
97
- end
98
- end
99
- end
100
-
101
- context 'with required set to false' do
102
- context 'with_index set to false' do
103
- context 'with default set' do
104
- let(:options) do
105
- {
106
- indicate_default_message: true,
107
- with_index: false,
108
- default: 'two',
109
- required: false
110
- }
111
- end
112
-
113
- it 'should return the value the user selects' do
114
- allow(highline).to receive(:ask).and_return(1)
115
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
116
- expect(resp).to eq({ value: 'one' })
117
- end
118
-
119
- it 'should return the default if the user skips' do
120
- allow(highline).to receive(:ask).and_return(0)
121
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
122
- expect(resp).to eq({ value: 'two' })
123
- end
124
-
125
- it 'should call to return the defaults if the user skips' do
126
- allow(highline).to receive(:ask).and_return(0)
127
- expect(subject).to receive(:return_defaults).and_call_original
128
- expect(subject).to receive(:puts)
129
- subject.ask(Faker::Lorem.sentence, choices, options)
130
- end
131
-
132
- context 'when the indicate_default_message is false' do
133
- let(:options) do
134
- {
135
- indicate_default_message: false,
136
- with_indexes: false,
137
- default: 'two',
138
- required: false
139
- }
140
- end
141
-
142
- it 'should call to return the defaults if the user skips' do
143
- allow(highline).to receive(:ask).and_return(0)
144
- expect(subject).to receive(:return_defaults)
145
- expect(subject).not_to receive(:puts)
146
- subject.ask(Faker::Lorem.sentence, choices, options)
147
- end
148
- end
149
- end
150
-
151
- context 'with default nil' do
152
- let(:options) do
153
- {
154
- with_index: false,
155
- default: nil,
156
- required: false
157
- }
158
- end
159
-
160
- it 'should return the value the user selects' do
161
- allow(highline).to receive(:ask).and_return(1)
162
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
163
- expect(resp).to eq({ value: 'one' })
164
- end
165
-
166
- it 'should return nil if the user skips' do
167
- allow(highline).to receive(:ask).and_return(0)
168
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
169
- expect(resp).to eq(nil)
170
- end
171
- end
172
- end
173
-
174
- context 'with_index set to true' do
175
- context 'with default set' do
176
- let(:options) do
177
- {
178
- with_index: true,
179
- default: 'two',
180
- required: false
181
- }
182
- end
183
-
184
- it 'should return the value the user selects' do
185
- allow(highline).to receive(:ask).and_return(1)
186
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
187
- expect(resp).to eq({ value: 'one', index: 0 })
188
- end
189
-
190
- it 'should return the default if the user skips' do
191
- allow(highline).to receive(:ask).and_return(0)
192
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
193
- expect(resp).to eq({ value: 'two', index: 1 })
194
- end
195
- end
196
-
197
- context 'with default nil' do
198
- let(:options) do
199
- {
200
- with_index: true,
201
- default: nil,
202
- required: false
203
- }
204
- end
205
-
206
- it 'should return the value the user selects' do
207
- allow(highline).to receive(:ask).and_return(1)
208
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
209
- expect(resp).to eq({ value: 'one', index: 0 })
210
- end
211
-
212
- it 'should return nil if the user skips' do
213
- allow(highline).to receive(:ask).and_return(0)
214
- resp = subject.ask(Faker::Lorem.sentence, choices, options)
215
- expect(resp).to eq(nil)
216
- end
217
-
218
- it 'should call the return empty defaults message' do
219
- allow(highline).to receive(:ask).and_return(0)
220
- expect(subject).to receive(:return_empty_defaults)
221
- subject.ask(Faker::Lorem.sentence, choices, options)
222
- end
223
- end
224
- end
225
- end
226
- end
@@ -1,108 +0,0 @@
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
@@ -1,114 +0,0 @@
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