/Languages/Ruby/Tests/mspec/rubyspec/library/getoptlong/shared/get.rb

http://github.com/IronLanguages/main · Ruby · 75 lines · 62 code · 13 blank · 0 comment · 12 complexity · f750809713560a25160cdba455f5899b MD5 · raw file

  1. describe :getoptlong_get, :shared => true do
  2. before(:each) do
  3. @opts = GetoptLong.new(
  4. [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ],
  5. [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
  6. [ '--query', '-q', GetoptLong::NO_ARGUMENT ],
  7. [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ]
  8. )
  9. @opts.quiet = true # silence using $deferr
  10. end
  11. it "returns the next option name and its argument as an Array" do
  12. begin
  13. old_argv = ARGV
  14. ARGV = [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ]
  15. @opts.send(@method).should == [ "--size", "10k" ]
  16. @opts.send(@method).should == [ "--verbose", "" ]
  17. @opts.send(@method).should == [ "--query", ""]
  18. @opts.send(@method).should == nil
  19. ensure
  20. ARGV = old_argv
  21. end
  22. end
  23. it "shifts ARGV on each call" do
  24. begin
  25. old_argv = ARGV
  26. ARGV = [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ]
  27. @opts.send(@method)
  28. ARGV.should == [ "-v", "-q", "a.txt", "b.txt" ]
  29. @opts.send(@method)
  30. ARGV.should == [ "-q", "a.txt", "b.txt" ]
  31. @opts.send(@method)
  32. ARGV.should == [ "a.txt", "b.txt" ]
  33. @opts.send(@method)
  34. ARGV.should == [ "a.txt", "b.txt" ]
  35. ensure
  36. ARGV = old_argv
  37. end
  38. end
  39. it "terminates processing when encountering '--'" do
  40. begin
  41. old_argv = ARGV
  42. ARGV = [ "--size", "10k", "--", "-v", "-q", "a.txt", "b.txt" ]
  43. @opts.send(@method)
  44. ARGV.should == ["--", "-v", "-q", "a.txt", "b.txt"]
  45. @opts.send(@method)
  46. ARGV.should == ["-v", "-q", "a.txt", "b.txt"]
  47. @opts.send(@method)
  48. ARGV.should == ["-v", "-q", "a.txt", "b.txt"]
  49. ensure
  50. ARGV = old_argv
  51. end
  52. end
  53. it "raises a if an argument was required, but none given" do
  54. begin
  55. old_argv = ARGV
  56. ARGV = [ "--size" ]
  57. lambda { @opts.send(@method) }.should raise_error(GetoptLong::MissingArgument)
  58. ensure
  59. ARGV = old_argv
  60. end
  61. end
  62. end