qoobaa-user-choices 1.1.7

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.
Files changed (50) hide show
  1. data/.document +5 -0
  2. data/.gitignore +5 -0
  3. data/LICENSE +39 -0
  4. data/README.rdoc +7 -0
  5. data/Rakefile +56 -0
  6. data/VERSION +1 -0
  7. data/examples/older/README.txt +133 -0
  8. data/examples/older/command-line.rb +46 -0
  9. data/examples/older/default-values.rb +41 -0
  10. data/examples/older/multiple-sources.rb +58 -0
  11. data/examples/older/postprocess.rb +39 -0
  12. data/examples/older/switches.rb +44 -0
  13. data/examples/older/two-args.rb +31 -0
  14. data/examples/older/types.rb +61 -0
  15. data/examples/tutorial/css/LICENSE.txt +1 -0
  16. data/examples/tutorial/css/bg2.gif +0 -0
  17. data/examples/tutorial/css/left.gif +0 -0
  18. data/examples/tutorial/css/left_on.gif +0 -0
  19. data/examples/tutorial/css/main.css +242 -0
  20. data/examples/tutorial/css/right.gif +0 -0
  21. data/examples/tutorial/css/right_on.gif +0 -0
  22. data/examples/tutorial/css/tvline.gif +0 -0
  23. data/examples/tutorial/css/von-foerster.jpg +0 -0
  24. data/examples/tutorial/css/von-foerster2.jpg +0 -0
  25. data/examples/tutorial/index.html +703 -0
  26. data/examples/tutorial/tutorial1.rb +41 -0
  27. data/examples/tutorial/tutorial2.rb +44 -0
  28. data/examples/tutorial/tutorial3.rb +47 -0
  29. data/examples/tutorial/tutorial4.rb +47 -0
  30. data/examples/tutorial/tutorial5.rb +35 -0
  31. data/examples/tutorial/tutorial6.rb +35 -0
  32. data/examples/tutorial/tutorial7.rb +41 -0
  33. data/lib/user-choices.rb +131 -0
  34. data/lib/user-choices/arglist-strategies.rb +179 -0
  35. data/lib/user-choices/builder.rb +118 -0
  36. data/lib/user-choices/command-line-source.rb +224 -0
  37. data/lib/user-choices/command.rb +42 -0
  38. data/lib/user-choices/conversions.rb +169 -0
  39. data/lib/user-choices/ruby-extensions.rb +20 -0
  40. data/lib/user-choices/sources.rb +278 -0
  41. data/lib/user-choices/version.rb +3 -0
  42. data/test/arglist_strategy_test.rb +42 -0
  43. data/test/builder_test.rb +631 -0
  44. data/test/command_line_source_test.rb +443 -0
  45. data/test/conversion_test.rb +172 -0
  46. data/test/source_test.rb +451 -0
  47. data/test/test_helper.rb +9 -0
  48. data/test/user_choices_slow_test.rb +276 -0
  49. data/user-choices.gemspec +104 -0
  50. metadata +122 -0
