2 # $:.unshift File.join(File.dirname(__FILE__),'..','lib')
9 TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
10 SCRIPT_PATH = "write_fasta.rb"
12 class OptionTest < Test::Unit::TestCase
15 @bp = Biopieces.new(true)
18 # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
20 test "Biopieces#parse with all cast keys don't raise" do
22 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
23 @bp.expects(:print_usage_and_exit).with()
24 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
27 test "Biopieces#parse with illegal long cast values raises" do
28 [nil, true, false, 1, 0, "a"].each do |long|
30 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
31 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
35 test "Biopieces#parse with legal long cast values don't raise" do
36 ["foo", "!!", "0123"].each do |long|
38 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
39 @bp.expects(:print_usage_and_exit).with()
40 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
44 test "Biopieces#parse with illegal short cast values raises" do
45 [nil, true, false, "foo"].each do |short|
47 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
48 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
52 test "Biopieces#parse with legal short cast values don't raise" do
53 ["!", "1", "a"].each do |short|
55 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
56 @bp.expects(:print_usage_and_exit).with()
57 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
61 test "Biopieces#parse with illegal type cast values raises" do
62 [nil, true, false, "foo", 12, 0].each do |type|
64 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
65 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
69 test "Biopieces#parse with legal type cast values don't raise" do
72 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
73 @bp.expects(:print_usage_and_exit).with()
74 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
78 test "Biopieces#parse with illegal mandatory cast values raises" do
79 ["yes", 12, 0, nil].each do |mandatory|
81 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
82 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
86 test "Biopieces#parse with legal mandatory cast values don't raise" do
87 [true, false].each do |mandatory|
89 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
90 @bp.expects(:print_usage_and_exit).with()
91 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
95 test "Biopieces#parse with illegal default cast values raises" do
96 [true, false, [], {}].each do |default|
98 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
99 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
103 test "Biopieces#parse with legal default cast values don't raise" do
104 ["foo", nil, 0, 0.0, 1, 1.2].each do |default|
106 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
107 @bp.expects(:print_usage_and_exit).with()
108 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
112 test "Biopieces#parse with illegal allowed cast values raises" do
113 [true, false, {}, [], 0, 0.1].each do |allowed|
115 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
116 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
120 test "Biopieces#parse with legal allowed cast values don't raise" do
121 ["foo,bar",nil].each do |allowed|
123 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
124 @bp.expects(:print_usage_and_exit).with()
125 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
129 test "Biopieces#parse with illegal disallowed cast values raises" do
130 [true, false, {}, [], 0, 0.1].each do |disallowed|
132 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
133 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
137 test "Biopieces#parse with legal disallowed cast values don't raise" do
138 ["foo,bar",nil].each do |disallowed|
140 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
141 @bp.expects(:print_usage_and_exit).with()
142 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
146 test "Biopieces#parse with duplicate long cast values raises" do
149 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
150 casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
151 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
154 test "Biopieces#parse with duplicate short cast values raises" do
157 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
158 casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
159 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
162 test "Biopieces#parse without duplicate long and short cast values don't raise" do
165 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
166 casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
167 @bp.expects(:print_usage_and_exit).with()
168 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
171 # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
173 test "Biopieces#parse with empty argv and missing wiki file raises" do
176 assert_raise() { @bp.parse(argv,casts, "foo") }
179 test "Biopieces#parse with empty argv and existing wiki file don't raise" do
182 @bp.expects(:print_usage_and_exit).with()
183 assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
186 test "Biopieces#parse with --help in argv and existing wiki output long usage" do
188 @bp.expects(:print_usage_and_exit).with(true)
189 assert_nothing_raised { @bp.parse(argv,[],SCRIPT_PATH) }
192 # # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
193 # test "Options.parse with type cast flag with an argument raises" do
194 # casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
195 # opt_parser = Options.new(casts)
196 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
199 test "Biopieces#parse with --stream_in <arg> returns options['stream_in']=>[<arg>]" do
200 argv = ["--stream_in", __FILE__]
201 options = @bp.parse(argv,[],SCRIPT_PATH)
202 assert_equal([__FILE__], options["stream_in"])
205 test "Biopieces#parse with -I <arg> returns options['stream_in']=>[<arg>]" do
206 argv = ["-I", __FILE__]
208 options = @bp.parse(argv, casts, SCRIPT_PATH)
209 assert_equal([__FILE__], options["stream_in"])
212 test "Biopieces#parse use cast default value if no argument given" do
213 argv = ["-I", __FILE__]
214 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
215 options = @bp.parse(argv, casts, SCRIPT_PATH)
216 assert_equal(options["foo"], "bar")
219 test "Biopieces#parse don't use default value if argument given" do
220 argv = ["--foo", "bleh", "-I", __FILE__]
221 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
222 options = @bp.parse(argv, casts, SCRIPT_PATH)
223 assert_equal(options["foo"], "bleh")
226 test "Biopieces#parse with mandatory cast and no argument raises" do
227 argv = ["-I", __FILE__]
228 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
229 assert_raise(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
232 test "Biopieces#parse with mandatory cast and argument don't raise" do
233 argv = ["--foo", "bar", "-I", __FILE__]
234 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
235 assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
238 # # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
239 # # So it appears that this is tested in OptionParser already.
240 # test "Options.parse with type cast int and non-int value raises" do
241 # ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
242 # casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
243 # opt_parser = Options.new(casts)
244 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
248 test "Biopieces#parse with type cast int don't raise" do
249 [0,-1,1,327649123746293746374276347824].each do |val|
250 argv = ["--foo", "#{val}", "-I", __FILE__]
251 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
252 assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
256 # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
258 test "Biopieces#parse with type cast uint don't raise" do
259 [0,1,327649123746293746374276347824].each do |val|
260 argv = ["--foo", "#{val}", "-I", __FILE__]
261 casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
262 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
266 test "Biopieces#parse with file! cast and file don't exists raises" do
267 argv = ["--foo", "bleh", "-I", __FILE__]
268 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
269 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
272 test "Biopieces#parse with file! cast and existing file don't raise" do
273 argv = ["--foo", __FILE__, "-I", __FILE__]
274 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
275 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
278 test "Biopieces#parse with files! cast and a file don't exists raises" do
279 argv = ["--foo", __FILE__ + ",bleh", "-I", __FILE__]
280 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
281 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
284 test "Biopieces#parse with files! cast and files exists don't raise" do
285 argv = ["--foo", __FILE__ + "," + __FILE__, "-I", __FILE__]
286 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
287 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
290 # TODO replace the absolute part below the file location with File.dirname(__FILE__)
291 test "Biopieces#parse with glob argument expands correctly" do
292 argv = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", __FILE__]
293 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
294 options = @bp.parse(argv, casts, SCRIPT_PATH)
295 assert_equal(options["foo"], ["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"])
298 test "Biopieces#parse with dir! cast and dir don't exists raises" do
299 argv = ["--foo", "bleh", "-I", __FILE__]
300 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
301 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
304 test "Biopieces#parse with dir! cast and dir exists don't raise" do
305 argv = ["--foo", "/", "-I", __FILE__]
306 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
307 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
310 test "Biopieces#parse with allowed cast and not allowed value raises" do
311 ["bleh", "2", "3.3"].each do |val|
312 argv = ["--foo", "#{val}", "-I", __FILE__]
313 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
314 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
318 test "Biopieces#parse with allowed cast and allowed values don't raise" do
319 ["0", "-1", "0.0", "1", "bar"].each do |val|
320 argv = ["--foo", "#{val}", "-I", __FILE__]
321 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
322 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
326 test "Biopieces#parse with disallowed cast and disallowed value raises" do
327 ["0", "-1", "0.0", "1", "bar"].each do |val|
328 argv = ["--foo", "#{val}", "-I", __FILE__]
329 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
330 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
334 test "Biopieces#parse with disallowed cast and allowed values don't raise" do
335 ["bleh", "2", "3.3"].each do |val|
336 argv = ["--foo", "#{val}", "-I", __FILE__]
337 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
338 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }