]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/test_biopieces.rb
2dd2430b910eb500419c35ac9fffc24d420d2cd5
[biopieces.git] / code_ruby / test / maasha / test_biopieces.rb
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This software is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 require 'test/unit'
28 require 'maasha/biopieces'
29 require 'stringio'
30 require 'pp'
31
32 Biopieces::TEST     = true
33 OptionHandler::TEST = true
34
35 TYPES       = %w[flag string list int uint float file file! files files! dir dir! genome]
36 DUMMY_FILE  = __FILE__
37 SCRIPT_PATH = "write_fasta"
38
39 class BiopiecesTest < Test::Unit::TestCase
40   # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
41
42   def test_Biopieces_options_parse_with_all_cast_keys_dont_raise
43     argv  = []
44     casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
45     assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
46   end
47
48   def test_Biopieces_options_parse_with_illegal_long_cast_values_raises
49     [nil, true, false, 1, 0, "a"].each do |long|
50       argv  = []
51       casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
52       assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
53     end
54   end
55
56   def test_Biopieces_options_parse_with_legal_long_cast_values_dont_raise
57     ["foo", "!!", "0123"].each do |long|
58       argv  = []
59       casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
60       assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
61     end
62   end
63
64   def test_Biopieces_options_parse_with_illegal_short_cast_values_raises
65     [nil, true, false, "foo"].each do |short|
66       argv  = []
67       casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
68       assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
69     end
70   end
71
72   def test_Biopieces_options_parse_with_legal_short_cast_values_dont_raise
73     ["!", "1", "a"].each do |short|
74       argv  = []
75       casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
76       assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
77     end
78   end
79
80   def test_Biopieces_options_parse_with_illegal_type_cast_values_raises
81     [nil, true, false, "foo", 12, 0].each do |type|
82       argv  = []
83       casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
84       assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
85     end
86   end
87
88   def test_Biopieces_options_parse_with_legal_type_cast_values_dont_raise
89     TYPES.each do |type|
90       argv  = []
91       casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
92       assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
93     end
94   end
95
96   def test_Biopieces_options_parse_with_illegal_mandatory_cast_values_raises
97     ["yes", 12, 0, nil].each do |mandatory|
98       argv  = []
99       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
100       assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
101     end
102   end
103
104   def test_Biopieces_options_parse_with_legal_mandatory_cast_values_dont_raise
105     [true, false].each do |mandatory|
106       argv  = [ "--foo", "1" ]
107       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
108       assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
109     end
110   end
111
112   def test_Biopieces_options_parse_with_illegal_default_cast_values_raises
113     [true, false, [], {}].each do |default|
114       argv  = []
115       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
116       assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
117     end
118   end
119
120   def test_Biopieces_options_parse_with_legal_default_cast_values_dont_raise
121     [nil, 0, 1, -1].each do |default|
122       argv  = []
123       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
124       assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
125     end
126   end
127
128   def test_Biopieces_options_parse_with_illegal_allowed_cast_values_raises
129     [true, false, {}, [], 0, 0.1].each do |allowed|
130       argv  = []
131       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
132       assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
133     end
134   end
135
136   def test_Biopieces_options_parse_with_legal_allowed_cast_values_dont_raise
137     ["foo,bar,0",nil].each do |allowed|
138       argv  = []
139       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
140       assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
141     end
142   end
143
144   def test_Biopieces_options_parse_with_illegal_disallowed_cast_values_raises
145     [true, false, {}, [], 0, 0.1].each do |disallowed|
146       argv  = []
147       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
148       assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
149     end
150   end
151
152   def test_Biopieces_options_parse_with_legal_disallowed_cast_values_dont_raise
153     ["foo,bar,0",nil].each do |disallowed|
154       argv  = []
155       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
156       assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
157     end
158   end
159
160   def test_Biopieces_options_parse_with_duplicate_long_cast_values_raises
161     argv  = []
162     casts = []
163     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
164     casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
165     assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
166   end
167
168   def test_Biopieces_options_parse_with_duplicate_short_cast_values_raises
169     argv  = []
170     casts = []
171     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
172     casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
173     assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
174   end
175
176   def test_Biopieces_options_parse_without_duplicate_long_and_short_cast_values_dont_raise
177     argv  = []
178     casts = []
179     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
180     casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
181     assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
182   end
183
184   # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
185
186   def test_Biopieces_options_parse_with_empty_argv_and_missing_wiki_file_raises
187     argv  = []
188     casts = []
189     assert_raise(RuntimeError) { Biopieces.options_parse(argv,casts, "foo") }
190   end
191
192   def test_Biopieces_options_parse_with_empty_argv_and_existing_wiki_file_dont_raise
193     argv  = []
194     casts = []
195     assert_nothing_raised { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
196   end
197
198   def test_Biopieces_options_parse_with_help_in_argv_and_existing_wiki_output_long_usage
199     argv = ["--help"]
200     assert_nothing_raised { Biopieces.options_parse(argv,[],SCRIPT_PATH) }
201   end
202
203   def test_Biopieces_options_parse_with_stream_in_argv_returns_correct_options
204     argv = ["--stream_in", DUMMY_FILE]
205     options = Biopieces.options_parse(argv,[],SCRIPT_PATH)
206     assert_equal(DUMMY_FILE, options[:stream_in])
207   end
208  
209    def test_Biopieces_options_parse_with_I_argv_returns_correct_options
210      argv    = ["-I", DUMMY_FILE]
211      casts   = []
212      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
213      assert_equal(DUMMY_FILE, options[:stream_in])
214    end
215  
216    def test_Biopieces_options_parse_use_cast_default_value_if_no_argument_given
217      argv    = ["-I", DUMMY_FILE]
218      casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
219      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
220      assert_equal(options[:foo], "bar")
221    end
222  
223    def test_Biopieces_options_parse_dont_use_default_value_if_argument_given
224      argv    = ["--foo", "bleh", "-I", DUMMY_FILE]
225      casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
226      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
227      assert_equal(options[:foo], "bleh")
228    end
229  
230    def test_Biopieces_options_parse_with_mandatory_cast_and_no_argument_raises
231      argv  = ["-I", DUMMY_FILE]
232      casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
233      assert_raise(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
234    end
235  
236    def test_Biopieces_options_parse_with_mandatory_cast_and_argument_dont_raise
237      argv  = ["--foo", "bar", "-I", DUMMY_FILE]
238      casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
239      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
240    end
241  
242    def test_Biopieces_options_parse_with_type_cast_int_dont_raise
243      [0,-1,1,327649123746293746374276347824].each do |val|
244        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
245        casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
246        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
247      end
248    end
249  
250    def test_Biopieces_options_parse_with_type_cast_uint_dont_raise
251      [0,1,327649123746293746374276347824].each do |val|
252        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
253        casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
254        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
255      end
256    end
257  
258    def test_Biopieces_options_parse_with_file_cast_and_file_dont_exists_raises
259      argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
260      casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
261      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
262    end
263  
264    def test_Biopieces_options_parse_with_file_cast_and_existing_file_dont_raise
265      argv  = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
266      casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
267      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
268    end
269  
270    def test_Biopieces_options_parse_with_files_cast_and_a_file_dont_exists_raises
271      argv  = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
272      casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
273      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
274    end
275  
276    def test_Biopieces_options_parse_with_files_cast_and_files_exists_dont_raise
277      argv  = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
278      casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
279      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
280    end
281  
282    def test_Biopieces_options_parse_with_glob_argument_expands_correctly
283      path = File.join(ENV['BP_DIR'], "bp_test")
284      argv    = ["--foo", "#{path}/te*,#{path}/lib/*.sh", "-I", DUMMY_FILE]
285      casts   = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
286      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
287      assert_equal(["#{path}/test_all", "#{path}/lib/test.sh"], options[:foo])
288    end
289  
290    def test_Biopieces_options_parse_with_dir_cast_and_dir_dont_exists_raises
291      argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
292      casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
293      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
294    end
295  
296    def test_Biopieces_options_parse_with_dir_cast_and_dir_exists_dont_raise
297      argv  = ["--foo", "/", "-I", DUMMY_FILE]
298      casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
299      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
300    end
301  
302    def test_Biopieces_options_parse_with_allowed_cast_and_not_allowed_value_raises
303      ["bleh", "2", "3.3"].each do |val|
304        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
305        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
306        assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
307      end
308    end
309  
310    def test_Biopieces_options_parse_with_allowed_cast_and_allowed_values_dont_raise
311      ["0", "-1", "0.0", "1", "bar"].each do |val|
312        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
313        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
314        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
315      end
316    end
317  
318    def test_Biopieces_options_parse_with_disallowed_cast_and_disallowed_value_raises
319      ["0", "-1", "0.0", "1", "bar"].each do |val|
320        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
321        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
322        assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
323      end
324    end
325  
326    def test_Biopieces_options_parse_with_disallowed_cast_and_allowed_values_dont_raise
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=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
330        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
331      end
332    end
333 end