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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
29 require 'maasha/biopieces'
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 def test_Biopieces_parse_with_all_cast_keys_dont_raise
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 def test_Biopieces_parse_with_illegal_long_cast_values_raises
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 def test_Biopieces_parse_with_legal_long_cast_values_dont_raise
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 def test_Biopieces_parse_with_illegal_short_cast_values_raises
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 def test_Biopieces_parse_with_legal_short_cast_values_dont_raise
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 def test_Biopieces_parse_with_illegal_type_cast_values_raises
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 def test_Biopieces_parse_with_legal_type_cast_values_dont_raise
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 def test_Biopieces_parse_with_illegal_mandatory_cast_values_raises
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 def test_Biopieces_parse_with_legal_mandatory_cast_values_dont_raise
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 def test_Biopieces_parse_with_illegal_default_cast_values_raises
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 def test_Biopieces_parse_with_legal_default_cast_values_dont_raise
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 def test_Biopieces_parse_with_illegal_allowed_cast_values_raises
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 def test_Biopieces_parse_with_legal_allowed_cast_values_dont_raise
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 def test_Biopieces_parse_with_illegal_disallowed_cast_values_raises
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 def test_Biopieces_parse_with_legal_disallowed_cast_values_dont_raise
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 def test_Biopieces_parse_with_duplicate_long_cast_values_raises
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 def test_Biopieces_parse_with_duplicate_short_cast_values_raises
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 def test_Biopieces_parse_without_duplicate_long_and_short_cast_values_dont_raise
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 def test_Biopieces_parse_with_empty_argv_and_missing_wiki_file_raises
194 assert_raise(RuntimeError) { @bp.parse(argv,casts, "foo") }
197 def test_Biopieces_parse_with_empty_argv_and_existing_wiki_file_dont_raise
200 assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
203 def test_Biopieces_parse_with_help_in_argv_and_existing_wiki_output_long_usage
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
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 def test_Biopieces_parse_with_stream_in_argv_returns_correct_options
216 argv = ["--stream_in", DUMMY_FILE]
217 options = @bp.parse(argv,[],SCRIPT_PATH)
218 assert_equal([DUMMY_FILE], options[:stream_in])
221 def test_Biopieces_parse_with_I_argv_returns_correct_options
222 argv = ["-I", DUMMY_FILE]
224 options = @bp.parse(argv, casts, SCRIPT_PATH)
225 assert_equal([DUMMY_FILE], options[:stream_in])
228 def test_Biopieces_parse_use_cast_default_value_if_no_argument_given
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 def test_Biopieces_parse_dont_use_default_value_if_argument_given
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".to_sym)
242 def test_Biopieces_parse_with_mandatory_cast_and_no_argument_raises
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 def test_Biopieces_parse_with_mandatory_cast_and_argument_dont_raise
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 def test_Biopieces_parse_with_type_cast_int_dont_raise
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 def test_Biopieces_parse_with_type_cast_uint_dont_raise
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 def test_Biopieces_parse_with_file_cast_and_file_dont_exists_raises
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 def test_Biopieces_parse_with_file_cast_and_existing_file_dont_raise
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 def test_Biopieces_parse_with_files_cast_and_a_file_dont_exists_raises
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 def test_Biopieces_parse_with_files_cast_and_files_exists_dont_raise
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 def test_Biopieces_parse_with_glob_argument_expands_correctly
308 flunk("This test is weird")
309 argv = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", DUMMY_FILE]
310 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
311 options = @bp.parse(argv, casts, SCRIPT_PATH)
312 assert_equal(["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"], options[:foo])
315 def test_Biopieces_parse_with_dir_cast_and_dir_dont_exists_raises
316 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
317 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
318 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
321 def test_Biopieces_parse_with_dir_cast_and_dir_exists_dont_raise
322 argv = ["--foo", "/", "-I", DUMMY_FILE]
323 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
324 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
327 def test_Biopieces_parse_with_allowed_cast_and_not_allowed_value_raises
328 ["bleh", "2", "3.3"].each do |val|
329 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
330 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
331 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
335 def test_Biopieces_parse_with_allowed_cast_and_allowed_values_dont_raise
336 ["0", "-1", "0.0", "1", "bar"].each do |val|
337 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
338 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
339 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
343 def test_Biopieces_parse_with_disallowed_cast_and_disallowed_value_raises
344 ["0", "-1", "0.0", "1", "bar"].each do |val|
345 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
346 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
347 assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
351 def test_Biopieces_parse_with_disallowed_cast_and_allowed_values_dont_raise
352 ["bleh", "2", "3.3"].each do |val|
353 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
354 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
355 assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }