]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/test/test_biopieces.rb
123b866f372f410d3d50131984a40a3d8ef16f52
[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 'mocha'
30 require 'biopieces'
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    test "Biopieces#parse with all cast keys don't raise" do
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   test "Biopieces#parse with illegal long cast values raises" do
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   test "Biopieces#parse with legal long cast values don't raise" do
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   test "Biopieces#parse with illegal short cast values raises" do
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   test "Biopieces#parse with legal short cast values don't raise" do
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   test "Biopieces#parse with illegal type cast values raises" do
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   test "Biopieces#parse with legal type cast values don't raise" do
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   test "Biopieces#parse with illegal mandatory cast values raises" do
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   test "Biopieces#parse with legal mandatory cast values don't raise" do
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   test "Biopieces#parse with illegal default cast values raises" do
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   test "Biopieces#parse with legal default cast values don't raise" do
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   test "Biopieces#parse with illegal allowed cast values raises" do
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   test "Biopieces#parse with legal allowed cast values don't raise" do
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   test "Biopieces#parse with illegal disallowed cast values raises" do
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   test "Biopieces#parse with legal disallowed cast values don't raise" do
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   test "Biopieces#parse with duplicate long cast values raises" do
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   test "Biopieces#parse with duplicate short cast values raises" do
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   test "Biopieces#parse without duplicate long and short cast values don't raise" do
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   test "Biopieces#parse with empty argv and missing wiki file raises" do
192     argv  = []
193     casts = []
194     assert_raise() { @bp.parse(argv,casts, "foo") }
195   end
196
197   test "Biopieces#parse with empty argv and existing wiki file don't raise" do
198     argv  = []
199     casts = []
200     assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
201   end
202
203    test "Biopieces#parse with --help in argv and existing wiki output long usage" do
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" do
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   test "Biopieces#parse with --stream_in <arg> returns options['stream_in']=>[<arg>]" do
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   test "Biopieces#parse with -I <arg> returns options['stream_in']=>[<arg>]" do
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   test "Biopieces#parse use cast default value if no argument given" do
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   test "Biopieces#parse don't use default value if argument given" do
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")
240   end
241
242   test "Biopieces#parse with mandatory cast and no argument raises" do
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   test "Biopieces#parse with mandatory cast and argument don't raise" do
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   test "Biopieces#parse with type cast int don't raise" do
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   test "Biopieces#parse with type cast uint don't raise" do
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   test "Biopieces#parse with file! cast and file don't exists raises" do
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   test "Biopieces#parse with file! cast and existing file don't raise" do
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   test "Biopieces#parse with files! cast and a file don't exists raises" do
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   test "Biopieces#parse with files! cast and files exists don't raise" do
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   test "Biopieces#parse with glob argument expands correctly" do
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(options["foo"], ["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"])
312   end
313
314   test "Biopieces#parse with dir! cast and dir don't exists raises" do
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   test "Biopieces#parse with dir! cast and dir exists don't raise" do
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   test "Biopieces#parse with allowed cast and not allowed value raises" do
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   test "Biopieces#parse with allowed cast and allowed values don't raise" do
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   test "Biopieces#parse with disallowed cast and disallowed value raises" do
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   test "Biopieces#parse with disallowed cast and allowed values don't raise" do
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