3 # Copyright (C) 2007-2010 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This software is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
28 require 'maasha/biopieces'
32 Biopieces::TEST = true
33 OptionHandler::TEST = true
35 TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
37 SCRIPT_PATH = "write_fasta"
39 class BiopiecesTest < Test::Unit::TestCase
40 # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
42 def test_Biopieces_options_parse_with_all_cast_keys_dont_raise
44 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
45 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
48 def test_Biopieces_options_parse_with_illegal_long_cast_values_raises
49 [nil, true, false, 1, 0, "a"].each do |long|
51 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
52 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
56 def test_Biopieces_options_parse_with_legal_long_cast_values_dont_raise
57 ["foo", "!!", "0123"].each do |long|
59 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
60 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
64 def test_Biopieces_options_parse_with_illegal_short_cast_values_raises
65 [nil, true, false, "foo"].each do |short|
67 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
68 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
72 def test_Biopieces_options_parse_with_legal_short_cast_values_dont_raise
73 ["!", "1", "a"].each do |short|
75 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
76 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
80 def test_Biopieces_options_parse_with_illegal_type_cast_values_raises
81 [nil, true, false, "foo", 12, 0].each do |type|
83 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
84 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
88 def test_Biopieces_options_parse_with_legal_type_cast_values_dont_raise
91 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
92 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
96 def test_Biopieces_options_parse_with_illegal_mandatory_cast_values_raises
97 ["yes", 12, 0, nil].each do |mandatory|
99 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
100 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
104 def test_Biopieces_options_parse_with_legal_mandatory_cast_values_dont_raise
105 [true, false].each do |mandatory|
106 argv = [ "--foo", "1" ]
107 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
108 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
112 def test_Biopieces_options_parse_with_illegal_default_cast_values_raises
113 [true, false, [], {}].each do |default|
115 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
116 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
120 def test_Biopieces_options_parse_with_legal_default_cast_values_dont_raise
121 [nil, 0, 1, -1].each do |default|
123 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
124 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
128 def test_Biopieces_options_parse_with_illegal_allowed_cast_values_raises
129 [true, false, {}, [], 0, 0.1].each do |allowed|
131 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
132 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
136 def test_Biopieces_options_parse_with_legal_allowed_cast_values_dont_raise
137 ["foo,bar,0",nil].each do |allowed|
139 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
140 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
144 def test_Biopieces_options_parse_with_illegal_disallowed_cast_values_raises
145 [true, false, {}, [], 0, 0.1].each do |disallowed|
147 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
148 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
152 def test_Biopieces_options_parse_with_legal_disallowed_cast_values_dont_raise
153 ["foo,bar,0",nil].each do |disallowed|
155 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
156 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
160 def test_Biopieces_options_parse_with_duplicate_long_cast_values_raises
163 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
164 casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
165 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
168 def test_Biopieces_options_parse_with_duplicate_short_cast_values_raises
171 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
172 casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
173 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
176 def test_Biopieces_options_parse_without_duplicate_long_and_short_cast_values_dont_raise
179 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
180 casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
181 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
184 # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
186 def test_Biopieces_options_parse_with_empty_argv_and_missing_wiki_file_raises
189 assert_raise(RuntimeError) { Biopieces.options_parse(argv,casts, "foo") }
192 def test_Biopieces_options_parse_with_empty_argv_and_existing_wiki_file_dont_raise
195 assert_nothing_raised { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
198 def test_Biopieces_options_parse_with_help_in_argv_and_existing_wiki_output_long_usage
200 assert_nothing_raised { Biopieces.options_parse(argv,[],SCRIPT_PATH) }
203 # # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
204 # test "Options.parse with type cast flag with and argument raises
205 # casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
206 # opt_parser = Options.new(casts)
207 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
210 def test_Biopieces_options_parse_with_stream_in_argv_returns_correct_options
211 argv = ["--stream_in", DUMMY_FILE]
212 options = Biopieces.options_parse(argv,[],SCRIPT_PATH)
213 assert_equal(DUMMY_FILE, options[:stream_in])
216 def test_Biopieces_options_parse_with_I_argv_returns_correct_options
217 argv = ["-I", DUMMY_FILE]
219 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
220 assert_equal(DUMMY_FILE, options[:stream_in])
223 def test_Biopieces_options_parse_use_cast_default_value_if_no_argument_given
224 argv = ["-I", DUMMY_FILE]
225 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
226 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
227 assert_equal(options[:foo], "bar")
230 def test_Biopieces_options_parse_dont_use_default_value_if_argument_given
231 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
232 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
233 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
234 assert_equal(options[:foo], "bleh")
237 def test_Biopieces_options_parse_with_mandatory_cast_and_no_argument_raises
238 argv = ["-I", DUMMY_FILE]
239 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
240 assert_raise(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
243 def test_Biopieces_options_parse_with_mandatory_cast_and_argument_dont_raise
244 argv = ["--foo", "bar", "-I", DUMMY_FILE]
245 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
246 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
249 # # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
250 # # So it appears that this is tested in OptionParser already.
251 # test "Options.parse with type cast int and non-int value raises" do
252 # ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
253 # casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
254 # opt_parser = Options.new(casts)
255 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
259 def test_Biopieces_options_parse_with_type_cast_int_dont_raise
260 [0,-1,1,327649123746293746374276347824].each do |val|
261 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
262 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
263 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
267 # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
269 def test_Biopieces_options_parse_with_type_cast_uint_dont_raise
270 [0,1,327649123746293746374276347824].each do |val|
271 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
272 casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
273 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
277 def test_Biopieces_options_parse_with_file_cast_and_file_dont_exists_raises
278 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
279 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
280 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
283 def test_Biopieces_options_parse_with_file_cast_and_existing_file_dont_raise
284 argv = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
285 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
286 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
289 def test_Biopieces_options_parse_with_files_cast_and_a_file_dont_exists_raises
290 argv = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
291 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
292 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
295 def test_Biopieces_options_parse_with_files_cast_and_files_exists_dont_raise
296 argv = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
297 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
298 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
301 # # TODO replace the absolute part below the file location with File.dirname(__FILE__)
302 # def test_Biopieces_options_parse_with_glob_argument_expands_correctly
303 # flunk("This test is flawed and need fixing")
304 # argv = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", DUMMY_FILE]
305 # casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
306 # options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
307 # assert_equal(["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"], options[:foo])
310 def test_Biopieces_options_parse_with_dir_cast_and_dir_dont_exists_raises
311 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
312 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
313 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
316 def test_Biopieces_options_parse_with_dir_cast_and_dir_exists_dont_raise
317 argv = ["--foo", "/", "-I", DUMMY_FILE]
318 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
319 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
322 def test_Biopieces_options_parse_with_allowed_cast_and_not_allowed_value_raises
323 ["bleh", "2", "3.3"].each do |val|
324 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
325 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
326 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
330 def test_Biopieces_options_parse_with_allowed_cast_and_allowed_values_dont_raise
331 ["0", "-1", "0.0", "1", "bar"].each do |val|
332 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
333 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
334 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
338 def test_Biopieces_options_parse_with_disallowed_cast_and_disallowed_value_raises
339 ["0", "-1", "0.0", "1", "bar"].each do |val|
340 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
341 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
342 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
346 def test_Biopieces_options_parse_with_disallowed_cast_and_allowed_values_dont_raise
347 ["bleh", "2", "3.3"].each do |val|
348 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
349 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
350 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }