From: Martin Hansen on
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
--
Posted via http://www.ruby-forum.com/.

From: Robert Klemme on
2010/4/22 Martin Hansen <mail(a)maasha.dk>:
> 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

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: Martin Hansen on
@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



--
Posted via http://www.ruby-forum.com/.