@@ -0,0 +1,276 @@
1
+ require 'test/unit'
2
+ require 's4t-utils'
3
+ require 'user-choices'
4
+ include S4tUtils
5
+ set_test_paths(__FILE__)
6
+
7
+
8
+
9
+ class Examples < Test::Unit::TestCase
10
+ def evalue(command)
11
+ result = `#{command}`
12
+ eval(result)
13
+ end
14
+
15
+ LIB="-I#{PACKAGE_ROOT}/lib"
16
+ EX = "ruby #{LIB} #{PACKAGE_ROOT}/examples/older/"
17
+ TUT = "ruby #{LIB} #{PACKAGE_ROOT}/examples/tutorial/"
18
+
19
+ require "#{PACKAGE_ROOT}/examples/older/command-line"
20
+ # require "#{PACKAGE_ROOT}/examples/default-values" # not needed
21
+ require "#{PACKAGE_ROOT}/examples/older/multiple-sources"
22
+ # require "#{PACKAGE_ROOT}/examples/postprocess" # not needed
23
+ require "#{PACKAGE_ROOT}/examples/older/switches"
24
+ require "#{PACKAGE_ROOT}/examples/older/two-args"
25
+ require "#{PACKAGE_ROOT}/examples/older/types"
26
+
27
+
28
+
29
+ def test_succeeding_examples
30
+ val = evalue("#{EX}command-line.rb --choice cho sophie paul dawn me")
31
+ assert_equal({:names => ["sophie", "paul", "dawn", "me"],
32
+ :choice=>"cho"},
33
+ val)
34
+
35
+ val = evalue("#{EX}command-line.rb -c choice")
36
+ assert_equal({:names => [], :choice => "choice"}, val)
37
+
38
+ val = evalue("#{EX}command-line.rb -cchoice")
39
+ assert_equal({:names => [], :choice => "choice"}, val)
40
+
41
+ val = evalue("#{EX}command-line.rb --choi choice")
42
+ assert_equal({:names => [], :choice => "choice"}, val)
43
+
44
+ val = evalue("#{EX}command-line.rb --choi choice -- -name1- -name2-")
45
+ assert_equal({:names => ['-name1-', '-name2-'], :choice => 'choice'}, val)
46
+
47
+
48
+ val = evalue("#{EX}default-values.rb --choice specific")
49
+ assert_equal({:choice => 'specific'}, val)
50
+
51
+ val = evalue("#{EX}default-values.rb")
52
+ assert_equal({:choice => 'default'}, val)
53
+
54
+ val = evalue("#{EX}default-values.rb only-arg")
55
+ assert_equal({:choice => 'default', :name => 'only-arg'}, val)
56
+
57
+
58
+ val = evalue("#{EX}types.rb --must-be-integer 3 argument")
59
+ assert_equal({:arg => 'argument', :must_be_integer => 3}, val)
60
+
61
+
62
+ val = evalue("#{EX}switches.rb 1 2")
63
+ assert_equal({:switch=> false, :args => ['1', '2']}, val)
64
+
65
+ val = evalue("#{EX}switches.rb --switch 1 2")
66
+ assert_equal({:switch=> true, :args => ['1', '2']}, val)
67
+
68
+ val = evalue("#{EX}switches.rb -s 2 1 ")
69
+ assert_equal({:switch=> true, :args => ['2', '1']}, val)
70
+
71
+ val = evalue("#{EX}switches.rb --no-switch 1 2")
72
+ assert_equal({:switch=> false, :args => ['1', '2']}, val)
73
+
74
+ val = evalue("#{EX}switches.rb 1 2 3 4")
75
+ assert_equal({:switch=> false, :args => ['1', '2', '3', '4']}, val)
76
+
77
+
78
+ val = evalue("#{EX}two-args.rb 1 2 ")
79
+ assert_equal({:args => ['1', '2']}, val)
80
+
81
+
82
+ val = evalue("#{EX}postprocess.rb 1 2")
83
+ assert_equal({:infile => '1', :outfile => '2', :args => ['1', '2']},
84
+ val)
85
+ end
86
+
87
+ def test_multiple_sources_xml
88
+ xml = "<config><ordinary_choice>greetings</ordinary_choice></config>"
89
+
90
+ with_local_config_file("ms-config.xml", xml) {
91
+ val = evalue("#{EX}multiple-sources.rb")
92
+ assert_equal({:names => [], :ordinary_choice => 'greetings'}, val)
93
+ }
94
+
95
+ with_local_config_file("ms-config.xml", xml) {
96
+ with_environment_vars("ms_ordinary_choice" => 'hi') {
97
+ val = evalue("#{EX}multiple-sources.rb ")
98
+ assert_equal({:names => [], :ordinary_choice => 'hi'}, val)
99
+ }
100
+ }
101
+
102
+
103
+ with_local_config_file("ms-config.xml", xml) {
104
+ with_environment_vars("ms_ordinary_choice" => 'hi') {
105
+ val = evalue("#{EX}multiple-sources.rb --ordinary-choice hello")
106
+ assert_equal({:names => [], :ordinary_choice => 'hello'}, val)
107
+ }
108
+ }
109
+
110
+ end
111
+
112
+
113
+ def test_multiple_sources_yaml
114
+ yml = "ordinary_choice: greetings"
115
+
116
+ with_local_config_file("ms-config.yml", yml) {
117
+ val = evalue("#{EX}multiple-sources.rb")
118
+ assert_equal({:names => [], :ordinary_choice => 'greetings'}, val)
119
+ }
120
+
121
+ with_local_config_file("ms-config.yml", yml) {
122
+ with_environment_vars("ms_ordinary_choice" => 'hi') {
123
+ val = evalue("#{EX}multiple-sources.rb ")
124
+ assert_equal({:names => [], :ordinary_choice => 'hi'}, val)
125
+ }
126
+ }
127
+
128
+
129
+ with_local_config_file("ms-config.yml", yml) {
130
+ with_environment_vars("ms_ordinary_choice" => 'hi') {
131
+ val = evalue("#{EX}multiple-sources.rb --ordinary-choice hello")
132
+ assert_equal({:names => [], :ordinary_choice => 'hello'}, val)
133
+ }
134
+ }
135
+
136
+ end
137
+
138
+
139
+ def error(klass, args)
140
+ capturing_stderr {
141
+ with_pleasant_exceptions {
142
+ with_command_args(args) {
143
+ klass.new.execute
144
+ }
145
+ }
146
+ }
147
+ end
148
+
149
+ def test_error_checking
150
+ assert_match(/missing argument: --choice/,
151
+ error(CommandLineExample, "--choice"))
152
+
153
+
154
+ assert_match(/invalid option: --other/,
155
+ error(CommandLineExample, "--other 3 -- choice"))
156
+
157
+
158
+ assert_match(/--a-or-b's value must be one of 'a' or 'b'.*'not-a' doesn't look right/,
159
+ error(TypesExample, "--a-or-b not-a argument"))
160
+
161
+
162
+ assert_match(/--must-be-integer's value must be an integer/,
163
+ error(TypesExample, "--must-be-integer 1d argument"))
164
+
165
+
166
+ assert_match(/0 arguments given, 1 expected/,
167
+ error(TypesExample, ""))
168
+
169
+ assert_match(/2 arguments given, 1 expected/,
170
+ error(TypesExample, "argument extra"))
171
+
172
+ assert_match(/1 argument given, 2 to 4 expected/,
173
+ error(SwitchExample, "1"))
174
+
175
+ assert_match(/5 arguments given, 2 to 4 expected/,
176
+ error(SwitchExample, "1 2 3 4 5"))
177
+
178
+ assert_match(/1 argument given, 2 expected/,
179
+ error(TwoArgExample, "1"))
180
+
181
+ assert_match(/3 arguments given, 2 expected/,
182
+ error(TwoArgExample, "1 2 3"))
183
+
184
+ end
185
+
186
+ def test_bad_xml
187
+ xml = "<config><names"
188
+ with_local_config_file("ms-config.xml", xml) {
189
+ assert_match(/Badly formatted configuration file/,
190
+ error(MultipleSourcesExample, "1 2") )
191
+ }
192
+ end
193
+
194
+
195
+ def test_help
196
+ result = error(CommandLineExample, "--help")
197
+ assert_match(/Usage.*Options:.*--choice.*CHOICE can be.*Show this/m,
198
+ result)
199
+
200
+ result = error(SwitchExample, "--help")
201
+ assert_match(/Usage.*Options:.*--\[no-\]switch.*Show this message/m,
202
+ result)
203
+ end
204
+
205
+
206
+ def test_tutorial_usage_section
207
+ assert_match(/There are 0 connections./, `#{TUT}tutorial1.rb `)
208
+
209
+ with_local_config_file(".myprog-config.yml", "connections: 19") do
210
+ assert_match(/There are 19 connections./, `#{TUT}tutorial1.rb `)
211
+
212
+ with_environment_vars("myprog_connections" => '3') do
213
+ assert_match(/There are 3 connections./, `#{TUT}tutorial1.rb `)
214
+
215
+ assert_match(/There are 999 connections./,
216
+ `#{TUT}tutorial1.rb --connection 999`)
217
+ end
218
+
219
+ with_environment_vars("myprog_connections" => 'hi') do
220
+ assert_match(/Error in the environment: myprog_connections's value must be an integer, and 'hi' doesn't look right/,
221
+ `#{TUT}tutorial1.rb 2>&1`)
222
+
223
+ end
224
+
225
+ output = `#{TUT}tutorial1.rb --connections hi 2>&1`
226
+ assert_match(/Error in the command line: --connections's value must be an integer, and 'hi' doesn't look right/,
227
+ output)
228
+ assert_match(/Usage: ruby.*tutorial1.rb/, output)
229
+ end
230
+ end
231
+
232
+ def test_tutorial_command_line_behavior_section
233
+ assert_match(/SSH should be used/, `#{TUT}tutorial2.rb --ssh`)
234
+ assert_match(/SSH should be used/, `#{TUT}tutorial2.rb -s`)
235
+ assert_match(/-s,\s+--\[no-\]ssh/, `#{TUT}tutorial2.rb --help 2>&1`)
236
+
237
+ output = `#{TUT}tutorial3.rb arg1 arg2`
238
+ assert_match(/:files\s*=>\s*\["arg1", "arg2"\]/, output)
239
+
240
+ yaml = "
241
+ connections: 19
242
+ files:
243
+ - one
244
+ - two
245
+ "
246
+
247
+ with_local_config_file(".myprog-config.yml", yaml) do
248
+ output = `#{TUT}tutorial3.rb cmd`
249
+ assert_match(/:files\s*=>\s*\["cmd"\]/, output)
250
+
251
+ output = `#{TUT}tutorial3.rb`
252
+ assert_match(/:files\s*=>\s*\["one", "two"\]/, output)
253
+
254
+ output = `#{TUT}tutorial4.rb 1 2 3 2>&1`
255
+ assert_match(/Error in the command line: 3 arguments given, 1 or 2 expected/, output)
256
+
257
+ output = `#{TUT}tutorial4.rb`
258
+ assert_match(/:files\s*=>\s*\["one", "two"\]/, output)
259
+ end
260
+
261
+ assert_match(/:infile=>"1"/, `#{TUT}tutorial5.rb 1`)
262
+ assert_match(/Error in the command line: 0 arguments given, 1 expected./,
263
+ `#{TUT}tutorial5.rb 2>&1`)
264
+
265
+ assert_match(/\{\}/, `#{TUT}tutorial6.rb`)
266
+ end
267
+
268
+ def test_tutorial_touchup_section
269
+ output = `#{TUT}tutorial7.rb one two`
270
+ assert_match(/:infile=>"one"/, output)
271
+ assert_match(/:outfile=>"two"/, output)
272
+ assert_match(/:files=>\["one", "two"\]/, output)
273
+ end
274
+
275
+ end
276
+
@@ -0,0 +1,104 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{user-choices}
5
+ s.version = "1.1.7"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Brian Marick"]
9
+ s.date = %q{2009-05-22}
10
+ s.email = %q{marick@exampler.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "examples/older/README.txt",
23
+ "examples/older/command-line.rb",
24
+ "examples/older/default-values.rb",
25
+ "examples/older/multiple-sources.rb",
26
+ "examples/older/postprocess.rb",
27
+ "examples/older/switches.rb",
28
+ "examples/older/two-args.rb",
29
+ "examples/older/types.rb",
30
+ "examples/tutorial/css/LICENSE.txt",
31
+ "examples/tutorial/css/bg2.gif",
32
+ "examples/tutorial/css/left.gif",
33
+ "examples/tutorial/css/left_on.gif",
34
+ "examples/tutorial/css/main.css",
35
+ "examples/tutorial/css/right.gif",
36
+ "examples/tutorial/css/right_on.gif",
37
+ "examples/tutorial/css/tvline.gif",
38
+ "examples/tutorial/css/von-foerster.jpg",
39
+ "examples/tutorial/css/von-foerster2.jpg",
40
+ "examples/tutorial/index.html",
41
+ "examples/tutorial/tutorial1.rb",
42
+ "examples/tutorial/tutorial2.rb",
43
+ "examples/tutorial/tutorial3.rb",
44
+ "examples/tutorial/tutorial4.rb",
45
+ "examples/tutorial/tutorial5.rb",
46
+ "examples/tutorial/tutorial6.rb",
47
+ "examples/tutorial/tutorial7.rb",
48
+ "lib/user-choices.rb",
49
+ "lib/user-choices/arglist-strategies.rb",
50
+ "lib/user-choices/builder.rb",
51
+ "lib/user-choices/command-line-source.rb",
52
+ "lib/user-choices/command.rb",
53
+ "lib/user-choices/conversions.rb",
54
+ "lib/user-choices/ruby-extensions.rb",
55
+ "lib/user-choices/sources.rb",
56
+ "lib/user-choices/version.rb",
57
+ "test/arglist_strategy_test.rb",
58
+ "test/builder_test.rb",
59
+ "test/command_line_source_test.rb",
60
+ "test/conversion_test.rb",
61
+ "test/source_test.rb",
62
+ "test/test_helper.rb",
63
+ "test/user_choices_slow_test.rb",
64
+ "user-choices.gemspec"
65
+ ]
66
+ s.homepage = %q{http://github.com/qoobaa/user-choices}
67
+ s.rdoc_options = ["--charset=UTF-8"]
68
+ s.require_paths = ["lib"]
69
+ s.rubygems_version = %q{1.3.3}
70
+ s.summary = %q{Unified interface to command-line, environment, and configuration files.}
71
+ s.test_files = [
72
+ "test/arglist_strategy_test.rb",
73
+ "test/builder_test.rb",
74
+ "test/test_helper.rb",
75
+ "test/user_choices_slow_test.rb",
76
+ "test/conversion_test.rb",
77
+ "test/source_test.rb",
78
+ "test/command_line_source_test.rb",
79
+ "examples/tutorial/tutorial1.rb",
80
+ "examples/tutorial/tutorial7.rb",
81
+ "examples/tutorial/tutorial3.rb",
82
+ "examples/tutorial/tutorial4.rb",
83
+ "examples/tutorial/tutorial5.rb",
84
+ "examples/tutorial/tutorial2.rb",
85
+ "examples/tutorial/tutorial6.rb",
86
+ "examples/older/two-args.rb",
87
+ "examples/older/postprocess.rb",
88
+ "examples/older/multiple-sources.rb",
89
+ "examples/older/types.rb",
90
+ "examples/older/command-line.rb",
91
+ "examples/older/switches.rb",
92
+ "examples/older/default-values.rb"
93
+ ]
94
+
95
+ if s.respond_to? :specification_version then
96
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
97
+ s.specification_version = 3
98
+
99
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
100
+ else
101
+ end
102
+ else
103
+ end
104
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qoobaa-user-choices
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.7
5
+ platform: ruby
6
+ authors:
7
+ - Brian Marick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: marick@exampler.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - examples/older/README.txt
33
+ - examples/older/command-line.rb
34
+ - examples/older/default-values.rb
35
+ - examples/older/multiple-sources.rb
36
+ - examples/older/postprocess.rb
37
+ - examples/older/switches.rb
38
+ - examples/older/two-args.rb
39
+ - examples/older/types.rb
40
+ - examples/tutorial/css/LICENSE.txt
41
+ - examples/tutorial/css/bg2.gif
42
+ - examples/tutorial/css/left.gif
43
+ - examples/tutorial/css/left_on.gif
44
+ - examples/tutorial/css/main.css
45
+ - examples/tutorial/css/right.gif
46
+ - examples/tutorial/css/right_on.gif
47
+ - examples/tutorial/css/tvline.gif
48
+ - examples/tutorial/css/von-foerster.jpg
49
+ - examples/tutorial/css/von-foerster2.jpg
50
+ - examples/tutorial/index.html
51
+ - examples/tutorial/tutorial1.rb
52
+ - examples/tutorial/tutorial2.rb
53
+ - examples/tutorial/tutorial3.rb
54
+ - examples/tutorial/tutorial4.rb
55
+ - examples/tutorial/tutorial5.rb
56
+ - examples/tutorial/tutorial6.rb
57
+ - examples/tutorial/tutorial7.rb
58
+ - lib/user-choices.rb
59
+ - lib/user-choices/arglist-strategies.rb
60
+ - lib/user-choices/builder.rb
61
+ - lib/user-choices/command-line-source.rb
62
+ - lib/user-choices/command.rb
63
+ - lib/user-choices/conversions.rb
64
+ - lib/user-choices/ruby-extensions.rb
65
+ - lib/user-choices/sources.rb
66
+ - lib/user-choices/version.rb
67
+ - test/arglist_strategy_test.rb
68
+ - test/builder_test.rb
69
+ - test/command_line_source_test.rb
70
+ - test/conversion_test.rb
71
+ - test/source_test.rb
72
+ - test/test_helper.rb
73
+ - test/user_choices_slow_test.rb
74
+ - user-choices.gemspec
75
+ has_rdoc: false
76
+ homepage: http://github.com/qoobaa/user-choices
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: "0"
93
+ version:
94
+ requirements: []
95
+
96
+ rubyforge_project:
97
+ rubygems_version: 1.2.0
98
+ signing_key:
99
+ specification_version: 3
100
+ summary: Unified interface to command-line, environment, and configuration files.
101
+ test_files:
102
+ - test/arglist_strategy_test.rb
103
+ - test/builder_test.rb
104
+ - test/test_helper.rb
105
+ - test/user_choices_slow_test.rb
106
+ - test/conversion_test.rb
107
+ - test/source_test.rb
108
+ - test/command_line_source_test.rb
109
+ - examples/tutorial/tutorial1.rb
110
+ - examples/tutorial/tutorial7.rb
111
+ - examples/tutorial/tutorial3.rb
112
+ - examples/tutorial/tutorial4.rb
113
+ - examples/tutorial/tutorial5.rb
114
+ - examples/tutorial/tutorial2.rb
115
+ - examples/tutorial/tutorial6.rb
116
+ - examples/older/two-args.rb
117
+ - examples/older/postprocess.rb
118
+ - examples/older/multiple-sources.rb
119
+ - examples/older/types.rb
120
+ - examples/older/command-line.rb
121
+ - examples/older/switches.rb
122
+ - examples/older/default-values.rb