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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
32 TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
34 SCRIPT_PATH = "write_fasta"
36 class BiopiecesTest < Test::Unit::TestCase
40 @output = StringIO.new
41 @bp = Biopieces.new(true, @input, @output)
44 # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
46 def test_Biopieces_parse_with_all_cast_keys_dont_raise
48 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
49 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
52 def test_Biopieces_parse_with_illegal_long_cast_values_raises
53 [nil, true, false, 1, 0, "a"].each do |long|
55 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
56 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
60 def test_Biopieces_parse_with_legal_long_cast_values_dont_raise
61 ["foo", "!!", "0123"].each do |long|
63 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
64 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
68 def test_Biopieces_parse_with_illegal_short_cast_values_raises
69 [nil, true, false, "foo"].each do |short|
71 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
72 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
76 def test_Biopieces_parse_with_legal_short_cast_values_dont_raise
77 ["!", "1", "a"].each do |short|
79 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
80 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
84 def test_Biopieces_parse_with_illegal_type_cast_values_raises
85 [nil, true, false, "foo", 12, 0].each do |type|
87 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
88 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
92 def test_Biopieces_parse_with_legal_type_cast_values_dont_raise
95 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
96 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
100 def test_Biopieces_parse_with_illegal_mandatory_cast_values_raises
101 ["yes", 12, 0, nil].each do |mandatory|
103 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
104 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
108 def test_Biopieces_parse_with_legal_mandatory_cast_values_dont_raise
109 [true, false].each do |mandatory|
110 argv = [ "--foo", "1" ]
111 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
112 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
116 def test_Biopieces_parse_with_illegal_default_cast_values_raises
117 [true, false, [], {}].each do |default|
119 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
120 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
124 def test_Biopieces_parse_with_legal_default_cast_values_dont_raise
125 [nil, 0, 1, -1].each do |default|
127 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
128 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
132 def test_Biopieces_parse_with_illegal_allowed_cast_values_raises
133 [true, false, {}, [], 0, 0.1].each do |allowed|
135 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
136 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
140 def test_Biopieces_parse_with_legal_allowed_cast_values_dont_raise
141 ["foo,bar,0",nil].each do |allowed|
143 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
144 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
148 def test_Biopieces_parse_with_illegal_disallowed_cast_values_raises
149 [true, false, {}, [], 0, 0.1].each do |disallowed|
151 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
152 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
156 def test_Biopieces_parse_with_legal_disallowed_cast_values_dont_raise
157 ["foo,bar,0",nil].each do |disallowed|
159 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
160 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
164 def test_Biopieces_parse_with_duplicate_long_cast_values_raises
167 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
168 casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
169 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
172 def test_Biopieces_parse_with_duplicate_short_cast_values_raises
175 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
176 casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
177 assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
180 def test_Biopieces_parse_without_duplicate_long_and_short_cast_values_dont_raise
183 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
184 casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
185 assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
188 # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
190 def test_Biopieces_parse_with_empty_argv_and_missing_wiki_file_raises
193 assert_raise(RuntimeError) { @bp.parse(argv,casts, "foo") }
196 def test_Biopieces_parse_with_empty_argv_and_existing_wiki_file_dont_raise
199 assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
202 def test_Biopieces_parse_with_help_in_argv_and_existing_wiki_output_long_usage
204 assert_nothing_raised { @bp.parse(argv,[],SCRIPT_PATH) }
207 # # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
208 # test "Options.parse with type cast flag with an argument raises
209 # casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
210 # opt_parser = Options.new(casts)
211 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
214 def test_Biopieces_parse_with_stream_in_argv_returns_correct_options
215 argv = ["--stream_in", DUMMY_FILE]
216 options = @bp.parse(argv,[],SCRIPT_PATH)
217 assert_equal([DUMMY_FILE], options[:stream_in])
220 def test_Biopieces_parse_with_I_argv_returns_correct_options
221 argv = ["-I", DUMMY_FILE]
223 options = @bp.parse(argv, casts, SCRIPT_PATH)
224 assert_equal([DUMMY_FILE], options[:stream_in])
227 def test_Biopieces_parse_use_cast_default_value_if_no_argument_given
228 argv = ["-I", DUMMY_FILE]
229 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
230 options = @bp.parse(argv, casts, SCRIPT_PATH)
231 assert_equal(options[:foo], "bar")
234 def test_Biopieces_parse_dont_use_default_value_if_argument_given
235 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
236 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
237 options = @bp.parse(argv, casts, SCRIPT_PATH)
238 assert_equal(options[:foo], "bleh".to_sym)
241 def test_Biopieces_parse_with_mandatory_cast_and_no_argument_raises
242 argv = ["-I", DUMMY_FILE]
243 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
244 assert_raise(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
247 def test_Biopieces_parse_with_mandatory_cast_and_argument_dont_raise
248 argv = ["--foo", "bar", "-I", DUMMY_FILE]
249 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
250 assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
253 # # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
254 # # So it appears that this is tested in OptionParser already.
255 # test "Options.parse with type cast int and non-int value raises" do
256 # ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
257 # casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
258 # opt_parser = Options.new(casts)
259 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
263 def test_Biopieces_parse_with_type_cast_int_dont_raise
264 [0,-1,1,327649123746293746374276347824].each do |val|
265 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
266 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
267 assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
271 # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
273 def test_Biopieces_parse_with_type_cast_uint_dont_raise
274 [0,1,327649123746293746374276347824].each do |val|
275 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
276 casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
277 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
281 def test_Biopieces_parse_with_file_cast_and_file_dont_exists_raises
282 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
283 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
284 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
287 def test_Biopieces_parse_with_file_cast_and_existing_file_dont_raise
288 argv = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
289 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
290 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
293 def test_Biopieces_parse_with_files_cast_and_a_file_dont_exists_raises
294 argv = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
295 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
296 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
299 def test_Biopieces_parse_with_files_cast_and_files_exists_dont_raise
300 argv = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
301 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
302 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
305 # TODO replace the absolute part below the file location with File.dirname(__FILE__)
306 def test_Biopieces_parse_with_glob_argument_expands_correctly
307 flunk("This test is weird")
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(["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"], options[:foo])
314 def test_Biopieces_parse_with_dir_cast_and_dir_dont_exists_raises
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 def test_Biopieces_parse_with_dir_cast_and_dir_exists_dont_raise
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 def test_Biopieces_parse_with_allowed_cast_and_not_allowed_value_raises
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 def test_Biopieces_parse_with_allowed_cast_and_allowed_values_dont_raise
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 def test_Biopieces_parse_with_disallowed_cast_and_disallowed_value_raises
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 def test_Biopieces_parse_with_disallowed_cast_and_allowed_values_dont_raise
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) }