]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/test_biopieces.rb
49d10a539fb7b0a39dc7d90ebb306ac4a1330f1b
[biopieces.git] / code_ruby / test / maasha / 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 'maasha/biopieces'
30 require 'stringio'
31 require 'pp'
32
33 TYPES       = %w[flag string list int uint float file file! files files! dir dir! genome]
34 DUMMY_FILE  = __FILE__
35 SCRIPT_PATH = "write_fasta"
36
37 class BiopiecesTest < Test::Unit::TestCase
38
39   def setup
40     @input  = StringIO.new
41     @output = StringIO.new
42     @bp     = Biopieces.new(true, @input, @output)
43   end
44
45   # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
46
47   def test_Biopieces_parse_with_all_cast_keys_dont_raise
48     argv  = []
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) }
51   end
52
53   def test_Biopieces_parse_with_illegal_long_cast_values_raises
54     [nil, true, false, 1, 0, "a"].each do |long|
55       argv  = []
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) }
58     end
59   end
60
61   def test_Biopieces_parse_with_legal_long_cast_values_dont_raise
62     ["foo", "!!", "0123"].each do |long|
63       argv  = []
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) }
66     end
67   end
68
69   def test_Biopieces_parse_with_illegal_short_cast_values_raises
70     [nil, true, false, "foo"].each do |short|
71       argv  = []
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) }
74     end
75   end
76
77   def test_Biopieces_parse_with_legal_short_cast_values_dont_raise
78     ["!", "1", "a"].each do |short|
79       argv  = []
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) }
82     end
83   end
84
85   def test_Biopieces_parse_with_illegal_type_cast_values_raises
86     [nil, true, false, "foo", 12, 0].each do |type|
87       argv  = []
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) }
90     end
91   end
92
93   def test_Biopieces_parse_with_legal_type_cast_values_dont_raise
94     TYPES.each do |type|
95       argv  = []
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) }
98     end
99   end
100
101   def test_Biopieces_parse_with_illegal_mandatory_cast_values_raises
102     ["yes", 12, 0, nil].each do |mandatory|
103       argv  = []
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) }
106     end
107   end
108
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) }
114     end
115   end
116
117   def test_Biopieces_parse_with_illegal_default_cast_values_raises
118     [true, false, [], {}].each do |default|
119       argv  = []
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) }
122     end
123   end
124
125   def test_Biopieces_parse_with_legal_default_cast_values_dont_raise
126     [nil, 0, 1, -1].each do |default|
127       argv  = []
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) }
130     end
131   end
132
133   def test_Biopieces_parse_with_illegal_allowed_cast_values_raises
134     [true, false, {}, [], 0, 0.1].each do |allowed|
135       argv  = []
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) }
138     end
139   end
140
141   def test_Biopieces_parse_with_legal_allowed_cast_values_dont_raise
142     ["foo,bar,0",nil].each do |allowed|
143       argv  = []
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) }
146     end
147   end
148
149   def test_Biopieces_parse_with_illegal_disallowed_cast_values_raises
150     [true, false, {}, [], 0, 0.1].each do |disallowed|
151       argv  = []
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) }
154     end
155   end
156
157   def test_Biopieces_parse_with_legal_disallowed_cast_values_dont_raise
158     ["foo,bar,0",nil].each do |disallowed|
159       argv  = []
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) }
162     end
163   end
164
165   def test_Biopieces_parse_with_duplicate_long_cast_values_raises
166     argv  = []
167     casts = []
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) }
171   end
172
173   def test_Biopieces_parse_with_duplicate_short_cast_values_raises
174     argv  = []
175     casts = []
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) }
179   end
180
181   def test_Biopieces_parse_without_duplicate_long_and_short_cast_values_dont_raise
182     argv  = []
183     casts = []
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) }
187   end
188
189   # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
190
191   def test_Biopieces_parse_with_empty_argv_and_missing_wiki_file_raises
192     argv  = []
193     casts = []
194     assert_raise(RuntimeError) { @bp.parse(argv,casts, "foo") }
195   end
196
197   def test_Biopieces_parse_with_empty_argv_and_existing_wiki_file_dont_raise
198     argv  = []
199     casts = []
200     assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
201   end
202
203   def test_Biopieces_parse_with_help_in_argv_and_existing_wiki_output_long_usage
204     argv = ["--help"]
205     assert_nothing_raised { @bp.parse(argv,[],SCRIPT_PATH) }
206   end
207
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) }
213 #   end
214
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])
219   end
220
221   def test_Biopieces_parse_with_I_argv_returns_correct_options
222     argv    = ["-I", DUMMY_FILE]
223     casts   = []
224     options = @bp.parse(argv, casts, SCRIPT_PATH)
225     assert_equal([DUMMY_FILE], options[:stream_in])
226   end
227
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")
233   end
234
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)
240   end
241
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) }
246   end
247
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) }
252   end
253
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) }
261 #    end
262 #  end
263
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) }
269     end
270   end
271
272   # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
273
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) }
279     end
280   end
281
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) }
286   end
287
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) }
292   end
293
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) }
298   end
299
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) }
304   end
305
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])
313   end
314
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) }
319   end
320
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) }
325   end
326
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) }
332     end
333   end
334
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) }
340     end
341   end
342
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) }
348     end
349   end
350
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) }
356     end
357   end
358 end