3 # Copyright (C) 2007-2011 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 def test_Biopieces_options_parse_with_stream_in_argv_returns_correct_options
204 argv = ["--stream_in", DUMMY_FILE]
205 options = Biopieces.options_parse(argv,[],SCRIPT_PATH)
206 assert_equal(DUMMY_FILE, options[:stream_in])
209 def test_Biopieces_options_parse_with_I_argv_returns_correct_options
210 argv = ["-I", DUMMY_FILE]
212 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
213 assert_equal(DUMMY_FILE, options[:stream_in])
216 def test_Biopieces_options_parse_use_cast_default_value_if_no_argument_given
217 argv = ["-I", DUMMY_FILE]
218 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
219 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
220 assert_equal(options[:foo], "bar")
223 def test_Biopieces_options_parse_dont_use_default_value_if_argument_given
224 argv = ["--foo", "bleh", "-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], "bleh")
230 def test_Biopieces_options_parse_with_mandatory_cast_and_no_argument_raises
231 argv = ["-I", DUMMY_FILE]
232 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
233 assert_raise(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
236 def test_Biopieces_options_parse_with_mandatory_cast_and_argument_dont_raise
237 argv = ["--foo", "bar", "-I", DUMMY_FILE]
238 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
239 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
242 def test_Biopieces_options_parse_with_type_cast_int_dont_raise
243 [0,-1,1,327649123746293746374276347824].each do |val|
244 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
245 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
246 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
250 def test_Biopieces_options_parse_with_type_cast_uint_dont_raise
251 [0,1,327649123746293746374276347824].each do |val|
252 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
253 casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
254 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
258 def test_Biopieces_options_parse_with_file_cast_and_file_dont_exists_raises
259 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
260 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
261 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
264 def test_Biopieces_options_parse_with_file_cast_and_existing_file_dont_raise
265 argv = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
266 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
267 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
270 def test_Biopieces_options_parse_with_files_cast_and_a_file_dont_exists_raises
271 argv = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
272 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
273 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
276 def test_Biopieces_options_parse_with_files_cast_and_files_exists_dont_raise
277 argv = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
278 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
279 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
282 def test_Biopieces_options_parse_with_glob_argument_expands_correctly
283 path = File.join(ENV['BP_DIR'], "bp_test")
284 argv = ["--foo", "#{path}/te*,#{path}/lib/*.sh", "-I", DUMMY_FILE]
285 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
286 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
287 assert_equal(["#{path}/test_all", "#{path}/lib/test.sh"], options[:foo])
290 def test_Biopieces_options_parse_with_dir_cast_and_dir_dont_exists_raises
291 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
292 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
293 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
296 def test_Biopieces_options_parse_with_dir_cast_and_dir_exists_dont_raise
297 argv = ["--foo", "/", "-I", DUMMY_FILE]
298 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
299 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
302 def test_Biopieces_options_parse_with_allowed_cast_and_not_allowed_value_raises
303 ["bleh", "2", "3.3"].each do |val|
304 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
305 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
306 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
310 def test_Biopieces_options_parse_with_allowed_cast_and_allowed_values_dont_raise
311 ["0", "-1", "0.0", "1", "bar"].each do |val|
312 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
313 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
314 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
318 def test_Biopieces_options_parse_with_disallowed_cast_and_disallowed_value_raises
319 ["0", "-1", "0.0", "1", "bar"].each do |val|
320 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
321 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
322 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
326 def test_Biopieces_options_parse_with_disallowed_cast_and_allowed_values_dont_raise
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=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
330 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }