2010/4/22 Martin Hansen said:
I am looking for a stack of unit tests examples to check the behavior of
OptionParser for parsing command line options, such as:
[option]:
=A0--help
=A0--output_file
=A0--cutoff_value
=A0--list_of_keys
=A0--verbose
What exactly do you expect? Do you want to test whether OptionParser
works properly or do you want to test your option spec? For the
former you'll likely find tests where OptionParser sources are. For
the latter you will have to provide different argument lists to OP and
see whether it works as intended, e.g.
require 'optparse'
def my_parse(argv)
options =3D {}
OptionParser.new do |opts|
opts.on '--help' do |val|
options[:help] =3D val
end
end.parse! argv
options
end
[
[[], [], {}],
[%w{--help}, [], {:help =3D> true}],
].each do |inp, outp, opts|
printf "%-30s argv=3D%p\n", "before", inp
options =3D my_parse inp
printf "%-30s argv=3D%p options=3D%p\n", "after", inp, options
puts "options ok #{opts =3D=3D options}",
"argv ok #{outp =3D=3D inp}"
end
Kind regards
robert
--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/