]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/test/test_biopieces.rb
ruby unit tests refactored
[biopieces.git] / code_ruby / Maasha / test / test_biopieces.rb
1 #!/usr/bin/env ruby
2 # $:.unshift File.join(File.dirname(__FILE__),'..','lib')
3
4 require 'test/unit'
5 require 'mocha'
6 require 'biopieces'
7 require 'pp'
8
9 TYPES       = %w[flag string list int uint float file file! files files! dir dir! genome]
10 SCRIPT_PATH = "write_fasta.rb"
11
12 class OptionTest < Test::Unit::TestCase
13
14   def setup
15     @bp = Biopieces.new(true)
16   end
17
18   # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
19
20   test "Biopieces#parse with all cast keys don't raise" do
21     argv  = []
22     casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
23     @bp.expects(:print_usage_and_exit).with()
24     assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
25   end
26
27   test "Biopieces#parse with illegal long cast values raises" do
28     [nil, true, false, 1, 0, "a"].each do |long|
29       argv  = []
30       casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
31       assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
32     end
33   end
34
35   test "Biopieces#parse with legal long cast values don't raise" do
36     ["foo", "!!", "0123"].each do |long|
37       argv  = []
38       casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
39       @bp.expects(:print_usage_and_exit).with()
40       assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
41     end
42   end
43
44   test "Biopieces#parse with illegal short cast values raises" do
45     [nil, true, false, "foo"].each do |short|
46       argv  = []
47       casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
48       assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
49     end
50   end
51
52   test "Biopieces#parse with legal short cast values don't raise" do
53     ["!", "1", "a"].each do |short|
54       argv  = []
55       casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
56       @bp.expects(:print_usage_and_exit).with()
57       assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
58     end
59   end
60
61   test "Biopieces#parse with illegal type cast values raises" do
62     [nil, true, false, "foo", 12, 0].each do |type|
63       argv  = []
64       casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
65       assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
66     end
67   end
68
69   test "Biopieces#parse with legal type cast values don't raise" do
70     TYPES.each do |type|
71       argv  = []
72       casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
73       @bp.expects(:print_usage_and_exit).with()
74       assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
75     end
76   end
77
78   test "Biopieces#parse with illegal mandatory cast values raises" do
79     ["yes", 12, 0, nil].each do |mandatory|
80       argv  = []
81       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
82       assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
83     end
84   end
85
86   test "Biopieces#parse with legal mandatory cast values don't raise" do
87     [true, false].each do |mandatory|
88       argv  = []
89       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
90       @bp.expects(:print_usage_and_exit).with()
91       assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
92     end
93   end
94
95   test "Biopieces#parse with illegal default cast values raises" do
96     [true, false, [], {}].each do |default|
97       argv  = []
98       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
99       assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
100     end
101   end
102
103   test "Biopieces#parse with legal default cast values don't raise" do
104     ["foo", nil, 0, 0.0, 1, 1.2].each do |default|
105       argv  = []
106       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
107       @bp.expects(:print_usage_and_exit).with()
108       assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
109     end
110   end
111
112   test "Biopieces#parse with illegal allowed cast values raises" do
113     [true, false, {}, [], 0, 0.1].each do |allowed|
114       argv  = []
115       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
116       assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
117     end
118   end
119
120   test "Biopieces#parse with legal allowed cast values don't raise" do
121     ["foo,bar",nil].each do |allowed|
122       argv  = []
123       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
124       @bp.expects(:print_usage_and_exit).with()
125       assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
126     end
127   end
128
129   test "Biopieces#parse with illegal disallowed cast values raises" do
130     [true, false, {}, [], 0, 0.1].each do |disallowed|
131       argv  = []
132       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
133       assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
134     end
135   end
136
137   test "Biopieces#parse with legal disallowed cast values don't raise" do
138     ["foo,bar",nil].each do |disallowed|
139       argv  = []
140       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
141       @bp.expects(:print_usage_and_exit).with()
142       assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
143     end
144   end
145
146   test "Biopieces#parse with duplicate long cast values raises" do
147     argv  = []
148     casts = []
149     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
150     casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
151     assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
152   end
153
154   test "Biopieces#parse with duplicate short cast values raises" do
155     argv  = []
156     casts = []
157     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
158     casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
159     assert_raise(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
160   end
161
162   test "Biopieces#parse without duplicate long and short cast values don't raise" do
163     argv  = []
164     casts = []
165     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
166     casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
167     @bp.expects(:print_usage_and_exit).with()
168     assert_nothing_raised(CastError) { @bp.parse(argv, casts, SCRIPT_PATH) }
169   end
170
171   # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
172
173   test "Biopieces#parse with empty argv and missing wiki file raises" do
174     argv  = []
175     casts = []
176     assert_raise() { @bp.parse(argv,casts, "foo") }
177   end
178
179   test "Biopieces#parse with empty argv and existing wiki file don't raise" do
180     argv  = []
181     casts = []
182     @bp.expects(:print_usage_and_exit).with()
183     assert_nothing_raised { @bp.parse(argv, casts, SCRIPT_PATH) }
184   end
185
186    test "Biopieces#parse with --help in argv and existing wiki output long usage" do
187      argv = ["--help"]
188      @bp.expects(:print_usage_and_exit).with(true)
189      assert_nothing_raised { @bp.parse(argv,[],SCRIPT_PATH) }
190    end
191
192 #  # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
193 #  test "Options.parse with type cast flag with an argument raises" do
194 #    casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
195 #    opt_parser = Options.new(casts)
196 #    assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
197 #   end
198
199   test "Biopieces#parse with --stream_in <arg> returns options['stream_in']=>[<arg>]" do
200     argv = ["--stream_in", __FILE__]
201     options = @bp.parse(argv,[],SCRIPT_PATH)
202     assert_equal([__FILE__], options["stream_in"])
203   end
204
205   test "Biopieces#parse with -I <arg> returns options['stream_in']=>[<arg>]" do
206     argv    = ["-I", __FILE__]
207     casts   = []
208     options = @bp.parse(argv, casts, SCRIPT_PATH)
209     assert_equal([__FILE__], options["stream_in"])
210   end
211
212   test "Biopieces#parse use cast default value if no argument given" do
213     argv    = ["-I", __FILE__]
214     casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
215     options = @bp.parse(argv, casts, SCRIPT_PATH)
216     assert_equal(options["foo"], "bar")
217   end
218
219   test "Biopieces#parse don't use default value if argument given" do
220     argv    = ["--foo", "bleh", "-I", __FILE__]
221     casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
222     options = @bp.parse(argv, casts, SCRIPT_PATH)
223     assert_equal(options["foo"], "bleh")
224   end
225
226   test "Biopieces#parse with mandatory cast and no argument raises" do
227     argv  = ["-I", __FILE__]
228     casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
229     assert_raise(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
230   end
231
232   test "Biopieces#parse with mandatory cast and argument don't raise" do
233     argv  = ["--foo", "bar", "-I", __FILE__]
234     casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
235     assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
236   end
237
238 #  # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
239 #  # So it appears that this is tested in OptionParser already.
240 #  test "Options.parse with type cast int and non-int value raises" do
241 #    ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
242 #      casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
243 #      opt_parser = Options.new(casts)
244 #      assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
245 #    end
246 #  end
247
248   test "Biopieces#parse with type cast int don't raise" do
249     [0,-1,1,327649123746293746374276347824].each do |val|
250       argv  = ["--foo", "#{val}", "-I", __FILE__]
251       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
252       assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
253     end
254   end
255
256   # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
257
258   test "Biopieces#parse with type cast uint don't raise" do
259     [0,1,327649123746293746374276347824].each do |val|
260       argv  = ["--foo", "#{val}", "-I", __FILE__]
261       casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
262       assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
263     end
264   end
265
266   test "Biopieces#parse with file! cast and file don't exists raises" do
267     argv  = ["--foo", "bleh", "-I", __FILE__]
268     casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
269     assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
270   end
271
272   test "Biopieces#parse with file! cast and existing file don't raise" do
273     argv  = ["--foo", __FILE__, "-I", __FILE__]
274     casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
275     assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
276   end
277
278   test "Biopieces#parse with files! cast and a file don't exists raises" do
279     argv  = ["--foo", __FILE__ + ",bleh", "-I", __FILE__]
280     casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
281     assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
282   end
283
284   test "Biopieces#parse with files! cast and files exists don't raise" do
285     argv  = ["--foo", __FILE__ + "," + __FILE__, "-I", __FILE__]
286     casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
287     assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
288   end
289
290   # TODO replace the absolute part below the file location with File.dirname(__FILE__)
291   test "Biopieces#parse with glob argument expands correctly" do
292     argv    = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", __FILE__]
293     casts   = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
294     options = @bp.parse(argv, casts, SCRIPT_PATH)
295     assert_equal(options["foo"], ["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"])
296   end
297
298   test "Biopieces#parse with dir! cast and dir don't exists raises" do
299     argv  = ["--foo", "bleh", "-I", __FILE__]
300     casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
301     assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
302   end
303
304   test "Biopieces#parse with dir! cast and dir exists don't raise" do
305     argv  = ["--foo", "/", "-I", __FILE__]
306     casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
307     assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
308   end
309
310   test "Biopieces#parse with allowed cast and not allowed value raises" do
311     ["bleh", "2", "3.3"].each do |val|
312       argv  = ["--foo", "#{val}", "-I", __FILE__]
313       casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
314       assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
315     end
316   end
317
318   test "Biopieces#parse with allowed cast and allowed values don't raise" do
319     ["0", "-1", "0.0", "1", "bar"].each do |val|
320       argv  = ["--foo", "#{val}", "-I", __FILE__]
321       casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
322       assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
323     end
324   end
325
326   test "Biopieces#parse with disallowed cast and disallowed value raises" do
327     ["0", "-1", "0.0", "1", "bar"].each do |val|
328       argv  = ["--foo", "#{val}", "-I", __FILE__]
329       casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
330       assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
331     end
332   end
333
334   test "Biopieces#parse with disallowed cast and allowed values don't raise" do
335     ["bleh", "2", "3.3"].each do |val|
336       argv  = ["--foo", "#{val}", "-I", __FILE__]
337       casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
338       assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
339     end
340   end
341 end