I just tried to integrate a simple test to check some options passed as argument to a class to be executed: [code] require ''spec_helper'' module PropertyInjector describe Options do describe "#source folder option" do it "sets up a source folder" do argv = %w{-e path/to/some/folder} puts "argv[0]: #{argv[0]}" puts "argv[1]: #{argv[1]}" options = Options.new(argv) puts "options.export_folder: #{options.export_folder}" options.export_folder.should == argv[1] end end end end [/code] class Options: [code] module PropertyInjector class Options def initialize(argv) @bundle_folder = DEFAULT_BUNDLE_FOLDER @export_folder = DEFAULT_EXPORT_FOLDER @import_file_path = File.join(DEFAULT_IMPORT_FOLDER, EXCEL_FILE_NAME) @export = true @locales = [''en''] parse(argv) end private def parse(argv) OptionParser.new do |opts| opts.banner = "Usage: translation properties injector [options]." opts.on("-e [EXPORT_FOLDER]", "--export [EXPORT_FOLDER]", String, "Path to the export folder to contain a generated Excel file, (default=#{DEFAULT_EXPORT_FOLDER}") do |folder| @export = true @export_folder = folder if folder end opts.on("-i [EXCEL_FILE_PATH]", "--import [EXCEL_FILE_PATH]", String, "Path to the Excel file to import translations from (default=#{DEFAULT_IMPORT_FOLDER}/#{EXCEL_FILE_NAME})") do |file_path| @export = false @import_file_path = file_path if file_path end opts.on("-b [BUNDLE_FOLDER]", "--bundle [BUNDLE_FOLDER]", String, "Path to the folder containing properties files, (default=#{DEFAULT_BUNDLE_FOLDER})") do |folder| @bundle_folder = folder if folder end opts.on("-l [l1,l2,l3]", "--langs [l1,l2,l3]", Array, "Locales values to use separated by comma, starting from the known one, default=en") do |langs| if langs @locales = langs.map(&:downcase) @locales.uniq! end end opts.on("-", "--help", "Show this message") do puts opts exit end begin argv = ["-h"] if argv.empty? opts.parse!(argv) rescue OptionParser::ParseError => e STDERR.puts e.message, "\n", opts exit(1) end end end end end [/code] After running the test, here is what I got: [code] PropertyInjector::Options #source folder option argv[0]: -e argv[1]: path/to/some/folder options.export_folder: path/to/some/folder sets up a source folder (FAILED - 1) Failures: 1) PropertyInjector::Options#source folder option sets up a source folder Failure/Error: options.export_folder.should == argv[1] expected: nil got: "path/to/some/folder" (using ==) # ./spec/property_injector/options_spec.rb:12:in `block (3 levels) in <module:PropertyInjector>'' Finished in 0.01562 seconds 1 example, 1 failure Failed examples: rspec ./spec/property_injector/options_spec.rb:6 # PropertyInjector::Options#source folder option sets up a source folder [/code] WHY the second argument is NIL if it WAS NOT before (path/to/some/folder)? Thank you in advance. -- Posted via http://www.ruby-forum.com/.
On Wed, Feb 15, 2012 at 5:57 PM, Serguei Cambour <lists at ruby-forum.com> wrote:> WHY the second argument is NIL if it WAS NOT before > (path/to/some/folder)?If you change this:> options.export_folder.should == argv[1]to this: options.export_folder.should eq(argv[1]) You will see that you get the value that you''ve set in DEFAULT_EXPORT_FOLDER. That''s not quite the desired behavior either, though. I think the issue is with the OptionParser... you define all the options, but I can''t see that you ever actually tell it to parse.> option_parser = OptionParser.new do |opts| > # lots of definitions > endIf I''m reading this correctly, the missing piece of code is this: option_parser.parse(argv) Note that you don''t want to use the bang version, because that will change your argv array, and when you compare it in the spec, argv[1] will be nil. Cheers, Katrina
On rereading, I see that you do parse the options using the parse! method, so the answer is to use parse(argv) instead of parse!(argv). The == vs eq() is probably irrelevant. Sorry to have brought it up. As an aside, i would recommend using a gist with an actual working (failing) examole, as that will make it easier to read, easy to run, and therefore easier to help. Hope this helps, Katrina -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120215/d54a3543/attachment.html>
Morten Møller Riis
2012-Feb-15 21:31 UTC
[rspec-users] Such a simple test fails. But why ?
This looks a little funny in my phone. But what happens if you do this? ... options = Options.new(argv) puts "argv[0]: #{argv[0]}" puts "argv[1]: #{argv[1]}" ... Best regards Morten Sent from my iPhone On 15/02/2012, at 19.26, Serguei Cambour <lists at ruby-forum.com> wrote:> I just tried to integrate a simple test to check some options passed as > argument to a class to be executed: > [code] > require ''spec_helper'' > > module PropertyInjector > describe Options do > describe "#source folder option" do > it "sets up a source folder" do > argv = %w{-e path/to/some/folder} > puts "argv[0]: #{argv[0]}" > puts "argv[1]: #{argv[1]}" > options = Options.new(argv) > puts "options.export_folder: #{options.export_folder}" > options.export_folder.should == argv[1] > end > end > end > end > [/code] > class Options: > [code] > module PropertyInjector > class Options > def initialize(argv) > @bundle_folder = DEFAULT_BUNDLE_FOLDER > @export_folder = DEFAULT_EXPORT_FOLDER > @import_file_path = File.join(DEFAULT_IMPORT_FOLDER, > EXCEL_FILE_NAME) > @export = true > @locales = [''en''] > parse(argv) > end > > private > def parse(argv) > OptionParser.new do |opts| > opts.banner = "Usage: translation properties injector > [options]." > > opts.on("-e [EXPORT_FOLDER]", "--export [EXPORT_FOLDER]", > String, "Path to the export folder to contain a generated Excel file, > (default=#{DEFAULT_EXPORT_FOLDER}") do |folder| > @export = true > @export_folder = folder if folder > end > > opts.on("-i [EXCEL_FILE_PATH]", "--import [EXCEL_FILE_PATH]", > String, "Path to the Excel file to import translations from > (default=#{DEFAULT_IMPORT_FOLDER}/#{EXCEL_FILE_NAME})") do |file_path| > @export = false > @import_file_path = file_path if file_path > end > > opts.on("-b [BUNDLE_FOLDER]", "--bundle [BUNDLE_FOLDER]", > String, "Path to the folder containing properties files, > (default=#{DEFAULT_BUNDLE_FOLDER})") do |folder| > @bundle_folder = folder if folder > end > > opts.on("-l [l1,l2,l3]", "--langs [l1,l2,l3]", Array, "Locales > values to use separated by comma, starting from the known one, > default=en") do |langs| > if langs > @locales = langs.map(&:downcase) > @locales.uniq! > end > > end > > opts.on("-", "--help", "Show this message") do > puts opts > exit > end > > begin > argv = ["-h"] if argv.empty? > opts.parse!(argv) > rescue OptionParser::ParseError => e > STDERR.puts e.message, "\n", opts > exit(1) > end > end > end > end > end > [/code] > After running the test, here is what I got: > [code] > PropertyInjector::Options > #source folder option > argv[0]: -e > argv[1]: path/to/some/folder > options.export_folder: path/to/some/folder > sets up a source folder (FAILED - 1) > > Failures: > > 1) PropertyInjector::Options#source folder option sets up a source > folder > Failure/Error: options.export_folder.should == argv[1] > expected: nil > got: "path/to/some/folder" (using ==) > # ./spec/property_injector/options_spec.rb:12:in `block (3 levels) > in <module:PropertyInjector>'' > > Finished in 0.01562 seconds > 1 example, 1 failure > > Failed examples: > > rspec ./spec/property_injector/options_spec.rb:6 # > PropertyInjector::Options#source folder option sets up a source folder > [/code] > WHY the second argument is NIL if it WAS NOT before > (path/to/some/folder)? > Thank you in advance. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
On Feb 15, 2012, at 9:57 AM, Serguei Cambour wrote:> I just tried to integrate a simple test to check some options passed as > argument to a class to be executed: > [code] > require ''spec_helper'' > > module PropertyInjector > describe Options do > describe "#source folder option" do > it "sets up a source folder" do > argv = %w{-e path/to/some/folder} > puts "argv[0]: #{argv[0]}" > puts "argv[1]: #{argv[1]}" > options = Options.new(argv) > puts "options.export_folder: #{options.export_folder}" > options.export_folder.should == argv[1] > end > end > end > end > [/code] > class Options: > [code] > module PropertyInjector > class Options > def initialize(argv) > @bundle_folder = DEFAULT_BUNDLE_FOLDER > @export_folder = DEFAULT_EXPORT_FOLDER > @import_file_path = File.join(DEFAULT_IMPORT_FOLDER, > EXCEL_FILE_NAME) > @export = true > @locales = [''en''] > parse(argv) > end > > private > def parse(argv) > OptionParser.new do |opts| > opts.banner = "Usage: translation properties injector > [options]." > > opts.on("-e [EXPORT_FOLDER]", "--export [EXPORT_FOLDER]", > String, "Path to the export folder to contain a generated Excel file, > (default=#{DEFAULT_EXPORT_FOLDER}") do |folder| > @export = true > @export_folder = folder if folder > end > > opts.on("-i [EXCEL_FILE_PATH]", "--import [EXCEL_FILE_PATH]", > String, "Path to the Excel file to import translations from > (default=#{DEFAULT_IMPORT_FOLDER}/#{EXCEL_FILE_NAME})") do |file_path| > @export = false > @import_file_path = file_path if file_path > end > > opts.on("-b [BUNDLE_FOLDER]", "--bundle [BUNDLE_FOLDER]", > String, "Path to the folder containing properties files, > (default=#{DEFAULT_BUNDLE_FOLDER})") do |folder| > @bundle_folder = folder if folder > end > > opts.on("-l [l1,l2,l3]", "--langs [l1,l2,l3]", Array, "Locales > values to use separated by comma, starting from the known one, > default=en") do |langs| > if langs > @locales = langs.map(&:downcase) > @locales.uniq! > end > > end > > opts.on("-", "--help", "Show this message") do > puts opts > exit > end > > begin > argv = ["-h"] if argv.empty? > opts.parse!(argv) > rescue OptionParser::ParseError => e > STDERR.puts e.message, "\n", opts > exit(1) > end > end > end > end > end > [/code] > After running the test, here is what I got: > [code] > PropertyInjector::Options > #source folder option > argv[0]: -e > argv[1]: path/to/some/folder > options.export_folder: path/to/some/folder > sets up a source folder (FAILED - 1) > > Failures: > > 1) PropertyInjector::Options#source folder option sets up a source > folder > Failure/Error: options.export_folder.should == argv[1] > expected: nil > got: "path/to/some/folder" (using ==) > # ./spec/property_injector/options_spec.rb:12:in `block (3 levels) > in <module:PropertyInjector>'' > > Finished in 0.01562 seconds > 1 example, 1 failure > > Failed examples: > > rspec ./spec/property_injector/options_spec.rb:6 # > PropertyInjector::Options#source folder option sets up a source folder > [/code] > WHY the second argument is NIL if it WAS NOT before > (path/to/some/folder)? > Thank you in advance. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersOptionParser is obviously mutating "argv". Try passing "argv.dup"
I found why and shame on me :(((. The array variable ''argv'' was passed by reference into the Options class and was modified there by ''parse'' method. That''s why, when returned back to the testing code, the array was nil. -- Posted via http://www.ruby-forum.com/.