diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/library/getoptlong | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/library/getoptlong')
-rw-r--r-- | spec/rubyspec/library/getoptlong/each_option_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/each_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/error_message_spec.rb | 23 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/get_option_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/get_spec.rb | 7 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/initialize_spec.rb | 28 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/ordering_spec.rb | 38 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/set_options_spec.rb | 98 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/shared/each.rb | 18 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/shared/get.rb | 55 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/terminate_spec.rb | 30 | ||||
-rw-r--r-- | spec/rubyspec/library/getoptlong/terminated_spec.rb | 17 |
12 files changed, 335 insertions, 0 deletions
diff --git a/spec/rubyspec/library/getoptlong/each_option_spec.rb b/spec/rubyspec/library/getoptlong/each_option_spec.rb new file mode 100644 index 0000000000..c58815bfa9 --- /dev/null +++ b/spec/rubyspec/library/getoptlong/each_option_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'getoptlong' +require File.expand_path('../shared/each', __FILE__) + +describe "GetoptLong#each_option" do + it_behaves_like(:getoptlong_each, :each_option) +end diff --git a/spec/rubyspec/library/getoptlong/each_spec.rb b/spec/rubyspec/library/getoptlong/each_spec.rb new file mode 100644 index 0000000000..d09f84a6db --- /dev/null +++ b/spec/rubyspec/library/getoptlong/each_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'getoptlong' +require File.expand_path('../shared/each', __FILE__) + +describe "GetoptLong#each" do + it_behaves_like(:getoptlong_each, :each) +end diff --git a/spec/rubyspec/library/getoptlong/error_message_spec.rb b/spec/rubyspec/library/getoptlong/error_message_spec.rb new file mode 100644 index 0000000000..3f44f538c6 --- /dev/null +++ b/spec/rubyspec/library/getoptlong/error_message_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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_error(ArgumentError) { |e| + e.message.should == "argument error" + opts.error_message.should == "argument error" + } + end + end +end diff --git a/spec/rubyspec/library/getoptlong/get_option_spec.rb b/spec/rubyspec/library/getoptlong/get_option_spec.rb new file mode 100644 index 0000000000..c56903e68e --- /dev/null +++ b/spec/rubyspec/library/getoptlong/get_option_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'getoptlong' +require File.expand_path('../shared/get', __FILE__) + +describe "GetoptLong#get_option" do + it_behaves_like(:getoptlong_get, :get_option) +end diff --git a/spec/rubyspec/library/getoptlong/get_spec.rb b/spec/rubyspec/library/getoptlong/get_spec.rb new file mode 100644 index 0000000000..ba1a1be6ad --- /dev/null +++ b/spec/rubyspec/library/getoptlong/get_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require 'getoptlong' +require File.expand_path('../shared/get', __FILE__) + +describe "GetoptLong#get" do + it_behaves_like(:getoptlong_get, :get) +end diff --git a/spec/rubyspec/library/getoptlong/initialize_spec.rb b/spec/rubyspec/library/getoptlong/initialize_spec.rb new file mode 100644 index 0000000000..6ac46b8b5d --- /dev/null +++ b/spec/rubyspec/library/getoptlong/initialize_spec.rb @@ -0,0 +1,28 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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/rubyspec/library/getoptlong/ordering_spec.rb b/spec/rubyspec/library/getoptlong/ordering_spec.rb new file mode 100644 index 0000000000..e445de2255 --- /dev/null +++ b/spec/rubyspec/library/getoptlong/ordering_spec.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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 + + lambda { + opts.ordering = GetoptLong::PERMUTE + }.should raise_error(ArgumentError) + end + end + + it "raises an ArgumentError if given an invalid value" do + opts = GetoptLong.new + + lambda { + opts.ordering = 12345 + }.should raise_error(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/rubyspec/library/getoptlong/set_options_spec.rb b/spec/rubyspec/library/getoptlong/set_options_spec.rb new file mode 100644 index 0000000000..39d6991bf5 --- /dev/null +++ b/spec/rubyspec/library/getoptlong/set_options_spec.rb @@ -0,0 +1,98 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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 + lambda { + @opts.set_options(["--size", GetoptLong::NO_ARGUMENT, GetoptLong::REQUIRED_ARGUMENT]) + }.should raise_error(ArgumentError) + end + end + + it "raises a RuntimeError if processing has already started" do + argv [] do + @opts.get + lambda { + @opts.set_options() + }.should raise_error(RuntimeError) + end + end + + it "raises an ArgumentError if no argument flag was given" do + argv [] do + lambda { + @opts.set_options(["--size"]) + }.should raise_error(ArgumentError) + end + end + + it "raises an ArgumentError if one of the given arguments is not an Array" do + argv [] do + lambda { + @opts.set_options( + ["--size", GetoptLong::REQUIRED_ARGUMENT], + "test") + }.should raise_error(ArgumentError) + end + end + + it "raises an ArgumentError if the same option is given twice" do + argv [] do + lambda { + @opts.set_options( + ["--size", GetoptLong::NO_ARGUMENT], + ["--size", GetoptLong::OPTIONAL_ARGUMENT]) + }.should raise_error(ArgumentError) + + lambda { + @opts.set_options( + ["--size", GetoptLong::NO_ARGUMENT], + ["-s", "--size", GetoptLong::OPTIONAL_ARGUMENT]) + }.should raise_error(ArgumentError) + end + end + + it "raises an ArgumentError if the given option is invalid" do + argv [] do + lambda { + @opts.set_options(["-size", GetoptLong::NO_ARGUMENT]) + }.should raise_error(ArgumentError) + end + end +end diff --git a/spec/rubyspec/library/getoptlong/shared/each.rb b/spec/rubyspec/library/getoptlong/shared/each.rb new file mode 100644 index 0000000000..b534e24c0f --- /dev/null +++ b/spec/rubyspec/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/rubyspec/library/getoptlong/shared/get.rb b/spec/rubyspec/library/getoptlong/shared/get.rb new file mode 100644 index 0000000000..276da5abb4 --- /dev/null +++ b/spec/rubyspec/library/getoptlong/shared/get.rb @@ -0,0 +1,55 @@ +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 + lambda { @opts.send(@method) }.should raise_error(GetoptLong::MissingArgument) + end + end +end diff --git a/spec/rubyspec/library/getoptlong/terminate_spec.rb b/spec/rubyspec/library/getoptlong/terminate_spec.rb new file mode 100644 index 0000000000..ad9f9a1623 --- /dev/null +++ b/spec/rubyspec/library/getoptlong/terminate_spec.rb @@ -0,0 +1,30 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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 proccessing" 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 processsing 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/rubyspec/library/getoptlong/terminated_spec.rb b/spec/rubyspec/library/getoptlong/terminated_spec.rb new file mode 100644 index 0000000000..feaf2bc09e --- /dev/null +++ b/spec/rubyspec/library/getoptlong/terminated_spec.rb @@ -0,0 +1,17 @@ +require File.expand_path('../../../spec_helper', __FILE__) +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.terminated?.should == false + + opts.get.should == ["--size", "10k"] + opts.terminated?.should == false + + opts.get.should == nil + opts.terminated?.should == true + end + end +end |