cliprompt 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03cc6802622eb3c7eb64f31d500a384f5d1ef049
4
- data.tar.gz: 20bdbb38baf9f01106dc7f969346467ac6b20a61
3
+ metadata.gz: 8f310eea2a9640504286446d80f3fe6d22a65607
4
+ data.tar.gz: 187a52dd0480192fe77571d0b937f8c2b4c66c4c
5
5
  SHA512:
6
- metadata.gz: 58a33b33150b6dd8722725ba1be6c55c77c9b9fb36b9b1ebaad9b7b183c5c3da84e020d065c701d59a092f68550d089044ef1294870b7beb7a43cf7ab2d3881f
7
- data.tar.gz: cbb7e51ad1548c61b8c4c33c2ae2ecebd9bbe4e8732fd7abae7146dbc3ad2cd34cef5a3774a536992c1b1ae52212e42736774f434af5710409d06662df3d3468
6
+ metadata.gz: e74304a82d4c37c39f3d5d2289cb8239635ec57738e4bf36d250745dcdb032ae8cd20494901fa8ad2870a01c4010678d0c9385968910574abf1b05b4f7a968cf
7
+ data.tar.gz: 2b7acdb26a178e024213beac9a4be22f5e6f08f6ae5cbc7ebca69a9b781aa7fed84f198f8ca9947f70c6bac64da62ca14894371159f92a13aa909a6f369e5bda
data/CHANGELOG.md CHANGED
@@ -1,7 +1,13 @@
1
1
  Cliprompt Changelog
2
2
  =====================
3
3
 
4
- v0.0.1 - wip
4
+ v0.0.2 - 2014-05-20
5
+ -----------
6
+
7
+ - fix case when numbers are used in choices
8
+ - added some test coverage for further evolution easiness
9
+
10
+ v0.0.1 - 2014-05-19
5
11
  -------------
6
12
 
7
13
  - first draft
data/README.md CHANGED
@@ -1,19 +1,19 @@
1
1
  Cliprompt
