Test::Unit examples and OptionParser

M

Martin Hansen

Hello,


I am looking for a stack of unit tests examples to check the behavior of
OptionParser for parsing command line options, such as:


[option]:
--help
--output_file
--cutoff_value
--list_of_keys
--verbose


Cheers,


Martin
 
R

Robert Klemme

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/
 
M

Martin Hansen

@Robert
What exactly do you expect?

I was hoping to see a bunch of real-life Test::Unit examples of how to
validate different types of arguments (option specs) given to a script
via OptionParser.


Cheers,


Martin
 
R

Robert Klemme

2010/4/22 Martin Hansen said:
@Robert


I was hoping to see a bunch of real-life Test::Unit examples of how to
validate different types of arguments (option specs) given to a script
via OptionParser.

Well, there you have it. Just replace "==" tests with assertions and
wrap the whole stuff in test methods.

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top