2 $:.unshift File.join(File.dirname(__FILE__), '..', '..')
4 # Copyright (C) 2007-2011 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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30 require 'maasha/biopieces'
33 Biopieces::TEST = true
34 OptionHandler::TEST = true
36 TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
38 SCRIPT_PATH = "write_fasta"
40 class BiopiecesTest < Test::Unit::TestCase
41 # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
43 test "Biopieces.options_parse with all cast keys dont raise" do
45 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
46 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
49 test "Biopieces.options_parse with illegal long cast values raises" do
50 [nil, true, false, 1, 0, "a"].each do |long|
52 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
53 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
57 test "Biopieces.options_parse with legal long cast values dont raise" do
58 ["foo", "!!", "0123"].each do |long|
60 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
61 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
65 test "Biopieces.options_parse with illegal short cast values raises" do
66 [nil, true, false, "foo"].each do |short|
68 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
69 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
73 test "Biopieces.options_parse with legal short cast values dont raise" do
74 ["!", "1", "a"].each do |short|
76 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
77 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
81 test "Biopieces.options_parse with illegal type cast values raises" do
82 [nil, true, false, "foo", 12, 0].each do |type|
84 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
85 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
89 test "Biopieces.options_parse with legal type cast values dont raise" do
92 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
93 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
97 test "Biopieces.options_parse with illegal mandatory cast values raises" do
98 ["yes", 12, 0, nil].each do |mandatory|
100 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
101 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
105 test "Biopieces.options_parse with legal mandatory cast values dont raise" do
106 [true, false].each do |mandatory|
107 argv = [ "--foo", "1" ]
108 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
109 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
113 test "Biopieces.options_parse with illegal default cast values raises" do
114 [true, false, [], {}].each do |default|
116 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
117 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
121 test "Biopieces.options_parse with legal default cast values dont raise" do
122 [nil, 0, 1, -1].each do |default|
124 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
125 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
129 test "Biopieces.options_parse with illegal allowed cast values raises" do
130 [true, false, {}, [], 0, 0.1].each do |allowed|
132 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
133 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
137 test "Biopieces.options_parse with legal allowed cast values dont raise" do
138 ["foo,bar,0",nil].each do |allowed|
140 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
141 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
145 test "Biopieces.options_parse with illegal disallowed cast values raises" do
146 [true, false, {}, [], 0, 0.1].each do |disallowed|
148 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
149 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
153 test "Biopieces.options_parse with legal disallowed cast values dont raise" do
154 ["foo,bar,0",nil].each do |disallowed|
156 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
157 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
161 test "Biopieces.options_parse with duplicate long cast values raises" do
164 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
165 casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
166 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
169 test "Biopieces.options_parse with duplicate short cast values raises" do
172 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
173 casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
174 assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
177 test "Biopieces.options_parse without duplicate long and short cast values dont raise" do
180 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
181 casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
182 assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
185 # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
187 test "Biopieces.options_parse with empty argv and missing wiki file raises" do
190 assert_raise(RuntimeError) { Biopieces.options_parse(argv,casts, "foo") }
193 test "Biopieces.options_parse with empty argv and existing wiki file dont raise" do
196 assert_nothing_raised { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
199 test "Biopieces.options_parse with help in argv and existing wiki output long usage" do
201 assert_nothing_raised { Biopieces.options_parse(argv,[],SCRIPT_PATH) }
204 test "Biopieces.options_parse with stream in argv returns correct options" do
205 argv = ["--stream_in", DUMMY_FILE]
206 options = Biopieces.options_parse(argv,[],SCRIPT_PATH)
207 assert_equal(DUMMY_FILE, options[:stream_in])
210 test "Biopieces.options_parse with I argv returns correct options" do
211 argv = ["-I", DUMMY_FILE]
213 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
214 assert_equal(DUMMY_FILE, options[:stream_in])
217 test "Biopieces.options_parse use cast default value if no argument given" do
218 argv = ["-I", DUMMY_FILE]
219 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
220 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
221 assert_equal(options[:foo], "bar")
224 test "Biopieces.options_parse dont use default value if argument given" do
225 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
226 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
227 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
228 assert_equal(options[:foo], "bleh")
231 test "Biopieces.options_parse with mandatory cast and no argument raises" do
232 argv = ["-I", DUMMY_FILE]
233 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
234 assert_raise(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
237 test "Biopieces.options_parse with mandatory cast and argument dont raise" do
238 argv = ["--foo", "bar", "-I", DUMMY_FILE]
239 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
240 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
243 test "Biopieces.options_parse with type cast int dont raise" do
244 [0,-1,1,327649123746293746374276347824].each do |val|
245 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
246 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
247 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
251 test "Biopieces.options_parse with type cast uint dont raise" do
252 [0,1,327649123746293746374276347824].each do |val|
253 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
254 casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
255 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
259 test "Biopieces.options_parse with file cast and file dont exists raises" do
260 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
261 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
262 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
265 test "Biopieces.options_parse with file cast and existing file dont raise" do
266 argv = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
267 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
268 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
271 test "Biopieces.options_parse with files cast and a file dont exists raises" do
272 argv = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
273 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
274 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
277 test "Biopieces.options_parse with files cast and files exists dont raise" do
278 argv = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
279 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
280 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
283 test "Biopieces.options_parse with glob argument expands correctly" do
284 path = File.join(ENV['BP_DIR'], "bp_test")
285 argv = ["--foo", "#{path}/te*,#{path}/lib/*.sh", "-I", DUMMY_FILE]
286 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
287 options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
288 assert_equal(["#{path}/test_all", "#{path}/lib/test.sh"], options[:foo])
291 test "Biopieces.options_parse with dir cast and dir dont exists raises" do
292 argv = ["--foo", "bleh", "-I", DUMMY_FILE]
293 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
294 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
297 test "Biopieces.options_parse with dir cast and dir exists dont raise" do
298 argv = ["--foo", "/", "-I", DUMMY_FILE]
299 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
300 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
303 test "Biopieces.options_parse with allowed cast and not allowed value raises" do
304 ["bleh", "2", "3.3"].each do |val|
305 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
306 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
307 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
311 test "Biopieces.options_parse with allowed cast and allowed values dont raise" do
312 ["0", "-1", "0.0", "1", "bar"].each do |val|
313 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
314 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
315 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
319 test "Biopieces.options_parse with disallowed cast and disallowed value raises" do
320 ["0", "-1", "0.0", "1", "bar"].each do |val|
321 argv = ["--foo", "#{val}", "-I", DUMMY_FILE]
322 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
323 assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
327 test "Biopieces.options_parse with disallowed cast and allowed values dont raise" do
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=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
331 assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }