diff options
Diffstat (limited to 'spec/ruby/library/getoptlong')
| -rw-r--r-- | spec/ruby/library/getoptlong/each_option_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/each_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/error_message_spec.rb | 23 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/get_option_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/get_spec.rb | 7 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/initialize_spec.rb | 28 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/ordering_spec.rb | 38 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/set_options_spec.rb | 98 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/shared/each.rb | 18 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/shared/get.rb | 62 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/terminate_spec.rb | 30 | ||||
| -rw-r--r-- | spec/ruby/library/getoptlong/terminated_spec.rb | 17 |
12 files changed, 342 insertions, 0 deletions
diff --git a/spec/ruby/library/getoptlong/each_option_spec.rb b/spec/ruby/library/getoptlong/each_option_spec.rb new file mode 100644 index 0000000000..c6d82af86d --- /dev/null +++ b/spec/ruby/library/getoptlong/each_option_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require 'getoptlong' +require_relative 'shared/each' + +describe "GetoptLong#each_option" do + it_behaves_like :getoptlong_each, :each_option +end diff --git a/spec/ruby/library/getoptlong/each_spec.rb b/spec/ruby/library/getoptlong/each_spec.rb new file mode 100644 index 0000000000..d9022f02af --- /dev/null +++ b/spec/ruby/library/getoptlong/each_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require 'getoptlong' +require_relative 'shared/each' + +describe "GetoptLong#each" do + it_behaves_like :getoptlong_each, :each +end diff --git a/spec/ruby/library/getoptlong/error_message_spec.rb b/spec/ruby/library/getoptlong/error_message_spec.rb new file mode 100644 index 0000000000..10435b1350 --- /dev/null +++ b/spec/ruby/library/getoptlong/error_message_spec.rb @@ -0,0 +1,23 @@ +require_relative '../../spec_helper' +require 'getoptlong' + +describe "GetoptLong#error_message" do + it "returns nil if no error occurred" do + opts = GetoptLong.new + opts.error_message.should == nil + end + + it "returns the error message of the last error that occurred" do + argv [] do + opts = GetoptLong.new + opts.quiet = true + opts.get + -> { + opts.ordering = GetoptLong::PERMUTE + }.should.raise(ArgumentError) { |e| + e.message.should == "argument error" + opts.error_message.should == "argument error" + } + end + end +end diff --git a/spec/ruby/library/getoptlong/get_option_spec.rb b/spec/ruby/library/getoptlong/get_option_spec.rb new file mode 100644 index 0000000000..3cb2044379 --- /dev/null +++ b/spec/ruby/library/getoptlong/get_option_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require 'getoptlong' +require_relative 'shared/get' + +describe "GetoptLong#get_option" do + it_behaves_like :getoptlong_get, :get_option +end diff --git a/spec/ruby/library/getoptlong/get_spec.rb b/spec/ruby/library/getoptlong/get_spec.rb new file mode 100644 index 0000000000..a8ec586fc9 --- /dev/null +++ b/spec/ruby/library/getoptlong/get_spec.rb @@ -0,0 +1,7 @@ +require_relative '../../spec_helper' +require 'getoptlong' +require_relative 'shared/get' + +describe "GetoptLong#get" do + it_behaves_like :getoptlong_get, :get +end diff --git a/spec/ruby/library/getoptlong/initialize_spec.rb b/spec/ruby/library/getoptlong/initialize_spec.rb new file mode 100644 index 0000000000..782edbd981 --- /dev/null +++ b/spec/ruby/library/getoptlong/initialize_spec.rb @@ -0,0 +1,28 @@ +require_relative '../../spec_helper' +require 'getoptlong' + +describe "GetoptLong#initialize" do + it "sets ordering to REQUIRE_ORDER if ENV['POSIXLY_CORRECT'] is set" do + begin + old_env_value = ENV["POSIXLY_CORRECT"] + ENV["POSIXLY_CORRECT"] = "" + + opt = GetoptLong.new + opt.ordering.should == GetoptLong::REQUIRE_ORDER + ensure + ENV["POSIXLY_CORRECT"] = old_env_value + end + end + + it "sets ordering to PERMUTE if ENV['POSIXLY_CORRECT'] is not set" do + begin + old_env_value = ENV["POSIXLY_CORRECT"] + ENV["POSIXLY_CORRECT"] = nil + + opt = GetoptLong.new + opt.ordering.should == GetoptLong::PERMUTE + ensure + ENV["POSIXLY_CORRECT"] = old_env_value + end + end +end diff --git a/spec/ruby/library/getoptlong/ordering_spec.rb b/spec/ruby/library/getoptlong/ordering_spec.rb new file mode 100644 index 0000000000..60ce73afaa --- /dev/null +++ b/spec/ruby/library/getoptlong/ordering_spec.rb @@ -0,0 +1,38 @@ +require_relative '../../spec_helper' +require 'getoptlong' + +describe "GetoptLong#ordering=" do + it "raises an ArgumentError if called after processing has started" do + argv [ "--size", "10k", "--verbose" ] do + opts = GetoptLong.new([ '--size', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', GetoptLong::NO_ARGUMENT ]) + opts.quiet = true + opts.get + + -> { + opts.ordering = GetoptLong::PERMUTE + }.should.raise(ArgumentError) + end + end + + it "raises an ArgumentError if given an invalid value" do + opts = GetoptLong.new + + -> { + opts.ordering = 12345 + }.should.raise(ArgumentError) + end + + it "does not allow changing ordering to PERMUTE if ENV['POSIXLY_CORRECT'] is set" do + begin + old_env_value = ENV['POSIXLY_CORRECT'] + ENV['POSIXLY_CORRECT'] = "" + + opts = GetoptLong.new + opts.ordering = GetoptLong::PERMUTE + opts.ordering.should == GetoptLong::REQUIRE_ORDER + ensure + ENV['POSIXLY_CORRECT'] = old_env_value + end + end +end diff --git a/spec/ruby/library/getoptlong/set_options_spec.rb b/spec/ruby/library/getoptlong/set_options_spec.rb new file mode 100644 index 0000000000..f60dcc87a3 --- /dev/null +++ b/spec/ruby/library/getoptlong/set_options_spec.rb @@ -0,0 +1,98 @@ +require_relative '../../spec_helper' +require 'getoptlong' + +describe "GetoptLong#set_options" do + before :each do + @opts = GetoptLong.new + end + + it "allows setting command line options" do + argv ["--size", "10k", "-v", "arg1", "arg2"] do + @opts.set_options( + ["--size", GetoptLong::REQUIRED_ARGUMENT], + ["--verbose", "-v", GetoptLong::NO_ARGUMENT] + ) + + @opts.get.should == ["--size", "10k"] + @opts.get.should == ["--verbose", ""] + @opts.get.should == nil + end + end + + it "discards previously defined command line options" do + argv ["--size", "10k", "-v", "arg1", "arg2"] do + @opts.set_options( + ["--size", GetoptLong::REQUIRED_ARGUMENT], + ["--verbose", "-v", GetoptLong::NO_ARGUMENT] + ) + + @opts.set_options( + ["-s", "--size", GetoptLong::REQUIRED_ARGUMENT], + ["-v", GetoptLong::NO_ARGUMENT] + ) + + @opts.get.should == ["-s", "10k"] + @opts.get.should == ["-v", ""] + @opts.get.should == nil + end + end + + it "raises an ArgumentError if too many argument flags where given" do + argv [] do + -> { + @opts.set_options(["--size", GetoptLong::NO_ARGUMENT, GetoptLong::REQUIRED_ARGUMENT]) + }.should.raise(ArgumentError) + end + end + + it "raises a RuntimeError if processing has already started" do + argv [] do + @opts.get + -> { + @opts.set_options() + }.should.raise(RuntimeError) + end + end + + it "raises an ArgumentError if no argument flag was given" do + argv [] do + -> { + @opts.set_options(["--size"]) + }.should.raise(ArgumentError) + end + end + + it "raises an ArgumentError if one of the given arguments is not an Array" do + argv [] do + -> { + @opts.set_options( + ["--size", GetoptLong::REQUIRED_ARGUMENT], + "test") + }.should.raise(ArgumentError) + end + end + + it "raises an ArgumentError if the same option is given twice" do + argv [] do + -> { + @opts.set_options( + ["--size", GetoptLong::NO_ARGUMENT], + ["--size", GetoptLong::OPTIONAL_ARGUMENT]) + }.should.raise(ArgumentError) + + -> { + @opts.set_options( + ["--size", GetoptLong::NO_ARGUMENT], + ["-s", "--size", GetoptLong::OPTIONAL_ARGUMENT]) + }.should.raise(ArgumentError) + end + end + + it "raises an ArgumentError if the given option is invalid" do + argv [] do + -> { + @opts.set_options(["-size", GetoptLong::NO_ARGUMENT]) + }.should.raise(ArgumentError) + end + end +end diff --git a/spec/ruby/library/getoptlong/shared/each.rb b/spec/ruby/library/getoptlong/shared/each.rb new file mode 100644 index 0000000000..b534e24c0f --- /dev/null +++ b/spec/ruby/library/getoptlong/shared/each.rb @@ -0,0 +1,18 @@ +describe :getoptlong_each, shared: true do + before :each do + @opts = GetoptLong.new( + [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], + [ '--query', '-q', GetoptLong::NO_ARGUMENT ], + [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] + ) + end + + it "passes each argument/value pair to the block" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + pairs = [] + @opts.send(@method) { |arg, val| pairs << [ arg, val ] } + pairs.should == [ [ "--size", "10k" ], [ "--verbose", "" ], [ "--query", ""] ] + end + end +end diff --git a/spec/ruby/library/getoptlong/shared/get.rb b/spec/ruby/library/getoptlong/shared/get.rb new file mode 100644 index 0000000000..8d24c4c255 --- /dev/null +++ b/spec/ruby/library/getoptlong/shared/get.rb @@ -0,0 +1,62 @@ +describe :getoptlong_get, shared: true do + before :each do + @opts = GetoptLong.new( + [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], + [ '--query', '-q', GetoptLong::NO_ARGUMENT ], + [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] + ) + @opts.quiet = true # silence using $deferr + end + + it "returns the next option name and its argument as an Array" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + @opts.send(@method).should == [ "--size", "10k" ] + @opts.send(@method).should == [ "--verbose", "" ] + @opts.send(@method).should == [ "--query", ""] + @opts.send(@method).should == nil + end + end + + it "shifts ARGV on each call" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + @opts.send(@method) + ARGV.should == [ "-v", "-q", "a.txt", "b.txt" ] + + @opts.send(@method) + ARGV.should == [ "-q", "a.txt", "b.txt" ] + + @opts.send(@method) + ARGV.should == [ "a.txt", "b.txt" ] + + @opts.send(@method) + ARGV.should == [ "a.txt", "b.txt" ] + end + end + + it "terminates processing when encountering '--'" do + argv [ "--size", "10k", "--", "-v", "-q", "a.txt", "b.txt" ] do + @opts.send(@method) + ARGV.should == ["--", "-v", "-q", "a.txt", "b.txt"] + + @opts.send(@method) + ARGV.should == ["-v", "-q", "a.txt", "b.txt"] + + @opts.send(@method) + ARGV.should == ["-v", "-q", "a.txt", "b.txt"] + end + end + + it "raises a if an argument was required, but none given" do + argv [ "--size" ] do + -> { @opts.send(@method) }.should.raise(GetoptLong::MissingArgument) + end + end + + # https://bugs.ruby-lang.org/issues/13858 + it "returns multiline argument" do + argv [ "--size=\n10k\n" ] do + @opts.send(@method).should == [ "--size", "\n10k\n" ] + end + end +end diff --git a/spec/ruby/library/getoptlong/terminate_spec.rb b/spec/ruby/library/getoptlong/terminate_spec.rb new file mode 100644 index 0000000000..a12d1df2ef --- /dev/null +++ b/spec/ruby/library/getoptlong/terminate_spec.rb @@ -0,0 +1,30 @@ +require_relative '../../spec_helper' +require 'getoptlong' + +describe "GetoptLong#terminate" do + before :each do + @opts = GetoptLong.new( + [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ], + [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ], + [ '--query', '-q', GetoptLong::NO_ARGUMENT ], + [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ] + ) + end + + it "terminates option processing" do + argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do + @opts.get.should == [ "--size", "10k" ] + @opts.terminate + @opts.get.should == nil + end + end + + it "returns self when option processing is terminated" do + @opts.terminate.should == @opts + end + + it "returns nil when option processing was already terminated" do + @opts.terminate + @opts.terminate.should == nil + end +end diff --git a/spec/ruby/library/getoptlong/terminated_spec.rb b/spec/ruby/library/getoptlong/terminated_spec.rb new file mode 100644 index 0000000000..6108a7f6e9 --- /dev/null +++ b/spec/ruby/library/getoptlong/terminated_spec.rb @@ -0,0 +1,17 @@ +require_relative '../../spec_helper' +require 'getoptlong' + +describe "GetoptLong#terminated?" do + it "returns true if option processing has terminated" do + argv [ "--size", "10k" ] do + opts = GetoptLong.new(["--size", GetoptLong::REQUIRED_ARGUMENT]) + opts.should_not.terminated? + + opts.get.should == ["--size", "10k"] + opts.should_not.terminated? + + opts.get.should == nil + opts.should.terminated? + end + end +end |
