Test::Unit examples and OptionParser

Hello,

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

–output_file
–cutoff_value
–list_of_keys
–verbose

Cheers,

Martin

2010/4/22 Martin H. [email protected]:

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

–output_file
–cutoff_value
–list_of_keys
–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 = {}

OptionParser.new do |opts|
opts.on ‘–help’ do |val|
options[:help] = val
end
end.parse! argv

options
end

[
[[], [], {}],
[%w{–help}, [], {:help => true}],
].each do |inp, outp, opts|
printf “%-30s argv=%p\n”, “before”, inp
options = my_parse inp
printf “%-30s argv=%p options=%p\n”, “after”, inp, options
puts “options ok #{opts == options}”,
“argv ok #{outp == inp}”
end

Kind regards

robert

@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

2010/4/22 Martin H. [email protected]:

@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.

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

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs