]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/test/maasha/test_biopieces.rb
cc77410a8c4c788a72342612b6067ddd0bd28c46
[biopieces.git] / code_ruby / test / maasha / test_biopieces.rb
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2010 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 #  # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
204 #  test "Options.parse with type cast flag with and argument raises
205 #    casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
206 #    opt_parser = Options.new(casts)
207 #    assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
208 #   end
209
210   def test_Biopieces_options_parse_with_stream_in_argv_returns_correct_options
211     argv = ["--stream_in", DUMMY_FILE]
212     options = Biopieces.options_parse(argv,[],SCRIPT_PATH)
213     assert_equal(DUMMY_FILE, options[:stream_in])
214   end
215  
216    def test_Biopieces_options_parse_with_I_argv_returns_correct_options
217      argv    = ["-I", DUMMY_FILE]
218      casts   = []
219      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
220      assert_equal(DUMMY_FILE, options[:stream_in])
221    end
222  
223    def test_Biopieces_options_parse_use_cast_default_value_if_no_argument_given
224      argv    = ["-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], "bar")
228    end
229  
230    def test_Biopieces_options_parse_dont_use_default_value_if_argument_given
231      argv    = ["--foo", "bleh", "-I", DUMMY_FILE]
232      casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
233      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
234      assert_equal(options[:foo], "bleh")
235    end
236  
237    def test_Biopieces_options_parse_with_mandatory_cast_and_no_argument_raises
238      argv  = ["-I", DUMMY_FILE]
239      casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
240      assert_raise(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
241    end
242  
243    def test_Biopieces_options_parse_with_mandatory_cast_and_argument_dont_raise
244      argv  = ["--foo", "bar", "-I", DUMMY_FILE]
245      casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
246      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
247    end
248  
249 #  # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
250 #  # So it appears that this is tested in OptionParser already.
251 #  test "Options.parse with type cast int and non-int value raises" do
252 #    ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
253 #      casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
254 #      opt_parser = Options.new(casts)
255 #      assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
256 #    end
257 #  end
258
259    def test_Biopieces_options_parse_with_type_cast_int_dont_raise
260      [0,-1,1,327649123746293746374276347824].each do |val|
261        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
262        casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
263        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
264      end
265    end
266  
267    # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
268  
269    def test_Biopieces_options_parse_with_type_cast_uint_dont_raise
270      [0,1,327649123746293746374276347824].each do |val|
271        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
272        casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
273        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
274      end
275    end
276  
277    def test_Biopieces_options_parse_with_file_cast_and_file_dont_exists_raises
278      argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
279      casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
280      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
281    end
282  
283    def test_Biopieces_options_parse_with_file_cast_and_existing_file_dont_raise
284      argv  = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
285      casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
286      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
287    end
288  
289    def test_Biopieces_options_parse_with_files_cast_and_a_file_dont_exists_raises
290      argv  = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
291      casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
292      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
293    end
294  
295    def test_Biopieces_options_parse_with_files_cast_and_files_exists_dont_raise
296      argv  = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
297      casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
298      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
299    end
300  
301 #   # TODO replace the absolute part below the file location with File.dirname(__FILE__)
302 #   def test_Biopieces_options_parse_with_glob_argument_expands_correctly
303 #     flunk("This test is flawed and need fixing")
304 #     argv    = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", DUMMY_FILE]
305 #     casts   = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
306 #     options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
307 #     assert_equal(["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"], options[:foo])
308 #   end
309  
310    def test_Biopieces_options_parse_with_dir_cast_and_dir_dont_exists_raises
311      argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
312      casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
313      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
314    end
315  
316    def test_Biopieces_options_parse_with_dir_cast_and_dir_exists_dont_raise
317      argv  = ["--foo", "/", "-I", DUMMY_FILE]
318      casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
319      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
320    end
321  
322    def test_Biopieces_options_parse_with_allowed_cast_and_not_allowed_value_raises
323      ["bleh", "2", "3.3"].each do |val|
324        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
325        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
326        assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
327      end
328    end
329  
330    def test_Biopieces_options_parse_with_allowed_cast_and_allowed_values_dont_raise
331      ["0", "-1", "0.0", "1", "bar"].each do |val|
332        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
333        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
334        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
335      end
336    end
337  
338    def test_Biopieces_options_parse_with_disallowed_cast_and_disallowed_value_raises
339      ["0", "-1", "0.0", "1", "bar"].each do |val|
340        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
341        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
342        assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
343      end
344    end
345  
346    def test_Biopieces_options_parse_with_disallowed_cast_and_allowed_values_dont_raise
347      ["bleh", "2", "3.3"].each do |val|
348        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
349        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
350        assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
351      end
352    end
353 end