user-choices 1.1.0

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