]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/test/test_biopieces.rb
add dist option to find_adaptor
[biopieces.git] / code_ruby / Maasha / test / test_biopieces.rb
1 #!/usr/bin/env ruby
2 $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
4 # Copyright (C) 2007-2010 Martin A. Hansen.
5
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.
10
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.
15
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.
19
20 # http://www.gnu.org/copyleft/gpl.html
21
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23
24 # This software is part of the Biopieces framework (www.biopieces.org).
25
26 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27
28 require 'test/unit'
29 require 'biopieces'
30 require 'pp'
31
32 TYPES       = %w[flag string list int uint float file file! files files! dir dir! genome]
33 DUMMY_FILE  = __FILE__
34 SCRIPT_PATH = "write_fasta"
35
36 class BiopiecesTest < Test::Unit::TestCase
37
38   def setup
39     @input  = StringIO.new
40     @output = StringIO.new
41     @bp     = Biopieces.new(true, @input, @output)
42   end
43
44   # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
45
46   def test_Biopieces_parse_with_all_cast_keys_dont_raise
47     argv  = []
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) }
50   end
51
52   def test_Biopieces_parse_with_illegal_long_cast_values_raises
53     [nil, true, false, 1, 0, "a"].each do |long|
54       argv  = []
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) }
57     end
58   end
59
60   def test_Biopieces_parse_with_legal_long_cast_values_dont_raise
61     ["foo", "!!", "0123"].each do |long|
62       argv  = []
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) }
65     end
66   end
67
68   def test_Biopieces_parse_with_illegal_short_cast_values_raises
69     [nil, true, false, "foo"].each do |short|
70       argv  = []
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) }
73     end
74   end
75
76   def test_Biopieces_parse_with_legal_short_cast_values_dont_raise
77     ["!", "1", "a"].each do |short|
78       argv  = []
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) }
81     end
82   end
83
84   def test_Biopieces_parse_with_illegal_type_cast_values_raises
85     [nil, true, false, "foo", 12, 0].each do |type|
86       argv  = []
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) }
89     end
90   end
91
92   def test_Biopieces_parse_with_legal_type_cast_values_dont_raise
93     TYPES.each do |type|
94       argv  = []
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) }
97     end
98   end
99
100   def test_Biopieces_parse_with_illegal_mandatory_cast_values_raises
101     ["yes", 12, 0, nil].each do |mandatory|
102       argv  = []
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) }
105     end
106   end
107
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) }
113     end
114   end
115
116   def test_Biopieces_parse_with_illegal_default_cast_values_raises
117     [true, false, [], {}].each do |default|
118       argv  = []
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) }
121     end
122   end
123
124   def test_Biopieces_parse_with_legal_default_cast_values_dont_raise
125     [nil, 0, 1, -1].each do |default|
126       argv  = []
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) }
129     end
130   end
131
132   def test_Biopieces_parse_with_illegal_allowed_cast_values_raises
133     [true, false, {}, [], 0, 0.1].each do |allowed|
134       argv  = []
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) }
137     end
138   end
139
140   def test_Biopieces_parse_with_legal_allowed_cast_values_dont_raise
141     ["foo,bar,0",nil].each do |allowed|
142       argv  = []
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) }
145     end
146   end
147
148   def test_Biopieces_parse_with_illegal_disallowed_cast_values_raises
149     [true, false, {}, [], 0, 0.1].each do |disallowed|
150       argv  = []
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) }
153     end
154   end
155
156   def test_Biopieces_parse_with_legal_disallowed_cast_values_dont_raise
157     ["foo,bar,0",nil].each do |disallowed|
158       argv  = []
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) }
161     end
162   end
163
164   def test_Biopieces_parse_with_duplicate_long_cast_values_raises
165     argv  = []
166     casts = []
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) }
170   end
171
172   def test_Biopieces_parse_with_duplicate_short_cast_values_raises
173     argv  = []
174     casts = []
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) }
178   end
179
180   def test_Biopieces_parse_without_duplicate_long_and_short_cast_values_dont_raise
181     argv  = []
182     casts = []
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) }
186   end
187
188   # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
189
190   def test_Biopieces_parse_with_empty_argv_and_missing_wiki_file_raises
191     argv  = []
192     casts = []
193     assert_raise(RuntimeError) { @bp.parse(argv,casts, "foo") }
194   end
195
196   def test_Biopieces_parse_with_empty_argv_and_existing_wiki_file_dont_raise
197     argv  = []
198     casts = []
199     assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
200   end
201
202   def test_Biopieces_parse_with_help_in_argv_and_existing_wiki_output_long_usage
203     argv = ["--help"]
204     assert_nothing_raised { @bp.parse(argv,[],SCRIPT_PATH) }
205   end
206
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) }
212 #   end
213
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])
218   end
219
220   def test_Biopieces_parse_with_I_argv_returns_correct_options
221     argv    = ["-I", DUMMY_FILE]
222     casts   = []
223     options = @bp.parse(argv, casts, SCRIPT_PATH)
224     assert_equal([DUMMY_FILE], options[:stream_in])
225   end
226
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")
232   end
233
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)
239   end
240
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) }
245   end
246
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) }
251   end
252
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) }
260 #    end
261 #  end
262
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) }
268     end
269   end
270
271   # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
272
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) }
278     end
279   end
280
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) }
285   end
286
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) }
291   end
292
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) }
297   end
298
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) }
303   end
304
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])
312   end
313
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) }
318   end
319
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) }
324   end
325
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) }
331     end
332   end
333
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) }
339     end
340   end
341
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) }
347     end
348   end
349
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) }
355     end
356   end
357 end