2
2
  ==============
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/cliprompt.png)](http://rubygems.org/gems/cliprompt)
4
5
  [![Build Status](https://travis-ci.org/mose/cliprompt.png?branch=master)](https://travis-ci.org/mose/cliprompt)
5
6
  [![Coverage Status](https://coveralls.io/repos/mose/cliprompt/badge.png?branch=master)](https://coveralls.io/r/mose/cliprompt?branch=master)
6
7
  [![Dependency Status](https://gemnasium.com/mose/cliprompt.svg)](https://gemnasium.com/mose/cliprompt)
7
8
  [![Code Climate](https://codeclimate.com/github/mose/cliprompt.png)](https://codeclimate.com/github/mose/cliprompt)
8
9
 
9
- This library provides a simple DSL for managing user interaction in a CLI application.
10
+ This library provides a simple DSL for managing user interaction in a CLI application. Still under development, not stable yet.
10
11
 
11
12
  Features
12
13
  ----------
13
14
 
14
- - manages questions, choices, default values, yes/no values
15
- - makes possible to have env vars set for defaults
16
- - makes possible to colorize output
15
+ - manages questions, choices, default values, yes/no values (done)
16
+ - makes possible to have env vars set for defaults (todo)
17
17
 
18
18
  Usage
19
19
  ----------
data/cliprompt.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^spec/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency 'dye'
21
+ spec.add_dependency 'paint'
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.6"
24
24
  spec.add_development_dependency "rake"
data/example.rb CHANGED
@@ -7,6 +7,10 @@ class Myclass
7
7
  end
8
8
 
9
9
  def askit
10
+ puts '-------------------'
11
+ puts 'numeric default'
12
+ show "what is your age?", 42
13
+ show "what is your size?", 6.2
10
14
  puts '-------------------'
11
15
  puts 'Free form'
12
16
  show "This simply ask for a simple form mandatory thing?"
@@ -22,10 +26,10 @@ class Myclass
22
26
  show 'a boolean?', boolean: true, default: false
23
27
  puts '-------------------'
24
28
  puts 'a list of choices'
25
- show 'a list without default?', ['22', '33', '44', '55']
26
- show 'a list without default?', choices: ['22', '33', '44', '55'], default: '22'
29
+ show 'a list without default?', [22, 33, 44, '55']
27
30
  show 'a list with default?', ['22', '33', '=44', '55']
28
- show 'a list with default?', choices: ['22', '33', '=44', '55']
31
+ show 'a list without default?', choices: ['22', '33', '44', '55'], default: 22
32
+ show 'a list with default?', choices: ['22', 33, '=44', '55']
29
33
  end
30
34
 
31
35
  def show(*args)
@@ -12,16 +12,18 @@ module Cliprompt
12
12
  @default = nil
13
13
  @boolean = false
14
14
  @envdefault = nil
15
- unless options.nil?
16
- meth = "parse_#{options.class.name.downcase}".to_sym
17
- if respond_to? meth
18
- send(meth, options)
19
- else
20
- fail OptionException, "Undefined parser ::#{meth}"
21
- end
15
+ @type = options.class.name.downcase
16
+ meth = "parse_#{@type}".to_sym
17
+ if respond_to? meth
18
+ send(meth, options)
19
+ else
20
+ fail OptionException, "Undefined parser ::#{meth}"
22
21
  end
23
22
  end
24
23
 
24
+ def parse_nilclass(args)
25
+ end
26
+
25
27
  def parse_hash(args)
26
28
  @choices = args[:choices] || args['choices'] || []
27
29
  parse_array @choices
@@ -36,7 +38,7 @@ module Cliprompt
36
38
  end
37
39
 
38
40
  def parse_array(args)
39
- @choices = args.map do |a|
41
+ @choices = args.map(&:to_s).map do |a|
40
42
  if a[0] && a[0] == '='
41
43
  @default = a[1..-1]
42
44
  else
@@ -45,6 +47,14 @@ module Cliprompt
45
47
  end
46
48
  end
47
49
 
50
+ def parse_fixnum(arg)
51
+ @default = arg.to_s
52
+ end
53
+
54
+ def parse_float(arg)
55
+ @default = arg.to_s
56
+ end
57
+
48
58
  def parse_string(arg)
49
59
  if arg.downcase.match /^y(es)?(\/)?n(o)?/
50
60
  @boolean = true
@@ -58,10 +68,6 @@ module Cliprompt
58
68
  end
59
69
  end
60
70
 
61
- def boolean?
62
- @boolean
63
- end
64
-
65
71
  def display
66
72
  back = ''
67
73
  if @boolean
@@ -75,5 +81,38 @@ module Cliprompt
75
81
  return back
76
82
  end
77
83
 
84
+ def validate(question, answer)
85
+ if answer == ''
86
+ check_default question
87
+ elsif @boolean
88
+ check_boolean question, answer
89
+ elsif @choices.count > 0
90
+ check_choices question, answer
91
+ else
92
+ answer
93
+ end
94
+ end
95
+
96
+ def check_default(question)
97
+ return ask_again(question, Cliprompt::MSG_MANDATORY_TEXT) if @default.nil?
98
+ @default
99
+ end
100
+
101
+ def check_boolean(question, answer)
102
+ answer.downcase!
103
+ return ask_again(question, Cliprompt::MSG_YES_OR_NO) unless /^(y(es)?|n(o)?)$/.match(answer)
104
+ !/^y(es)?$/.match(answer).nil?
105
+ end
106
+
107
+ def check_choices(question, answer)
108
+ return ask_again(question, Cliprompt::MSG_CHOSE_IN_LIST) unless @choices.include?(answer)
109
+ answer
110
+ end
111
+
112
+ def ask_again(question, msg)
113
+ Cliprompt.shout msg
114
+ Cliprompt.ask question, self
115
+ end
116
+
78
117
  end
79
118
  end
@@ -1,3 +1,3 @@
1
1
  module Cliprompt
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/cliprompt.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "dye"
1
+ require "paint"
2
2
 
3
3
  require "cliprompt/version"
4
4
  require "cliprompt/optionset"
@@ -7,15 +7,9 @@ module Cliprompt
7
7
 
8
8
  module_function
9
9
 
10
- CUSTOM_STYLES = {
11
- error: [ :bold, :red ],
12
- ok: [ :bold, :green ]
13
- }
14
- define_dye_method CUSTOM_STYLES
15
-
16
- MSG_MANDATORY_TEXT = Dye.dye("Sorry you need to fill that information.", [ :bold, :red ])
17
- MSG_YES_OR_NO = Dye.dye("You need to answer by yes, no, y or n.", [ :bold, :red ])
18
- MSG_CHOSE_IN_LIST = Dye.dye("You need to chose between the available options.", [ :bold, :red ])
10
+ MSG_MANDATORY_TEXT = "Sorry you need to fill that information."
11
+ MSG_YES_OR_NO = "You need to answer by yes, no, y or n."
12
+ MSG_CHOSE_IN_LIST = "You need to chose between the available options."
19
13
 
20
14
  def ask(question, *options)
21
15
  if options[0].class == Optionset
@@ -26,32 +20,15 @@ module Cliprompt
26
20
  output.print "#{question} #{opts.display} "
27
21
  answer = input.gets.chomp
28
22
  output.flush
29
- check(answer, question, opts)
23
+ opts.validate(question, answer)
30
24
  end
31
25
 
32
- def check(answer, question, opts)
33
- if answer == ''
34
- if !opts.default.nil?
35
- answer = opts.default
36
- else
37
- output.puts MSG_MANDATORY_TEXT
38
- ask(question, opts)
39
- end
40
- else
41
- if opts.boolean
42
- if /^(y(es)?|n(o)?)$/.match(answer.downcase)
43
- answer = !/^y(es)?$/.match(answer.downcase).nil?
44
- else
45
- output.puts MSG_YES_OR_NO
46
- ask(question, opts)
47
- end
48
- elsif opts.choices.count > 0 && !opts.choices.include?(answer)
49
- output.puts MSG_CHOSE_IN_LIST
50
- ask(question, opts)
51
- else
52
- answer
53
- end
54
- end
26
+ def say(message)
27
+ output.puts message
28
+ end
29
+
30
+ def shout(message)
31
+ output.puts Paint[message, :bold, :red ]
55
32
  end
56
33
 
57
34
  def setio(input, output)
@@ -1,24 +1,33 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
+ require 'cliprompt'
4
5
  require 'cliprompt/optionset'
5
6
 
6
7
  describe Cliprompt::Optionset do
7
8
 
9
+ describe '.new' do
10
+ context "when an unknown objkect is passed as option," do
11
+ When(:options) { Object.new }
12
+ Then { expect{ Cliprompt::Optionset.new(options) }.to raise_error(Cliprompt::OptionException) }
13
+ end
14
+ end
15
+
8
16
  describe '.parse_array' do
9
- Given(:options) { ['xxx', 'yyy', 'zzz'] }
10
17
 
11
18
  context "when there is no default (#{['xxx', 'yyy', 'zzz'].to_s})," do
19
+ Given(:options) { ['xxx', 'yyy', 'zzz'] }
12
20
  Given(:set) { Cliprompt::Optionset.new(options) }
13
21
  When(:choices) { set.choices }
14
22
  When(:default) { set.default }
15
23
  When(:display) { set.display }
16
24
  Then { expect(choices).to eq options }
17
25
  Then { expect(default).to be_false }
18
- Then { expect(display).to eq "(xxx / yyy / zzz)" }
26
+ Then { expect(display).to eq '(xxx / yyy / zzz)' }
19
27
  end
20
28
 
21
29
  context "when there is a default specified (#{['xxx', '=yyy', 'zzz'].to_s})," do
30
+ Given(:options) { ['xxx', 'yyy', 'zzz'] }
22
31
  Given(:options_with_default) { ['xxx', '=yyy', 'zzz'] }
23
32
  Given(:set) { Cliprompt::Optionset.new(options_with_default) }
24
33
  When(:choices) { set.choices }
@@ -26,13 +35,24 @@ describe Cliprompt::Optionset do
26
35
  When(:display) { set.display }
27
36
  Then { expect(choices).to eq options }
28
37
  Then { expect(default).to eq 'yyy' }
29
- Then { expect(display).to eq "(xxx / yyy / zzz)[yyy]" }
38
+ Then { expect(display).to eq '(xxx / yyy / zzz)[yyy]' }
39
+ end
40
+
41
+ context "when there is a mixed numeric and string choices (#{[22, 'yyy', 'zzz'].to_s})," do
42
+ Given(:options) { [22, 'yyy', 'zzz'] }
43
+ Given(:set) { Cliprompt::Optionset.new(options) }
44
+ When(:choices) { set.choices }
45
+ When(:default) { set.default }
46
+ When(:display) { set.display }
47
+ Then { expect(choices).to eq options.map(&:to_s) }
48
+ Then { expect(default).to be_false }
49
+ Then { expect(display).to eq '(22 / yyy / zzz)' }
30
50
  end
31
51
  end
32
52
 
33
53
  describe '.parse_hash' do
34
54
 
35
- context "when there is choices and default," do
55
+ context 'when there is choices and default,' do
36
56
  context "when using sym keys (#{{ default: 'xxx', choices: ['xxx', 'yyy', 'zzz'] }.to_s})," do
37
57
  Given(:options) { { default: 'xxx', choices: ['xxx', 'yyy', 'zzz'] } }
38
58
  Given(:set) { Cliprompt::Optionset.new(options) }
@@ -41,7 +61,7 @@ describe Cliprompt::Optionset do
41
61
  When(:display) { set.display }
42
62
  Then { expect(choices).to eq ['xxx', 'yyy', 'zzz'] }
43
63
  Then { expect(default).to eq 'xxx' }
44
- Then { expect(display).to eq "(xxx / yyy / zzz)[xxx]" }
64
+ Then { expect(display).to eq '(xxx / yyy / zzz)[xxx]' }
45
65
  end
46
66
  context "when using string keys (#{{ 'default' => 'xxx', 'choices' => ['xxx', 'yyy', 'zzz'] }.to_s})," do
47
67
  Given(:options) { { 'default' => 'xxx', 'choices' => ['xxx', 'yyy', 'zzz'] } }
@@ -51,12 +71,12 @@ describe Cliprompt::Optionset do
51
71
  When(:display) { set.display }
52
72
  Then { expect(choices).to eq ['xxx', 'yyy', 'zzz'] }
53
73
  Then { expect(default).to eq 'xxx' }
54
- Then { expect(display).to eq "(xxx / yyy / zzz)[xxx]" }
74
+ Then { expect(display).to eq '(xxx / yyy / zzz)[xxx]' }
55
75
  end
56
76
  end
57
77
 
58
- context "when there is only boolean," do
59
- context "when no default is given, default booean is Y," do
78
+ context 'when there is only boolean,' do
79
+ context 'when no default is given, default booean is Y,' do
60
80
  Given(:options) { { boolean: true } }
61
81
  Given(:set) { Cliprompt::Optionset.new(options) }
62
82
  When(:default) { set.default }
@@ -64,9 +84,9 @@ describe Cliprompt::Optionset do
64
84
  When(:boolean) { set.boolean }
65
85
  Then { expect(default).to be_true }
66
86
  Then { expect(boolean).to be_true }
67
- Then { expect(display).to eq "[Y/n]" }
87
+ Then { expect(display).to eq '[Y/n]' }
68
88
  end
69
- context "when default is given as true, default booean is Y," do
89
+ context 'when default is given as true, default booean is Y,' do
70
90
  Given(:options) { { boolean: true, default: true } }
71
91
  Given(:set) { Cliprompt::Optionset.new(options) }
72
92
  When(:default) { set.default }
@@ -74,9 +94,9 @@ describe Cliprompt::Optionset do
74
94
  When(:boolean) { set.boolean }
75
95
  Then { expect(default).to be_true }
76
96
  Then { expect(boolean).to be_true }
77
- Then { expect(display).to eq "[Y/n]" }
97
+ Then { expect(display).to eq '[Y/n]' }
78
98
  end
79
- context "when default is given as false, default boolean is N," do
99
+ context 'when default is given as false, default boolean is N,' do
80
100
  Given(:options) { { boolean: true, default: false } }
81
101
  Given(:set) { Cliprompt::Optionset.new(options) }
82
102
  When(:default) { set.default }
@@ -84,13 +104,34 @@ describe Cliprompt::Optionset do
84
104
  When(:boolean) { set.boolean }
85
105
  Then { expect(default).to be_false }
86
106
  Then { expect(boolean).to be_true }
87
- Then { expect(display).to eq "[y/N]" }
107
+ Then { expect(display).to eq '[y/N]' }
88
108
  end
89
109
  end
90
110
  end
91
111
 
112
+ describe '.parse_fixnum' do
113
+ Given(:options) { 42 }
114
+ Given(:set) { Cliprompt::Optionset.new(options) }
115
+ When(:default) { set.default }
116
+ Then { expect(default).to eq options.to_s }
117
+ end
118
+
119
+ describe '.parse_fixnum' do
120
+ Given(:options) { 3.14 }
121
+ Given(:set) { Cliprompt::Optionset.new(options) }
122
+ When(:default) { set.default }
123
+ Then { expect(default).to eq options.to_s }
124
+ end
125
+
92
126
  describe '.parse_string' do
93
127
 
128
+ context 'when a random string is passed, it is the default,' do
129
+ Given(:options) { 'something' }
130
+ Given(:set) { Cliprompt::Optionset.new(options) }
131
+ When(:default) { set.default }
132
+ Then { expect(default).to eq options }
133
+ end
134
+
94
135
  context 'when a "yesno" kind of string is passed,' do
95
136
  context 'when using yesno,' do
96
137
  Given(:options) { 'yesno' }
@@ -100,50 +141,156 @@ describe Cliprompt::Optionset do
100
141
  When(:boolean) { set.boolean }
101
142
  Then { expect(default).to be_true }
102
143
  Then { expect(boolean).to be_true }
103
- Then { expect(display).to eq "[Y/n]" }
144
+ Then { expect(display).to eq '[Y/n]' }
104
145
  end
105
- context "when using yn," do
146
+ context 'when using yn,' do
106
147
  Given(:options) { 'yn' }
107
148
  Given(:set) { Cliprompt::Optionset.new(options) }
108
149
  When(:default) { set.default }
109
150
  When(:display) { set.display }
110
151
  Then { expect(default).to be_true }
111
- Then { expect(display).to eq "[Y/n]" }
152
+ Then { expect(display).to eq '[Y/n]' }
112
153
  end
113
- context "when using YN," do
154
+ context 'when using YN,' do
114
155
  Given(:options) { 'YN' }
115
156
  Given(:set) { Cliprompt::Optionset.new(options) }
116
157
  When(:default) { set.default }
117
158
  When(:display) { set.display }
118
159
  Then { expect(default).to be_true }
119
- Then { expect(display).to eq "[Y/n]" }
160
+ Then { expect(display).to eq '[Y/n]' }
120
161
  end
121
- context "when using yesNo," do
162
+ context 'when using yesNo,' do
122
163
  Given(:options) { 'yesNo' }
123
164
  Given(:set) { Cliprompt::Optionset.new(options) }
124
165
  When(:default) { set.default }
125
166
  When(:display) { set.display }
126
167
  Then { expect(default).to be_false }
127
- Then { expect(display).to eq "[y/N]" }
168
+ Then { expect(display).to eq '[y/N]' }
128
169
  end
129
- context "when using yN," do
170
+ context 'when using yN,' do
130
171
  Given(:options) { 'yesNo' }
131
172
  Given(:set) { Cliprompt::Optionset.new(options) }
132
173
  When(:default) { set.default }
133
174
  When(:display) { set.display }
134
175
  Then { expect(default).to be_false }
135
- Then { expect(display).to eq "[y/N]" }
176
+ Then { expect(display).to eq '[y/N]' }
136
177
  end
137
- context "when using y/N," do
178
+ context 'when using y/N,' do
138
179
  Given(:options) { 'yesNo' }
139
180
  Given(:set) { Cliprompt::Optionset.new(options) }
140
181
  When(:default) { set.default }
141
182
  When(:display) { set.display }
142
183
  Then { expect(default).to be_false }
143
- Then { expect(display).to eq "[y/N]" }
184
+ Then { expect(display).to eq '[y/N]' }
144
185
  end
145
186
  end
146
187
 
147
188
  end
148
189
 
190
+ describe '.validate' do
191
+ Given(:question) { 'so what?' }
192
+ context 'when enter key is hit,' do
193
+ Given(:input) { '' }
194
+ Given(:set) { Cliprompt::Optionset.new() }
195
+ When { set.stub(:check_default).with(question) }
196
+ Then { expect(set).to receive(:check_default).with(question) }
197
+ And { expect{ set.validate(question, input) }.not_to raise_error }
198
+ end
199
+ context 'when it is a boolean,' do
200
+ Given(:input) { 'x' }
201
+ Given(:set) { Cliprompt::Optionset.new(boolean: true) }
202
+ When { set.stub(:check_boolean).with(question, input) }
203
+ Then { expect(set).to receive(:check_boolean).with(question, input) }
204
+ And { expect{ set.validate(question, input) }.not_to raise_error }
205
+ end
206
+ context 'when it is a choice list,' do
207
+ Given(:input) { 'x' }
208
+ Given(:set) { Cliprompt::Optionset.new(choices: [1,2,3]) }
209
+ When { set.stub(:check_choices).with(question, input) }
210
+ Then { expect(set).to receive(:check_choices).with(question, input) }
211
+ And { expect{ set.validate(question, input) }.not_to raise_error }
212
+ end
213
+ context 'when it is not a mandatory question, a boolean nor a choice list' do
214
+ Given(:input) { 'something' }
215
+ Given(:set) { Cliprompt::Optionset.new() }
216
+ Then { expect(set.validate(question, input)).to eq input }
217
+ end
218
+ end
219
+
220
+ describe '.check_default' do
221
+ Given(:question) { 'so what?' }
222
+ Given(:msg) { Cliprompt::MSG_MANDATORY_TEXT }
223
+ context 'when there is no default set,' do
224
+ Given(:set) { Cliprompt::Optionset.new() }
225
+ When { set.stub(:ask_again).with(question, msg) }
226
+ Then { expect(set).to receive(:ask_again).with(question, msg) }
227
+ And { expect{ set.check_default(question) }.not_to raise_error }
228
+ end
229
+ context 'when there is a default set,' do
230
+ Given(:default) { 'x' }
231
+ Given(:set) { Cliprompt::Optionset.new(default: default) }
232
+ When(:response) { set.check_default(question) }
233
+ Then { expect(response).to eq default }
234
+ end
235
+ end
236
+
237
+
238
+ describe '.check_boolean' do
239
+ Given(:question) { 'so what?' }
240
+ Given(:msg) { Cliprompt::MSG_YES_OR_NO }
241
+ Given(:set) { Cliprompt::Optionset.new(boolean: true) }
242
+ context 'when a non yes-no answer is given,' do
243
+ When(:input) { 'xxx' }
244
+ When { set.stub(:ask_again).with(question, msg) }
245
+ Then { expect(set).to receive(:ask_again).with(question, msg) }
246
+ And { expect{ set.check_boolean(question, input) }.not_to raise_error }
247
+ end
248
+ context 'when a no answer is given,' do
249
+ When(:input) { 'no' }
250
+ Then { expect(set.check_boolean(question, input)).to be_false }
251
+ end
252
+ context 'when a N answer is given,' do
253
+ When(:input) { 'N' }
254
+ Then { expect(set.check_boolean(question, input)).to be_false }
255
+ end
256
+ context 'when a yes answer is given,' do
257
+ When(:input) { 'yes' }
258
+ Then { expect(set.check_boolean(question, input)).to be_true }
259
+ end
260
+ context 'when a Y answer is given,' do
261
+ When(:input) { 'Y' }
262
+ Then { expect(set.check_boolean(question, input)).to be_true }
263
+ end
264
+ end
265
+
266
+ describe '.check_choices' do
267
+ Given(:question) { 'so what?' }
268
+ Given(:msg) { Cliprompt::MSG_CHOSE_IN_LIST }
269
+ Given(:choices) { %w(a b c) }
270
+ Given(:set) { Cliprompt::Optionset.new(choices: choices) }
271
+ context 'when answer is not in choices list,' do
272
+ When(:input) { 'x' }
273
+ When { set.stub(:ask_again).with(question, msg) }
274
+ Then { expect(set).to receive(:ask_again).with(question, msg) }
275
+ And { expect{ set.check_choices(question, input) }.not_to raise_error }
276
+ end
277
+ context 'when answer is in choices list,' do
278
+ When(:input) { 'a' }
279
+ When(:response) { set.check_choices(question, input) }
280
+ Then { expect(response).to eq input }
281
+ end
282
+ end
283
+
284
+ describe '.ask_again' do
285
+ Given(:question) { 'so what?' }
286
+ Given(:msg) { 'heho gimme something' }
287
+ Given(:set) { Cliprompt::Optionset.new() }
288
+ When { Cliprompt.stub(:shout).with(msg) }
289
+ When { Cliprompt.stub(:ask).with(question, set) }
290
+ Then { expect(Cliprompt).to receive(:shout).with(msg) }
291
+ And { expect(Cliprompt).to receive(:ask).with(question, set) }
292
+ And { expect{ set.ask_again(question, msg)}.not_to raise_error }
293
+ end
294
+
295
+
149
296
  end
@@ -2,43 +2,47 @@
2
2
 
3
3
  require 'spec_helper'
4
4
  require 'cliprompt'
5
+ require 'cliprompt/optionset'
5
6
 
6
7
  describe Cliprompt do
8
+ Given(:input) { StringIO.new }
9
+ Given(:output) { StringIO.new }
10
+ Given { subject.setio(input, output) }
7
11
 
8
12
  describe '.ask' do
9
- Given(:input) { StringIO.new }
10
- Given(:output) { StringIO.new }
11
- Given { subject.setio(input, output) }
12
-
13
- # context 'when it is free form,' do
14
- # When(:question) { 'wazza?' }
15
- # context 'without default,' do
16
- # When(:args) { }
17
- # When { subject.input.stub(:gets).and_return("\n") }
18
- # When { subject.ask(question, args) }
19
- # Then { expect(output).to receive(:print).with("#{question} ") }
20
- # context 'when enter key is used,' do
21
- # When(:answer) { subject.ask(question) }
22
- # When { input.gets("\n") }
23
- # Then { expect(output).to receive(:puts).with(Cliprompt::MSG_MANDATORY_TEXT)}
24
- # end
25
- # end
26
- # it 'is displayed ay the end of the question' do
27
- # end
28
- # end
29
- context 'when there is no preset choices,' do
30
- it 'only displays the question' do
31
- end
13
+ Given(:question) { 'wazza?' }
14
+ Given(:answer) { 'xxx' }
15
+ context 'without default,' do
16
+ When(:args) { }
17
+ When { input.stub(:gets).and_return answer }
18
+ Then { expect(subject.ask(question, args)).to eq answer }
19
+ And { expect(output.string).to eq "#{question} " }
32
20
  end
33
- context 'when enter is typed,' do
34
- it 'returns the default value' do
35
- end
21
+ context 'with a default,' do
22
+ When(:default) { 'ooo' }
23
+ When(:args) { { default: default } }
24
+ When { input.stub(:gets).and_return answer }
25
+ Then { expect(subject.ask(question, args)).to eq answer }
26
+ And { expect(output.string).to eq "#{question} [#{default}] " }
36
27
  end
37
- context 'when a value is provided' do
38
- it 'returns the value' do
39
- end
28
+ context 'with an optionset,' do
29
+ When(:args) { Cliprompt::Optionset.new() }
30
+ When { input.stub(:gets).and_return answer }
31
+ Then { expect(subject.ask(question, args)).to eq answer }
32
+ And { expect(output.string).to eq "#{question} " }
40
33
  end
34
+ end
35
+
36
+ describe '.say' do
37
+ Given(:msg) { "hah" }
38
+ When { subject.say msg }
39
+ Then { expect(output.string).to eq "#{msg}\n" }
40
+ end
41
41
 
42
+ describe '.shout' do
43
+ Given(:msg) { "hah" }
44
+ When { subject.shout msg }
45
+ Then { expect(output.string).to eq "#{Paint[msg, :bold, :red ]}\n" }
42
46
  end
43
47
 
44
48
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cliprompt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-19 00:00:00.000000000 Z
11
+ date: 2014-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: dye
14
+ name: paint
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="