2 $:.unshift File.join(File.dirname(__FILE__),'..','lib')
4 # Copyright (C) 2007-2010 Martin A. Hansen.
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24 # This software is part of the Biopieces framework (www.biopieces.org).
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
33 TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
35 SCRIPT_PATH = "write_fasta"
37 class BiopiecesTest < Test::Unit::TestCase
41 @output = StringIO.new
42 @bp = Biopieces.new(true, @input, @output)
45 # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
47 test "Biopieces#parse with all cast keys don't raise" do
49 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
50 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
53 test "Biopieces#parse with illegal long cast values raises" do
54 [nil, true, false, 1, 0, "a"].each do |long|
56 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
57 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
61 test "Biopieces#parse with legal long cast values don't raise" do
62 ["foo", "!!", "0123"].each do |long|
64 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
65 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
69 test "Biopieces#parse with illegal short cast values raises" do
70 [nil, true, false, "foo"].each do |short|
72 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
73 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
77 test "Biopieces#parse with legal short cast values don't raise" do
78 ["!", "1", "a"].each do |short|
80 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
81 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
85 test "Biopieces#parse with illegal type cast values raises" do
86 [nil, true, false, "foo", 12, 0].each do |type|
88 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
89 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
93 test "Biopieces#parse with legal type cast values don't raise" do
96 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
97 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
101 test "Biopieces#parse with illegal mandatory cast values raises" do
102 ["yes", 12, 0, nil].each do |mandatory|
104 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
105 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
109 test "Biopieces#parse with legal mandatory cast values don't raise" do
110 [true, false].each do |mandatory|
111 argv = [ "--foo", "1" ]
112 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
113 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
117 test "Biopieces#parse with illegal default cast values raises" do
118 [true, false, [], {}].each do |default|
120 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
121 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
125 test "Biopieces#parse with legal default cast values don't raise" do
126 [nil, 0, 1, -1].each do |default|
128 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
129 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
133 test "Biopieces#parse with illegal allowed cast values raises" do
134 [true, false, {}, [], 0, 0.1].each do |allowed|
136 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
137 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
141 test "Biopieces#parse with legal allowed cast values don't raise" do
142 ["foo,bar,0",nil].each do |allowed|
144 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
145 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
149 test "Biopieces#parse with illegal disallowed cast values raises" do
150 [true, false, {}, [], 0, 0.1].each do |disallowed|
152 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
153 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
157 test "Biopieces#parse with legal disallowed cast values don't raise" do
158 ["foo,bar,0",nil].each do |disallowed|
160 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
161 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
165 test "Biopieces#parse with duplicate long cast values raises" do
168 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
169 casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
170 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
173 test "Biopieces#parse with duplicate short cast values raises" do
176 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
177 casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
178 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
181 test "Biopieces#parse without duplicate long and short cast values don't raise" do
184 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
185 casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
186 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
189 # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
191 test "Biopieces#parse with empty argv and missing wiki file raises" do
194 assert_raise() { @bp.parse(argv,casts, "foo") }
197 test "Biopieces#parse with empty argv and existing wiki file don't raise" do
200 assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
203 test "Biopieces#parse with --help in argv and existing wiki output long usage" do
205 assert_nothing_raised { @bp.parse(argv,[],SCRIPT_PATH) }
208 # # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
209 # test "Options.parse with type cast flag with an argument raises" do
210 # casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
211 # opt_parser = Options.new(casts)
212 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
215 test "Biopieces#parse with --stream_in <arg> returns options['stream_in']=>[<arg>]" do
216 argv = ["--stream_in", DUMMY_FILE]
217 options = @bp.parse(argv,[],SCRIPT_PATH)
218 assert_equal([DUMMY_FILE], options["stream_in"])
221 test "Biopieces#parse with -I <arg> returns options['stream_in']=>[<arg>]" do
222 argv = ["-I", DUMMY_FILE]
224 options = @bp.parse(argv, casts, SCRIPT_PATH)
225 assert_equal([DUMMY_FILE], options["stream_in"])
228 test "Biopieces#parse use cast default value if no argument given" do
229 argv = ["-I", DUMMY_FILE]
230 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
231 options = @bp.parse(argv, casts, SCRIPT_PATH)
232 assert_equal(options["foo"], "bar")
235 test "Biopieces#parse don't use default value if argument given" do
236 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
237 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
238 options = @bp.parse(argv, casts, SCRIPT_PATH)
239 assert_equal(options["foo"], "bleh")
242 test "Biopieces#parse with mandatory cast and no argument raises" do
243 argv = ["-I", DUMMY_FILE]
244 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
245 assert_raise(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
248 test "Biopieces#parse with mandatory cast and argument don't raise" do
249 argv = ["--foo", "bar", "-I", DUMMY_FILE]
250 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
251 assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
254 # # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
255 # # So it appears that this is tested in OptionParser already.
256 # test "Options.parse with type cast int and non-int value raises" do
257 # ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
258 # casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
259 # opt_parser = Options.new(casts)
260 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
264 test "Biopieces#parse with type cast int don't raise" do
265 [0,-1,1,327649123746293746374276347824].each do |val|
266 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
267 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
268 assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
272 # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
274 test "Biopieces#parse with type cast uint don't raise" do
275 [0,1,327649123746293746374276347824].each do |val|
276 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
277 casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
278 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
282 test "Biopieces#parse with file! cast and file don't exists raises" do
283 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
284 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
285 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
288 test "Biopieces#parse with file! cast and existing file don't raise" do
289 argv = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
290 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
291 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
294 test "Biopieces#parse with files! cast and a file don't exists raises" do
295 argv = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
296 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
297 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
300 test "Biopieces#parse with files! cast and files exists don't raise" do
301 argv = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
302 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
303 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
306 # TODO replace the absolute part below the file location with File.dirname(__FILE__)
307 test "Biopieces#parse with glob argument expands correctly" do
308 argv = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", DUMMY_FILE]
309 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
310 options = @bp.parse(argv, casts, SCRIPT_PATH)
311 assert_equal(options["foo"], ["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"])
314 test "Biopieces#parse with dir! cast and dir don't exists raises" do
315 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
316 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
317 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
320 test "Biopieces#parse with dir! cast and dir exists don't raise" do
321 argv = ["--foo", "/", "-I", DUMMY_FILE]
322 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
323 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
326 test "Biopieces#parse with allowed cast and not allowed value raises" do
327 ["bleh", "2", "3.3"].each do |val|
328 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
329 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
330 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
334 test "Biopieces#parse with allowed cast and allowed values don't raise" do
335 ["0", "-1", "0.0", "1", "bar"].each do |val|
336 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
337 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
338 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
342 test "Biopieces#parse with disallowed cast and disallowed value raises" do
343 ["0", "-1", "0.0", "1", "bar"].each do |val|
344 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
345 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
346 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
350 test "Biopieces#parse with disallowed cast and allowed values don't raise" do
351 ["bleh", "2", "3.3"].each do |val|
352 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
353 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
354 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }