2 # $:.unshift File.join(File.dirname(__FILE__),'..','lib')
9 TYPES = %w[flag string list int uint float file file! files files! dir dir! genome]
10 SCRIPT_PATH = "write_fasta.rb"
12 class OptionTest < Test::Unit::TestCase
14 # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
16 test "Options.new with no argument don't raise" do
17 assert_nothing_raised { Options.new }
20 test "Options.new with empty list don't raise" do
21 assert_nothing_raised { Options.new([]) }
24 test "Options.new with non-array argument raises" do
25 %w[ "foo", 1, 1.2, {}, nil, false, true ].each do |arg|
26 assert_raise(TypeError) { Options.new(arg) }
30 test "Options.new with missing cast key raises" do
31 assert_raise(CastError) { Options.new([{}]) }
34 test "Options.new with all cast keys don't raise" do
35 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
36 assert_nothing_raised(CastError) { Options.new(casts) }
39 test "Options.new with illegal long cast values raises" do
40 [nil, true, false, 1, 0, "a"].each do |long|
41 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
42 assert_raise(CastError) { Options.new(casts) }
46 test "Options.new with legal long cast values don't raise" do
47 ["foo", "!!", "0123"].each do |long|
48 casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
49 assert_nothing_raised(CastError) { Options.new(casts) }
53 test "Options.new with illegal short cast values raises" do
54 [nil, true, false, "foo"].each do |short|
55 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
56 assert_raise(CastError) { Options.new(casts) }
60 test "Options.new with legal short cast values don't raise" do
61 ["!", "1", "a"].each do |short|
62 casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
63 assert_nothing_raised(CastError) { Options.new(casts) }
67 test "Options.new with illegal type cast values raises" do
68 [nil, true, false, "foo", 12, 0].each do |type|
69 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
70 assert_raise(CastError) { Options.new(casts) }
74 test "Options.new with legal type cast values don't raise" do
76 casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
77 assert_nothing_raised(CastError) { Options.new(casts) }
81 test "Options.new with illegal mandatory cast values raises" do
82 ["yes", 12, 0, nil].each do |mandatory|
83 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
84 assert_raise(CastError) {Options.new(casts) }
88 test "Options.new with legal mandatory cast values don't raise" do
89 [true, false].each do |mandatory|
90 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
91 assert_nothing_raised(CastError) {Options.new(casts) }
95 test "Options.new with illegal default cast values raises" do
96 [true, false, [], {}].each do |default|
97 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
98 assert_raise(CastError) { Options.new(casts) }
102 test "Options.new with legal default cast values don't raise" do
103 ["foo", nil, 0, 0.0, 1, 1.2].each do |default|
104 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
105 assert_nothing_raised(CastError) { Options.new(casts) }
109 test "Options.new with illegal allowed cast values raises" do
110 [true, false, {}, [], 0, 0.1].each do |allowed|
111 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
112 assert_raise(CastError) { Options.new(casts) }
116 test "Options.new with legal allowed cast values don't raise" do
117 ["foo,bar",nil].each do |allowed|
118 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
119 assert_nothing_raised(CastError) { Options.new(casts) }
123 test "Options.new with illegal disallowed cast values raises" do
124 [true, false, {}, [], 0, 0.1].each do |disallowed|
125 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
126 assert_raise(CastError) { Options.new(casts) }
130 test "Options.new with legal disallowed cast values don't raise" do
131 ["foo,bar",nil].each do |disallowed|
132 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
133 assert_nothing_raised(CastError) { Options.new(casts) }
137 test "Options.new with duplicate long cast values raises" do
139 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
140 casts << {:long=>"foo", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
141 assert_raise(CastError) { Options.new(casts) }
144 test "Options.new with duplicate short cast values raises" do
146 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
147 casts << {:long=>"bar", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
148 assert_raise(CastError) { Options.new(casts) }
151 test "Options.new without duplicate long and short cast values don't raise" do
153 casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
154 casts << {:long=>"bar", :short=>"b", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
155 assert_nothing_raised(CastError) { Options.new(casts) }
158 # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
160 test "Options.parse with empty argv and missing wiki file raises" do
161 opt_parser = Options.new
162 assert_raise() { opt_parser.parse([], "foo") }
165 test "Options.parse with empty argv and existing wiki file don't raise" do
166 opt_parser = Options.new
167 opt_parser.expects(:print_usage_and_exit).with()
168 assert_nothing_raised { opt_parser.parse([], SCRIPT_PATH) }
171 test "Options.parse with --help in argv and existing wiki output long usage" do
172 opt_parser = Options.new
173 opt_parser.expects(:print_usage_and_exit).with(true)
174 assert_nothing_raised { opt_parser.parse(["--help"],SCRIPT_PATH) }
177 # # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
178 # test "Options.parse with type cast flag with an argument raises" do
179 # casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
180 # opt_parser = Options.new(casts)
181 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
184 test "Options.parse with --stream_in <arg> returns options['stream_in']=>[<arg>]" do
185 opt_parser = Options.new
186 options = opt_parser.parse(["--stream_in", __FILE__],SCRIPT_PATH)
187 assert_equal([__FILE__], options["stream_in"])
190 test "Options.parse with -I <arg> returns options['stream_in']=>[<arg>]" do
191 opt_parser = Options.new
192 options = opt_parser.parse(["-I", __FILE__],SCRIPT_PATH)
193 assert_equal([__FILE__], options["stream_in"])
196 test "Options.parse use cast default value if no argument given" do
197 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
198 opt_parser = Options.new(casts)
199 options = opt_parser.parse(["-I", __FILE__],SCRIPT_PATH)
200 assert_equal(options["foo"], "bar")
203 test "Options.parse don't use default value if argument given" do
204 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
205 opt_parser = Options.new(casts)
206 options = opt_parser.parse(["--foo", "bleh", "-I", __FILE__],SCRIPT_PATH)
207 assert_equal(options["foo"], "bleh")
210 test "Options.parse with mandatory cast and no argument raises" do
211 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
212 opt_parser = Options.new(casts)
213 assert_raise(ArgumentError) { opt_parser.parse(["-I", __FILE__],SCRIPT_PATH) }
216 test "Options.parse with mandatory cast and argument don't raise" do
217 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
218 opt_parser = Options.new(casts)
219 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", "bar", "-I", __FILE__],SCRIPT_PATH) }
222 # # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
223 # # So it appears that this is tested in OptionParser already.
224 # test "Options.parse with type cast int and non-int value raises" do
225 # ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
226 # casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
227 # opt_parser = Options.new(casts)
228 # assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
232 test "Options.parse with type cast int don't raise" do
233 [0,-1,1,327649123746293746374276347824].each do |val|
234 casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
235 opt_parser = Options.new(casts)
236 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", "#{val}", "-I", __FILE__],SCRIPT_PATH) }
240 # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
242 test "Options.parse with type cast uint don't raise" do
243 [0,1,327649123746293746374276347824].each do |val|
244 casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
245 opt_parser = Options.new(casts)
246 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", "#{val}", "-I", __FILE__],SCRIPT_PATH) }
250 test "Options.parse with file! cast and file don't exists raises" do
251 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
252 opt_parser = Options.new(casts)
253 assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bleh", "-I", __FILE__],SCRIPT_PATH) }
256 test "Options.parse with file! cast and existing file don't raise" do
257 casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
258 opt_parser = Options.new(casts)
259 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", __FILE__, "-I", __FILE__],SCRIPT_PATH) }
262 test "Options.parse with files! cast and a file don't exists raises" do
263 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
264 opt_parser = Options.new(casts)
265 assert_raise(ArgumentError) { opt_parser.parse(["--foo", __FILE__ + ",bleh", "-I", __FILE__],SCRIPT_PATH) }
268 test "Options.parse with files! cast and files exists don't raise" do
269 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
270 opt_parser = Options.new(casts)
271 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", __FILE__ + "," + __FILE__, "-I", __FILE__],SCRIPT_PATH) }
274 # TODO replace the absolute part below the file location with File.dirname(__FILE__)
275 test "Options.parse with glob argument expands correctly" do
276 casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
277 argv = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", __FILE__]
278 opt_parser = Options.new(casts)
279 options = opt_parser.parse(argv,SCRIPT_PATH)
280 assert_equal(options["foo"], ["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"])
283 test "Options.parse with dir! cast and dir don't exists raises" do
284 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
285 opt_parser = Options.new(casts)
286 assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bleh", "-I", __FILE__],SCRIPT_PATH) }
289 test "Options.parse with dir! cast and dir exists don't raise" do
290 casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
291 opt_parser = Options.new(casts)
292 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", "/", "-I", __FILE__],SCRIPT_PATH) }
295 test "Options.parse with allowed cast and not allowed value raises" do
296 ["bleh", "2", "3.3"].each do |val|
297 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
298 opt_parser = Options.new(casts)
299 assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}", "-I", __FILE__],SCRIPT_PATH) }
303 test "Options.parse with allowed cast and allowed values don't raise" do
304 ["0", "-1", "0.0", "1", "bar"].each do |val|
305 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
306 opt_parser = Options.new(casts)
307 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", "#{val}", "-I", __FILE__],SCRIPT_PATH) }
311 test "Options.parse with disallowed cast and disallowed value raises" do
312 ["0", "-1", "0.0", "1", "bar"].each do |val|
313 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
314 opt_parser = Options.new(casts)
315 assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}", "-I", __FILE__],SCRIPT_PATH) }
319 test "Options.parse with disallowed cast and allowed values don't raise" do
320 ["bleh", "2", "3.3"].each do |val|
321 casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
322 opt_parser = Options.new(casts)
323 assert_nothing_raised(ArgumentError) { opt_parser.parse(["--foo", "#{val}", "-I", __FILE__],SCRIPT_PATH) }