]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/test_biopieces.rb
rewrite of FASTQ internals
[biopieces.git] / code_ruby / test / maasha / test_biopieces.rb
1 #!/usr/bin/env ruby
2 $:.unshift File.join(File.dirname(__FILE__), '..', '..')
3
4 # Copyright (C) 2007-2011 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 'test/helper'
30 require 'maasha/biopieces'
31 require 'stringio'
32
33 Biopieces::TEST     = true
34 OptionHandler::TEST = true
35
36 TYPES       = %w[flag string list int uint float file file! files files! dir dir! genome]
37 DUMMY_FILE  = __FILE__
38 SCRIPT_PATH = "write_fasta"
39
40 class BiopiecesTest < Test::Unit::TestCase
41   # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
42
43   test "Biopieces.options_parse with all cast keys dont raise" do
44     argv  = []
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) }
47   end
48
49   test "Biopieces.options_parse with illegal long cast values raises" do
50     [nil, true, false, 1, 0, "a"].each do |long|
51       argv  = []
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) }
54     end
55   end
56
57   test "Biopieces.options_parse with legal long cast values dont raise" do
58     ["foo", "!!", "0123"].each do |long|
59       argv  = []
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) }
62     end
63   end
64
65   test "Biopieces.options_parse with illegal short cast values raises" do
66     [nil, true, false, "foo"].each do |short|
67       argv  = []
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) }
70     end
71   end
72
73   test "Biopieces.options_parse with legal short cast values dont raise" do
74     ["!", "1", "a"].each do |short|
75       argv  = []
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) }
78     end
79   end
80
81   test "Biopieces.options_parse with illegal type cast values raises" do
82     [nil, true, false, "foo", 12, 0].each do |type|
83       argv  = []
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) }
86     end
87   end
88
89   test "Biopieces.options_parse with legal type cast values dont raise" do
90     TYPES.each do |type|
91       argv  = []
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) }
94     end
95   end
96
97   test "Biopieces.options_parse with illegal mandatory cast values raises" do
98     ["yes", 12, 0, nil].each do |mandatory|
99       argv  = []
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) }
102     end
103   end
104
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) }
110     end
111   end
112
113   test "Biopieces.options_parse with illegal default cast values raises" do
114     [true, false, [], {}].each do |default|
115       argv  = []
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) }
118     end
119   end
120
121   test "Biopieces.options_parse with legal default cast values dont raise" do
122     [nil, 0, 1, -1].each do |default|
123       argv  = []
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) }
126     end
127   end
128
129   test "Biopieces.options_parse with illegal allowed cast values raises" do
130     [true, false, {}, [], 0, 0.1].each do |allowed|
131       argv  = []
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) }
134     end
135   end
136
137   test "Biopieces.options_parse with legal allowed cast values dont raise" do
138     ["foo,bar,0",nil].each do |allowed|
139       argv  = []
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) }
142     end
143   end
144
145   test "Biopieces.options_parse with illegal disallowed cast values raises" do
146     [true, false, {}, [], 0, 0.1].each do |disallowed|
147       argv  = []
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) }
150     end
151   end
152
153   test "Biopieces.options_parse with legal disallowed cast values dont raise" do
154     ["foo,bar,0",nil].each do |disallowed|
155       argv  = []
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) }
158     end
159   end
160
161   test "Biopieces.options_parse with duplicate long cast values raises" do
162     argv  = []
163     casts = []
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) }
167   end
168
169   test "Biopieces.options_parse with duplicate short cast values raises" do
170     argv  = []
171     casts = []
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) }
175   end
176
177   test "Biopieces.options_parse without duplicate long and short cast values dont raise" do
178     argv  = []
179     casts = []
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) }
183   end
184
185   # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
186
187   test "Biopieces.options_parse with empty argv and missing wiki file raises" do
188     argv  = []
189     casts = []
190     assert_raise(RuntimeError) { Biopieces.options_parse(argv,casts, "foo") }
191   end
192
193   test "Biopieces.options_parse with empty argv and existing wiki file dont raise" do
194     argv  = []
195     casts = []
196     assert_nothing_raised { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
197   end
198
199   test "Biopieces.options_parse with help in argv and existing wiki output long usage" do
200     argv = ["--help"]
201     assert_nothing_raised { Biopieces.options_parse(argv,[],SCRIPT_PATH) }
202   end
203
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])
208   end
209  
210    test "Biopieces.options_parse with I argv returns correct options" do
211      argv    = ["-I", DUMMY_FILE]
212      casts   = []
213      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
214      assert_equal(DUMMY_FILE, options[:stream_in])
215    end
216  
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")
222    end
223  
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")
229    end
230  
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) }
235    end
236  
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) }
241    end
242  
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) }
248      end
249    end
250  
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) }
256      end
257    end
258  
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) }
263    end
264  
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) }
269    end
270  
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) }
275    end
276  
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) }
281    end
282  
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])
289    end
290  
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) }
295    end
296  
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) }
301    end
302  
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) }
308      end
309    end
310  
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) }
316      end
317    end
318  
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) }
324      end
325    end
326  
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) }
332      end
333    end
334 end